def lock_task_for_mapping(lock_task_dto: LockTaskDTO) -> TaskDTO: """ Sets the task_locked status to locked so no other user can work on it :param lock_task_dto: DTO with data needed to lock the task :raises TaskServiceError :return: Updated task, or None if not found """ task = MappingService.get_task(lock_task_dto.task_id, lock_task_dto.project_id) if not task.is_mappable(): raise MappingServiceError('Task in invalid state for mapping') user_can_map, error_reason = ProjectService.is_user_permitted_to_map( lock_task_dto.project_id, lock_task_dto.user_id) if not user_can_map: if error_reason == MappingNotAllowed.USER_NOT_ACCEPTED_LICENSE: raise UserLicenseError( 'User must accept license to map this task') else: raise MappingServiceError( f'Mapping not allowed because: {error_reason.name}') task.lock_task_for_mapping(lock_task_dto.user_id) return task.as_dto_with_instructions(lock_task_dto.preferred_locale)
def test_user_not_permitted_to_map_if_already_locked_tasks(self, mock_project, mock_user_tasks): # Arrange mock_project.return_value = Project() mock_user_tasks.return_value = [1] #Act allowed, reason = ProjectService.is_user_permitted_to_map(1, 1) # Assert self.assertFalse(allowed)
def test_user_cant_map_if_project_not_published(self, mock_project): # Arrange stub_project = Project() stub_project.status = ProjectStatus.DRAFT.value mock_project.return_value = stub_project # Act allowed, reason = ProjectService.is_user_permitted_to_map(1, 1) # Assert self.assertFalse(allowed) self.assertEqual(reason, MappingNotAllowed.PROJECT_NOT_PUBLISHED)
def test_user_not_permitted_to_map_if_user_is_blocked( self, mock_project, mock_user_blocked): # Arrange mock_project.return_value = Project() mock_user_blocked.return_value = True # Act allowed, reason = ProjectService.is_user_permitted_to_map(1, 1) # Assert self.assertFalse(allowed) self.assertEqual(reason, MappingNotAllowed.USER_NOT_ON_ALLOWED_LIST)
def test_user_not_permitted_to_map_if_user_has_not_accepted_license( self, mock_project, mock_user_tasks, mock_user_service): # Arrange stub_project = Project() stub_project.license_id = 11 mock_project.return_value = stub_project mock_user_tasks.return_value = [] mock_user_service.return_value = False #Act allowed, reason = ProjectService.is_user_permitted_to_map(1, 1) # Assert self.assertFalse(allowed)