# Path: salmon/core/authentication.py
# class SettingsAuthentication(authentication.BaseAuthentication):
#     def authenticate(self, request):
#         has_api_key = hasattr(settings, 'API_KEY')
#         using_basic_auth = 'HTTP_AUTHORIZATION' in request.META
#         if using_basic_auth and has_api_key:
#             auth = request.META['HTTP_AUTHORIZATION'].split()
#             if len(auth) == 2 and auth[0].lower() == "basic":
#                 key = base64.b64decode(auth[1]).split(':')[0]
#                 if constant_time_compare(settings.API_KEY, key):
#                     request._salmon_allowed = True
#                     return (AnonymousUser, None)
#                 else:
#                     raise exceptions.AuthenticationFailed('No such user')
#         return None


import base64
from django.conf import settings
from django.utils import unittest
from django.test.client import RequestFactory
from django.test.utils import override_settings
from rest_framework import exceptions
from salmon.core.authentication import SettingsAuthentication



class TestAuthentication(unittest.TestCase):
    def setUp(self):
        self.req_factory = RequestFactory()

    @override_settings(API_KEY='test')
    def test_valid_auth(self):
        http_auth = 'Basic {0}'.format(
            base64.encodestring(settings.API_KEY))
        request = self.req_factory.post('/api/v1/metric/', {},
                                        HTTP_AUTHORIZATION=http_auth)
        auth = SettingsAuthentication()
        auth_resp = auth.authenticate(request)
        self.assertFalse(auth_resp is None, "Authentication failed")

    @override_settings(API_KEY='test')
    def test_invalid_auth(self):
        http_auth = 'Basic {0}'.format(
            base64.encodestring('wrongkey'))
        request = self.req_factory.post('/api/v1/metric/', {},
                               HTTP_AUTHORIZATION=http_auth)
        auth = SettingsAuthentication()
        self.assertRaises(exceptions.AuthenticationFailed, auth.authenticate,
                          request)

    @override_settings(API_KEY='test')
    def test_no_auth(self):
        request = self.req_factory.post('/api/v1/metric/', {})

# Path: salmon/core/authentication.py
# class SettingsAuthentication(authentication.BaseAuthentication):
#     def authenticate(self, request):
#         has_api_key = hasattr(settings, 'API_KEY')
#         using_basic_auth = 'HTTP_AUTHORIZATION' in request.META
#         if using_basic_auth and has_api_key:
#             auth = request.META['HTTP_AUTHORIZATION'].split()
#             if len(auth) == 2 and auth[0].lower() == "basic":
#                 key = base64.b64decode(auth[1]).split(':')[0]
#                 if constant_time_compare(settings.API_KEY, key):
#                     request._salmon_allowed = True
#                     return (AnonymousUser, None)
#                 else:
#                     raise exceptions.AuthenticationFailed('No such user')
#         return None


# Path: salmon/core/tests/test_authentication.py
import base64
from django.conf import settings
from django.utils import unittest
from django.test.client import RequestFactory
from django.test.utils import override_settings
from rest_framework import exceptions
from salmon.core.authentication import SettingsAuthentication




class TestAuthentication(unittest.TestCase):
    def setUp(self):
        self.req_factory = RequestFactory()

    @override_settings(API_KEY='test')
    def test_valid_auth(self):
        http_auth = 'Basic {0}'.format(
            base64.encodestring(settings.API_KEY))
        request = self.req_factory.post('/api/v1/metric/', {},
                                        HTTP_AUTHORIZATION=http_auth)
        auth = SettingsAuthentication()
        auth_resp = auth.authenticate(request)
        self.assertFalse(auth_resp is None, "Authentication failed")

    @override_settings(API_KEY='test')
    def test_invalid_auth(self):
        http_auth = 'Basic {0}'.format(
            base64.encodestring('wrongkey'))
        request = self.req_factory.post('/api/v1/metric/', {},
                               HTTP_AUTHORIZATION=http_auth)
        auth = SettingsAuthentication()
        self.assertRaises(exceptions.AuthenticationFailed, auth.authenticate,
                          request)

    @override_settings(API_KEY='test')
    def test_no_auth(self):
        request = self.req_factory.post('/api/v1/metric/', {})

        auth = SettingsAuthentication()