|
| 1 | +"""API-level tests for the user activity endpoint.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from httpx import AsyncClient |
| 5 | +from fastapi import status |
| 6 | +from uuid import uuid4, uuid1 |
| 7 | +from datetime import datetime, timezone |
| 8 | +from unittest.mock import AsyncMock, patch |
| 9 | + |
| 10 | +from app.main import app |
| 11 | +from app.core.config import settings |
| 12 | +from app.models.user_activity import UserActivity |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def sample_activities(): |
| 17 | + userid = uuid4() |
| 18 | + now = datetime.now(timezone.utc) |
| 19 | + return [ |
| 20 | + UserActivity( |
| 21 | + userid=userid, |
| 22 | + day=now.strftime("%Y-%m-%d"), |
| 23 | + activity_type="view", |
| 24 | + activity_id=uuid1(), |
| 25 | + activity_timestamp=now, |
| 26 | + ), |
| 27 | + UserActivity( |
| 28 | + userid=userid, |
| 29 | + day=now.strftime("%Y-%m-%d"), |
| 30 | + activity_type="comment", |
| 31 | + activity_id=uuid1(), |
| 32 | + activity_timestamp=now, |
| 33 | + ), |
| 34 | + ], userid |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.asyncio |
| 38 | +async def test_get_user_activity_success(sample_activities): |
| 39 | + activities, userid = sample_activities |
| 40 | + |
| 41 | + with patch( |
| 42 | + "app.api.v1.endpoints.user_activity.user_activity_service.list_user_activity", |
| 43 | + new_callable=AsyncMock, |
| 44 | + ) as mock_list: |
| 45 | + mock_list.return_value = (activities, 2) |
| 46 | + |
| 47 | + async with AsyncClient(app=app, base_url="http://test") as ac: |
| 48 | + resp = await ac.get( |
| 49 | + f"{settings.API_V1_STR}/users/{userid}/activity", |
| 50 | + ) |
| 51 | + |
| 52 | + assert resp.status_code == status.HTTP_200_OK |
| 53 | + body = resp.json() |
| 54 | + assert len(body["data"]) == 2 |
| 55 | + assert body["pagination"]["totalItems"] == 2 |
| 56 | + mock_list.assert_awaited_once() |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +async def test_get_user_activity_empty(): |
| 61 | + with patch( |
| 62 | + "app.api.v1.endpoints.user_activity.user_activity_service.list_user_activity", |
| 63 | + new_callable=AsyncMock, |
| 64 | + ) as mock_list: |
| 65 | + mock_list.return_value = ([], 0) |
| 66 | + |
| 67 | + async with AsyncClient(app=app, base_url="http://test") as ac: |
| 68 | + resp = await ac.get( |
| 69 | + f"{settings.API_V1_STR}/users/{uuid4()}/activity", |
| 70 | + ) |
| 71 | + |
| 72 | + assert resp.status_code == status.HTTP_200_OK |
| 73 | + body = resp.json() |
| 74 | + assert body["data"] == [] |
| 75 | + assert body["pagination"]["totalItems"] == 0 |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_get_user_activity_with_type_filter(sample_activities): |
| 80 | + activities, userid = sample_activities |
| 81 | + view_only = [a for a in activities if a.activity_type == "view"] |
| 82 | + |
| 83 | + with patch( |
| 84 | + "app.api.v1.endpoints.user_activity.user_activity_service.list_user_activity", |
| 85 | + new_callable=AsyncMock, |
| 86 | + ) as mock_list: |
| 87 | + mock_list.return_value = (view_only, 1) |
| 88 | + |
| 89 | + async with AsyncClient(app=app, base_url="http://test") as ac: |
| 90 | + resp = await ac.get( |
| 91 | + f"{settings.API_V1_STR}/users/{userid}/activity?activity_type=view", |
| 92 | + ) |
| 93 | + |
| 94 | + assert resp.status_code == status.HTTP_200_OK |
| 95 | + body = resp.json() |
| 96 | + assert len(body["data"]) == 1 |
| 97 | + item = body["data"][0] |
| 98 | + assert item["activityType"] == "view" |
| 99 | + # Verify the filter was passed through |
| 100 | + call_kwargs = mock_list.call_args[1] |
| 101 | + assert call_kwargs["activity_type"] == "view" |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.asyncio |
| 105 | +async def test_get_user_activity_invalid_uuid(): |
| 106 | + """Invalid UUID in path returns 422.""" |
| 107 | + async with AsyncClient(app=app, base_url="http://test") as ac: |
| 108 | + resp = await ac.get(f"{settings.API_V1_STR}/users/not-a-uuid/activity") |
| 109 | + assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.asyncio |
| 113 | +async def test_get_user_activity_pagination(sample_activities): |
| 114 | + activities, userid = sample_activities |
| 115 | + |
| 116 | + with patch( |
| 117 | + "app.api.v1.endpoints.user_activity.user_activity_service.list_user_activity", |
| 118 | + new_callable=AsyncMock, |
| 119 | + ) as mock_list: |
| 120 | + mock_list.return_value = ([activities[1]], 2) |
| 121 | + |
| 122 | + async with AsyncClient(app=app, base_url="http://test") as ac: |
| 123 | + resp = await ac.get( |
| 124 | + f"{settings.API_V1_STR}/users/{userid}/activity?page=2&pageSize=1", |
| 125 | + ) |
| 126 | + |
| 127 | + assert resp.status_code == status.HTTP_200_OK |
| 128 | + body = resp.json() |
| 129 | + assert len(body["data"]) == 1 |
| 130 | + assert body["pagination"]["currentPage"] == 2 |
| 131 | + assert body["pagination"]["pageSize"] == 1 |
| 132 | + assert body["pagination"]["totalItems"] == 2 |
| 133 | + assert body["pagination"]["totalPages"] == 2 |
0 commit comments