<commit_before>from django.test import TestCase

# Create your tests here.<commit_msg>add unit test for auth function;<commit_after>'''
The MIT License (MIT)

Copyright (c) 2014 NTHUOJ team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
from django.core.urlresolvers import reverse
from django.test import TestCase, Client
from users.models import User
# Create your tests here.


class UserTestCase(TestCase):
    def setUp(self):
        User.objects.create_user(username='henry', password='<PASSWORD>')

    def test_login(self):
        '''Test basic login function'''
        response = self.client.login(username='henry', password='<PASSWORD>')
        self.assertEqual(response, False)
        response = self.client.login(username='henry', password='<PASSWORD>')
        self.assertEqual(response, True)
        self.client.logout()

    def test_block_wrong_tries(self):
        '''Test if the client will be blocked for over 3 wrong tries'''
        # wrong tries for 3 times
        for wrong_tries in range(3):
            response = self.client.post(
                reverse('users:login'),
                {'username': 'henry', 'password': '<PASSWORD>'})
            self.assertNotContains(response, 'Page Not Found')

        # get blocked for forth wrong tries
        response = self.client.post(
            reverse('users:login'),
            {'username': 'henry', 'password': '<PASSWORD>'})
        self.assertContains(response, 'Page Not Found')

    def test_sign_up(self):
        '''Test if the client is able to sign up'''
        # initial user count
        user_count = User.objects.all().count()

        # sign up with different password confirmation
        response = self.client.post(
            reverse('users:create'),
            {'username': 'boss', 'password1': '<PASSWORD>',
            'password2':'<PASSWORD>', 'email': '<EMAIL>'})
        self.assertNotEqual(User.objects.all().count(), user_count + 1)

        # sign up with invalid email format
        response = self.client.post(
            reverse('users:create'),
            {'username': 'boss', 'password1': '<PASSWORD>',
            'password2':'<PASSWORD>', 'email': 'oj'})
        self.assertNotEqual(User.objects.all().count(), user_count + 1)

        # sign up with used username
        response = self.client.post(
            reverse('users:create'),
            {'username': 'henry', 'password1': '<PASSWORD>',
            'password2':'<PASSWORD>', 'email': 'oj'})
        self.assertNotEqual(User.objects.all().count(), user_count + 1)

        # valid sign up
        response = self.client.post(
            reverse('users:create'),
            {'username': 'boss', 'password1': '<PASSWORD>',
            'password2':'<PASSWORD>', 'email': '<EMAIL>'})
        self.assertEqual(User.objects.all().count(), user_count + 1)
{'fraction_non_alphanumeric': 0.11527377521613832, 'fraction_numerical': 0.004977731202515065, 'mean_word_length': 3.1545157780195865, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '4843112', 'n_tokens_mistral': 1022, 'n_tokens_neox': 988, 'n_words': 363}