def test_empty_form_invalid(self): form = HomebuyerSignupForm({}) self.assertFalse(form.is_valid()) self.assertIn('password', form.errors) self.assertIn('password_confirmation', form.errors) self.assertIn('first_name', form.errors) self.assertIn('last_name', form.errors)
def test_passwords_dont_match_invalid(self): form = HomebuyerSignupForm({ 'password': '******', 'password_confirmation': 'bar', }) self.assertFalse(form.is_valid()) self.assertIn('password_confirmation', form.errors)
def test_matching_token_valid(self): form = HomebuyerSignupForm({ 'password': '******', 'password_confirmation': 'foo', 'first_name': 'f', 'last_name': 'l', }) self.assertTrue(form.is_valid())
def post(self, request, *args, **kwargs): """ Handles the creation of User/Homebuyer/Couple instances when signing up a new homebuyer. If the form is not valid, re-render it with errors so the user can correct them. Otherwise: - Create the User object from the form data. - Check to see if there is an existing Couple instance (which happens if there partner already registered). If there is, create the Homebuyer instance and connect with the existing Couple instance. Otherwise create a brand new instance. - Check to see if everyone in the PendingCouple instance has been registered. If so, the PendingCouple/PendingHomebuyer instances have been translated into real users and can be deleted. - Authenticate/login the newly registered user and direct them to the home page. """ token = kwargs.get('registration_token') pending_homebuyer = PendingHomebuyer.objects.get( registration_token=token) realtor = pending_homebuyer.pending_couple.realtor form = HomebuyerSignupForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data with transaction.atomic(): email = pending_homebuyer.email password = cleaned_data['password'] user = User.objects.create_user( email=email, password=password, first_name=cleaned_data['first_name'], last_name=cleaned_data['last_name'], phone=cleaned_data['phone'], email_confirmed=True) pending_couple = pending_homebuyer.pending_couple couple = pending_couple.couple if not couple: couple = Couple.objects.create(realtor=realtor) Homebuyer.objects.create(user=user, couple=couple) if pending_couple.registered: pending_couple.pendinghomebuyer_set.all().delete() pending_couple.delete() user = authenticate(email=email, password=password) login(request, user) messages.success(request, "Welcome!") return redirect(reverse(settings.LOGIN_REDIRECT_URL)) context = { 'registration_token': token, 'signup_form': form, } return render(request, self.template_name, context)
def get(self, request, *args, **kwargs): """ Renders the signup form for registering a homebuyer. """ token = kwargs.get('registration_token') pending_homebuyer = PendingHomebuyer.objects.get( registration_token=token) realtor = pending_homebuyer.pending_couple.realtor context = { 'registration_token': token, 'signup_form': HomebuyerSignupForm(initial={ 'email': pending_homebuyer.email, 'first_name': pending_homebuyer.first_name, 'last_name': pending_homebuyer.last_name, }), } msg = ("Welcome, {name}.<br>You have been invited by {realtor_name} " "({realtor_email}).<br>Please fill out the form below to " "register for {app_name}.".format( name=pending_homebuyer.first_name, realtor_name=realtor.full_name, realtor_email=realtor.email, app_name=settings.APP_NAME)) messages.info(request, msg) return render(request, self.template_name, context)