|
| 1 | +from unittest.mock import AsyncMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +import pytest_asyncio |
| 5 | +from httpx import ASGITransport, AsyncClient |
| 6 | + |
| 7 | +from app.main import app |
| 8 | +from app.middleware.jw_auth import jw_auth_middleware |
| 9 | + |
| 10 | + |
| 11 | +@pytest_asyncio.fixture |
| 12 | +async def client(): |
| 13 | + transport = ASGITransport(app=app) |
| 14 | + async with AsyncClient( |
| 15 | + transport=transport, base_url="http://notifications-test" |
| 16 | + ) as ac: |
| 17 | + yield ac |
| 18 | + |
| 19 | + |
| 20 | +def get_permissions_url(): |
| 21 | + return "/notifications/user/history" |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.asyncio |
| 25 | +async def test_get_user_history_success(client): |
| 26 | + app.dependency_overrides[jw_auth_middleware] = lambda: { |
| 27 | + "user_id": "user_id_123", |
| 28 | + "user_email": "user@email.com", |
| 29 | + } |
| 30 | + with patch( |
| 31 | + "app.api.v1.endpoints.user_history.get_user_history_service" |
| 32 | + ) as mock_service: |
| 33 | + mock_service.return_value = [] |
| 34 | + response = await client.get(get_permissions_url()) |
| 35 | + assert response.status_code == 200 |
| 36 | + assert isinstance(response.json(), list) |
| 37 | + app.dependency_overrides.pop(jw_auth_middleware) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_get_user_history_missing_auth(client): |
| 42 | + response = await client.get(get_permissions_url()) |
| 43 | + assert response.status_code == 401 |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +async def test_read_notification_success(client): |
| 48 | + payload = {"notification_id": "123e4567-e89b-12d3-a456-426614174000"} |
| 49 | + with patch( |
| 50 | + "app.api.v1.endpoints.user_history.update_user_read_service", |
| 51 | + new_callable=AsyncMock, |
| 52 | + ) as mock_update: |
| 53 | + mock_update.return_value = None |
| 54 | + |
| 55 | + response = await client.put(get_permissions_url(), json=payload) |
| 56 | + assert response.status_code == 200 |
| 57 | + assert response.json() == {"message": "Read succesfully set!"} |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.asyncio |
| 61 | +async def test_read_notification_http_exception(client): |
| 62 | + payload = {"notification_id": "123e4567-e89b-12d3-a456-426614174000"} |
| 63 | + |
| 64 | + with patch( |
| 65 | + "app.api.v1.endpoints.user_history.update_user_read_service", |
| 66 | + new_callable=AsyncMock, |
| 67 | + ) as mock_update: |
| 68 | + from fastapi import HTTPException |
| 69 | + |
| 70 | + mock_update.side_effect = HTTPException( |
| 71 | + status_code=404, detail="Notification not found" |
| 72 | + ) |
| 73 | + |
| 74 | + response = await client.put(get_permissions_url(), json=payload) |
| 75 | + assert response.status_code == 404 |
| 76 | + json_resp = response.json() |
| 77 | + assert json_resp.get("title") == "History Error" |
| 78 | + assert "Notification not found" in json_resp.get("detail", "") |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.asyncio |
| 82 | +async def test_read_notification_generic_exception(client): |
| 83 | + payload = {"notification_id": "123e4567-e89b-12d3-a456-426614174000"} |
| 84 | + |
| 85 | + with patch( |
| 86 | + "app.api.v1.endpoints.user_history.update_user_read_service", |
| 87 | + new_callable=AsyncMock, |
| 88 | + ) as mock_update: |
| 89 | + mock_update.side_effect = Exception("Some unexpected error") |
| 90 | + |
| 91 | + response = await client.put(get_permissions_url(), json=payload) |
| 92 | + assert response.status_code == 500 |
| 93 | + json_resp = response.json() |
| 94 | + assert json_resp.get("title") == "Internal Server Error" |
| 95 | + assert "Unexpected error occurred" in json_resp.get("detail", "") |
0 commit comments