"""This module contains the tests for the v2 api-related views of the accounts app"""

from django.core.urlresolvers import reverse

from rest_framework import status
from rest_framework.test import APITestCase

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


class CurrentUserViewTestCase(MTPTestCase, APITestCase):
    """Test cases for the ``CurrentUser`` api view"""

    @classmethod
    def setUpClass(cls):
        """Get the url of the view"""

        super().setUpClass()

        cls.me_url = reverse('apiv2:user-me')

    def test_route_should_return_the_serialized_authenticated_user(self):
        """Test that the route should return the serialized authenticated user"""

        self.login_as_trainee()
        response = self.client.get(self.me_url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data,
            {
                'id': self.trainee.pk,
                'username': self.trainee.username,
                'first_name': self.trainee.first_name,
                'last_name': self.trainee.last_name,
                'email': self.trainee.email,
                'language': self.trainee.natural_language,
                'profile_picture': None,
                'groups': [
                    group.name
                    for group in self.trainee.mgroups.non_deleted()
                ],
            }
        )

    def test_route_should_reject_unauthenticated_user(self):
        """Test that the route should reject an unauthenticated user"""

        response = self.client.get(self.me_url)

        self.assertEqual(response.status_code, 403)
