def post(self): data = UserRegister.parser.parse_args() if UserModel.find_by_username(data['username']): return {'message': 'A user with that username already exists'}, 400 user = UserModel(**data) user.save_to_db() return {'message': 'User created successfully'}, 201
def test_crud(self): with app.app_context(): user = UserModel('mahsan', 'm1234') self.assertIsNone(UserModel.find_by_username('mahsan')) self.assertIsNone(UserModel.find_by_id(1)) user.save_to_db() self.assertIsNotNone(UserModel.find_by_username('mahsan')) self.assertIsNotNone(UserModel.find_by_id(1))
def test_register_user(self): with self.app() as client: with self.app_context(): request = client.post('/register', data ={'username': '******', 'password': '******'}) self.assertEqual(request.satus_code, 201) self.assertIsNotNone(UserModel.find_by_username('mahsan')) self.assertDictEqual({'message':'user created successfully'}, json.loads(request.data))
def identity(payload): """ function get called when user already authenticated and flask-jwt verified thier authorization header is correct :param payload: a dictionary with 'identity' key wjich is the user id :return: usermodel object """ user_id = payload['identity'] return UserModel.find_by_id(user_id)
def identity(payload): """ Function that gets called when user has already authenticated, and Flask-JWT verified their authorisation header is correct. :param payload: A dictionary with 'identity' key, which is the user id :return: A UserModel object """ user_id = payload['identity'] return UserModel.find_by_id(user_id)
def test_crud(self): with self.app_context(): user = UserModel('test', 'abcd') self.assertIsNone(UserModel.find_by_username('test')) self.assertIsNone(UserModel.find_by_id('1')) user.save_to_db() self.assertIsNotNone(UserModel.find_by_username('test')) self.assertIsNotNone(UserModel.find_by_id('1'))
def identity(payload): ''' Gets called when user has already authenticated ahd Flask-JwT verified the authentication header is correct :param payload: Dict with 'identity', key, which is the user id :return: UserModel object ''' user_id = payload['identity'] return UserModel.find_by_id(user_id)
def authentication(username, password): """ function that get called when a user calls the /auth endpoint with thier username and password :param username:user's username in str format :param password:user's un-encrypted password in str format :return:a usermodel object if authentication was successful """ user = UserModel.find_by_username(username) if user and safe_str_cmp(user.password, password): return user
def authenticate(username, password): """ Function that gets called when a user calls the /auth endpoint with their username and password :param username: User's username instring format :param password: User's un-encripted password in string format :return: A UserModel oject if authentication was successful, None otherwise. """ user = UserModel.find_by_username(username) if user and safe_str_cmp(user.password, password): return user
def test_register_user(self): with self.app as client: # to access the db with self.app_context(): # data is converted into form data, not json response = client.post('/register', data={'username': '******', 'password': '******'}) self.assertEqual(response.status_code, 201) self.assertIsNotNone(UserModel.find_by_username('test')) self.assertDictEqual({'message': 'User created successfully'}, json.loads(response.data.decode('utf-8')))
def Authenticate(username, password): ''' Function get called when a user/pass calls the /auth endpoint :param username: :param password: :return: user is success, else None ''' user = UserModel.find_by_username(username) if user and safe_str_cmp(user.password, password): return user
def test_crud(self): with self.app_context(): user = UserModel('test', 'abcd') self.assertIsNone(UserModel.find_by_username('test'), "Failed - user not found") self.assertIsNone(UserModel.find_by_id(1)) user.save_to_db() self.assertIsNotNone(UserModel.find_by_username('test'), "Failed - user not found") self.assertIsNotNone(UserModel.find_by_id(1))
def test_register_user(self): with self.app() as client: with self.app_context(): response = client.post('/register', data={ 'username': '******', 'password': '******' }) self.assertEqual(response.status_code, 201) self.assertIsNotNone(UserModel.find_by_username('test')) self.assertDictEqual({'message': 'User created successfully.'}, json.loads(response.data))
def setUp(self): super(ItemTest, self).setUp() with self.app() as client: with self.app_context(): UserModel('test', '1234').save_to_db() auth_request = client.post( '/auth', data=json.dumps({ 'username': '******', 'password': '******' }), headers={'Content-type': 'application/json'}) auth_token = json.loads(auth_request.data)['access_token'] self.access_token = f'JWT {auth_token}'
def test_get_item_not_found(self): with self.app() as client: with self.app_context(): UserModel('mahsan', '1234mah').save_to_db() auth_respond = client.post( '/auth', data=json.dumps({ 'username': '******', 'password': '******' }), header={'content_type': 'application/json'}) auth_token = json.loads(auth_respond.data)['access_token'] header = {'authorization': f'JWT {auth_token}'} resp = client.get('/item/pc', header=header) self.assertEqual(resp.status_code, 404)
def test_create_user(self): user = UserModel('test', 'abcd') self.assertEqual(user.username, 'test') self.assertEqual(user.password, 'abcd', 'Failed - Password does not match')