# Path: app/api/auth.py
# def authorization_required(func=None):
#     def decorator(view_func):
#         def wrapper(request, *args, **kwargs):
#             api_key = 'HTTP_X_TAVERNATOKEN'
#             if api_key not in request.META:
#                 data = {'message': 'Set your api_key in X-TavernaToken header.'}
#                 return JsonResponse(data, status=401)
# 
#             try:
#                 ApiKey.objects.get(token=request.META[api_key], revoked=False)
#             except (ApiKey.DoesNotExist, TypeError):
#                 data = {'message': 'Invalid Token.'}
#                 return JsonResponse(data, status=401)
# 
#             return view_func(request, *args, **kwargs)
# 
#         return wrapper
# 
#     if func:
#         return decorator(func)
# 
#     return decorator
# 
# Path: app/api/tests/utils.py
# def obtain_api_key(client):
#         credentials = '{}:{}'.format(
#             admin_test_credentials[0],
#             admin_test_credentials[2]
#         )
#         b64_encoded_credentials = b64encode(credentials.encode('utf-8'))
#         return client.post(
#             '/api/api_key',
#             **{'HTTP_AUTHORIZATION': 'Basic %s' % b64_encoded_credentials.decode('utf-8')}
#         ).json()['api_key']
# 
# def create_admin_account():
#     return User.objects.create_superuser(*admin_test_credentials)


import json
from django.http import HttpRequest, JsonResponse
from django.test import Client, TestCase
from ..auth import authorization_required
from .utils import obtain_api_key, create_admin_account
        self.request.method = 'GET'
        self.request.META['HTTP_X_TAVERNATOKEN'] = 'a49d7536849be9da859a67bae2d7256f'
        response = self.make_request()

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

    def test_api_get_request_with_valid_api_key_header(self):
        self.request.method = 'GET'
        create_admin_account()
        self.request.META['HTTP_X_TAVERNATOKEN'] = obtain_api_key(self.client)
        response = self.make_request()

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

    def test_api_post_request_without_api_key_header(self):
        self.request.method = 'POST'
        response = self.make_request()

        self.assertEqual('Set your api_key in X-TavernaToken header.', response['message'])

    def test_api_post_request_with_invalid_api_key_header(self):
        self.request.method = 'POST'
        self.request.META['HTTP_X_TAVERNATOKEN'] = 'a49d7536849be9da859a67bae2d7256f'
        response = self.make_request()

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

    def test_api_post_request_with_valid_api_key_header(self):
        self.request.method = 'POST'
        create_admin_account()

# Path: app/api/auth.py
# def authorization_required(func=None):
#     def decorator(view_func):
#         def wrapper(request, *args, **kwargs):
#             api_key = 'HTTP_X_TAVERNATOKEN'
#             if api_key not in request.META:
#                 data = {'message': 'Set your api_key in X-TavernaToken header.'}
#                 return JsonResponse(data, status=401)
# 
#             try:
#                 ApiKey.objects.get(token=request.META[api_key], revoked=False)
#             except (ApiKey.DoesNotExist, TypeError):
#                 data = {'message': 'Invalid Token.'}
#                 return JsonResponse(data, status=401)
# 
#             return view_func(request, *args, **kwargs)
# 
#         return wrapper
# 
#     if func:
#         return decorator(func)
# 
#     return decorator
# 
# Path: app/api/tests/utils.py
# def obtain_api_key(client):
#         credentials = '{}:{}'.format(
#             admin_test_credentials[0],
#             admin_test_credentials[2]
#         )
#         b64_encoded_credentials = b64encode(credentials.encode('utf-8'))
#         return client.post(
#             '/api/api_key',
#             **{'HTTP_AUTHORIZATION': 'Basic %s' % b64_encoded_credentials.decode('utf-8')}
#         ).json()['api_key']
# 
# def create_admin_account():
#     return User.objects.create_superuser(*admin_test_credentials)


# Path: app/api/tests/test_auth.py
import json
from django.http import HttpRequest, JsonResponse
from django.test import Client, TestCase
from ..auth import authorization_required
from .utils import obtain_api_key, create_admin_account

        self.request.method = 'GET'
        self.request.META['HTTP_X_TAVERNATOKEN'] = 'a49d7536849be9da859a67bae2d7256f'
        response = self.make_request()

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

    def test_api_get_request_with_valid_api_key_header(self):
        self.request.method = 'GET'
        create_admin_account()
        self.request.META['HTTP_X_TAVERNATOKEN'] = obtain_api_key(self.client)
        response = self.make_request()

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

    def test_api_post_request_without_api_key_header(self):
        self.request.method = 'POST'
        response = self.make_request()

        self.assertEqual('Set your api_key in X-TavernaToken header.', response['message'])

    def test_api_post_request_with_invalid_api_key_header(self):
        self.request.method = 'POST'
        self.request.META['HTTP_X_TAVERNATOKEN'] = 'a49d7536849be9da859a67bae2d7256f'
        response = self.make_request()

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

    def test_api_post_request_with_valid_api_key_header(self):
        self.request.method = 'POST'
        create_admin_account()

        self.request.META['HTTP_X_TAVERNATOKEN'] = obtain_api_key(self.client)