import logging

from django.test.utils import override_settings

from rest_framework import status
from rest_framework.test import APITestCase, APIRequestFactory

from drf_authentication import utils
from tests.test_views import login_check_view


logger = logging.getLogger('logging_handler')

__author__ = 'cenk'


class LoginDjangoBackend(APITestCase):
    def setUp(self):
        UserModel = utils.get_user_model()
        user = UserModel(username="test")
        user.set_password("secret")
        user.save()

        user = UserModel(username="test1", is_active=False)
        user.set_password("secret1")
        user.save()


    def test_login(self):
        logger.info('LoginDjangoBackend test_login Starts')
        UserModel = utils.get_user_model()
        url = '/login/'

        data = {'username': 'test', 'password': 'secret'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        user = UserModel.objects.get(username='test')
        factory = APIRequestFactory()
        request = factory.get('/admin/')
        request.user = user
        response = login_check_view(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = {'username': 'test', 'password': 'secret1'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

        data = {'username': 'test1', 'password': 'secret'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

        data = {'username': 'test1', 'password': 'secret1'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        user = UserModel.objects.get(username='test1')
        factory = APIRequestFactory()
        request = factory.get('/admin/')
        request.user = user
        response = login_check_view(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        logger.info('LoginDjangoBackend test_login Ends')


@override_settings(DRF_AUTH={
    'AUTHENTICATION_BACKENDS': ('drf_authentication.backends.basic_authentication.BasicAuthenticationBackend',)
})
class LoginBasicAuthentication(APITestCase):
    def setUp(self):
        UserModel = utils.get_user_model()
        user = UserModel(username="test")
        user.set_password("secret")
        user.save()

        user = UserModel(username="test1", is_active=False)
        user.set_password("secret1")
        user.save()


    def test_login(self):
        logger.info('LoginBasicAuthentication test_login Starts')
        UserModel = utils.get_user_model()
        url = '/login/'

        data = {'username': 'test', 'password': 'secret'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        user = UserModel.objects.get(username='test')
        factory = APIRequestFactory()
        request = factory.get('/admin/')
        request.user = user
        response = login_check_view(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        data = {'username': 'test', 'password': 'secret1'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

        data = {'username': 'test1', 'password': 'secret'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

        data = {'username': 'test1', 'password': 'secret1'}
        response = self.client.post(url, data, format='json')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

        user = UserModel.objects.get(username='test1')
        factory = APIRequestFactory()
        request = factory.get('/admin/')
        request.user = user
        response = login_check_view(request)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        logger.info('LoginBasicAuthentication test_login Ends')


