|
| 1 | +from fastadmin import TortoiseModelAdmin, register |
| 2 | + |
| 3 | +from tests.api.tortoise.helpers import sign_in |
| 4 | +from fastadmin.models.helpers import unregister_admin_model |
| 5 | + |
| 6 | + |
| 7 | +async def test_add(superuser, tournament, event, client): |
| 8 | + |
| 9 | + @register(event.__class__) |
| 10 | + class EventAdmin(TortoiseModelAdmin): |
| 11 | + pass |
| 12 | + |
| 13 | + await sign_in(client, superuser) |
| 14 | + r = await client.post( |
| 15 | + f"/api/add/{event.__class__.__name__}", |
| 16 | + json={ |
| 17 | + "name": "new name", |
| 18 | + "tournament_id": tournament.id, |
| 19 | + "participants": [superuser.id], |
| 20 | + } |
| 21 | + ) |
| 22 | + assert r.status_code == 200 |
| 23 | + item = r.json() |
| 24 | + event = await event.__class__.get(id=item["id"]) |
| 25 | + assert item["name"] == "new name" |
| 26 | + assert item["tournament_id"] == tournament.id |
| 27 | + assert item["created_at"] == event.created_at.isoformat() |
| 28 | + assert item["updated_at"] == event.updated_at.isoformat() |
| 29 | + assert item["participants"] == [superuser.id] |
| 30 | + |
| 31 | + |
| 32 | +async def test_add_401(superuser, tournament, event, client): |
| 33 | + r = await client.post( |
| 34 | + f"/api/add/{event.__class__.__name__}", |
| 35 | + json={ |
| 36 | + "name": "new name", |
| 37 | + "tournament_id": tournament.id, |
| 38 | + "participants": [superuser.id], |
| 39 | + } |
| 40 | + ) |
| 41 | + assert r.status_code == 401 |
| 42 | + |
| 43 | + |
| 44 | +async def test_add_404(superuser, tournament, event, client): |
| 45 | + unregister_admin_model([event.__class__]) |
| 46 | + await sign_in(client, superuser) |
| 47 | + r = await client.post( |
| 48 | + f"/api/add/{event.__class__.__name__}", |
| 49 | + json={ |
| 50 | + "name": "new name", |
| 51 | + "tournament_id": tournament.id, |
| 52 | + "participants": [superuser.id], |
| 53 | + } |
| 54 | + ) |
| 55 | + assert r.status_code == 404 |
0 commit comments