async def _( db=db, second_session=second_session, user_factory=user_factory, cleanup_db=cleanup_db, ): db = cast(AsyncSession, db) second_session = cast(AsyncSession, second_session) await user_factory(email="*****@*****.**") await db.commit() repository = UsersRepository(db) found_user = await repository.get_by_email("*****@*****.**") query = select(User).where(User.email == "*****@*****.**") raw_query_user: User = (await second_session.execute(query)).scalar() # Check what we get executing the "raw query" # and what the repository returned. Is this the same thing? assert found_user.id == raw_query_user.id assert found_user.date_joined == raw_query_user.date_joined assert found_user.username == raw_query_user.username assert found_user.fullname == raw_query_user.fullname assert found_user.gender == raw_query_user.gender assert found_user.date_birth == raw_query_user.date_birth assert found_user.open_to_newsletter is raw_query_user.open_to_newsletter assert found_user.open_to_recruiting is raw_query_user.open_to_recruiting assert found_user.country == raw_query_user.country assert found_user.is_active is raw_query_user.is_active assert found_user.is_staff is raw_query_user.is_staff assert found_user.is_superuser is raw_query_user.is_superuser
async def _(db=db, user_factory=user_factory): await user_factory(email="*****@*****.**", fullname="Marco Ciao", name="") await user_factory(email="*****@*****.**", fullname="Nina Nana", name="") await user_factory(email="*****@*****.**", fullname="Patrick Ciao", name="") repository = UsersRepository(db) found_users = await repository.search("nono") assert len(found_users) == 0
async def _(db=db, user_factory=user_factory): await user_factory(email="*****@*****.**", fullname="Marco Ciao", name="") await user_factory(email="*****@*****.**", fullname="Nina Nana", name="") user = await user_factory(email="*****@*****.**", fullname="Not In my name", name="") repository = UsersRepository(db) found_users = await repository.search("ohhello") assert len(found_users) == 1 assert found_users[0].id == user.id
async def _(db=db, user_factory=user_factory): await user_factory(email="*****@*****.**", fullname="Hello Ciao", name="") await user_factory(email="*****@*****.**", fullname="Nina Nana", name="Nope Hello!") await user_factory(email="*****@*****.**", fullname="Not In my name", name="") await user_factory(email="*****@*****.**", fullname="Ciao mondo!", name="") repository = UsersRepository(db) found_users = await repository.search("") assert len(found_users) == 0
async def _(db=db, user_factory=user_factory): user_1 = await user_factory(email="*****@*****.**", fullname="Marco Ciao", name="") await user_factory(email="*****@*****.**", fullname="Nina Nana", name="") user_3 = await user_factory(email="*****@*****.**", fullname="Patrick Ciao", name="") repository = UsersRepository(db) found_users = await repository.search("ciao") assert len(found_users) == 2 ids = [u.id for u in found_users] assert user_1.id in ids assert user_3.id in ids
async def _(db=db, second_session=second_session, cleanup_db=cleanup_db): db = cast(AsyncSession, db) second_session = cast(AsyncSession, second_session) repository = UsersRepository(db) await repository.create_user( User( email="*****@*****.**", password="******", date_joined=datetime.datetime(2020, 1, 1, 1, tzinfo=datetime.timezone.utc), username="******", fullname="Nanan Nana", gender="male", date_birth=datetime.date(2000, 5, 1), open_to_newsletter=False, open_to_recruiting=True, country="IT", is_active=True, is_staff=False, is_superuser=False, )) await repository.commit() query = select(User).where(User.email == "*****@*****.**") db_user: User = (await second_session.execute(query)).scalar() assert db_user assert db_user.date_joined == datetime.datetime( 2020, 1, 1, 1, tzinfo=datetime.timezone.utc) assert db_user.username == "username" assert db_user.fullname == "Nanan Nana" assert db_user.gender == "male" assert db_user.date_birth == datetime.date(2000, 5, 1) assert db_user.open_to_newsletter is False assert db_user.open_to_recruiting is True assert db_user.country == "IT" assert db_user.is_active is True assert db_user.is_staff is False assert db_user.is_superuser is False
async def _(db=db, second_session=second_session, user_factory=user_factory): db = cast(AsyncSession, db) second_session = cast(AsyncSession, second_session) user = await user_factory(email="*****@*****.**", id=1) user_2 = await user_factory(email="*****@*****.**", id=2) user_3 = await user_factory(email="*****@*****.**", id=3) repository = UsersRepository(db) paginable = await repository.get_users() page = await paginable.page(0, 1) assert page.total_count == 3 assert page.items == [user] page = await paginable.page(1, 3) assert page.total_count == 3 assert page.items == [user_2, user_3]
async def _(db=db, user_factory=user_factory): user_1 = await user_factory(email="*****@*****.**", fullname="Hello Ciao", name="") user_2 = await user_factory(email="*****@*****.**", fullname="Nina Nana", name="Nope Hello!") await user_factory(email="*****@*****.**", fullname="Not In my name", name="") repository = UsersRepository(db) found_users = await repository.search("Nina Ciao") assert len(found_users) == 2 ids = [u.id for u in found_users] assert user_1.id in ids assert user_2.id in ids
async def _(graphql_client=graphql_client, db=db, user_factory=user_factory): user = await user_factory( email="*****@*****.**", password="******", is_active=True, jwt_auth_id=1, ) token = user.create_reset_password_token() db_query = select(User).where(User.id == user.id) raw_query_user: User = (await db.execute(db_query)).scalar() raw_query_user.jwt_auth_id = 2 repository = UsersRepository(db) await repository.save_user(raw_query_user) await repository.commit() query = """ mutation($input: ResetPasswordInput!) { resetPassword(input: $input) { __typename ... on ResetPasswordTokenInvalid { message } } } """ response = await graphql_client.query( query, variables={"input": {"token": token, "newPassword": "******"}} ) assert not response.errors assert response.data["resetPassword"]["__typename"] == "ResetPasswordTokenInvalid" query = select(User).where(User.email == "*****@*****.**") raw_query_user: User = (await db.execute(query)).scalar() assert raw_query_user.check_password("hello")
async def _(db=db, user_factory=user_factory): u1 = await user_factory(email="*****@*****.**", fullname="Hello Ciao", name="") u2 = await user_factory(email="*****@*****.**", fullname="Nina Nana", name="Nope Hello!") u3 = await user_factory(email="*****@*****.**", fullname="Not In my name", name="") u4 = await user_factory(email="*****@*****.**", fullname="Ciao mondo!", name="") repository = UsersRepository(db) users = await repository.get_batch_by_ids([u1.id, u2.id, u3.id]) assert len(users) == 3 found_ids = [u.id for u in users] assert u1.id in found_ids assert u2.id in found_ids assert u3.id in found_ids assert u4.id not in found_ids
def users_repository(self) -> UsersRepository: return UsersRepository(session=self.session)
async def repositories_middleware(request, call_next): request.state.users_repository = UsersRepository(request.state.session) return await call_next(request)
logging.basicConfig(level=logging.INFO) app = Starlette( debug=DEBUG, routes=[ Route("/graphql", GraphQL()), Route("/admin-api", AdminGraphQL()), Route("/login/google", google_login), Route("/login/google/auth", google_login_auth, name="auth"), ], middleware=[ Middleware(SessionMiddleware, secret_key=SESSION_SECRET_KEY), Middleware( AuthenticationMiddleware, backend=JWTAuthBackend(UsersRepository()), on_error=on_auth_error, ), ], ) @app.middleware("http") async def repositories_middleware(request, call_next): request.state.users_repository = UsersRepository(request.state.session) return await call_next(request) @app.middleware("http") async def async_session_middleware(request, call_next): async with get_session(request.app.state.engine) as session: