from django.core.urlresolvers import reverse
from django.contrib.auth import get_user_model

from rest_framework import status

from core.tests.base import BaseTestCase


class UserAPIViewTestCase(BaseTestCase):

    def test_user_change_email(self):

        test_user_email_url = reverse('api:user:email')
        test_new_email = "new@example.com"

        test_data = {
            'new_email': test_new_email,
        }

        response = self.client.patch(
            test_user_email_url,
            test_data,
        )

        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
        )
        self.assertEqual(
            get_user_model().objects.last().email,
            test_new_email,
        )

    def test_user_change_password(self):

        test_user_password_url = reverse('api:user:password')
        test_new_password = "test_new_password"

        test_data = {
            'current_password': self.test_password,
            'new_password': test_new_password,
            'confirm_new_password': test_new_password,
        }

        response = self.client.patch(
            test_user_password_url,
            test_data,
        )

        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
        )
        self.assertTrue(
            response.data.get("is_current_password_valid"),
        )
        self.assertTrue(
            response.data.get("does_match_confirm_password"),
        )
        self.assertTrue(
            get_user_model().objects.last().check_password(test_new_password),
        )

    def test_user_enter_wrong_current_password(self):

        test_user_password_url = reverse('api:user:password')
        test_wrong_password = "test_wrong_password"
        test_new_password = "test_new_password"

        test_data = {
            'current_password': test_wrong_password,
            'new_password': test_new_password,
            'confirm_new_password': test_new_password,
        }

        response = self.client.patch(
            test_user_password_url,