def test_add_multiple_people(self, person, repo, json_file, faker): # GIVEN a PersonJsonRepository # AND a person is already added to the repo repo.add_person(person) # AND a second person second_person = Person( faker.name(), faker.date_between(start_date="-60y", end_date="-5y")) # WHEN adding the second the person to the repo repo.add_person(second_person) # THEN the person is added to the json file with the new id # AND the last_person_id is set with json_file.open() as people_file: people_json = json.load(people_file) assert people_json == { "last_person_id": 2, "people": [person.as_dict(), second_person.as_dict()], } # AND the id property is set both persons assert person.id == 1 assert second_person.id == 2
def add_person(self, person: Person) -> None: _validate_person_id_not_set(person) people_json = self._load_people_json() with self.__json_file.open("w") as people_file: next_person_id = people_json["last_person_id"] + 1 person.id = next_person_id people_json["last_person_id"] = next_person_id people_json["people"].append(person.as_dict()) json.dump(people_json, people_file)
def given_a_person(self): self.person = Person(id='8', is_registered=True, username='******', email='b', is_email_confirmed=False) return self
def given_a_person_entity_with_db_person_id(self): self.person = Person(id=self.orm_person.id, is_registered=True, username='******', email='E', is_email_confirmed=True) return self
def given_an_updated_person(self): self.updated_person = Person(id='4', is_registered=True, username='******', email='[email protected]', is_email_confirmed=True) return self
def given_a_person_with_confirmed_email(self): self.person = Person(id='8', is_registered=True, username='******', email='e', is_email_confirmed=True) return self
def test_incorrect_email_raises_invalid_entity_exception(self): TestRegisterUsernameAndEmailInteractor.ScenarioMaker() \ .given_a_person_validator_that_returns( error=InvalidEntityException(source='e', code='i', message='m')) \ .given_a_person_repo_that_returns_on_get(Person(id='3', email='b', is_email_confirmed=False)) \ .when_execute(logged_person_id='1', username='******', email='e') \ .then_should_call_person_repo_get_with(id='1') \ .then_should_call_person_validator_with(Person(id='3', email='e', is_email_confirmed=False)) \ .then_should_call_profile_repo_get_with(False) \ .then_should_call_profile_validator_with(False) \ .then_should_call_profile_repo_update_with(False) \ .then_should_call_person_repo_update_with(False) \ .then_should_call_confirmation_token_repo_delete_with(False) \ .then_should_call_confirmation_token_repo_create_with(False) \ .then_should_call_mailer_with(False) \ .then_should_raise(InvalidEntityException(source='e', code='i', message='m'))
def then_should_call_repo_update_with_new_username_and_email(self): self.person_repo.update_person.assert_called_once_with( Person(id=self.person.id, is_registered=True, username=self.username, email=self.email, is_email_confirmed=False)) return self
def then_should_call_person_repo_update_with_is_email_confirmed_true( self): update_person = Person(id=self.person.id, email=self.person.email, is_email_confirmed=True) self.person_repo.update_person.assert_called_once_with( update_person) return self
def find_person_by_id(self, person_id: int) -> Optional[Person]: people_json = self._load_people_json() try: return next( Person.from_dict(person) for person in people_json["people"] if person["id"] == person_id) except StopIteration: return None
def test_create_new_person(self, name, date_of_birth): # GIVEN some person attributes # WHEN creating a person person = Person(name, date_of_birth) # THEN the person's attributes are set assert person.name == name assert person.date_of_birth == date_of_birth assert person.id is None
def given_a_person_repo_that_returns_a_person_with_email_confirmed( self): self.person_repo = Mock() person_with_confirmation = Person(id='2', is_registered=True, username='******', email='[email protected]', is_email_confirmed=True) self.person_repo.get_person.return_value = person_with_confirmation return self
def test_correct_username_and_email_when_profile_exists(self): TestRegisterUsernameAndEmailInteractor.ScenarioMaker() \ .given_a_person_validator_that_returns(True) \ .given_a_person_repo_that_returns_on_get(Person(id='3', email='b', is_email_confirmed=False)) \ .given_a_person_repo_that_returns_on_update(Person(id='4', email='o', is_email_confirmed=False)) \ .given_a_profile_validator_that_returns(True) \ .given_a_profile_repo_that_returns_on_get(Profile(person_id='7', username='******')) \ .given_a_confirmation_token_repo_that_returns('KT') \ .when_execute(logged_person_id='1', username='******', email='e') \ .then_should_call_person_repo_get_with(id='1') \ .then_should_call_person_validator_with(Person(id='3', email='e', is_email_confirmed=False)) \ .then_should_call_profile_repo_get_with(person_id='1', logged_person_id='1') \ .then_should_call_profile_validator_with(Profile(person_id='7', username='******')) \ .then_should_call_profile_repo_update_with(Profile(person_id='7', username='******')) \ .then_should_call_person_repo_update_with(Person(id='3', email='e', is_email_confirmed=False)) \ .then_should_call_confirmation_token_repo_delete_with(person_id='1') \ .then_should_call_confirmation_token_repo_create_with(person_id='1') \ .then_should_call_mailer_with(confirmation_token='KT', username='******', email='e') \ .then_should_return(True)
def find_person_by_id(self, person_id: int) -> Optional[Person]: cursor = self.__conn.cursor() cursor.execute("select name, date_of_birth from people where id = ?", [person_id]) person_data = cursor.fetchone() if person_data is None: return None person = Person(person_data[0], date.fromisoformat(person_data[1])) setattr(person, "_Person__id", person_id) return person
def test_cannot_register_once_email_is_confirmed(self): TestRegisterUsernameAndEmailInteractor.ScenarioMaker() \ .given_a_person_validator_that_returns(True) \ .given_a_person_repo_that_returns_on_get(Person(id='3', email='b', is_email_confirmed=True)) \ .when_execute(logged_person_id='1', username='******', email='e') \ .then_should_call_person_repo_get_with(id='1') \ .then_should_call_person_validator_with(False) \ .then_should_call_profile_repo_get_with(False) \ .then_should_call_profile_validator_with(False) \ .then_should_call_profile_repo_update_with(False) \ .then_should_call_person_repo_update_with(False) \ .then_should_call_confirmation_token_repo_delete_with(False) \ .then_should_call_confirmation_token_repo_create_with(False) \ .then_should_call_mailer_with(False) \ .then_should_raise( ConflictException(source='person', code='already_registered', message='Person already registered'))
def execute(self): if self.logged_person_id is None: raise NoLoggedException() person = self.person_repo.get_person(id=self.logged_person_id) if person.is_email_confirmed: raise ConflictException(source='person', code='already_registered', message='Person already registered') updated_person = Person(id=person.id, is_registered=True, username=self.username, email=self.email, is_email_confirmed=False) self.person_validator.validate(updated_person) updated_person = self.person_repo.update_person(updated_person) self.confirmation_token_repo.delete_confirmation_tokens(person_id=updated_person.id) confirmation_token = self.confirmation_token_repo.create_confirmation_token(person_id=updated_person.id) self.mailer_service.send_ask_confirmation_mail(confirmation_token=confirmation_token, username=updated_person.username, email=updated_person.email) return updated_person
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 given_a_person_entity_with_db_person_id(self): self.person = Person(id=str(self.orm_person.id), email='E', is_email_confirmed=True) return self
def given_a_person_repo_that_returns_a_person_without_email_confirmed(self): self.person_repo = Mock() person_without_confirmation = Person(id='2', email='[email protected]', is_email_confirmed=False) self.person_repo.get_person.return_value = person_without_confirmation return self
def given_a_second_person_with_that_params(self): self.second_person = Person(id='2', email=self.email) return self
def given_a_person_with_that_params(self): self.person = Person(id='1', email=self.email) return self
def person(date_of_birth, name): """Return a :class:Person instance without an id for the test function""" return Person(name, date_of_birth)
def given_a_person(self): self.person = Person(id='9', email='e') return self
def given_a_person_repo_that_returns_person_without_confirmed_email( self): self.person_repo.get_person.return_value = Person( id='5', is_email_confirmed=False) return self
def given_a_person_repo_that_returns_a_person(self): self.person = Person(id='3') self.person_repo = Mock() self.person_repo.create_guest_person.return_value = self.person return self
def given_a_person(self): self.person = Person(id='8', email='e', is_email_confirmed=True) return self
def given_a_second_person(self): self.second_person = Person(id='9', is_registered=True, username='******', email='i') return self
def given_an_updated_person(self): self.updated_person = Person(id='4', email='[email protected]', is_email_confirmed=True) return self
def given_a_person(self): self.person = Person(id='4', email='[email protected]', is_email_confirmed=False) return self
def test_from_dict(self, person_with_id): assert Person.from_dict(person_with_id.as_dict()) == person_with_id