Skip to content

Commit a497bb9

Browse files
arthur-mesquitadan5e3s6ares
authored andcommitted
Add test for unauthorized access in news endpoint
1 parent f6b785f commit a497bb9

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

app/routers/libraries/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from fastapi import APIRouter, Header, HTTPException, Request, status
44
from fastapi.params import Depends
55
from pydantic import BaseModel
6-
from services.encryption import encrypt_email
76

87
from app.routers.authentication import get_current_active_community
98
from app.schemas import Library as LibrarySchema
@@ -20,6 +19,7 @@
2019
)
2120
from app.services.database.orm.library_request import insert_library_request
2221
from app.services.database.orm.subscription import upsert_multiple_subscription
22+
from app.services.encryption import encrypt_email
2323
from app.services.limiter import limiter
2424

2525

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# --- Configurações do Banco de Dados em Memória para Testes ---
2323
# Usamos engine e AsyncSessionLocal apenas para os testes.
2424
# Isso garante que os testes são isolados e usam o banco de dados em memória.
25-
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
25+
TEST_DATABASE_URL = "sqlite+aiosqlite:////tmp/pynewsdb.db"
2626
os.environ["ADMIN_USER"] = "ADMIN_USER"
2727
os.environ["ADMIN_PASSWORD"] = "ADMIN_PASSWORD"
2828
os.environ["ADMIN_EMAIL"] = "ADMIN_EMAIL"

tests/test_news.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,35 @@ async def test_news_likes_endpoint(
423423
assert stored_news is not None
424424
assert stored_news.likes == 0
425425
assert stored_news.user_email_list == "[]"
426+
427+
428+
@pytest.mark.asyncio
429+
async def test_news_endpoint_blocks_unauthorized_access(
430+
async_client: AsyncClient,
431+
):
432+
news_data = {
433+
"title": "Test News",
434+
"content": "Test news content.",
435+
"category": "test_category",
436+
"tags": "test_tag",
437+
"source_url": "https://example.com/test-news",
438+
"social_media_url": "https://test.com/test_news",
439+
}
440+
response: Response = await async_client.post(
441+
url="/api/news", json=news_data
442+
)
443+
assert response.status_code == status.HTTP_401_UNAUTHORIZED
444+
445+
response: Response = await async_client.get(url="/api/news")
446+
assert response.status_code == status.HTTP_401_UNAUTHORIZED
447+
448+
response: Response = await async_client.put(
449+
url="/api/news/1", json=news_data
450+
)
451+
assert response.status_code == status.HTTP_401_UNAUTHORIZED
452+
453+
response: Response = await async_client.post(url="/api/news/1/like")
454+
assert response.status_code == status.HTTP_401_UNAUTHORIZED
455+
456+
response: Response = await async_client.delete(url="/api/news/1/like")
457+
assert response.status_code == status.HTTP_401_UNAUTHORIZED

0 commit comments

Comments
 (0)