def validate_experience(self, experience): if experience.author_id is None: raise InvalidEntityException(source='author', code='empty_attribute', message='Author cannot be empty') if experience.title is None: raise InvalidEntityException(source='title', code='empty_attribute', message='Title cannot be empty') if type(experience.title) is not str: raise InvalidEntityException(source='title', code='wrong_type', message='Title must be string') if len(experience.title) < ExperienceValidator.MIN_TITLE_LENGHT or \ len(experience.title) > ExperienceValidator.MAX_TITLE_LENGHT: raise InvalidEntityException( source='title', code='wrong_size', message='Title must be between 1 and 30 chars') if experience.description is not None and type( experience.description) is not str: raise InvalidEntityException(source='description', code='wrong_type', message='Description must be string') return True
def test_invalid_scene_returns_error_and_doesnt_update_it(self): scene = Scene(id='1', title='', description='', latitude=0, longitude=0, experience_id=0) scene_repo = Mock() scene_repo.get_scene.return_value = scene scene_validator = Mock() scene_validator.validate_scene.side_effect = InvalidEntityException( source='s', code='c', message='m') updated_scene = Scene(id='1', title='Other', description='some', latitude=3, longitude=8, experience_id=1) try: ModifySceneInteractor(scene_repo, scene_validator) \ .set_params(id='1', title='Other', description='some', latitude=3, longitude=8, experience_id=1).execute() assert False except InvalidEntityException as invalid_exc: assert invalid_exc.source == 's' assert invalid_exc.code == 'c' assert str(invalid_exc) == 'm' scene_repo.get_scene.assert_called_once_with(id='1') scene_repo.update_scene.assert_not_called() scene_validator.validate_scene.assert_called_once_with( updated_scene)
def given_an_experience_validator_that_raises_invalid_entity_exception( self): self.experience_validator = Mock() self.experience_validator.validate_experience.side_effect = \ InvalidEntityException(source='title', code='empty_attribute', message='Title must be between 1 and 20 chars') return self
def validate(self, client_secret_key): if client_secret_key != self.valid_client_secret_key: raise InvalidEntityException(source='client_secret_key', code='invalid', message='Invalid client secret key') else: return True
def given_a_client_secret_key_validator_that_doesnt_accept_that_key( self): self.client_secret_key_validator = Mock() self.client_secret_key_validator.validate.side_effect = InvalidEntityException( source='client_secret_key', code='invalid', message='Invalid client secret key') return self
def given_a_person_validator_that_raises_invalid_entity_exception( self): self.person_validator = Mock() self.person_validator.validate.side_effect = InvalidEntityException( source='username', code='already_used', message='Username already used') return self
def execute(self): if self.logged_person_id is None: raise NoLoggedException() try: person_id = self.confirmation_token_repo.get_person_id(confirmation_token=self.confirmation_token) except EntityDoesNotExistException: raise InvalidEntityException(source='confirmation_token', code='invalid', message='Invalid confirmation token') if person_id != self.logged_person_id: raise InvalidEntityException(source='confirmation_token', code='invalid', message='Invalid confirmation token') self.confirmation_token_repo.delete_confirmation_tokens(person_id=person_id) person = self.person_repo.get_person(id=self.logged_person_id) updated_person = Person(id=person.id, is_registered=person.is_registered, username=person.username, email=person.email, is_email_confirmed=True) updated_person = self.person_repo.update_person(updated_person) return updated_person
def validate(self, person): if len(person.username) < PersonValidator.USERNAME_MIN_LENGTH or \ len(person.username) > PersonValidator.USERNAME_MAX_LENGTH: raise InvalidEntityException( source='username', code='wrong_size', message='Username length should be between 1 and 20 chars') if not re.match(PersonValidator.USERNAME_REGEX, person.username): raise InvalidEntityException(source='username', code='not_allowed', message='Username not allowed') if self.project_name in person.username: raise InvalidEntityException(source='username', code='not_allowed', message='Username not allowed') if person.username in self.forbidden_usernames: raise InvalidEntityException(source='username', code='not_allowed', message='Username not allowed') try: self.person_repo.get_person(username=person.username) raise InvalidEntityException(source='username', code='not_allowed', message='Username not allowed') except EntityDoesNotExistException: pass if not re.match(r"[^@]+@[^@]+\.[^@]+", person.email): raise InvalidEntityException(source='email', code='wrong', message='Email is wrong') if person.email.split('@')[-1] in self.forbidden_email_domains: raise InvalidEntityException(source='email', code='not_allowed', message='Email not allowed') try: self.person_repo.get_person(email=person.email) raise InvalidEntityException(source='email', code='not_allowed', message='Email not allowed') except EntityDoesNotExistException: pass return True
def test_invalid_experience_returns_error_and_doesnt_create_it(self): experience = Experience(title='', description='') experience_repo = Mock() experience_validator = Mock() experience_validator.validate_experience.side_effect = InvalidEntityException( source='s', code='c', message='m') try: CreateNewExperienceInteractor(experience_repo, experience_validator) \ .set_params(title='', description='').execute() assert False except InvalidEntityException as invalid_exc: assert invalid_exc.source == 's' assert invalid_exc.code == 'c' assert str(invalid_exc) == 'm' experience_repo.create_experience.assert_not_called() experience_validator.validate_experience.assert_called_once_with( experience)
def test_invalid_scene_returns_error_and_doesnt_create_it(self): scene = Scene(title='', description='', latitude=0, longitude=0, experience_id=0) scene_repo = Mock() scene_validator = Mock() scene_validator.validate_scene.side_effect = InvalidEntityException( source='s', code='c', message='m') try: CreateNewSceneInteractor(scene_repo, scene_validator) \ .set_params(title='', description='', latitude=0, longitude=0, experience_id=0).execute() assert False except InvalidEntityException as invalid_exc: assert invalid_exc.source == 's' assert invalid_exc.code == 'c' assert str(invalid_exc) == 'm' scene_repo.create_scene.assert_not_called() scene_validator.validate_scene.assert_called_once_with(scene)
def test_invalid_experience_returns_error_and_doesnt_update_it(self): experience = Experience(id='1', title='', description='') experience_repo = Mock() experience_repo.get_experience.return_value = experience experience_validator = Mock() experience_validator.validate_experience.side_effect = InvalidEntityException( source='s', code='c', message='m') updated_experience = Experience(id='1', title='Other', description='some') try: ModifyExperienceInteractor(experience_repo, experience_validator) \ .set_params(id='1', title='Other', description='some').execute() assert False except InvalidEntityException as invalid_exc: assert invalid_exc.source == 's' assert invalid_exc.code == 'c' assert str(invalid_exc) == 'm' experience_repo.get_experience.assert_called_once_with(id='1') experience_repo.update_experience.assert_not_called() experience_validator.validate_experience.assert_called_once_with( updated_experience)
def raiser_func(): raise InvalidEntityException('title', 'empty_attribute', 'Title must not be empty')
def given_an_scene_validator_that_raises_invalid_params(self): self.scene_validator = Mock() self.scene_validator.validate_scene.side_effect = InvalidEntityException( source='s', code='c', message='m') return self
def validate_scene(self, scene): if scene.title is None: raise InvalidEntityException(source='title', code='empty_attribute', message='Title cannot be empty') if type(scene.title) is not str: raise InvalidEntityException(source='title', code='wrong_type', message='Title must be string') if len(scene.title) < SceneValidator.MIN_TITLE_LENGHT or len( scene.title) > SceneValidator.MAX_TITLE_LENGHT: raise InvalidEntityException( source='title', code='wrong_size', message='Title must be between 1 and 30 chars') if scene.description is not None and type( scene.description) is not str: raise InvalidEntityException(source='description', code='wrong_type', message='Description must be string') if scene.latitude is None: raise InvalidEntityException(source='latitude', code='empty_attribute', message='Latitude cannot be empty') if not isinstance(scene.latitude, (int, float, complex)): raise InvalidEntityException(source='latitude', code='wrong_type', message='Latitude must be numeric') if scene.latitude < SceneValidator.MIN_LATITUDE or scene.latitude > SceneValidator.MAX_LATITUDE: raise InvalidEntityException( source='latitude', code='wrong_size', message='Latitude must be between -90 and +90') if scene.longitude is None: raise InvalidEntityException(source='longitude', code='empty_attribute', message='Longitude cannot be empty') if not isinstance(scene.longitude, (int, float, complex)): raise InvalidEntityException(source='longitude', code='wrong_type', message='Longitude must be numeric') if scene.longitude < SceneValidator.MIN_LONGITUDE or scene.longitude > SceneValidator.MAX_LONGITUDE: raise InvalidEntityException( source='longitude', code='wrong_size', message='Longitude must be between -180 and +180') if scene.experience_id is None: raise InvalidEntityException( source='experience_id', code='empty_attribute', message='Experience id cannot be empty') try: self.experience_repo.get_experience(scene.experience_id) except EntityDoesNotExistException: raise InvalidEntityException(source='experience_id', code='does_not_exist', message='Experience does not exist') return True