# Path: django_sonic_screwdriver/apps/ban/models.py
# class UserBan(Ban):
#     banned_user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         on_delete=models.CASCADE,
#         verbose_name=_("User"),
#         related_name="banned_user",
#         blank=False,
#         null=True,
#         help_text="This is the banned user or the receiver of the ban.",
#     )
# 
#     class Meta:
#         verbose_name = _("User Ban")
#         verbose_name_plural = _("User Bans")
# 
#     def __str__(self):
#         return self.banned_user.get_username()
# 
# class IPBan(Ban):
#     ip = models.GenericIPAddressField(
#         verbose_name=_("IP"),
#         blank=False,
#         null=True,
#         help_text=_(
#             "This is the banned ip. Every request from this IP will result in 403."
#         ),
#     )
# 
#     class Meta:
#         verbose_name = _("IP Ban")
#         verbose_name_plural = _("IP Bans")
# 
#     def __str__(self):
#         return str(self.ip)


from django.test import TestCase
from django.contrib.auth import get_user_model
from django_sonic_screwdriver.apps.ban.models import UserBan, IPBan


User = get_user_model()


class AppBanTest(TestCase):
    def test_user_ban_create(self):
        user = User.objects.create(username="test", password="123")
        UserBan.objects.create(banned_user=user)
        self.assertEqual(UserBan.objects.count(), 1)

    def test_banned_user_will_receive_403(self):
        user = User.objects.create(username="test", password="123")
        self.client.force_login(user)

        res = self.client.get("/home/")
        self.assertEqual(res.status_code, 200)


# Path: django_sonic_screwdriver/apps/ban/models.py
# class UserBan(Ban):
#     banned_user = models.ForeignKey(
#         settings.AUTH_USER_MODEL,
#         on_delete=models.CASCADE,
#         verbose_name=_("User"),
#         related_name="banned_user",
#         blank=False,
#         null=True,
#         help_text="This is the banned user or the receiver of the ban.",
#     )
# 
#     class Meta:
#         verbose_name = _("User Ban")
#         verbose_name_plural = _("User Bans")
# 
#     def __str__(self):
#         return self.banned_user.get_username()
# 
# class IPBan(Ban):
#     ip = models.GenericIPAddressField(
#         verbose_name=_("IP"),
#         blank=False,
#         null=True,
#         help_text=_(
#             "This is the banned ip. Every request from this IP will result in 403."
#         ),
#     )
# 
#     class Meta:
#         verbose_name = _("IP Ban")
#         verbose_name_plural = _("IP Bans")
# 
#     def __str__(self):
#         return str(self.ip)


# Path: tests/test_app_ban.py
from django.test import TestCase
from django.contrib.auth import get_user_model
from django_sonic_screwdriver.apps.ban.models import UserBan, IPBan



User = get_user_model()


class AppBanTest(TestCase):
    def test_user_ban_create(self):
        user = User.objects.create(username="test", password="123")
        UserBan.objects.create(banned_user=user)
        self.assertEqual(UserBan.objects.count(), 1)

    def test_banned_user_will_receive_403(self):
        user = User.objects.create(username="test", password="123")
        self.client.force_login(user)

        res = self.client.get("/home/")
        self.assertEqual(res.status_code, 200)


        UserBan.objects.create_for_user(banned_user=user)