# 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 obtain_api_key(client):
# def create_admin_account():
# def create_normal_user_acount():
# def make_request(client, query, method='GET'):


from base64 import b64encode
from django.contrib.auth.models import User
from django.test import Client, TestCase
from ..models import ApiKey
from .utils import (create_admin_account, admin_test_credentials,
                    normal_user_credentials, create_normal_user_acount,)
    def test_api_key_issuance_handler_with_invalid_credentials(self):
        response = self.client.post(
            self.api_key_issuance_endpoint,
            **{'HTTP_AUTHORIZATION': 'Basic {}'.format(
                self.b64encode_credentials('support', 'qwerty')
            )}
        ).json()

        self.assertEqual('Invalid Credentials.', response['message'])

    def test_api_key_issuance_handler_with_normal_user_credentials(self):
        uname, email, passwd = normal_user_credentials
        response = self.client.post(
            self.api_key_issuance_endpoint,
            **{'HTTP_AUTHORIZATION': 'Basic {}'.format(self.b64encode_credentials(uname, passwd))}
        ).json()

        self.assertEqual('Unauthorized.', response['message'])

    def test_api_key_issuance_handler_with_admin_user_credentials(self):
        uname, email, passwd = admin_test_credentials
        response = self.client.post(
            self.api_key_issuance_endpoint,
            **{'HTTP_AUTHORIZATION': 'Basic {}'.format(self.b64encode_credentials(uname, passwd))}
        ).json()
        issued_api_key = ApiKey.objects.get(owner__username=uname, revoked=False)

        self.assertEqual(issued_api_key.token.hashid, response['api_key'])

    def test_api_key_issuance_handler_with_url_ending_with_api_key(self):

# 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 obtain_api_key(client):
# def create_admin_account():
# def create_normal_user_acount():
# def make_request(client, query, method='GET'):


# Path: app/api/tests/test_views.py
from base64 import b64encode
from django.contrib.auth.models import User
from django.test import Client, TestCase
from ..models import ApiKey
from .utils import (create_admin_account, admin_test_credentials,
                    normal_user_credentials, create_normal_user_acount,)

    def test_api_key_issuance_handler_with_invalid_credentials(self):
        response = self.client.post(
            self.api_key_issuance_endpoint,
            **{'HTTP_AUTHORIZATION': 'Basic {}'.format(
                self.b64encode_credentials('support', 'qwerty')
            )}
        ).json()

        self.assertEqual('Invalid Credentials.', response['message'])

    def test_api_key_issuance_handler_with_normal_user_credentials(self):
        uname, email, passwd = normal_user_credentials
        response = self.client.post(
            self.api_key_issuance_endpoint,
            **{'HTTP_AUTHORIZATION': 'Basic {}'.format(self.b64encode_credentials(uname, passwd))}
        ).json()

        self.assertEqual('Unauthorized.', response['message'])

    def test_api_key_issuance_handler_with_admin_user_credentials(self):
        uname, email, passwd = admin_test_credentials
        response = self.client.post(
            self.api_key_issuance_endpoint,
            **{'HTTP_AUTHORIZATION': 'Basic {}'.format(self.b64encode_credentials(uname, passwd))}
        ).json()
        issued_api_key = ApiKey.objects.get(owner__username=uname, revoked=False)

        self.assertEqual(issued_api_key.token.hashid, response['api_key'])

    def test_api_key_issuance_handler_with_url_ending_with_api_key(self):

        uname, email, passwd = admin_test_credentials