Skip to content

Commit fd64624

Browse files
Fix post news endpoint
- update tests - test integration with get
1 parent 50c85f6 commit fd64624

4 files changed

Lines changed: 128 additions & 4 deletions

File tree

app/routers/news/routes.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from fastapi.params import Header
55
from pydantic import BaseModel
66
from routers.authentication import get_current_active_community
7-
from services.database.orm.news import get_news_by_query_params
7+
from services.database.orm.news import create_news, get_news_by_query_params
88

9+
from app.schemas import News
910
from app.services.database.models import Community as DBCommunity
1011

1112

@@ -28,10 +29,22 @@ def setup():
2829
summary="News endpoint",
2930
description="Creates news and returns a confirmation message",
3031
)
31-
async def post_news():
32+
async def post_news(
33+
request: Request,
34+
current_community: Annotated[
35+
DBCommunity, Depends(get_current_active_community)
36+
],
37+
news: News,
38+
user_email: str = Header(..., alias="user-email"),
39+
):
3240
"""
3341
News endpoint that creates news and returns a confirmation message.
3442
"""
43+
news_dict = news.__dict__
44+
news_dict["user_email"] = user_email
45+
await create_news(
46+
session=request.app.db_session_factory, news=news_dict
47+
)
3548
return NewsPostResponse()
3649

3750
@router.get(
@@ -47,7 +60,7 @@ async def get_news(
4760
DBCommunity, Depends(get_current_active_community)
4861
],
4962
id: str | None = None,
50-
user_email: str | None = Header(..., alias="user-email"),
63+
user_email: str = Header(..., alias="user-email"),
5164
category: str | None = None,
5265
tags: str | None = None,
5366
):

app/schemas.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ class CommunityInDB(Community):
2121
password: str
2222

2323

24+
class News(BaseModel):
25+
title: str
26+
content: str
27+
category: str
28+
tags: str | None = None
29+
source_url: str
30+
social_media_url: str | None = None
31+
likes: int = 0
32+
33+
2434
class Token(BaseModel):
2535
access_token: str
2636
token_type: str

app/services/database/orm/news.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
from sqlmodel.ext.asyncio.session import AsyncSession
66

77

8+
async def create_news(session: AsyncSession, news: dict) -> None:
9+
_news = News(
10+
title=news["title"],
11+
content=news["content"],
12+
category=news["category"],
13+
user_email=news["user_email"],
14+
source_url=news["source_url"],
15+
tags=news["tags"] or "",
16+
social_media_url=news["social_media_url"] or "",
17+
likes=news["likes"],
18+
)
19+
session.add(_news)
20+
await session.commit()
21+
await session.refresh(_news)
22+
23+
824
async def get_news_by_query_params(
925
session: AsyncSession,
1026
email: Optional[str] = None,

tests/test_news.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
100145
async 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

Comments
 (0)