def test_valid_form(self): proto_user = UserFactory.build() form = SignupForm( { "username": proto_user.username, "email": proto_user.email, "password1": proto_user._password, "password2": proto_user._password, } ) assert form.is_valid()
def test_missing_email(self): proto_user = UserFactory.build() form = SignupForm( { "username": proto_user.username, "email": "", "password1": proto_user._password, "password2": proto_user._password, } ) assert not form.is_valid() assert len(form.errors) == 1 assert "email" in form.errors
def test_duplicate_email(self, user): proto_user = UserFactory.build() form = SignupForm( { "username": proto_user.username, "email": user.email, "password1": proto_user._password, "password2": proto_user._password, } ) assert not form.is_valid() assert len(form.errors) == 1 assert "email" in form.errors
def test_banned_username(self): proto_user = UserFactory.build() form = SignupForm( { "username": "******", "email": proto_user.email, "password1": proto_user._password, "password2": proto_user._password, } ) assert not form.is_valid() assert len(form.errors) == 1 assert "username" in form.errors
def test_mismatched_password(self): proto_user = UserFactory.build() form = SignupForm( { "username": proto_user.username, "email": proto_user.email, "password1": proto_user._password, "password2": fake.password(), } ) assert not form.is_valid() assert len(form.errors) == 1 assert "password2" in form.errors
def test_poor_password(self): proto_user = UserFactory.build() form = SignupForm( { "username": proto_user.username, "email": proto_user.email, "password1": 'password', "password2": 'password', } ) assert not form.is_valid() assert len(form.errors) == 1 assert "password1" in form.errors
class SignupFormView(View): form = SignupForm() template_name = "accounts/signup.html" def get(self, request): print(request.method) return render(request, self.template_name, {"form": self.form}) def post(self, request): if request.method == "POST": form = SignupForm(request.POST) if form.is_valid(): user = form.save(commit=False) print(form.cleaned_data) username = form.cleaned_data["username"] raw_password = form.cleaned_data["password1"] user.save() user = authenticate(username=username, password=raw_password) if user: login(request, user) return redirect("success") else: print(form.errors) return render(request, self.template_name, {"form": form}) else: return render(request, self.template_name, {"form": self.form})
def sign_up(request): if request.method == 'POST': form = SignupForm(request.POST) if form.is_valid(): user = form.save() # Log-in the user right away, then redirect home messages.success(request, 'Account created successfully. Welcome!') login(request, user) return redirect('home') else: form = SignupForm() context = { 'form': form, } return render(request, 'accounts/signup.html', context)
def sign_up(request): if request.method == 'POST': form = SignupForm(request.POST) if form.is_valid(): user = form.save(commit=False) password = forms.CharField(widget=forms.PasswordInput) user.set_password(form.cleaned_data['password']) user.save() # Log-in the user right away messages.success(request, 'Account created successfully. Welcome!') login(request, user) return redirect('/') else: form = SignupForm() context = { 'form': form, } return render(request, 'accounts/signup.html', context)