<commit_before>"""
dj-stripe Admin Tests.
"""
from typing import Sequence

import pytest
from django.apps import apps
from django.contrib.admin import site
from django.contrib.auth import get_user_model
from django.core.exceptions import FieldError
from django.test import TestCase
from django.test.client import RequestFactory
from django.urls import reverse
from jsonfield import JSONField

from djstripe import admin as djstripe_admin
from djstripe import models

pytestmark = pytest.mark.django_db


@pytest.mark.parametrize(
    "output,input",
    [
        (["event", "stripe_trigger_account"], models.WebhookEventTrigger),
        (
            [
                "djstripe_owner_account",
                "customer",
                "default_payment_method",
                "default_source",
                "latest_invoice",
                "pending_setup_intent",
                "plan",
                "schedule",
                "default_tax_rates",
            ],
            models.Subscription,
        ),
        (
            [
                "djstripe_owner_account",
                "default_source",
                "coupon",
                "default_payment_method",
                "subscriber",
            ],
            models.Customer,
        ),
    ],
)
def test_get_forward_relation_fields_for_model(output, input):
    assert output == djstripe_admin.get_forward_relation_fields_for_model(input)


class TestAdminRegisteredModels(TestCase):
    def setUp(self):
        self.admin = get_user_model().objects.create_superuser(
            username="admin", email="<EMAIL>", password="<PASSWORD>"
        )
        self.factory = RequestFactory()
        # the 2 models that do not inherit from StripeModel and hence
        # do not inherit from StripeModelAdmin
        self.ignore_models = ["WebhookEventTrigger", "IdempotencyKey"]

    def test_get_list_display_links(self):
        app_label = "djstripe"
        app_config = apps.get_app_config(app_label)
        all_models_lst = app_config.get_models()

        for model in all_models_lst:
            if model in site._registry.keys():
                model_admin = site._registry.get(model)
                # get the standard changelist_view url
                url = reverse(
                    f"admin:{model._meta.app_label}_{model.__name__.lower()}_changelist"
                )

                # add the admin user to the mocked request
                request = self.factory.get(url)
                request.user = self.admin<commit_msg>Add APIKey to list of models not inheriting from StripeModelAdmin<commit_after>"""
dj-stripe Admin Tests.
"""
from typing import Sequence

import pytest
from django.apps import apps
from django.contrib.admin import site
from django.contrib.auth import get_user_model
from django.core.exceptions import FieldError
from django.test import TestCase
from django.test.client import RequestFactory
from django.urls import reverse
from jsonfield import JSONField

from djstripe import admin as djstripe_admin
from djstripe import models

pytestmark = pytest.mark.django_db


@pytest.mark.parametrize(
    "output,input",
    [
        (["event", "stripe_trigger_account"], models.WebhookEventTrigger),
        (
            [
                "djstripe_owner_account",
                "customer",
                "default_payment_method",
                "default_source",
                "latest_invoice",
                "pending_setup_intent",
                "plan",
                "schedule",
                "default_tax_rates",
            ],
            models.Subscription,
        ),
        (
            [
                "djstripe_owner_account",
                "default_source",
                "coupon",
                "default_payment_method",
                "subscriber",
            ],
            models.Customer,
        ),
    ],
)
def test_get_forward_relation_fields_for_model(output, input):
    assert output == djstripe_admin.get_forward_relation_fields_for_model(input)


class TestAdminRegisteredModels(TestCase):
    def setUp(self):
        self.admin = get_user_model().objects.create_superuser(
            username="admin", email="<EMAIL>", password="<PASSWORD>"
        )
        self.factory = RequestFactory()
        # the 2 models that do not inherit from StripeModel and hence
        # do not inherit from StripeModelAdmin
        self.ignore_models = ["WebhookEventTrigger", "IdempotencyKey", "APIKey"]

    def test_get_list_display_links(self):
        app_label = "djstripe"
        app_config = apps.get_app_config(app_label)
        all_models_lst = app_config.get_models()

        for model in all_models_lst:
            if model in site._registry.keys():
                model_admin = site._registry.get(model)
                # get the standard changelist_view url
                url = reverse(
                    f"admin:{model._meta.app_label}_{model.__name__.lower()}_changelist"
                )

                # add the admin user to the mocked request
                request = self.factory.get(url)
                request.user = self.admin
{'fraction_non_alphanumeric': 0.08274922118380063, 'fraction_numerical': 0.0003894080996884735, 'mean_word_length': 2.096443640747438, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2806454', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1366, 'n_words': 307}