def test_password_and_repassword_must_match(self): """ In the signup form, the two passwords that the user enters must match with one another. """ signup_form = SignupForm( data={ "username": self.username, "email": self.email, "password": random_password(self.rd), "repassword": random_password(self.rd), } ) self.assertFalse(signup_form.is_valid())
def test_signup_as_registered_email_or_user(self): """ Attempt to sign up as new user with registered email address or username. """ # Start by adding a new user to the database user = User.objects.create_user(email=self.email, username=self.username, password=self.password) # Attempt to sign up a user with the same email newpass = random_password(self.rd) form_data = { "email": self.email, "username": random_username(self.rd), "password": newpass, "repassword": newpass, } response = self.client.post(reverse("signup"), form_data) self.assertEqual(len(User.objects.all()), 1) self.assertEqual(user, User.objects.get(email=self.email)) self.assertFormError(response, "form", "email", "User with this Email already exists.") # Attempt to sign up a user with the same username form_data["email"] = random_email(self.rd) form_data["username"] = self.username response = self.client.post(reverse("signup"), form_data) self.assertEqual(len(User.objects.all()), 1) self.assertEqual(user, User.objects.get(username=self.username)) self.assertFormError(response, "form", "username", "User with this Username already exists.")
def test_nonmatching_passwords(self): # Meepy tries to sign up for the site, but enters different passwords # in the password fields. Her signup is unsuccessful, and the site # displays an error message to her. self.assertEqual(len(User.objects.all()), 0) self.sign_up( self.email, self.username, random_password(self.rd), repassword=random_password(self.rd), ) self.assertEqual(len(User.objects.all()), 0) alerts = self.browser.find_elements_by_class_name("form-error") self.assertEqual(len(alerts), 1) self.assertEqual( alerts[0].text, "The passwords you've entered don't match. Please try again.", )
def test_invalid_login(self): # Try a couple of invalid logins login_form = LoginForm( data={"username": self.username, "password": random_password(self.rd)} ) self.assertFalse(login_form.is_valid()) login_form = LoginForm( data={"username": random_username(self.rd), "password": self.password} ) self.assertFalse(login_form.is_valid())
def test_sign_up_with_nonmatching_passwords(self): """ Attempt to sign up with different data in the two password fields. """ self.form_data["repassword"] = random_password(self.rd) response = self.client.post(reverse("signup"), self.form_data) self.assertEqual(len(User.objects.all()), 0) self.assertFormError( response, "form", "password", "The passwords you've entered don't match. Please try again.", )
def test_invalid_login(self): # An invalid login should redirect to the login page username = random_username(self.rd) password = random_password(self.rd) login_data = {"username": username, "password": password} # Ensure we can't login self.client.login(username=username, password=password) self.assertFalse(get_user(self.client).is_authenticated) logout(self.client) # Attempt login through the login form self.client.post(reverse("login"), login_data) self.assertFalse(get_user(self.client).is_authenticated)
def test_signup_with_invalid_password(self): # Meepy tries to sign up with a password that's too short. self.assertEqual(len(User.objects.all()), 0) self.sign_up(self.email, self.username, random_password(self.rd)[:6]) self.assertEqual(len(User.objects.all()), 0) # Note: signup button should be disabled, there should be a min length # attribute on the password and repassword inputs. # Meepy tries to sign up with a password that's too basic self.sign_up(self.email, self.username, "password") self.assertEqual(len(User.objects.all()), 0) alerts = self.browser.find_elements_by_class_name("form-error") self.assertEqual(len(alerts), 1) self.assertEqual(alerts[0].text, "This password is too common.")
def test_signup_with_existing_username_or_email(self): # Somebody else comes along and creates a user with the username and # email address that Meepy wants to sign up with. user = User.objects.create_user(email=self.email, username=self.username, password=random_password(self.rd)) user_password = user.password # Meepy tries to sign up for the site. First, she tries to sign up with # an email address that already has an account. self.assertEqual(len(User.objects.all()), 1) self.sign_up(self.email, random_username(self.rd), self.password) self.assertEqual(len(User.objects.all()), 1) alerts = self.browser.find_elements_by_class_name("form-error") self.assertEqual(len(alerts), 1) self.assertEqual(alerts[0].text, "User with this Email already exists.") # (Existing user's password should not have been modified) self.assertEqual( User.objects.get(email=self.email).password, user_password) # Failing that, Meepy tries to sign up with a username that's already # been registered. self.sign_up(random_email(self.rd), self.username, self.password) self.assertEqual(len(User.objects.all()), 1) alerts = self.browser.find_elements_by_class_name("form-error") self.assertEqual(len(alerts), 1) self.assertEqual(alerts[0].text, "User with this Username already exists.") # (Existing user's password should not have been modified) self.assertEqual( User.objects.get(username=self.username).password, user_password)