def test_lock_tasks_for_validation_raises_error_if_task_not_found( self, mock_task): # Arrange mock_task.return_value = None lock_dto = LockForValidationDTO() lock_dto.project_id = 1 lock_dto.task_ids = [1, 2] # Act / Assert with self.assertRaises(NotFound): ValidatorService.lock_tasks_for_validation(lock_dto)
def test_lock_tasks_for_validation_raises_error_if_task_not_mapped( self, mock_task): # Arrange task_stub = Task() task_stub.task_status = TaskStatus.READY.value mock_task.return_value = task_stub lock_dto = LockForValidationDTO() lock_dto.project_id = 1 lock_dto.task_ids = [1, 2] # Act / Assert with self.assertRaises(ValidatorServiceError): ValidatorService.lock_tasks_for_validation(lock_dto)
def test_lock_tasks_raises_error_if_project_validator_only_and_user_not_validator( self, mock_project, mock_task, mock_user): # Arrange task_stub = Task() task_stub.task_status = TaskStatus.MAPPED.value mock_task.return_value = task_stub mock_project.return_value = False, ValidatingNotAllowed.USER_NOT_VALIDATOR mock_user.return_value = True lock_dto = LockForValidationDTO() lock_dto.project_id = 1 lock_dto.task_ids = [1, 2] lock_dto.user_id = 1234 with self.assertRaises(ValidatorServiceError): ValidatorService.lock_tasks_for_validation(lock_dto)
def test_lock_tasks_raises_error_if_user_has_not_accepted_license( self, mock_project, mock_task, mock_user): # Arrange task_stub = Task() task_stub.task_status = TaskStatus.MAPPED.value mock_task.return_value = task_stub mock_project.return_value = ( False, ValidatingNotAllowed.USER_NOT_ACCEPTED_LICENSE, ) mock_user.return_value = True lock_dto = LockForValidationDTO() lock_dto.project_id = 1 lock_dto.task_ids = [1, 2] lock_dto.user_id = 123 lock_dto.mapped_by = 1234 with self.assertRaises(UserLicenseError): ValidatorService.lock_tasks_for_validation(lock_dto)
def post(self, project_id): """ Lock tasks for validation --- tags: - tasks produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - in: header name: Accept-Language description: Language user is requesting type: string required: true default: en - name: project_id in: path description: Project ID the tasks are associated with required: true type: integer default: 1 - in: body name: body required: true description: JSON object for locking task(s) schema: properties: taskIds: type: array items: type: integer description: Array of taskIds for locking default: [1,2] responses: 200: description: Task(s) locked for validation 400: description: Client Error 401: description: Unauthorized - Invalid credentials 403: description: Forbidden 404: description: Task not found 409: description: User has not accepted license terms of project 500: description: Internal Server Error """ try: validator_dto = LockForValidationDTO(request.get_json()) validator_dto.project_id = project_id validator_dto.user_id = token_auth.current_user() validator_dto.preferred_locale = request.environ.get( "HTTP_ACCEPT_LANGUAGE") validator_dto.validate() except DataError as e: current_app.logger.error(f"Error validating request: {str(e)}") return {"Error": "Unable to lock task"}, 400 try: tasks = ValidatorService.lock_tasks_for_validation(validator_dto) return tasks.to_primitive(), 200 except ValidatorServiceError as e: error_msg = f"Validator Lock API - {str(e)}" return {"Error": error_msg}, 403 except NotFound: return {"Error": "Task not found"}, 404 except UserLicenseError: return {"Error": "User not accepted license terms"}, 409 except Exception as e: error_msg = f"Validator Lock API - unhandled error: {str(e)}" current_app.logger.critical(error_msg) return {"Error": "Unable to lock task"}, 500