Coverage for tests\test_routes.py: 100%
59 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-05 19:00 +0800
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-05 19:00 +0800
1import httpx
2import pytest
4from auth.jwt_handler import create_access_token
5from models.events import Event
8@pytest.fixture(scope="module")
9async def access_token() -> str:
10 return create_access_token("testuser@packt.com")
13@pytest.fixture(scope="module")
14async 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 )
24 await Event.insert_one(new_event)
26 yield new_event
29@pytest.mark.asyncio
30async def test_get_events(default_client: httpx.AsyncClient, mock_event: Event) -> None:
31 response = await default_client.get("/event/")
33 assert response.status_code == 200
34 assert response.json()[0]["_id"] == str(mock_event.id)
37@pytest.mark.asyncio
38async 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)
42 assert response.status_code == 200
43 assert response.json()["creator"] == mock_event.creator
44 assert response.json()["_id"] == str(mock_event.id)
47@pytest.mark.asyncio
48async 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": ["python","fastapi","book","launch"],
54 "location": "Google Meet",
55 }
57 headers = {
58 "Content-Type": "application/json",
59 "Authorization": f"Bearer {access_token}"
60 }
62 test_response = {
63 "message": "Event created successfully"
64 }
66 response = await default_client.post("/event/new", json=payload, headers=headers)
68 assert response.status_code == 200
69 assert response.json() == test_response
73@pytest.mark.asyncio
74async def test_get_events_count(default_client: httpx.AsyncClient) -> None:
75 response = await default_client.get("/event/")
77 events = response.json()
79 assert response.status_code == 200
80 assert len(events) == 2
83@pytest.mark.asyncio
84async def test_update_event(default_client: httpx.AsyncClient, mock_event: Event, access_token: str) -> None:
85 test_payload = {
86 "title": "Updated FastAPI event"
87 }
89 headers = {
90 "Content-Type": "application/json",
91 "Authorization": f"Bearer {access_token}"
92 }
94 url = f"/event/{str(mock_event.id)}"
96 response = await default_client.put(url, json=test_payload, headers=headers)
98 assert response.status_code == 200
99 assert response.json()["title"] == test_payload["title"]
102@pytest.mark.asyncio
103async def test_delete_event(default_client: httpx.AsyncClient, mock_event: Event, access_token: str) -> None:
104 test_response = {
105 "message": "Event deleted successfully."
106 }
108 headers = {
109 "Content-Type": "application/json",
110 "Authorization": f"Bearer {access_token}"
111 }
113 url = f"/event/{mock_event.id}"
115 response = await default_client.delete(url, headers=headers)
117 assert response.status_code == 200
118 assert response.json() == test_response
121@pytest.mark.asyncio
122async def test_get_event_again(default_client: httpx.AsyncClient, mock_event: Event) -> None:
123 url = f"/event/{str(mock_event.id)}"
124 response = await default_client.get(url)
126 assert response.status_code == 404