|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | + |
| 4 | +from auth.jwt_handler import create_access_token |
| 5 | +from models.events import Event |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(scope="module") |
| 9 | +async def access_token() -> str: |
| 10 | + return create_access_token("testuser@packt.com") |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope="module") |
| 14 | +async def mock_event() -> Event: |
| 15 | + new_event = Event( |
| 16 | + creator="testuser@packt.com", |
| 17 | + title="FastAPI Book Launch", |
| 18 | + image="https://linktomyimage.com/image.png", |
| 19 | + description="We will be discussing the contents of the FastAPI book in this event.Ensure to come with your own copy to win gifts!", |
| 20 | + tags=["python", "fastapi", "book", "launch"], |
| 21 | + location="Google Meet" |
| 22 | + ) |
| 23 | + |
| 24 | + await Event.insert_one(new_event) |
| 25 | + |
| 26 | + yield new_event |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.asyncio |
| 30 | +async def test_get_events(default_client: httpx.AsyncClient, mock_event: Event) -> None: |
| 31 | + response = await default_client.get("/event/") |
| 32 | + |
| 33 | + assert response.status_code == 200 |
| 34 | + assert response.json()[0]["_id"] == str(mock_event.id) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_get_event(default_client: httpx.AsyncClient, mock_event: Event) -> None: |
| 39 | + url = f"/event/{str(mock_event.id)}" |
| 40 | + response = await default_client.get(url) |
| 41 | + |
| 42 | + assert response.status_code == 200 |
| 43 | + assert response.json()["creator"] == mock_event.creator |
| 44 | + assert response.json()["_id"] == str(mock_event.id) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.asyncio |
| 48 | +async def test_post_event(default_client: httpx.AsyncClient, access_token: str) -> None: |
| 49 | + payload = { |
| 50 | + "title": "FastAPI Book Launch", |
| 51 | + "image": "https://linktomyimage.com/image.png", |
| 52 | + "description": "We will be discussing the contents of the FastAPI book in this event.Ensure to come with your own copy to win gifts!", |
| 53 | + "tags": [ |
| 54 | + "python", |
| 55 | + "fastapi", |
| 56 | + "book", |
| 57 | + "launch" |
| 58 | + ], |
| 59 | + "location": "Google Meet", |
| 60 | + } |
| 61 | + |
| 62 | + headers = { |
| 63 | + "Content-Type": "application/json", |
| 64 | + "Authorization": f"Bearer {access_token}" |
| 65 | + } |
| 66 | + |
| 67 | + test_response = { |
| 68 | + "message": "Event created successfully" |
| 69 | + } |
| 70 | + |
| 71 | + response = await default_client.post("/event/new", json=payload, headers=headers) |
| 72 | + |
| 73 | + assert response.status_code == 200 |
| 74 | + assert response.json() == test_response |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.asyncio |
| 78 | +async def test_get_events_count(default_client: httpx.AsyncClient) -> None: |
| 79 | + response = await default_client.get("/event/") |
| 80 | + |
| 81 | + events = response.json() |
| 82 | + |
| 83 | + assert response.status_code == 200 |
| 84 | + assert len(events) == 2 |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +async def test_update_event(default_client: httpx.AsyncClient, mock_event: Event, access_token: str) -> None: |
| 89 | + test_payload = { |
| 90 | + "title": "Updated FastAPI event" |
| 91 | + } |
| 92 | + |
| 93 | + headers = { |
| 94 | + "Content-Type": "application/json", |
| 95 | + "Authorization": f"Bearer {access_token}" |
| 96 | + } |
| 97 | + |
| 98 | + url = f"/event/{str(mock_event.id)}" |
| 99 | + |
| 100 | + response = await default_client.put(url, json=test_payload, headers=headers) |
| 101 | + |
| 102 | + assert response.status_code == 200 |
| 103 | + assert response.json()["title"] == test_payload["title"] |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.asyncio |
| 107 | +async def test_delete_event(default_client: httpx.AsyncClient, mock_event: Event, access_token: str) -> None: |
| 108 | + test_response = { |
| 109 | + "message": "Event deleted successfully." |
| 110 | + } |
| 111 | + |
| 112 | + headers = { |
| 113 | + "Content-Type": "application/json", |
| 114 | + "Authorization": f"Bearer {access_token}" |
| 115 | + } |
| 116 | + |
| 117 | + url = f"/event/{mock_event.id}" |
| 118 | + |
| 119 | + response = await default_client.delete(url, headers=headers) |
| 120 | + |
| 121 | + assert response.status_code == 200 |
| 122 | + assert response.json() == test_response |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.asyncio |
| 126 | +async def test_get_event_again(default_client: httpx.AsyncClient, mock_event: Event) -> None: |
| 127 | + url = f"/event/{str(mock_event.id)}" |
| 128 | + response = await default_client.get(url) |
| 129 | + |
| 130 | + assert response.status_code == 404 |
| 131 | + |
0 commit comments