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_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 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(): 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 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 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_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))