def registered_user(postgres_db: sa.engine.Engine, faker: Faker) -> Iterator[Callable[..., Dict]]: created_user_ids = [] def creator(**user_kwargs) -> Dict[str, Any]: with postgres_db.connect() as con: # removes all users before continuing user_config = { "id": len(created_user_ids) + 1, "name": faker.name(), "email": faker.email(), "password_hash": faker.password(), "status": UserStatus.ACTIVE, "role": UserRole.USER, } user_config.update(user_kwargs) con.execute(users.insert().values(user_config).returning( sa.literal_column("*"))) # this is needed to get the primary_gid correctly result = con.execute( sa.select([users]).where(users.c.id == user_config["id"])) user = result.first() assert user created_user_ids.append(user["id"]) return dict(user) yield creator with postgres_db.connect() as con: con.execute(users.delete().where(users.c.id.in_(created_user_ids)))
async def test_cannot_remove_owner_that_owns_cluster( pg_engine: Engine, user_id: int, user_group_id: int, create_cluster: Callable[..., Awaitable[int]], ): cluster_id = await create_cluster(owner=user_group_id) # now try removing the user async with pg_engine.acquire() as conn: with pytest.raises(ForeignKeyViolation): await conn.execute(users.delete().where(users.c.id == user_id)) # now remove the cluster async with pg_engine.acquire() as conn: await conn.execute(clusters.delete().where(clusters.c.id == cluster_id)) # removing the user should work now async with pg_engine.acquire() as conn: await conn.execute(users.delete().where(users.c.id == user_id))
async def user_id(pg_engine: Engine) -> int: async with pg_engine.acquire() as conn: # a 'me' user uid = await conn.scalar( users.insert().values(**(random_user())).returning(users.c.id)) yield uid # cleanup async with pg_engine.acquire() as conn: # a 'me' user uid = await conn.execute(users.delete().where(users.c.id == uid))
def user_db(postgres_db: sa.engine.Engine, user_id: PositiveInt) -> Dict: with postgres_db.connect() as con: # removes all users before continuing con.execute(users.delete()) con.execute(users.insert().values( id=user_id, name="test user", email="*****@*****.**", password_hash="testhash", status=UserStatus.ACTIVE, role=UserRole.USER, ).returning(literal_column("*"))) # this is needed to get the primary_gid correctly result = con.execute(select([users]).where(users.c.id == user_id)) user = result.first() yield dict(user) con.execute(users.delete().where(users.c.id == user_id))
def user_db(postgres_db: sa.engine.Engine, user_id: PositiveInt) -> Dict: with postgres_db.connect() as con: result = con.execute(users.insert().values( id=user_id, name="test user", email="*****@*****.**", password_hash="testhash", status=UserStatus.ACTIVE, role=UserRole.USER, ).returning(literal_column("*"))) user = result.first() yield dict(user) con.execute(users.delete().where(users.c.id == user["id"]))
def user_id(postgres_db: sa.engine.Engine) -> Iterable[int]: # inject user in db # NOTE: Ideally this (and next fixture) should be done via webserver API but at this point # in time, the webserver service would bring more dependencies to other services # which would turn this test too complex. # pylint: disable=no-value-for-parameter stmt = users.insert().values(**random_user(name="test")).returning( users.c.id) print(str(stmt)) with postgres_db.connect() as conn: result = conn.execute(stmt) [usr_id] = result.fetchone() yield usr_id with postgres_db.connect() as conn: conn.execute(users.delete().where(users.c.id == usr_id))