<commit_before><commit_msg>User-based forms tests<commit_after># coding: utf-8

import unittest

from django.utils.translation import ugettext as _
from django import forms

from users.forms import (SimpleRegistrationForm, ForgotPasswordForm)
from users.models import Users
from test.factories import UserFactory


class SimpleRegistrationFormTest(unittest.TestCase):
    ''' Registration form test'''

    def setUp(self):
        self.data = {
            'first_name':'test',
            'last_name': 'user',
            'email': '<EMAIL>',
            'password1': '<PASSWORD>',
            'password2': '<PASSWORD>'
        }
        self.user = UserFactory()
    
    def tearDown(self):
        self.data = None
        self.user.delete()
        Users.objects.all().delete()

    def test_user_creation(self):
        form = SimpleRegistrationForm(self.data)
        self.assertTrue(form.is_bound)
        self.assertTrue(form.is_valid())

    def test_lost_firstname(self):
        self.data['first_name'] = None 
        form = SimpleRegistrationForm(self.data)
        self.assertTrue(form.is_bound)
        self.assertFalse(form.is_valid())
        self.assertEqual([_(u'This field is required.'),],
            form['first_name'].errors)

    def test_empty_firstname(self):
        self.data['first_name'] ='   '
        form = SimpleRegistrationForm(self.data)
        self.assertTrue(form.is_bound)
        self.assertFalse(form.is_valid())
        self.assertEqual([_(u'You must provide a first name!'),],
            form['first_name'].errors)

    def test_lost_lastname(self):
        self.data['last_name'] = None 
        form = SimpleRegistrationForm(self.data)
        self.assertTrue(form.is_bound)
        self.assertFalse(form.is_valid())
        self.assertEqual([_(u'This field is required.'),],
            form['last_name'].errors)

    def test_empty_lastname(self):
        self.data['last_name'] ='   '
        form = SimpleRegistrationForm(self.data)
        self.assertTrue(form.is_bound)
        self.assertFalse(form.is_valid())
        self.assertEqual([_(u'You must provide a last name!'),],
            form['last_name'].errors)

    def test_existing_email(self):
        self.data['email'] = self.user.email
        form = SimpleRegistrationForm(self.data)
        self.assertTrue(form.is_bound)
        self.assertFalse(form.is_valid())
        self.assertEqual([_(u'This email already registered'),],
            form['email'].errors)

    def test_passwords_diff(self):
        self.data['password1'] = '<PASSWORD>'
        form = SimpleRegistrationForm(self.data)
        self.assertTrue(form.is_bound)
        self.assertFalse(form.is_valid())
        self.assertEqual([_(u'The password fields did not match'),],
            form.errors['__all__'])


class TestForgotPasswordForm(unittest.TestCase):
    '''
    First step reset password functionality - validate email 
    '''

    def setUp(self):
        self.user = UserFactory()

    def tearDown(self):
        self.user.delete()

    def test_reset_password(self):
        data = {'email': self.user.email}
        form = ForgotPasswordForm(data)
        self.assertTrue(form.is_bound)
        self.assertTrue(form.is_valid())

    def test_unknown_email(self):
        data = {'email': '<EMAIL>',}
        form = ForgotPasswordForm(data)
        self.assertTrue(form.is_bound)
        self.assertFalse(form.is_valid())
        self.assertEqual([_(u'This email isn\'t registered'),],
            form['email'].errors)
{'fraction_non_alphanumeric': 0.12748438387279953, 'fraction_numerical': 0.001135718341851221, 'mean_word_length': 2.728042328042328, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '6587426', 'n_tokens_mistral': 1020, 'n_tokens_neox': 1010, 'n_words': 181}