Coverage for src/tests/api/v1/news/test_endpoints.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for testing the news endpoints.""" 

2 

3import pytest 

4 

5from fastapi import status 

6from fastapi.testclient import TestClient 

7from kwai_bc_portal.domain.news_item import NewsItemIdentifier 

8from kwai_bc_portal.news.news_item_db_repository import NewsItemDbRepository 

9from kwai_core.db.uow import Database, UnitOfWork 

10 

11 

12pytestmark = [pytest.mark.api, pytest.mark.db] 

13 

14 

15def test_get_news_items(client: TestClient): 

16 """Test /api/v1/news_items.""" 

17 response = client.get("/api/v1/news_items") 

18 assert response.status_code == status.HTTP_200_OK 

19 

20 json = response.json() 

21 assert "meta" in json, "There should be a meta object in the response" 

22 assert json["meta"]["limit"] == 10, "The limit should be 10" 

23 assert "data" in json, "There should be a data list in the response" 

24 

25 

26async def test_get_news_item(client: TestClient, make_news_item_in_db): 

27 """Test /api/v1/news_items/{id}.""" 

28 news_item = await make_news_item_in_db() 

29 response = client.get(f"/api/v1/news_items/{news_item.id}") 

30 json = response.json() 

31 assert response.status_code == status.HTTP_200_OK 

32 assert len(json["data"]) > 0, "There should be at least one news item." 

33 

34 json = response.json() 

35 assert "data" in json, "There should be data in the response" 

36 

37 resource = json["data"] 

38 assert resource is not None, f"News item with id {news_item.id} should exist" 

39 

40 

41async def test_create_news_item( 

42 secure_client: TestClient, database: Database, make_application_in_db 

43): 

44 """Test POST /api/v1/news_items.""" 

45 application = await make_application_in_db() 

46 payload = { 

47 "data": { 

48 "type": "news_items", 

49 "attributes": { 

50 "texts": [ 

51 { 

52 "locale": "en", 

53 "format": "md", 

54 "title": "Test New Website", 

55 "summary": "Testing the new website", 

56 "original_summary": "Testing the new website", 

57 } 

58 ], 

59 "enabled": True, 

60 "publish_date": "2023-01-02 12:00:00", 

61 }, 

62 "relationships": { 

63 "application": { 

64 "data": {"type": "applications", "id": str(application.id)} 

65 } 

66 }, 

67 }, 

68 } 

69 response = secure_client.post("/api/v1/news_items", json=payload) 

70 assert response.status_code == status.HTTP_201_CREATED, response.json() 

71 

72 news_item_id = NewsItemIdentifier(id_=response.json()["data"]["id"]) 

73 async with UnitOfWork(database): 

74 repo = NewsItemDbRepository(database) 

75 news_item = await repo.get_by_id(news_item_id) 

76 await NewsItemDbRepository(database).delete(news_item) 

77 

78 

79async def test_update_news_item( 

80 secure_client: TestClient, 

81 make_news_item_in_db, 

82 make_application_in_db, 

83): 

84 """Test PATCH /api/v1/news_items.""" 

85 application = await make_application_in_db() 

86 news_item = await make_news_item_in_db(application=application) 

87 payload = { 

88 "data": { 

89 "type": "news_items", 

90 "id": str(news_item.id), 

91 "attributes": { 

92 "texts": [ 

93 { 

94 "locale": "en", 

95 "format": "md", 

96 "title": "Test New Website", 

97 "summary": "Testing the new website (update)", 

98 "original_summary": "Testing the new website (update)", 

99 } 

100 ], 

101 "enabled": True, 

102 "publish_date": "2023-01-02 12:00:00", 

103 "priority": 0, 

104 "remark": "Updated", 

105 }, 

106 "relationships": { 

107 "application": { 

108 "data": {"type": "applications", "id": str(application.id)} 

109 } 

110 }, 

111 }, 

112 } 

113 response = secure_client.patch(f"/api/v1/news_items/{news_item.id}", json=payload) 

114 assert response.status_code == status.HTTP_200_OK, response.json() 

115 

116 

117async def test_delete_news_item(secure_client: TestClient, make_news_item_in_db): 

118 """Test DELETE /api/v1/news_items.""" 

119 news_item = await make_news_item_in_db() 

120 response = secure_client.delete(f"/api/v1/news_items/{news_item.id}") 

121 assert response.status_code == status.HTTP_200_OK, response.json()