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

1import httpx 

2import pytest 

3 

4from auth.jwt_handler import create_access_token 

5from models.events import Event 

6 

7 

8@pytest.fixture(scope="module") 

9async def access_token() -> str: 

10 return create_access_token("testuser@packt.com") 

11 

12 

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 ) 

23 

24 await Event.insert_one(new_event) 

25 

26 yield new_event 

27 

28 

29@pytest.mark.asyncio 

30async 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 

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) 

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 

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 } 

56 

57 headers = { 

58 "Content-Type": "application/json", 

59 "Authorization": f"Bearer {access_token}" 

60 } 

61 

62 test_response = { 

63 "message": "Event created successfully" 

64 } 

65 

66 response = await default_client.post("/event/new", json=payload, headers=headers) 

67 

68 assert response.status_code == 200 

69 assert response.json() == test_response 

70 

71 

72 

73@pytest.mark.asyncio 

74async def test_get_events_count(default_client: httpx.AsyncClient) -> None: 

75 response = await default_client.get("/event/") 

76 

77 events = response.json() 

78 

79 assert response.status_code == 200 

80 assert len(events) == 2 

81 

82 

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 } 

88 

89 headers = { 

90 "Content-Type": "application/json", 

91 "Authorization": f"Bearer {access_token}" 

92 } 

93 

94 url = f"/event/{str(mock_event.id)}" 

95 

96 response = await default_client.put(url, json=test_payload, headers=headers) 

97 

98 assert response.status_code == 200 

99 assert response.json()["title"] == test_payload["title"] 

100 

101 

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 } 

107 

108 headers = { 

109 "Content-Type": "application/json", 

110 "Authorization": f"Bearer {access_token}" 

111 } 

112 

113 url = f"/event/{mock_event.id}" 

114 

115 response = await default_client.delete(url, headers=headers) 

116 

117 assert response.status_code == 200 

118 assert response.json() == test_response 

119 

120 

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) 

125 

126 assert response.status_code == 404