class UsersTest(unittest.TestCase): def setUp(self): """ Using dicts instead of Shelve objects to speed up testing. """ self.fake_database = FakeDatabaseService() self.users = Users(self.fake_database) self.username = '******' self.password = '******' def test_confirm_passwords(self): self.assertTrue(confirmed_password_valid('foo', 'foo')) self.assertFalse(confirmed_password_valid('foo', 'bar')) def test_simple_add_users(self): self.assertFalse(self.users.has_user(self.username)) self.users.register_user(self.username, self.password) self.assertTrue(self.users.has_user(self.username)) self.assertTrue( \ self.users.try_login_user(self.username, self.password) is not None) def test_login_before_register(self): self.assertTrue( \ self.users.try_login_user(self.username, self.password) is None) def test_login_with_wrong_password(self): self.users.register_user(self.username, self.password) self.assertTrue( \ self.users.try_login_user(self.username, 'bad_password') is None)
def login(): """ Handle a login attempt Returns: A JSON string that JavaScript will process communicating the results of the login attempt. """ username = request.form['username'] password = request.form['password'] users = Users(g.database_service) user = users.try_login_user(username, password) if user is None: return fail_login('Login failed. Maybe you made a typo?') else: session['user_id'] = user.get_id() return success_login()