<commit_before>from django.core.urlresolvers import reverse
from django.test import TestCase
from django.contrib.auth import get_user_model

from rest_framework.test import APIClient
from rest_framework import status

from english_diary.celery import app


class UserAPIViewTestCase(TestCase):

    def setUp(self):

        # Run celery task synchronous
        app.conf.update(CELERY_ALWAYS_EAGER=True)

        self.test_username = "test_username"
        self.test_password = "<PASSWORD>"
        self.test_email = "<EMAIL>"

        # Create a user
        self.user = get_user_model().objects.create_user(
            username=self.test_username,
            password=<PASSWORD>,
            email=self.test_email,
        )

        # Login
        self.client = APIClient()
        self.client.login(
            username=self.test_username,
            password=<PASSWORD>,
        )

    def test_user_change_email(self):

        test_user_email_url = reverse('api:user:email')
        test_new_email = "<EMAIL>"

        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 = "<PASSWORD>"

        test_data = {
            'current_password': <PASSWORD>,
            'new_password': <PASSWORD>,
            'confirm_new_password': <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_<PASSWORD>password),
        )

    def test_user_enter_wrong_current_password(self):

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

        test_data = {
            'current_password': <PASSWORD>,
            'new_password': <PASSWORD>,
            'confirm_new_password': <PASSWORD>,
        }

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

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

    def test_new_password_does_not_match_cofirmation(self):

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

        test_data = {
            'current_password': <PASSWORD>,
            'new_password': <PASSWORD>,
            'confirm_new_password': <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.assertFalse(
            response.data.get("does_match_confirm_password"),
        )
        self.assertFalse(
            get_user_model().objects.last().check_password(<PASSWORD>_password),
        )

    def test_user_enter_wrong_current_password_and_new_password_does_not_match_cofirmation(self):

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

        test_data = {
            'current_password': <PASSWORD>,
            'new_password': <PASSWORD>,
            'confirm_new_password': <PASSWORD>+"!",
        }

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

        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
        )
        self.assertFalse(
            response.data.get("is_current_password_valid"),
        )
        self.assertFalse(
            response.data.get("does_match_confirm_password"),
        )
        self.assertFalse(
            get_user_model().objects.last().check_password(<PASSWORD>),
        )<commit_msg>update UserAPIViewTestCase: test to get email_notification<commit_after>from django.core.urlresolvers import reverse
from django.test import TestCase
from django.contrib.auth import get_user_model

from rest_framework.test import APIClient
from rest_framework import status

from english_diary.celery import app


class UserAPIViewTestCase(TestCase):

    def setUp(self):

        # Run celery task synchronous
        app.conf.update(CELERY_ALWAYS_EAGER=True)

        self.test_username = "test_username"
        self.test_password = "<PASSWORD>"
        self.test_email = "<EMAIL>"

        # Create a user
        self.user = get_user_model().objects.create_user(
            username=self.test_username,
            password=self.<PASSWORD>,
            email=self.test_email,
        )

        # Login
        self.client = APIClient()
        self.client.login(
            username=self.test_username,
            password=self.test_password,
        )

    def test_user_change_email(self):

        test_user_email_url = reverse('api:user:email')
        test_new_email = "<EMAIL>"

        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 = "<PASSWORD>"

        test_data = {
            'current_password': <PASSWORD>,
            'new_password': <PASSWORD>,
            'confirm_new_password': <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(<PASSWORD>),
        )

    def test_user_enter_wrong_current_password(self):

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

        test_data = {
            'current_password': <PASSWORD>,
            'new_password': <PASSWORD>,
            'confirm_new_password': <PASSWORD>,
        }

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

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

    def test_new_password_does_not_match_cofirmation(self):

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

        test_data = {
            'current_password': <PASSWORD>,
            'new_password': <PASSWORD>,
            'confirm_new_password': <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.assertFalse(
            response.data.get("does_match_confirm_password"),
        )
        self.assertFalse(
            get_user_model().objects.last().check_password(<PASSWORD>),
        )

    def test_user_enter_wrong_current_password_and_new_password_does_not_match_cofirmation(self):

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

        test_data = {
            'current_password': <PASSWORD>,
            'new_password': <PASSWORD>,
            'confirm_new_password': <PASSWORD>+"!",
        }

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

        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
        )
        self.assertFalse(
            response.data.get("is_current_password_valid"),
        )
        self.assertFalse(
            response.data.get("does_match_confirm_password"),
        )
        self.assertFalse(
            get_user_model().objects.last().check_password(<PASSWORD>),
        )

    def test_get_default_email_notification(self):

        test_user_email_notification_url = reverse(
            'api:user:email_notification',
        )

        response = self.client.get(
            test_user_email_notification_url,
        )

        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
        )
        self.assertTrue(
            response.data.get("email_notification"),
        )

    def test_get_changed_email_notification(self):

        test_user_email_notification_url = reverse(
            'api:user:email_notification',
        )

        self.user.email_notification = False
        self.user.save()

        response = self.client.get(
            test_user_email_notification_url,
        )

        self.assertEqual(
            response.status_code,
            status.HTTP_201_CREATED,
        )
        self.assertFalse(
            response.data.get("email_notification"),
        )
{'fraction_non_alphanumeric': 0.0947928126145948, 'fraction_numerical': 0.0033003300330033004, 'mean_word_length': 1.9781599781599781, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2389809', 'n_tokens_mistral': 3135, 'n_tokens_neox': 3034, 'n_words': 394}