"""This module contains the test cases for the api accounts-related views"""

import json

from django.contrib.auth import authenticate
from django.core.urlresolvers import reverse
from django.test.utils import override_settings

from mtp.apps.accounts.factories import AvatarFactory
from mtp.apps.accounts.models import MTPUser
from mtp.apps.api.tests.test_cases import MTPAPITestCase
from mtp.apps.common.tests.test_cases import MTPTestCase, DEFAULT_PASSWORD
from mtp.apps.documents.factories import NoteFactory
from mtp.apps.trainings.factories import TrainingSessionFactory


class UserApiTestCase(MTPTestCase):

    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()

        cls.url = reverse('api:user', kwargs={'user_uuid': cls.trainee.id})

        cls.NEW_USER_DATA = {
            'first_name': 'Kara',
            'last_name': 'Thrace',
            'mail': 'kara.thrace@galacti.ca'
        }

    def test_get_user_should_return_the_user(self):
        """
        Test that the ``user`` view return the user wanted.
        """
        response = self.client.get(self.url)

        self.assertEqual(response.status_code, 200)

        data = json.loads(response.content.decode('utf-8')).get('user')

        # Data verification
        self.assertEqual(data.get('id'), self.trainee.id)
        self.assertEqual(data.get('first_name'), self.trainee.first_name)
        self.assertEqual(data.get('username'), self.trainee.username)

    def test_get_user_with_notes(self):
        """
        Test that the ``user`` view return notes attributes to user.
        """
        note = NoteFactory(user=self.trainee)

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content.decode('utf-8')).get('user')

        # Data verification
        received_note = data.get('notes')[0]
        self.assertEqual(received_note.get('id'), note.id)
        self.assertEqual(received_note.get('text'), note.text)
        self.assertAlmostEqual(
            float(received_note.get('x')),
            note.coord_x,
            places=5,
        )

    def test_get_user_with_avatar(self):
        """Test that the ``user`` view return the avatars of the user."""

        avatar = AvatarFactory()
        self.trainee.photos.add(avatar)

        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        data = json.loads(response.content.decode('utf-8')).get('user')

        # Data verification
        received_avatar = data.get('photos')[0]
        self.assertEqual(received_avatar['id'], avatar.id)
        self.assertEqual(received_avatar['hash'], avatar.hash)
        self.assertEqual(received_avatar['img'], avatar.img.url)

    def test_post_user_should_update_the_user(self):
        """
        Test that the ``user`` view update user.
        """
        response = self.client.post(self.url, self.NEW_USER_DATA)

        self.assertEqual(response.status_code, 200)

        up_trainee = MTPUser.objects.get(id=self.trainee.id)

        # Data verification
        self.assertEqual(self.NEW_USER_DATA['first_name'], up_trainee.first_name)
        self.assertEqual(self.NEW_USER_DATA['last_name'], up_trainee.last_name)
        self.assertEqual(self.NEW_USER_DATA['mail'], up_trainee.email)
