예제 #1
0
async def test_fetch_all_by_user(database, make_user, make_heroes):
    users = make_user(), make_user()
    for user in users:
        insert_user(user.dict())

    users_heroes = pipe(
        users,
        lambda users:
        {user.id: make_heroes(10, user_id=user.id)
         for user in users},
        do(lambda mapping: [
            insert_hero(hero.dict()) for user, heroes in mapping.items()
            for hero in heroes
        ]),
    )

    async with database.transaction():
        for user in users:
            user_id = user.id
            heroes = users_heroes[user_id]

            result = list(await hero_repository.fetch_all_by_user(user_id))
            assert len(result) == len(heroes)
            for hero in result:
                assert hero in heroes
예제 #2
0
    async def test_non_unique_insertion(self, database, make_user):
        user = make_user()
        email, password_hash = attrgetter("email", "password_hash")(user)
        insert_user({**user.dict()})

        async with database.transaction():
            with pytest.raises(UniqueViolationError):
                await user_repository.persist(email, password_hash)
예제 #3
0
    async def test_has_result(self, database, make_user):
        user = make_user()
        insert_user({**user.dict()})
        getter = attrgetter("email", "password_hash")

        async with database.transaction():
            result = await user_repository.fetch_by_email(user.email)
            assert getter(user) == getter(result)
예제 #4
0
    async def test_found(self, database, make_user, make_hero):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        insert_hero(make_hero(id=id_, user_id=user.id).dict())

        async with database.transaction():
            assert await hero_repository.exists(id_)
예제 #5
0
 def test_success(self, test_client, credentials):
     insert_user({
         "email": credentials.email,
         "password_hash": hash_service.hash_(credentials.password),
     })
     with test_client as client:
         response = client.post(oauth2_token_url,
                                data=build_form_data(credentials))
         body = response.json()
         assert body["access_token"]
         assert body["expire"]
         assert body["token_type"] == "bearer"
         assert response.status_code == 200
예제 #6
0
    async def test_found(self, database, make_user, make_hero):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        user_id = user.id
        hero = make_hero(id=id_, user_id=user_id)
        insert_hero(hero.dict())

        async with database.transaction():
            result = await hero_repository.fetch(user_id, id_)

        assert result == hero
예제 #7
0
    async def test_success(self, database, make_user, create_hero_dto_factory):
        user = make_user()
        insert_user(user.dict())

        dto = create_hero_dto_factory()

        async with database.transaction():
            result = await hero_repository.persist(user.id, dto)

        assert attrgetter(
            "user_id", "name", "nickname", "power_class",
            "location")(result) == (user.id, *attrgetter(
                "name", "nickname", "power_class", "location")(dto))
예제 #8
0
async def test_fetch_all(database, make_user, make_heroes):
    user = make_user()
    insert_user(user.dict())

    heroes = make_heroes(10, user_id=user.id)
    for hero in heroes:
        insert_hero(hero.dict())

    async with database.transaction():
        result = list(await hero_repository.fetch_all())

    assert len(result) == len(heroes)
    for hero in result:
        assert hero in heroes
예제 #9
0
    def test_from_other_user(self, test_client, logged_user, make_user,
                             make_hero):
        other_user = make_user()
        insert_user(other_user.dict())

        id_ = 1
        hero = make_hero(id=id_, user_id=other_user.id)
        insert_hero(hero.dict())

        with test_client as client:
            response = client.get(f"/hero/{id_}",
                                  headers=auth_headers(
                                      logged_user.access_token))
        assert response.status_code == 404
예제 #10
0
    def test_conflict(self, test_client, user, credentials):
        with test_client as client:
            email = credentials.email
            insert_user({**user.dict(), "email": email})

            response = client.post("/account/user", json=credentials.dict())
            data, status_code = response.json(), response.status_code

            assert status_code == 409
            assert data == {
                "detail": {
                    "msg": "email already registered",
                    "email": email
                },
            }
예제 #11
0
    async def test_hero_not_unique_error(self, database, make_user, make_hero,
                                         create_hero_dto_factory):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        user_id = user.id
        hero = make_hero(id=id_, user_id=user_id)
        insert_hero(hero.dict())

        dto = create_hero_dto_factory()

        async with database.transaction():
            with pytest.raises(HeroNotUniqueError):
                await hero_repository.persist(user_id, dto)
예제 #12
0
    async def test_hero_not_unique_error(self, database, make_user, make_hero,
                                         update_hero_dto_factory):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        user_id = user.id
        hero = make_hero(id=id_, user_id=user_id)
        other_hero = make_hero(user_id=user_id)
        insert_hero([hero.dict(), other_hero.dict()])

        dto = update_hero_dto_factory(name=other_hero.name,
                                      nickname=other_hero.nickname)

        async with database.transaction():
            with pytest.raises(HeroNotUniqueError):
                await hero_repository.update(user_id, dto, id_)
예제 #13
0
파일: conftest.py 프로젝트: GArmane/iheroes
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"],
        )
예제 #14
0
    async def test_found(self, database, make_user, make_hero,
                         update_hero_dto_factory):
        user = make_user()
        insert_user(user.dict())

        id_ = 1
        user_id = user.id
        hero = make_hero(id=id_, user_id=user_id)
        insert_hero(hero.dict())

        dto = update_hero_dto_factory()

        async with database.transaction():
            result = await hero_repository.update(user_id, dto, id_)

        assert result
        assert (result.id, result.user_id) == (hero.id, hero.user_id)

        getter = attrgetter("name", "nickname", "power_class", "location")
        assert getter(result) != getter(hero)
예제 #15
0
    def test_success(self, test_client, logged_user, make_user, make_heroes):
        other_user = make_user()
        insert_user(other_user.dict())
        heroes = [
            hero.dict() for hero in [
                *make_heroes(10, user_id=logged_user.user.id),
                *make_heroes(10, user_id=other_user.id),
            ]
        ]
        insert_hero(heroes)

        with test_client as client:
            response = client.get("/hero",
                                  headers=auth_headers(
                                      logged_user.access_token))
        assert response.status_code == 200

        response_body = response.json()
        assert len(response_body) == len(heroes)
        for hero in response_body:
            assert hero in heroes
예제 #16
0
    def test_success(self, test_client, logged_user, make_user, make_heroes):
        other_user = make_user()
        insert_user(other_user.dict())
        heroes = [
            hero.dict() for hero in [
                *make_heroes(10, user_id=logged_user.user.id),
                *make_heroes(10, user_id=other_user.id),
            ]
        ]
        insert_hero(heroes)

        with test_client as client:
            response = client.get("/hero/me",
                                  headers=auth_headers(
                                      logged_user.access_token))

        assert response.status_code == 200

        response_body = response.json()
        cond = itemgetter("id")
        user_heroes = [
            hero for hero in heroes if hero["user_id"] == logged_user.user.id
        ]
        assert sorted(response_body, key=cond) == sorted(user_heroes, key=cond)