def testUserInfoNotValid_UsernameNotUnique(self): """ Attempt to register with a taken username. Verify that submission via POST with a username that is already in use rerenders the template and shows an error without creating the user account """ # Get the POST dict registration_form = Factory.create_user_registration_post_dict(self.organization) registration_form['username'] = self.user.username # Make the post request response = self.client.post('/register/{}'.format(self.info.session.uuid), registration_form, follow = True) # Verify that the register page was rendered self.assertTemplateUsed(response, 'user/register.html') # Verify both forms are included in the response self.assertTrue('user_form' in response.context) self.assertTrue('info_form' in response.context) # Verify form fields retain their set values user_form = response.context['user_form'] info_form = response.context['info_form'] # Validate self.validate_form(user_form, registration_form, ('username')) self.validate_form(info_form, registration_form, ())
def testUserInfoNotValid_PasswordConfirmationFailed(self): """ Verify that submission via POST with a failed password confirmation rerenders the template and shows an error without creating the user account """ # Get the POST dict registration_form = Factory.create_user_registration_post_dict(self.organization) registration_form['password2'] = 'incorrect' # Make the post request response = self.client.post('/register/{}'.format(self.info.session.uuid), registration_form, follow = True) # Verify that the register page was rendered self.assertTemplateUsed(response, 'user/register.html') # Verify both forms are included in the response self.assertTrue('user_form' in response.context) self.assertTrue('info_form' in response.context) # Verify form fields retain their set values user_form = response.context['user_form'] self.validate_form(user_form, registration_form, ('password2')) # lead stuff info_form = response.context['info_form'] self.validate_form(info_form, registration_form, ())
def testOrganizationCodeInvalid(self): """ Verify an invalid organization sends an error """ # Get the POST dict registration_form = Factory.create_user_registration_post_dict(self.organization) registration_form['organization_code'] = 'invalid' # Make the post request response = self.client.post('/register/{}'.format(self.info.session.uuid), registration_form, follow = True) # Get the forms user_form = response.context['user_form'] self.validate_form(user_form, registration_form, ()) info_form = response.context['info_form'] self.validate_form(info_form, registration_form, ('organization_code'))
def testValidInfo(self): """ Verify that submission of valid information creates the user account, the lead info, logs the user in, sets a success message, and redirects to the dashboard view """ # Get the POST dict registration_form = Factory.create_user_registration_post_dict(self.organization) # Make the post request response = self.client.post('/register/{}'.format(self.info.session.uuid), registration_form, follow = True) # Verify the user account is created with correct attributes user = User.objects.get(username = registration_form['username']) info = LeadUserInfo.objects.get(user = user) self.assertTrue(user is not None) self.assertEqual(user.username, registration_form['username']) self.assertEqual(user.email, registration_form['email']) self.assertNotEqual(user.password, registration_form['password1']) # Password should be hashed self.assertEqual(user.first_name, registration_form['first_name']) self.assertEqual(user.last_name, registration_form['last_name']) self.assertTrue(info is not None) self.assertEqual(info.user, user) self.assertEqual(info.gender, registration_form['gender']) self.assertEqual(info.major, registration_form['major']) self.assertEqual(info.organization, self.organization) # Verify success message set messages = response.context['messages'] self.assertEqual(len(messages), 1) for message in messages: self.assertEqual(message.message, "User account created successfully.") # Verify the user is redirected to the dashboard self.assertTemplateUsed(response, 'dashboard.html')