def register(request): """Register method of account""" if request.method == 'POST': form = forms.RegistrationForm(data=request.POST, files=request.FILES) if form.is_valid(): new_user = form.save() activation_key = form.set_activation_key() # send a signal that new user has been registered USER_REGISTERED_SIGNAL.send(sender=form.__class__, request=request, user=new_user) # Send confirmation email to new user if settings.DEFAULT_FROM_EMAIL and settings.AUTO_APPROVE_NEW_USERS: form.send_confirm_mail(request, activation_key) messages.add_message( request, messages.SUCCESS, _('Your account has been created, please check your mailbox for confirmation' )) else: messages.add_message( request, messages.WARNING, _('Your account has been created, but you need an administrator to activate it' )) messages.add_message(request, messages.INFO, _('Following is the administrator list')) # super-users can approve others for user in User.objects.filter(is_superuser=True): messages.add_message( request, messages.INFO, '<a href="mailto:{}">{}</a>'.format( user.email, user.get_full_name() or user.username)) # site admins should be able to do so too for name, email in settings.ADMINS: messages.add_message( request, messages.WARNING, '<a href="mailto:{}">{}</a>'.format(email, name)) return HttpResponseRedirect(reverse('core-views-index')) else: form = forms.RegistrationForm() context_data = { 'form': form, } return render(request, 'registration/registration_form.html', context_data)
def test_captcha_required_when_enabled(self): importlib.reload(forms) form = forms.RegistrationForm(data=self.data) self.assertFalse(form.is_valid()) self.assertIn("captcha", form.errors.keys()) self.assertIn(_("This field is required."), form.errors["captcha"])
def test_captcha_not_required_when_disabled(self): form = forms.RegistrationForm(data=self.data) self.assertTrue(form.is_valid()) self.assertNotIn("captcha", form.errors.keys())
def test_captcha_fails_when_wrong(self): data = self.data.copy() data["captcha_0"] = "correct" data["captcha_1"] = "WRONG" importlib.reload(forms) form = forms.RegistrationForm(data=data) self.assertFalse(form.is_valid()) self.assertIn("captcha", form.errors.keys()) self.assertIn(_("Invalid CAPTCHA"), form.errors["captcha"])
def test_user_created_when_commit(self): importlib.reload(forms) form = forms.RegistrationForm(data=self.data) user = form.save() self.assertIsNotNone(user.pk)
def test_user_not_created_when_commit(self): form = forms.RegistrationForm(data=self.data) user = form.save(commit=False) self.assertIsNone(user.pk)