def test_get_auth_token(self): """ Authentication tokens are created using Flask-Login's ``make_secure_token`` function and the email address and password of the user. """ user = User(email='email', password_hash='password_hash') with app.app_context(): self.assertEqual(user.get_auth_token(), make_secure_token('email', 'password_hash'))
def test_different_password_different_token(self): """ If a user has a different password hash, it will have a different token. """ user_1 = User(email='email', password_hash='password_hash') user_2 = User(email='email', password_hash='different_hash') with app.app_context(): self.assertNotEqual(user_1.get_auth_token(), user_2.get_auth_token())
def test_fake_token(self): """ If a token does not belong to a user, ``None`` is returned. """ self.app.post( '/signup', content_type='application/json', data=json.dumps(USER_DATA)) with app.app_context(): self.assertIsNone(load_user_from_token(auth_token='fake'))
def test_load_user_from_token(self): """ A user is loaded if their token is provided to ``load_user_from_token``. """ self.app.post('/signup', content_type='application/json', data=json.dumps(USER_DATA)) response = self.app.post('/login', content_type='application/json', data=json.dumps(USER_DATA)) cookies = response.headers.getlist('Set-Cookie') items = [list(parse_cookie(cookie).items())[0] for cookie in cookies] headers_dict = {key: value for key, value in items} token = headers_dict['remember_token'] with app.app_context(): user = load_user_from_id(user_id=USER_DATA['email']) self.assertEqual(load_user_from_token(auth_token=token), user)
def test_remember_me_cookie_set(self): """ A "Remember Me" token is in the response header of a successful login with the value of ``User.get_auth_token`` for the logged in user. """ self.app.post('/signup', content_type='application/json', data=json.dumps(USER_DATA)) response = self.app.post('/login', content_type='application/json', data=json.dumps(USER_DATA)) cookies = response.headers.getlist('Set-Cookie') items = [list(parse_cookie(cookie).items())[0] for cookie in cookies] headers_dict = {key: value for key, value in items} token = headers_dict['remember_token'] with app.app_context(): user = load_user_from_id(user_id=USER_DATA['email']) self.assertEqual(token, user.get_auth_token())
def test_load_user_from_token(self): """ A user is loaded if their token is provided to ``load_user_from_token``. """ self.app.post( '/signup', content_type='application/json', data=json.dumps(USER_DATA)) response = self.app.post( '/login', content_type='application/json', data=json.dumps(USER_DATA)) cookies = response.headers.getlist('Set-Cookie') items = [list(parse_cookie(cookie).items())[0] for cookie in cookies] headers_dict = {key: value for key, value in items} token = headers_dict['remember_token'] with app.app_context(): user = load_user_from_id(user_id=USER_DATA['email']) self.assertEqual(load_user_from_token(auth_token=token), user)
def test_remember_me_cookie_set(self): """ A "Remember Me" token is in the response header of a successful login with the value of ``User.get_auth_token`` for the logged in user. """ self.app.post( '/signup', content_type='application/json', data=json.dumps(USER_DATA)) response = self.app.post( '/login', content_type='application/json', data=json.dumps(USER_DATA)) cookies = response.headers.getlist('Set-Cookie') items = [list(parse_cookie(cookie).items())[0] for cookie in cookies] headers_dict = {key: value for key, value in items} token = headers_dict['remember_token'] with app.app_context(): user = load_user_from_id(user_id=USER_DATA['email']) self.assertEqual(token, user.get_auth_token())