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 and 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
    async def test_register_not_unique(self, user_repo, credentials, user):
        # Setup
        email = credentials.email
        password_hash = hash_service.hash_(credentials.password)

        user_repo.fetch_by_email.return_value = User(
            **{**user.dict(), "email": email, "password_hash": password_hash}
        )

        # Test
        with pytest.raises(EmailNotUniqueError) as excinfo:
            await user_service.register(user_repo, credentials)

        # Assertions
        error = excinfo.value
        assert error.msg == "email already registered"
        assert error.email == email
    async def test_register_unique(self, user_repo, credentials, user):
        # Setup
        email = credentials.email
        password_hash = hash_service.hash_(credentials.password)

        user_repo.fetch_by_email.return_value = None
        user_repo.persist.return_value = User(
            **{**user.dict(), "email": email, "password_hash": password_hash}
        )

        # Test
        result = await user_service.register(user_repo, credentials)

        # Assertions
        user_repo.fetch_by_email.assert_called_once()
        user_repo.persist.assert_called_once()
        assert result.email == credentials.email
Esempio n. 5
0
def logged_user(test_client, credentials):
    id_ = 1
    email = credentials.email
    password_hash = hash_service.hash_(credentials.password)

    register_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, valid_data):
            with pytest.raises(ValidationError) as excinfo:
                valid_data.pop("password_hash")
                User(**valid_data)

            self.assert_validation_error("value_error.missing", excinfo)
        def test_must_be_secret_str(self, valid_data):
            with pytest.raises(ValidationError) as excinfo:
                valid_data.update({"password_hash": ["some string"]})
                User(**valid_data)

            self.assert_validation_error("type_error.str", excinfo)
        def test_must_be_email(self, valid_data):
            with pytest.raises(ValidationError) as excinfo:
                valid_data.update({"email": ["some string"]})
                User(**valid_data)

            self.assert_validation_error("type_error.str", excinfo)
        def test_must_be_int(self, valid_data):
            with pytest.raises(ValidationError) as excinfo:
                valid_data.update({"id": "some_id"})
                User(**valid_data)

            self.assert_validation_error("type_error.integer", excinfo)
Esempio n. 10
0
 def test_immutability(self, valid_data):
     entity = User(**valid_data)
     for key in entity.dict().keys():
         with pytest.raises(TypeError):
             setattr(entity, key, "some value")
Esempio n. 11
0
 def test_invalidation(self, invalid_data):
     with pytest.raises(ValidationError):
         assert User(**invalid_data)
Esempio n. 12
0
 def test_validation(self, valid_data):
     assert User(**valid_data)
Esempio n. 13
0
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
Esempio n. 14
0
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})
Esempio n. 15
0
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