def test_encode_auth_token(self): user = User(email='*****@*****.**', password='******', registered_on=datetime.datetime.utcnow()) db.session.add(user) db.session.commit() auth_token = user.encode_auth_token(user.id) self.assertTrue(isinstance(auth_token, bytes))
def test_decode_auth_token(self): user = User(email='*****@*****.**', password='******', registered_on=datetime.datetime.utcnow()) db.session.add(user) db.session.commit() auth_token = User.encode_auth_token(user.id) self.assertTrue( User.decode_auth_token(auth_token.decode('utf-8')) == user.id)
def test_encode_auth_token(self): user = User(email='*****@*****.**', last_name='Arthur', name='Oliver', password='******', registered_on=datetime.datetime.utcnow()) db.session.add(user) db.session.commit() auth_token = User.encode_auth_token(user.id) self.assertTrue(isinstance(auth_token, bytes))
def generate_token(user): try: # generate the auth token auth_token = User.encode_auth_token(user.id) response_object = { 'status': 'success', 'message': 'Successfully registered.', 'Authorization': auth_token.decode() } return response_object, 201 except Exception as e: response_object = { 'status': 'fail', 'message': 'Some error occurred. Please try again.' } return response_object, 401
def login_user(data): try: # fetch the user data user = User.query.filter_by(email=data.get('email')).first() if user and user.check_password(data.get('password')): auth_token = User.encode_auth_token(user.id) if auth_token: response_object = { 'status': 'success', 'message': 'Successfully logged in.', 'Authorization': auth_token.decode() } return response_object, 200 else: response_object = { 'status': 'fail', 'message': 'email or password does not match.' } return response_object, 401 except Exception as e: print(e) response_object = {'status': 'fail', 'message': 'Try again'} return response_object, 500