# 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,)




class ApiViewTest(TestCase):
    """Test for API Views."""

    def setUp(self):
        self.client = Client()
        create_normal_user_acount()
        create_admin_account()
        self.api_key = self.create_api_key()
        self.api_key_issuance_endpoint = '/api/api_key'
        self.api_key_revocation_endpoint = '{}/{}'.format(
            self.api_key_issuance_endpoint,
            self.api_key
        )

    @classmethod
    def b64encode_credentials(cls, username, password):
        credentials = '{}:{}'.format(username, password)
        b64_encoded_credentials = b64encode(credentials.encode('utf-8'))

        return b64_encoded_credentials.decode('utf-8')

    def create_api_key(self):
        uname, email, passwd = admin_test_credentials
        admin = User.objects.get(username=uname)


# 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,)





class ApiViewTest(TestCase):
    """Test for API Views."""

    def setUp(self):
        self.client = Client()
        create_normal_user_acount()
        create_admin_account()
        self.api_key = self.create_api_key()
        self.api_key_issuance_endpoint = '/api/api_key'
        self.api_key_revocation_endpoint = '{}/{}'.format(
            self.api_key_issuance_endpoint,
            self.api_key
        )

    @classmethod
    def b64encode_credentials(cls, username, password):
        credentials = '{}:{}'.format(username, password)
        b64_encoded_credentials = b64encode(credentials.encode('utf-8'))

        return b64_encoded_credentials.decode('utf-8')

    def create_api_key(self):
        uname, email, passwd = admin_test_credentials
        admin = User.objects.get(username=uname)


        return ApiKey.objects.create(owner=admin, revoked=False).token.hashid