def decorated_function(*args, **kwargs): auth_header = request.headers.get('Authorization') if not auth_header: raise UnauthorizedException() auth_token = auth_header.split(" ")[1] user_id = User.decode_auth_token(auth_token) user = User.get(user_id) if not user or not user.active: raise UnauthorizedException(message='Something went wrong. Please contact us.') return f(user_id, *args, **kwargs)
def decorated(*args, **kwargs): token = None # get auth token if 'x-access-token' in request.headers: token = request.headers['x-access-token'] if not token: return {'message': 'Token is missing!'}, 401 resp = User.decode_auth_token(token) current_user = User.query.get(resp) return f(current_user=current_user, *args, **kwargs)
def test_user_model(self): name = "kenny" email = "*****@*****.**" add_user(name=name, email=email) user = User.query.filter_by(name=name).first() db.session.commit() auth_token = user.encode_auth_token(user.id) self.assertEqual(user.name, name) self.assertEqual(user.email, email) self.assertTrue(isinstance(auth_token, bytes)) self.assertTrue( User.decode_auth_token(auth_token.decode("utf-8")) == 1)
def test_login_success(self): name = "Joe" email = "*****@*****.**" response = register_user(self, name, email) data = json.loads(response.data.decode()) user_id = User.decode_auth_token(data['auth_token']) user = User.query.get(user_id) self.assertEqual(response.status_code, 200) self.assertEqual(data['status'], 'success') self.assertEqual(data['message'], 'Successfully logged in') self.assertTrue('auth_token' in data) self.assertEqual(user.email, email)
def test_model_user_decode_auth_token(self): """Ensure decoding auth token works""" user = add_user() auth_token = user.encode_auth_token() self.assertTrue(isinstance(auth_token, bytes)) self.assertTrue(User.decode_auth_token(auth_token), user.id)
def test_decode_auth_token(self): user = add_user('justatest', '*****@*****.**', 'test') auth_token = user.encode_auth_token() self.assertTrue(isinstance(auth_token, bytes)) self.assertTrue(User.decode_auth_token(auth_token), user.id)