<commit_before>import datetime
import re

from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import check_password
from django.contrib.auth.tokens import default_token_generator
from django.core import mail
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from mock import patch
from rest_framework import status
from rest_framework.test import APIRequestFactory

from user_management.api import models, views
from user_management.api.tests.test_throttling import THROTTLE_RATE_PATH
from user_management.models.tests.factories import AuthTokenFactory, UserFactory
from user_management.models.tests.models import BasicUser
from user_management.models.tests.utils import APIRequestTestCase


User = get_user_model()
TEST_SERVER = 'http://testserver'


class GetAuthTokenTest(APIRequestTestCase):
    model = models.AuthToken
    view_class = views.GetAuthToken

    def tearDown(self):
        cache.clear()

    def test_post(self):
        username = '<EMAIL>'
        password = '<PASSWORD>'
        UserFactory.create(email=username.lower(), password=password)

        data = {'username': username, 'password': password}
        request = self.create_request('post', auth=False, data=data)
        view = self.view_class.as_view()
        response = view(request)
        self.assertEqual(
            response.status_code, status.HTTP_200_OK, msg=response.data)

        # Ensure user has a token now
        token = self.model.objects.get(user__email=username.lower())
        self.assertEqual(response.data['token'], token.key)

    def test_post_non_existing_user(self):
        """Assert non existing raises an error."""
        username = '<EMAIL>'
        password = '<PASSWORD>'

        data = {'username': username, 'password': password}
        request = self.create_request('post', auth=False, data=data)
        view = self.view_class.as_view()
        response = view(request)
        self.assertEqual(
            response.status_code,
            status.HTTP_400_BAD_REQUEST,
            msg=response.data,
        )
        expected = 'Unable to log in with provided credentials.'
        self.assertIn(expected, response.data['non_field_errors'])
        self.assertNotIn('token', response.data)

    def test_post_user_not_confirmed(self):
        """Assert non active users can not log in."""
        username = '<EMAIL>'
        password = '<PASSWORD>'
        UserFactory.create(email=username.lower(), password=password, is_active=False)

        data = {'username': username, 'password': password}
        request = self.create_request('post', auth=False, data=data)<commit_msg>Remove `lower` for `GetAuthTokenTest`<commit_after>import datetime
import re

from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import check_password
from django.contrib.auth.tokens import default_token_generator
from django.core import mail
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from mock import patch
from rest_framework import status
from rest_framework.test import APIRequestFactory

from user_management.api import models, views
from user_management.api.tests.test_throttling import THROTTLE_RATE_PATH
from user_management.models.tests.factories import AuthTokenFactory, UserFactory
from user_management.models.tests.models import BasicUser
from user_management.models.tests.utils import APIRequestTestCase


User = get_user_model()
TEST_SERVER = 'http://testserver'


class GetAuthTokenTest(APIRequestTestCase):
    model = models.AuthToken
    view_class = views.GetAuthToken

    def setUp(self):
        self.username = '<EMAIL>'
        self.password = '<PASSWORD>'
        self.data = {'username': self.username, 'password': self.password}

    def tearDown(self):
        cache.clear()

    def test_post(self):
        UserFactory.create(email=self.username, password=<PASSWORD>)

        request = self.create_request('post', auth=False, data=self.data)
        view = self.view_class.as_view()
        response = view(request)
        self.assertEqual(
            response.status_code, status.HTTP_200_OK, msg=response.data)

        # Ensure user has a token now
        token = self.model.objects.get()
        self.assertEqual(response.data['token'], token.key)

    def test_post_non_existing_user(self):
        """Assert non existing raises an error."""
        request = self.create_request('post', auth=False, data=self.data)
        view = self.view_class.as_view()
        response = view(request)
        self.assertEqual(
            response.status_code,
            status.HTTP_400_BAD_REQUEST,
            msg=response.data,
        )
        expected = 'Unable to log in with provided credentials.'
        self.assertIn(expected, response.data['non_field_errors'])
        self.assertNotIn('token', response.data)

    def test_post_user_not_confirmed(self):
        """Assert non active users can not log in."""
        UserFactory.create(email=self.username, password=<PASSWORD>, is_active=False)

        request = self.create_request('post', auth=False, data=self.data)
{'fraction_non_alphanumeric': 0.09373266777592901, 'fraction_numerical': 0.002958032908116103, 'mean_word_length': 3.659776055124892, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '512678', 'n_tokens_mistral': 1487, 'n_tokens_neox': 1457, 'n_words': 364}