Skip to content

Commit

Permalink
Update store and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
armanddidierjean committed Nov 23, 2024
1 parent 4d338a5 commit 8bad5e9
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 7 deletions.
15 changes: 14 additions & 1 deletion app/core/myeclpay/cruds_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload

from app.core.myeclpay import models_myeclpay
from app.core.myeclpay import models_myeclpay, schemas_myeclpay
from app.core.myeclpay.types_myeclpay import (
TransactionStatus,
TransactionType,
Expand All @@ -22,6 +22,19 @@ async def create_store(
db.add(store)


async def update_store(
store_id: UUID,
store_update: schemas_myeclpay.StoreUpdate,
db: AsyncSession,
) -> None:
await db.execute(

Check warning on line 30 in app/core/myeclpay/cruds_myeclpay.py

View check run for this annotation

Codecov / codecov/patch

app/core/myeclpay/cruds_myeclpay.py#L30

Added line #L30 was not covered by tests
update(models_myeclpay.Store)
.where(models_myeclpay.Store.id == store_id)
.values(**store_update.model_dump(exclude_none=True)),
)
await db.commit()

Check warning on line 35 in app/core/myeclpay/cruds_myeclpay.py

View check run for this annotation

Codecov / codecov/patch

app/core/myeclpay/cruds_myeclpay.py#L35

Added line #L35 was not covered by tests


async def create_seller(
user_id: str,
store_id: UUID,
Expand Down
42 changes: 38 additions & 4 deletions app/core/myeclpay/endpoints_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,54 @@ async def delete_store_admin_seller(
detail="Seller does not exist",
)

if not seller.store_admin:
raise HTTPException(
status_code=400,
detail="Seller is not a store admin",
)

await cruds_myeclpay.delete_seller(
seller_user_id=seller_user_id,
store_id=store_id,
db=db,
)

await db.commit()


# Store admin #


@router.patch(
"/myeclpay/stores/{store_id}",
status_code=204,
)
async def patch_store(
store_id: UUID,
store_update: schemas_myeclpay.StoreUpdate,
db: AsyncSession = Depends(get_db),
user: CoreUser = Depends(is_user_an_ecl_member),
):
"""
Update a store
**The user must be a member of group Payment**
"""
seller = await cruds_myeclpay.get_seller(

Check warning on line 216 in app/core/myeclpay/endpoints_myeclpay.py

View check run for this annotation

Codecov / codecov/patch

app/core/myeclpay/endpoints_myeclpay.py#L216

Added line #L216 was not covered by tests
seller_user_id=user.id,
store_id=store_id,
db=db,
)

if seller is None:
raise HTTPException(

Check warning on line 223 in app/core/myeclpay/endpoints_myeclpay.py

View check run for this annotation

Codecov / codecov/patch

app/core/myeclpay/endpoints_myeclpay.py#L222-L223

Added lines #L222 - L223 were not covered by tests
status_code=400,
detail="Seller does not belong to the store",
status_code=403,
detail="You are not a seller for this store",
)

await cruds_myeclpay.delete_seller(
seller_user_id=seller_user_id,
await cruds_myeclpay.update_store(

Check warning on line 228 in app/core/myeclpay/endpoints_myeclpay.py

View check run for this annotation

Codecov / codecov/patch

app/core/myeclpay/endpoints_myeclpay.py#L228

Added line #L228 was not covered by tests
store_id=store_id,
store_update=store_update,
db=db,
)

Expand Down
5 changes: 5 additions & 0 deletions app/core/myeclpay/schemas_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Store(StoreBase):
wallet_id: UUID


class StoreUpdate(BaseModel):
name: str | None = None
membership: AvailableAssociationMembership | None = None


class SellerAdminCreation(BaseModel):
user_id: str

Expand Down
130 changes: 128 additions & 2 deletions tests/test_myeclpay.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
create_user_with_groups,
)

group_payment_user: models_core.CoreUser
group_payment_user_token: str

ecl_user: models_core.CoreUser
ecl_user_access_token: str
ecl_user_wallet: models_myeclpay.Wallet
Expand Down Expand Up @@ -62,6 +65,8 @@

store_seller_no_permission_user_access_token: str
store_seller_can_bank_user_access_token: str
store_seller_admin_user: models_core.CoreUser
store_seller_admin_user_access_token: str

unregistered_ecl_user_access_token: str

Expand All @@ -70,6 +75,10 @@

@pytest_asyncio.fixture(scope="module", autouse=True)
async def init_objects() -> None:
global group_payment_user, group_payment_user_token
group_payment_user = await create_user_with_groups(groups=[GroupType.payment])
group_payment_user_token = create_api_access_token(group_payment_user)

# ecl_user

global ecl_user, ecl_user_access_token
Expand Down Expand Up @@ -318,20 +327,137 @@ async def init_objects() -> None:
)
await add_object_to_db(store_seller_can_bank)

global store_seller_admin_user, store_seller_admin_user_access_token
store_seller_admin_user = await create_user_with_groups(
groups=[GroupType.student],
)
store_seller_admin_user_access_token = create_api_access_token(
store_seller_admin_user,
)
store_seller_admin = models_myeclpay.Seller(
user_id=store_seller_admin_user.id,
store_id=store.id,
can_bank=True,
can_see_historic=False,
can_cancel=False,
can_manage_sellers=False,
store_admin=True,
)
await add_object_to_db(store_seller_admin)

global unregistered_ecl_user_access_token
unregistered_ecl_user = await create_user_with_groups(
groups=[GroupType.student],
)
unregistered_ecl_user_access_token = create_api_access_token(unregistered_ecl_user)


async def get_cgu_for_unregistred_user(client: TestClient):
async def test_create_store(client: TestClient):
response = client.post(
"/myeclpay/stores",
headers={"Authorization": f"Bearer {group_payment_user_token}"},
json={
"name": "test_create_store Test Store",
"membership": AvailableAssociationMembership.aeecl,
},
)
assert response.status_code == 200
assert response.json()["id"] is not None


async def test_create_store_admin(client: TestClient):
user = await create_user_with_groups(
groups=[GroupType.student],
)
response = client.post(
f"/myeclpay/stores/{store.id}/admins",
headers={"Authorization": f"Bearer {group_payment_user_token}"},
json={
"user_id": user.id,
},
)
assert response.status_code == 204


async def test_get_store_admins(client: TestClient):
response = client.get(
f"/myeclpay/stores/{store.id}/admins",
headers={"Authorization": f"Bearer {group_payment_user_token}"},
)
assert response.status_code == 200
assert any(
user["user_id"] == store_seller_admin_user.id for user in response.json()
)


async def test_delete_store_admin_seller_does_not_exist(client: TestClient):
response = client.delete(
f"/myeclpay/stores/{store.id}/admins/{uuid4()}",
headers={"Authorization": f"Bearer {group_payment_user_token}"},
)
assert response.status_code == 404
assert response.json()["detail"] == "Seller does not exist"


async def test_delete_store_admin_seller_is_not_admin(client: TestClient):
user = await create_user_with_groups(
groups=[GroupType.student],
)
seller = models_myeclpay.Seller(
user_id=user.id,
store_id=store.id,
can_bank=False,
can_see_historic=False,
can_cancel=False,
can_manage_sellers=False,
store_admin=False,
)
await add_object_to_db(seller)

response = client.delete(
f"/myeclpay/stores/{store.id}/admins/{user.id}",
headers={"Authorization": f"Bearer {group_payment_user_token}"},
)
assert response.status_code == 400
assert response.json()["detail"] == "Seller is not a store admin"


async def test_delete_store_admin_seller(client: TestClient):
user = await create_user_with_groups(
groups=[GroupType.student],
)
seller = models_myeclpay.Seller(
user_id=user.id,
store_id=store.id,
can_bank=False,
can_see_historic=False,
can_cancel=False,
can_manage_sellers=False,
store_admin=True,
)
await add_object_to_db(seller)

response = client.delete(
f"/myeclpay/stores/{store.id}/admins/{user.id}",
headers={"Authorization": f"Bearer {group_payment_user_token}"},
)
assert response.status_code == 204

response = client.delete(
f"/myeclpay/stores/{store.id}/admins/{user.id}",
headers={"Authorization": f"Bearer {group_payment_user_token}"},
)
assert response.status_code == 404
assert response.json()["detail"] == "Seller does not exist"


async def test_get_cgu_for_unregistered_user(client: TestClient):
response = client.get(
"/myeclpay/users/me/cgu",
headers={"Authorization": f"Bearer {unregistered_ecl_user_access_token}"},
)
assert response.status_code == 400
assert response.json()["detail"] == "User is not registered for MyECL Pa"
assert response.json()["detail"] == "User is not registered for MyECL Pay"


async def test_register_new_user(client: TestClient):
Expand Down

0 comments on commit 8bad5e9

Please sign in to comment.