def test_cannot_sign_up_existing_username_or_email(self): """ Users should be unable to sign up with a username or email that's already been registered. """ signup_form = SignupForm(data=self.form_data) self.assertTrue(signup_form.is_valid()) User.objects.create_user( username=self.username, password=self.password, email=self.email ) # Can't sign up same username twice signup_form = SignupForm( data={ "username": self.username, "email": random_email(self.rd), "password": self.password, "repassword": self.password, } ) self.assertFalse(signup_form.is_valid()) # Can't sign up same email twice signup_form = SignupForm( data={ "username": random_username(self.rd), "email": self.email, "password": self.password, "repassword": self.password, } ) 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_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_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_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)