# Path: app/api/models.py
# class ApiKey(TimestampMixin):
#     """Model representing clients' api keys."""
# 
#     token = HashidField(
#         min_length=32,
#         alphabet='0123456789abcdef',
#         default=timestamp_seconds,
#         unique=True,
#         editable=False
#     )
#     revoked = models.BooleanField(default=False)
#     owner = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def clean(self):
#         if not self.owner.is_superuser:
#             raise ValidationError(_('Ensure the owner is a superuser.'))
# 
#         super().clean()
# 
#     def save(self, *args, **kwargs):
#         self.full_clean()
#         return super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.token.hashid
# 
# Path: app/api/tests/utils.py
# def create_admin_account():
#     return User.objects.create_superuser(*admin_test_credentials)
# 
# def create_normal_user_acount():
#     return User.objects.create_user(*normal_user_credentials)


from django.core.exceptions import ValidationError
from django.test import TestCase
from ..models import ApiKey
from .utils import create_admin_account, create_normal_user_acount



class ApiKeyTest(TestCase):
    """Tests the ApiKey model."""

    def test_api_key_creation_using_normal_users(self):
        normal_user = create_normal_user_acount()

        self.assertRaises(ValidationError, ApiKey.objects.create, owner=normal_user)

    def test_api_key_creation_using_super_users(self):
        super_user = create_admin_account()
        api_key = ApiKey.objects.create(owner=super_user)


# Path: app/api/models.py
# class ApiKey(TimestampMixin):
#     """Model representing clients' api keys."""
# 
#     token = HashidField(
#         min_length=32,
#         alphabet='0123456789abcdef',
#         default=timestamp_seconds,
#         unique=True,
#         editable=False
#     )
#     revoked = models.BooleanField(default=False)
#     owner = models.ForeignKey(User, on_delete=models.CASCADE)
# 
#     def clean(self):
#         if not self.owner.is_superuser:
#             raise ValidationError(_('Ensure the owner is a superuser.'))
# 
#         super().clean()
# 
#     def save(self, *args, **kwargs):
#         self.full_clean()
#         return super().save(*args, **kwargs)
# 
#     def __str__(self):
#         return self.token.hashid
# 
# Path: app/api/tests/utils.py
# def create_admin_account():
#     return User.objects.create_superuser(*admin_test_credentials)
# 
# def create_normal_user_acount():
#     return User.objects.create_user(*normal_user_credentials)


# Path: app/api/tests/test_models.py
from django.core.exceptions import ValidationError
from django.test import TestCase
from ..models import ApiKey
from .utils import create_admin_account, create_normal_user_acount




class ApiKeyTest(TestCase):
    """Tests the ApiKey model."""

    def test_api_key_creation_using_normal_users(self):
        normal_user = create_normal_user_acount()

        self.assertRaises(ValidationError, ApiKey.objects.create, owner=normal_user)

    def test_api_key_creation_using_super_users(self):
        super_user = create_admin_account()
        api_key = ApiKey.objects.create(owner=super_user)


        self.assertIsInstance(api_key, ApiKey)