async def test_valid_id(self, user_repo, user): # Setup id_ = 1 user_repo.fetch.return_value = User(**{**user.dict(), "id": id_}) # Test result = await user_service.get_by_id_or_raise(user_repo, id_) # Assertions user_repo.fetch.assert_called_once_with(id_) assert result assert result.id == id_
async def test_invalid_credentials(self, user_repo, credentials, user): # Setup email = credentials.email password_hash = hash_service.hash_("other password") user_repo.fetch_by_email.return_value = User( **{ **user.dict(), "email": email, "password_hash": password_hash }) # Test result = await user_service.get_by_credentials(user_repo, credentials) # Assertions user_repo.fetch_by_email.assert_called_once_with(email) assert not result
def logged_user(test_client, credentials): id_ = 1 email = credentials.email password_hash = hash_service.hash_(credentials.password) insert_user({ "id": id_, "email": credentials.email, "password_hash": password_hash }) with test_client as client: response = client.post(oauth2_token_url, data=build_form_data(credentials)) body = response.json() return LoggedUser( User(id=id_, email=email, password_hash=password_hash), body["access_token"], )
def test_is_required(self, user_valid_data): user_valid_data.pop("password_hash") with pytest.raises(ValidationError) as excinfo: User(**user_valid_data) self.assert_validation_error("value_error.missing", excinfo)
def test_must_be_secret_str(self, user_valid_data): user_valid_data.update({"password_hash": ["some string"]}) with pytest.raises(ValidationError) as excinfo: User(**user_valid_data) self.assert_validation_error("type_error.str", excinfo)
def test_must_be_email(self, user_valid_data): user_valid_data.update({"email": ["some string"]}) with pytest.raises(ValidationError) as excinfo: User(**user_valid_data) self.assert_validation_error("type_error.str", excinfo)
def test_must_be_int(self, user_valid_data): user_valid_data.update({"id": "some_id"}) with pytest.raises(ValidationError) as excinfo: User(**user_valid_data) self.assert_validation_error("type_error.integer", excinfo)
def test_immutability(self, user_valid_data): entity = User(**user_valid_data) for key in entity.dict().keys(): with pytest.raises(TypeError): setattr(entity, key, "some value")
def test_invalidation(self, user_invalid_data): with pytest.raises(ValidationError): assert User(**user_invalid_data)
def test_validation(self, user_valid_data): assert User(**user_valid_data)
async def fetch(id_: int) -> Optional[User]: query = Model.select().where(Model.c.id == id_) result = await database.fetch_one(query) return User.parse_obj(dict(result)) if result else None
async def persist(email: str, password_hash: str) -> User: values = {"email": email, "password_hash": password_hash} query = Model.insert().values(**values) last_record_id = await database.execute(query) return User.parse_obj({**values, "id": last_record_id})
async def fetch_by_email(email: str) -> Optional[User]: query = Model.select().where(Model.c.email == email) result = await database.fetch_one(query) return User.parse_obj(dict(result)) if result else None