"""This module contains the test cases for the permission classes of the ``api`` app"""

from django.test.utils import modify_settings, override_settings

from rest_framework.response import Response
from rest_framework.views import APIView

from mtp.apps.common.tests.test_cases import MTPTestCase

from ..permissions import IsAuthenticatedOrValidOAuthToken

from .test_cases import MTPOAuthClientTestCaseMixin


@modify_settings(MIDDLEWARE_CLASSES={
    'remove': 'mtp.apps.accounts.middleware.PasswordExpiryMiddleware',
})
@override_settings(ROOT_URLCONF='mtp.apps.common.tests.fake_urls')
class IsAuthenticatedOrValidOAuthTokenPermissionTestCase(MTPOAuthClientTestCaseMixin, MTPTestCase):
    """Test case for the ``IsAuthenticatedOrValidOAuthToken`` permission"""

    class SimpleView(APIView):
        """Simple view with the ``IsAuthenticatedOrValidOAuthToken`` permission"""

        permission_classes = (IsAuthenticatedOrValidOAuthToken,)

        def get(self, request, *args, **kwargs):  # pylint:disable=unused-argument
            """Simply return a string for the test needs"""

            return Response({})

    def test_simple_view_without_auth(self):
        """Test that the view without auth should return a 401"""

        response = self.client.get('/url-with-auth-or-token-permission/')

        self.assertEqual(
            response.status_code,
            401
        )

    def test_simple_view_with_standard_auth(self):
        """Test that the view with a standard auth should return a 200"""

        self.login_as_trainer()

        response = self.client.get('/url-with-auth-or-token-permission/')

        self.assertEqual(
            response.status_code,
            200
        )

    def test_simple_view_with_oauth(self):
        """Test that the view with an oauth application auth (no user) should return a 200"""

        response = self.oauth_client.get('/url-with-auth-or-token-permission/')

        self.assertEqual(
            response.status_code,
            200
        )
