@@ -91,11 +91,56 @@ async def test_post_news_endpoint(
9191 async_client : AsyncClient , valid_auth_headers : Mapping [str , str ]
9292):
9393 """Test the news endpoint returns correct status."""
94- response = await async_client .post ("/api/news" , headers = valid_auth_headers )
94+ news_data = {
95+ "title" : "Test News" ,
96+ "content" : "Test news content." ,
97+ "category" : "test_category" ,
98+ "source_url" : "https://example.com/test-news" ,
99+ }
100+ response = await async_client .post (
101+ "/api/news" , headers = valid_auth_headers , json = news_data
102+ )
95103 assert response .status_code == status .HTTP_200_OK
96104 assert response .json () == {"status" : "News Criada" }
97105
98106
107+ @pytest .mark .asyncio
108+ async def test_insert_news_via_post_news_endpoint (
109+ session : AsyncSession ,
110+ async_client : AsyncClient ,
111+ community : Community ,
112+ valid_auth_headers : Mapping [str , str ],
113+ ):
114+ news_data = {
115+ "title" : "Test News" ,
116+ "content" : "Test news content." ,
117+ "category" : "test_category" ,
118+ "tags" : "test_tag" ,
119+ "source_url" : "https://example.com/test-news" ,
120+ "social_media_url" : "https://test.com/test_news" ,
121+ }
122+ response = await async_client .post (
123+ "/api/news" , json = news_data , headers = valid_auth_headers
124+ )
125+ assert response .status_code == status .HTTP_200_OK
126+ statement = select (News ).where (News .title == news_data ["title" ])
127+ result = await session .exec (statement )
128+ stored_news = result .first ()
129+ assert stored_news is not None
130+ assert stored_news .title == news_data ["title" ]
131+ assert stored_news .content == news_data ["content" ]
132+ assert stored_news .category == news_data ["category" ]
133+ assert stored_news .user_email == community .email
134+ assert stored_news .source_url == news_data ["source_url" ]
135+ assert stored_news .tags == news_data ["tags" ]
136+ assert stored_news .social_media_url == news_data ["social_media_url" ]
137+ assert stored_news .likes == 0
138+ assert isinstance (stored_news .created_at , datetime )
139+ assert isinstance (stored_news .updated_at , datetime )
140+ assert stored_news .created_at <= datetime .now ()
141+ assert stored_news .updated_at >= stored_news .created_at
142+
143+
99144@pytest .mark .asyncio
100145async def test_get_news_endpoint (
101146 session : AsyncSession ,
@@ -198,3 +243,43 @@ async def test_get_news_empty_result(
198243 assert response .status_code == status .HTTP_200_OK
199244 assert "news_list" in data
200245 assert data ["news_list" ] == []
246+
247+
248+ @pytest .mark .asyncio
249+ async def test_news_integration (
250+ session : AsyncSession ,
251+ async_client : AsyncClient ,
252+ community : Community ,
253+ valid_auth_headers : Mapping [str , str ],
254+ ):
255+ news_data = {
256+ "title" : "Test News" ,
257+ "content" : "Test news content." ,
258+ "category" : "test_category" ,
259+ "tags" : "test_tag" ,
260+ "source_url" : "https://example.com/test-news" ,
261+ "social_media_url" : "https://test.com/test_news" ,
262+ }
263+ post_response = await async_client .post (
264+ "/api/news" , json = news_data , headers = valid_auth_headers
265+ )
266+ assert post_response .status_code == status .HTTP_200_OK
267+ get_response = await async_client .get (
268+ "/api/news" ,
269+ headers = valid_auth_headers ,
270+ )
271+ data = get_response .json ()
272+ assert get_response .status_code == status .HTTP_200_OK
273+ assert "news_list" in data
274+ assert len (data ["news_list" ]) == 1
275+ assert data ["news_list" ][0 ]["title" ] == news_data ["title" ]
276+ assert data ["news_list" ][0 ]["content" ] == news_data ["content" ]
277+ assert data ["news_list" ][0 ]["category" ] == news_data ["category" ]
278+ assert data ["news_list" ][0 ]["user_email" ] == community .email
279+ assert data ["news_list" ][0 ]["source_url" ] == news_data ["source_url" ]
280+ assert data ["news_list" ][0 ]["tags" ] == news_data ["tags" ]
281+ assert (
282+ data ["news_list" ][0 ]["social_media_url" ]
283+ == news_data ["social_media_url" ]
284+ )
285+ assert data ["news_list" ][0 ]["likes" ] == 0
0 commit comments