""" Integration tests for the notifications API. The notification model is user+workspace-scoped. Since the test database starts empty and the application creates notifications asynchronously (via background tasks), most tests work with the list/management surface directly: - List returns an empty array on a fresh workspace - Mark-all-read succeeds (idempotent on empty list) - Clear all succeeds (idempotent on empty list) - Delete a specific notification by ID - BOLA: user cannot delete another user's notification """ import uuid import pytest from datetime import datetime from httpx import AsyncClient from sqlalchemy.ext.asyncio import AsyncSession # --------------------------------------------------------------------------- # DB helper — inject a notification directly to avoid depending on background tasks # --------------------------------------------------------------------------- async def _seed_notification(db_session: AsyncSession, user_id: str, workspace_id: str) -> str: """Insert a raw notification record and return its ID.""" from models.notification import Notification notif = Notification( id=uuid.uuid4(), user_id=uuid.UUID(user_id), workspace_id=uuid.UUID(workspace_id), title="Test Notification", message="This is a test", is_read=False, ) db_session.add(notif) await db_session.commit() return str(notif.id) async def _get_user_id(db_session: AsyncSession, email: str) -> str: from sqlalchemy import select from models.user import User stmt = select(User.id).where(User.email == email) result = await db_session.execute(stmt) return str(result.scalar_one()) # --------------------------------------------------------------------------- # List # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_list_notifications_empty_initially(client: AsyncClient, token_a: str): """A fresh workspace has no notifications for the customer user.""" res = await client.get( "/api/v1/notifications/", headers={"Authorization": f"Bearer {token_a}"}, ) assert res.status_code == 200 assert res.json() == [] @pytest.mark.asyncio async def test_list_notifications_returns_seeded_notification( client: AsyncClient, token_a: str, db_session: AsyncSession, workspace_a_id: str, ): """A seeded notification appears in the authenticated user's list.""" user_id = await _get_user_id(db_session, "customer@a.com") await _seed_notification(db_session, user_id, workspace_a_id) res = await client.get( "/api/v1/notifications/", headers={"Authorization": f"Bearer {token_a}"}, ) assert res.status_code == 200 assert len(res.json()) == 1 assert res.json()[0]["title"] == "Test Notification" # --------------------------------------------------------------------------- # Mark read # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_mark_all_read_is_idempotent_on_empty(client: AsyncClient, token_a: str): """PATCH /notifications/mark-read/ succeeds even when there is nothing to mark.""" res = await client.patch( "/api/v1/notifications/mark-read/", headers={"Authorization": f"Bearer {token_a}"}, ) assert res.status_code == 200 @pytest.mark.asyncio async def test_mark_all_read_updates_unread_notifications( client: AsyncClient, token_a: str, db_session: AsyncSession, workspace_a_id: str, ): """After PATCH /mark-read/, previously unread notifications show is_read=True.""" user_id = await _get_user_id(db_session, "customer@a.com") await _seed_notification(db_session, user_id, workspace_a_id) mark_res = await client.patch( "/api/v1/notifications/mark-read/", headers={"Authorization": f"Bearer {token_a}"}, ) assert mark_res.status_code == 200 list_res = await client.get( "/api/v1/notifications/", headers={"Authorization": f"Bearer {token_a}"}, ) assert all(n["is_read"] for n in list_res.json()) # --------------------------------------------------------------------------- # Delete individual # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_delete_notification( client: AsyncClient, token_a: str, db_session: AsyncSession, workspace_a_id: str, ): """DELETE /notifications/{id}/ soft-deletes the notification.""" user_id = await _get_user_id(db_session, "customer@a.com") notif_id = await _seed_notification(db_session, user_id, workspace_a_id) del_res = await client.delete( f"/api/v1/notifications/{notif_id}/", headers={"Authorization": f"Bearer {token_a}"}, ) assert del_res.status_code == 200 # Verify it is gone from the list list_res = await client.get( "/api/v1/notifications/", headers={"Authorization": f"Bearer {token_a}"}, ) assert not any(n["id"] == notif_id for n in list_res.json()) @pytest.mark.asyncio async def test_delete_nonexistent_notification_returns_404( client: AsyncClient, token_a: str ): """Deleting an unknown notification ID returns 404.""" res = await client.delete( "/api/v1/notifications/00000000-0000-0000-0000-000000000000/", headers={"Authorization": f"Bearer {token_a}"}, ) assert res.status_code == 404 # --------------------------------------------------------------------------- # BOLA # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_cannot_delete_other_users_notification( client: AsyncClient, token_a: str, token_b: str, db_session: AsyncSession, workspace_a_id: str, ): """ User B cannot delete a notification that belongs to User A. The endpoint checks user_id ownership, so it returns 404. """ user_a_id = await _get_user_id(db_session, "customer@a.com") notif_id = await _seed_notification(db_session, user_a_id, workspace_a_id) res = await client.delete( f"/api/v1/notifications/{notif_id}/", headers={"Authorization": f"Bearer {token_b}"}, ) assert res.status_code == 404 # --------------------------------------------------------------------------- # Clear all # --------------------------------------------------------------------------- @pytest.mark.asyncio async def test_clear_all_notifications( client: AsyncClient, token_a: str, db_session: AsyncSession, workspace_a_id: str, ): """DELETE /notifications/clear/ removes all notifications for the user.""" user_id = await _get_user_id(db_session, "customer@a.com") await _seed_notification(db_session, user_id, workspace_a_id) await _seed_notification(db_session, user_id, workspace_a_id) clear_res = await client.delete( "/api/v1/notifications/clear/", headers={"Authorization": f"Bearer {token_a}"}, ) assert clear_res.status_code == 200 list_res = await client.get( "/api/v1/notifications/", headers={"Authorization": f"Bearer {token_a}"}, ) assert list_res.json() == []