Beispiel #1
0
 def test_username_regex_rule(self):
     ''' Test username abides by set regex rule '''
     self.entries['username'] = '******'
     form = RegistrationForm(data=self.entries)
     self.assertFalse(form.is_valid())
     with self.assertRaises(forms.ValidationError):
         form.clean_username()
Beispiel #2
0
 def test_unique_username(self):
     ''' Test only unique usernames accepted '''
     self.entries['username'] = '******'
     form = RegistrationForm(data=self.entries)
     self.assertFalse(form.is_valid())
     with self.assertRaises(forms.ValidationError):
         form.clean_username()
Beispiel #3
0
    def test_form_validation(self):
        ''' Test registration rorm validation works correctly '''

        form_data = {
            'username': '******',
        }
        form = RegistrationForm(data=form_data)
        self.assertFalse(form.is_valid())
Beispiel #4
0
    def test_passwords_dont_match(self):
        ''' Tests validation error thrown if the two passwords dont match '''

        form_addr = reverse('registration')
        self.entries['password2'] = 'test2'
        response = self.client.post(form_addr, self.entries)
        self.assertEqual(response.status_code, 200)
        msg = 'Passwords do not match!'
        self.assertFormError(response, 'form', 'password2', msg)
        form = RegistrationForm(data=self.entries)
        with self.assertRaises(forms.ValidationError):
            form.clean_password2()
Beispiel #5
0
def register_user(request):
    if request.user.is_authenticated():
        return HttpResponse(content="Forbidden", status=403)
    else:
        args = {}
        args.update(csrf(request))
        if request.method == 'POST':
            form = RegistrationForm(request.POST)
            args['form'] = form
            if form.is_valid():
                form.save()  # save user to database if form is valid

                username = form.cleaned_data['username']
                email = form.cleaned_data['email']
                salt = hashlib.sha1(str(random.random())).hexdigest()[:5]
                activation_key = hashlib.sha1(salt + email).hexdigest()
                key_expires = datetime.datetime.today() + datetime.timedelta(2)

                #Get user by username
                user = User.objects.get(username=username)

                # Create and save user profile
                new_profile = UserProfile(user=user,
                                          activation_key=activation_key,
                                          key_expires=key_expires)
                new_profile.save()

                # Send email with activation key
                email_subject = 'User account confirmation'
                email_body = "Hello, %s, thank you for creating your profile! \
                To activate your profile please open this link within next 48h \
                http://127.0.0.1:8000/user-account/submit/%s" % (
                    username, activation_key)

                send_mail(email_subject,
                          email_body,
                          '*****@*****.**', [email],
                          fail_silently=False)

                return HttpResponseRedirect(
                    '/user-account/account-successfully-created')
        else:
            args['form'] = RegistrationForm()

        return render_to_response('user_account/register.html',
                                  args,
                                  context_instance=RequestContext(request))