def test_register_new_user(self): """ Register a new user and assert that they're in the database. """ password = "******" expected_user = User( name="testo", email="*****@*****.**", password=password, ) nonexistent_user = User.get_by_email(expected_user.email) self.assertIsNone(nonexistent_user) response = self.register_new_user(expected_user.name, expected_user.email, password) self.assert_successful_registration(response) actual_user = User.get_by_email(expected_user.email) self.assertTrue(actual_user.check_password(password)) self.assertEqual(actual_user.email, expected_user.email) self.assertEqual(actual_user.name, expected_user.name)
def test_get_user_by_id(self): username = "******" password = "******" email = "*****@*****.**" resp = self.register_new_user( username=username, password=password, email=email, ) self.assert_successful_registration(resp) user = User.get_by_email("*****@*****.**") user_by_id = User.get_by_id(user.id) self.assertEqual(user.id, user_by_id.id) self.assertEqual(user.email, user_by_id.email)
def login(): form = LoginForm(request.form) # If form contains valid data (not necessarily good creds)... if form.validate_on_submit(): user = User.get_by_email(form.email.data) if user and user.check_password(form.password.data): login_user(user) flash(LOGIN_WELCOME_MESSAGE.format(username=user.name)) # If the user was redirected to this page from a page that requires being logged in, # then the query parameter `next` will hold the next URL to go to. next_url = request.args.get("next") or url_for("users.profile") return redirect(next_url) flash(INVALID_CREDS, "error-message") # By passing the form into the template, we refill the fields with the values the user provided, if any. return render_template('users/login.html', form=form)