def post(self): """ Add a new user """ from app.controllers.user_controller import UserController post_data = request.get_json() return UserController.create_user(post_data)
def post(self): """ Login user through telegram """ post_data = request.get_json() user_data = { "id": post_data.get("id", None), "first_name": post_data.get("first_name", None), "last_name": post_data.get("last_name", None), "username": post_data.get("username", None), "photo_url": post_data.get("photo_url", None), "auth_date": post_data.get("auth_date", None), "hash": post_data.get("hash", None) } data_check_string = self._concat_params_to_string(user_data) data_check_string_bytes = data_check_string.encode("utf-8") secret_key = os.environ.get("ACCESS_TOKEN", None) secret_key_bytes = hashlib.sha256(secret_key.encode("utf-8")).digest() hmac_string = hmac.new(secret_key_bytes, data_check_string_bytes, hashlib.sha256).hexdigest() # if everything is ok with telegram then from app.controllers.user_controller import UserController return UserController.create_user(post_data)
def test_create_user_method_handles_user_creation_with_non_existent_role_id( self, mock_request_params): with self.app.app_context(): user = UserFactory() role = RoleFactory(name="test_role") UserRoleFactory(role_id=role.id, user_id=user.id) non_existent_role_id = 100 mock_request_params.return_value = [ "Joseph", "Serunjogi", "*****@*****.**", non_existent_role_id, "male", str(datetime.now()), 1, "password", ] user_controller = UserController(self.request_context) response = user_controller.create_user() self.assertEqual(response.status_code, 400) self.assertEqual( response.get_json()["msg"], "Role with userTypeId(roleId) {} does not exist".format( non_existent_role_id), )
def test_create_user_succeeds(self, mock_request_params): with self.app.app_context(): mock_request_params.return_value = [ "*****@*****.**", "Eno", "Bassey", "-LXTuXlk2W4Gskt8KTte", False ] user_controller = UserController(self.request_context) # Act result = user_controller.create_user() # Assert assert result.status_code == 201 assert result.get_json()['msg'] == 'OK'
def test_create_user_method_handles_user_creation_with_duplicate_email_id( self, mock_request_params): with self.app.app_context(): user = UserFactory(email="*****@*****.**", password="******") mock_request_params.return_value = [ user.email, "Joseph", "Serunjogi", "-LXTuXlk2W4Gskt8KTte", False ] user_controller = UserController(self.request_context) response = user_controller.create_user() self.assertEqual(response.status_code, 400) self.assertEqual( response.get_json()['msg'], "User with email '{}' already exists".format(user.email))
def test_create_user_method_handles_user_creation_with_duplicate_user_id( self, mock_request_params): with self.app.app_context(): user = UserFactory(user_id="-LXTuXlk2W4Gskt8KTte") role = RoleFactory(name='test_role') UserRoleFactory(role_id=role.id, user_id=user.id) mock_request_params.return_value = [ "Joseph", "Serunjogi", None, None, user.user_id, role.id ] user_controller = UserController(self.request_context) response = user_controller.create_user() self.assertEqual(response.status_code, 400) self.assertEqual( response.get_json()['msg'], "User with userId '{}' already exists".format(user.user_id))
def test_create_user_succeeds(self, mock_request_params, mock_get_location): location = LocationFactory() role = RoleFactory(name='test_role') with self.app.app_context(): mock_get_location.return_value = location.id mock_request_params.return_value = [ "Joseph", "Serunjogi", None, None, "-LXTuXlk2W4Gskt8KTte", role.id ] user_controller = UserController(self.request_context) # Act result = user_controller.create_user() # Assert assert result.status_code == 201 assert result.get_json()['msg'] == 'OK'
def test_create_user_fails_for_existing_user( self, mock_user_role_repo_new_user_role, # mock_user_repo_new_user, mock_user_repo_exists, mock_role_repo_find_first, mock_request_params, mock_get_location, ): location = LocationFactory() role = RoleFactory(name="test_role") with self.app.app_context(): mock_get_location.return_value = location.id mock_role_repo_find_first.return_value = self.mock_role mock_user_repo_exists.return_value = self.mock_user2 # mock_user_repo_new_user.return_value = None mock_user_role_repo_new_user_role.return_value = self.mock_user_role mock_request_params.return_value = [ "Joseph", "Serunjogi", self.mock_user2.email, role.id, "male", str(datetime.now()), 1, "password", ] user_controller = UserController(self.request_context) # Act result = user_controller.create_user() print(result) print(result.get_json()) # Assert assert result.status_code == 400 assert ( result.get_json()["msg"] == f"User with email '{self.mock_user2.email}' already exists")
def index(): if request.method == 'GET': text_query = request.args.get('q', None) users = None if text_query != None: users = UserController.find_by_name(text_query) else: users = UserController.get_all_users() return {"users": users} if request.method == 'POST': req_body = request.json res = UserController.create_user(req_body) return { 'user': res.get('value'), 'success': res.get('success', False), 'message': res.get('message') } if request.method == 'DELETE': success = UserController.delete_all_users() return {"success": success}
def test_create_user_method_handles_user_creation_with_non_existent_role_id( self, mock_request_params): with self.app.app_context(): user = UserFactory(user_id="-LXTuXlk2W4Gskt8KTte") role = RoleFactory(name='test_role') UserRoleFactory(role_id=role.id, user_id=user.id) non_existent_role_id = 100 mock_request_params.return_value = [ "Joseph", "Serunjogi", None, None, user.user_id, non_existent_role_id ] user_controller = UserController(self.request_context) response = user_controller.create_user() self.assertEqual(response.status_code, 400) self.assertEqual( response.get_json()['msg'], "Role with userTypeId(roleId) {} does not exist".format( non_existent_role_id))
def test_create_user_succeeds( self, mock_user_role_repo_new_user_role, mock_user_repo_new_user, mock_user_repo_exists, mock_role_repo_find_first, mock_request_params, mock_get_location, ): location = LocationFactory() role = RoleFactory(name="test_role") with self.app.app_context(): mock_get_location.return_value = location.id mock_role_repo_find_first.return_value = self.mock_role mock_user_repo_exists.return_value = None mock_user_repo_new_user.return_value = self.mock_user2 mock_user_role_repo_new_user_role.return_value = self.mock_user_role mock_request_params.return_value = [ "Joseph", "Serunjogi", "*****@*****.**", role.id, "male", str(datetime.now()), 1, "password", ] user_controller = UserController(self.request_context) # Act result = user_controller.create_user() # Assert assert result.status_code == 201 assert result.get_json()["msg"] == "OK"