Example #1
0
    def test_registration_form(self):
        """
        Test that ``RegistrationForm`` enforces username constraints
        and matching passwords.

        """
        # Create a user so we can verify that duplicate usernames aren't
        # permitted.
        User.objects.create_user('alice', '*****@*****.**', 'secret')

        invalid_data_dicts = [
            # Non-alphanumeric username.
            {
                'data': {
                    'username': '******',
                    'email': '*****@*****.**',
                    'password1': 'foo',
                    'password2': 'foo'
                },
                'error': ('username', [
                    "This value may contain only letters, numbers and @/./+/-/_ characters."
                ])
            },
            # Already-existing username.
            {
                'data': {
                    'username': '******',
                    'email': '*****@*****.**',
                    'password1': 'secret',
                    'password2': 'secret'
                },
                'error':
                ('username', ["A user with that username already exists."])
            },
            # Mismatched passwords.
            {
                'data': {
                    'username': '******',
                    'email': '*****@*****.**',
                    'password1': 'foo',
                    'password2': 'bar'
                },
                'error': ('__all__', ["The two password fields didn't match."])
            },
        ]

        for invalid_dict in invalid_data_dicts:
            form = forms.RegistrationForm(data=invalid_dict['data'])
            self.assertFalse(form.is_valid())
            self.assertEqual(form.errors[invalid_dict['error'][0]],
                             invalid_dict['error'][1])

        form = forms.RegistrationForm(
            data={
                'username': '******',
                'email': '*****@*****.**',
                'password1': 'foo',
                'password2': 'foo'
            })
        self.assertTrue(form.is_valid())
Example #2
0
    def test_registration_form(self):
        """
        Test that ``RegistrationForm`` enforces username constraints
        and matching passwords.
        
        """
        invalid_data_dicts = [
            # Non-alphanumeric username.
            {
                'data': {
                    'username': '******',
                    'email': '*****@*****.**',
                    'password1': 'foo',
                    'password2': 'foo'
                },
                'error': ('username', [u"Enter a valid value."])
            },
            # Already-existing username.
            {
                'data': {
                    'username': '******',
                    'email': '*****@*****.**',
                    'password1': 'secret',
                    'password2': 'secret'
                },
                'error':
                ('username',
                 [u"This username is already taken. Please choose another."])
            },
            # Mismatched passwords.
            {
                'data': {
                    'username': '******',
                    'email': '*****@*****.**',
                    'password1': 'foo',
                    'password2': 'bar'
                },
                'error':
                ('__all__', [u"You must type the same password each time"])
            },
        ]

        for invalid_dict in invalid_data_dicts:
            form = forms.RegistrationForm(data=invalid_dict['data'])
            self.failIf(form.is_valid())
            self.assertEqual(form.errors[invalid_dict['error'][0]],
                             invalid_dict['error'][1])

        form = forms.RegistrationForm(
            data={
                'username': '******',
                'email': '*****@*****.**',
                'password1': 'foo',
                'password2': 'foo',
                'tos': True
            })
        self.failUnless(form.is_valid())
Example #3
0
    def test_registration_form(self):
        """
        Test that ``RegistrationForm`` enforces username constraints
        and matching passwords.

        """
        User = get_user_model()
        # Create a user so we can verify that duplicate usernames aren't
        # permitted.
        User.objects.create_user('alice', '*****@*****.**', 'secret')

        invalid_data_dicts = [
            # Non-alphanumeric username.
            {
                'data': {
                    'username': '******',
                    'email1': '*****@*****.**',
                    'email2': '*****@*****.**'
                },
                'error': ('username', [
                    "This value must contain only letters, numbers and underscores."
                ])
            },
            # Already-existing username.
            {
                'data': {
                    'username': '******',
                    'email1': '*****@*****.**',
                    'email2': '*****@*****.**'
                },
                'error':
                ('username', ["A user with that username already exists."])
            },
            # Mismatched email.
            {
                'data': {
                    'username': '******',
                    'email1': '*****@*****.**',
                    'email2': '*****@*****.**'
                },
                'error': ('__all__', ["The two email fields didn't match."])
            },
        ]

        for invalid_dict in invalid_data_dicts:
            form = forms.RegistrationForm(data=invalid_dict['data'])
            self.failIf(form.is_valid())
            self.assertEqual(form.errors[invalid_dict['error'][0]],
                             invalid_dict['error'][1])

        form = forms.RegistrationForm(
            data={
                'username': '******',
                'email1': '*****@*****.**',
                'email2': '*****@*****.**'
            })
        self.failUnless(form.is_valid())
Example #4
0
 def test_registration_form_first_last(self):
     """Test that First and Last name fields are required"""
     no_firstname = self.__good_data
     no_lastname = self.__good_data
     del no_firstname['first_name']
     del no_firstname['last_name']
     form = forms.RegistrationForm(data=no_lastname)
     self.failIf(form.is_valid())
     self.assertEqual(form.errors['last_name'],
                      [u"This field is required."])
     form = forms.RegistrationForm(data=no_firstname)
     self.failIf(form.is_valid())
     self.assertEqual(form.errors['first_name'],
                      [u"This field is required."])
Example #5
0
    def test_registration_form(self):
        '''Test that ``RegistrationForm`` enforces username constraints
        and matching passwords.'''
        # Create a user so we can verify that duplicate usernames aren't
        # permitted.
        User.objects.create_user('alice', '*****@*****.**', 'secret')

        invalid_data_dicts = [
            # Non-alphanumeric username.
            {'data': {'username': '******',
                      'email': '*****@*****.**',
                      'password1': 'mashup',
                      'password2': 'mashup'},
             'error': ('username', [u"May contain only letters, "\
                                     "numbers and @/./+/-/_."])},
            # Already-existing username.
            {'data': {'username': '******',
                      'email': '*****@*****.**',
                      'password1': '12345',
                      'password2': '12345'},
             'error': ('username', [u"A user with that username "\
                                     "already exists."])},
            # Mismatched passwords.
            {'data': {'username': '******',
                      'email': '*****@*****.**',
                      'password1': 'ana',
                      'password2': '12345'},
             'error': ('__all__', [u"The two password "\
                                    "fields didn't match."])},
            ]

        for invalid_dict in invalid_data_dicts:
            form = forms.RegistrationForm(data=invalid_dict['data'])  # pylint: disable=E1120,E1123
            self.failIf(False == form.is_valid())  # pylint: disable=E1101
            self.assertEqual(
                form.errors[invalid_dict['error'][0]],  # pylint: disable=E1101
                invalid_dict['error'][1])

        form = forms.RegistrationForm(
            data={
                'username': '******',  # pylint: disable=E1120,E1123
                'email': '*****@*****.**',
                'password1': '1234',
                'password2': '1234'
            })
        self.failUnless(form.is_valid())  # pylint: disable=E1101
Example #6
0
 def test_requires_passwords_to_match(self):
     registration_form = forms.RegistrationForm({
         "username": "******",
         "email": "*****@*****.**",
         "password1": "secret",
         "password2": "pswd",
     })
     self.failIf(registration_form.is_valid())
     self.assertEqual(["Passwords must match."],
                      registration_form.errors['__all__'])
Example #7
0
    def test_registration_form_adds_custom_user_name_field(self):
        """
        Test that ``RegistrationForm`` adds custom username
        field and does not raise errors

        """

        form = forms.RegistrationForm()

        self.assertTrue(UsernameField() in form.fields)
Example #8
0
 def test_requires_username_to_be_unique(self):
     User.objects.create_user("alice", "*****@*****.**", "secret")
     registration_form = forms.RegistrationForm({
         "username": "******",
         "email": "*****@*****.**",
         "password1": "secret",
         "password2": "secret",
     })
     self.failIf(registration_form.is_valid())
     self.assertEqual(["Username 'alice' is not available."],
                      registration_form.errors['username'])
Example #9
0
    def test_registration_form(self):
        """Test ``RegistrationForm`` enforces username constraints and matching passwords."""
        # Create a user so we can verify that duplicate usernames aren't permitted.
        User.objects.create_user('alice', '*****@*****.**', 'secret')

        invalid_data_dicts = [
            # Non-alphanumeric username.
            {'data': {'username':   '******',
                      'email':      '*****@*****.**',
                      'first_name': 'boo',
                      'last_name':  'bar',
                      'password1':  'foo',
                      'password2':  'foo'},
            'error': ('username', [u"This value may contain only letters, numbers and @/./+/-/_ characters. No spaces."])},
            # Non-alphabetic first name.
            {'data': {'username':   '******',
                      'email':      '*****@*****.**',
                      'first_name': 'bo*o',
                      'last_name':  'bar',
                      'password1':  'foo',
                      'password2':  'foo'},
              'error': ('first_name', [u"This value may contain only letters, spaces, dashes, and apostrophes."])},
             # Non-alphabetic last name.
            {'data': {'username':   '******',
                      'email':      '*****@*****.**',
                      'first_name': 'boo',
                      'last_name':  'ba}r',
                      'password1':  'foo',
                      'password2':  'foo'},
              'error': ('last_name', [u"This value may contain only letters, spaces, dashes, and apostrophes."])},
            # Already-existing username.
            {'data': {'username':   '******',
                      'email':      '*****@*****.**',
                      'first_name': 'alice',
                      'last_name':  'wonderland',
                      'password1':  'secret',
                      'password2':  'secret'},
            'error': ('username', [u"A user with that username already exists."])},
            # Mismatched passwords.
            {'data': {'username':   '******',
                      'email':      '*****@*****.**',
                      'first_name': 'alice',
                      'last_name':  'wonderland',
                      'password1':  'foo',
                      'password2':  'bar'},
            'error': ('password2', [u"The two password fields didn't match."])},
            ]

        for invalid_dict in invalid_data_dicts:
            form = forms.RegistrationForm(data=invalid_dict['data'])
            self.failIf(form.is_valid())
            self.assertEqual(form.errors[invalid_dict['error'][0]],
                             invalid_dict['error'][1])
Example #10
0
    def test_registration_form_subclass_is_valid(self):
        """
        Test that ``RegistrationForm`` subclasses can save

        """
        data = {'new_field': 'custom username',
                'email': '*****@*****.**',
                'password1': 'foo',
                'password2': 'foo'}

        form = forms.RegistrationForm(data=data)

        self.assertTrue(form.is_valid())
Example #11
0
    def test_registration_form_first_last(self):
        """
        Test that First and Last name fields are required
        """
        form = forms.RegistrationForm(
            data={
                'username': '******',
                'email': '*****@*****.**',
                'first_name': 'first',
                'password1': 'foo',
                'password2': 'foo'
            })
        self.failIf(form.is_valid())
        self.assertEqual(form.errors['last_name'],
                         [u"This field is required."])
        form = forms.RegistrationForm(
            data={
                'username': '******',
                'email': '*****@*****.**',
                'last_name': 'last',
                'password1': 'foo',
                'password2': 'foo'
            })
        self.failIf(form.is_valid())
        self.assertEqual(form.errors['first_name'],
                         [u"This field is required."])

        form = forms.RegistrationForm(
            data={
                'username': '******',
                'email': '*****@*****.**',
                'first_name': 'first',
                'last_name': 'last',
                'password1': 'foo',
                'password2': 'foo',
                'tos': 'on'
            })
        self.failUnless(form.is_valid())
Example #12
0
    def test_registration_form_subclass_is_valid_for_django_18(self):
        """
        Test that ``RegistrationForm`` subclasses can save in
        Django > 1.8

        """
        if DJANGO_VERSION >= StrictVersion('1.8'):
            data = {
                'new_field': 'custom username',
                'email': '*****@*****.**',
                'password1': 'foo',
                'password2': 'foo'
            }

            form = forms.RegistrationForm(data=data)

            self.assertTrue(form.is_valid())
Example #13
0
    def test_registration_form(self):
        """
        Test that ``RegistrationForm`` enforces username constraints
        and matching passwords.

        """
        # Create a user so we can verify that duplicate usernames aren't
        # permitted.
        UserModel().objects.create_user('alice', '*****@*****.**', 'secret')
        bad_username_error = (
            'Enter a valid username. This value may contain only letters, '
            'numbers, and @/./+/-/_ characters.')
        if django.VERSION < (1, 10):
            bad_username_error = bad_username_error.replace(
                'numbers,', 'numbers')
        elif six.PY2:
            bad_username_error = bad_username_error.replace(
                'letters', 'English letters')
        invalid_data_dicts = [
            # Non-alphanumeric username.
            {
                'data': {
                    'username': '******',
                    'email': '*****@*****.**',
                    'password1': 'foo',
                    'password2': 'foo'
                },
                'error': ('username', [bad_username_error])
            },
            # Already-existing username.
            {
                'data': {
                    'username': '******',
                    'email': '*****@*****.**',
                    'password1': 'secret',
                    'password2': 'secret'
                },
                'error':
                ('username', ["A user with that username already exists."])
            },
            # Mismatched passwords.
            {
                'data': {
                    'username': '******',
                    'email': '*****@*****.**',
                    'password1': 'foo',
                    'password2': 'bar'
                },
                'error':
                ('password2', ["The two password fields didn't match."])
            },
        ]

        for invalid_dict in invalid_data_dicts:
            form = forms.RegistrationForm(data=invalid_dict['data'])
            self.assertFalse(form.is_valid())
            self.assertEqual(form.errors[invalid_dict['error'][0]],
                             invalid_dict['error'][1])

        form = forms.RegistrationForm(
            data={
                'username': '******',
                'email': '*****@*****.**',
                'password1': 'foo',
                'password2': 'foo'
            })
        self.assertTrue(form.is_valid())
Example #14
0
 def test_correct_form_is_correct(self):
     """Test that a correct form with all required fields is valid"""
     form = forms.RegistrationForm(data = self.__good_data) 
     self.failUnless(form.is_valid())
Example #15
0
             first_name=u.first_name,
             last_name=u.last_name,
             appuser_id=t.appuser_id,
             profile_image_url=profile_image,
             access_token=t.access_token
         )
         # logout(request)
         # request.user_profile = None
         request.session['OPEN_PROFILE'] = open_profile
     else:
         return HttpResponseRedirect(reverse('account'))
 redirect_to = request.REQUEST.get(redirect_field_name, '')
 open_profile = request.session.get('OPEN_PROFILE', None)
 if request.method == 'POST':
     captcha_answer = captcha.get_answer(request)
     form = forms.RegistrationForm(captcha_answer=captcha_answer, data=request.POST, files=request.FILES, open_profile=open_profile, session=request.session)
     if form.is_valid():
         user = form.save()
         try:
             if open_profile:
                 del request.session['OPEN_PROFILE']
         except KeyError:
             pass
         password=form.cleaned_data['password1']
         user = authenticate(username=user.username, password=password)
         user_profile = getattr(user, "_profile", UserProfile.objects.get(user=user))
         login(request, user)
         user.message_set.create(message=_(u'Thank you for signing-up!'))
         _log.info("User registered: %s", user.username)
         email_template('Welcome to %s!' % settings.UI_SETTINGS['UI_SITE_TITLE'],
                            'registration/email/welcome.html',