|
| 1 | +"""Integration tests for the V2 task API endpoints.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | + |
| 6 | +class TestTasksV2: |
| 7 | + """Tests for the /tasks async V2 endpoints.""" |
| 8 | + |
| 9 | + @patch( |
| 10 | + "summary.api.route.tasks_v2.process_audio_transcribe_v2_task.apply_async", |
| 11 | + return_value=MagicMock(id="transcribe-task-id-abc"), |
| 12 | + ) |
| 13 | + def test_create_transcribe_task_v2_returns_task_id(self, mock_apply_async, client): |
| 14 | + """POST /async/transcribe creates a task and injects tenant_id.""" |
| 15 | + response = client.post( |
| 16 | + "/api/v2/async/transcribe", |
| 17 | + headers={"Authorization": "Bearer test-api-token"}, |
| 18 | + json={ |
| 19 | + "remote_id": "remote-001", |
| 20 | + "remote_owner_id": "owner-123", |
| 21 | + "cloud_storage_url": "https://example.com/audio.mp3", |
| 22 | + "language": "en", |
| 23 | + "context_language": "fr", |
| 24 | + }, |
| 25 | + ) |
| 26 | + |
| 27 | + assert response.status_code == 200 |
| 28 | + assert response.json() == { |
| 29 | + "id": "transcribe-task-id-abc", |
| 30 | + "message": "Transcribe task created", |
| 31 | + } |
| 32 | + |
| 33 | + args = mock_apply_async.call_args.kwargs["args"] |
| 34 | + assert args == [ |
| 35 | + { |
| 36 | + "remote_id": "remote-001", |
| 37 | + "remote_owner_id": "owner-123", |
| 38 | + "cloud_storage_url": "https://example.com/audio.mp3", |
| 39 | + "language": "en", |
| 40 | + "context_language": "fr", |
| 41 | + "tenant_id": "test-tenant", |
| 42 | + } |
| 43 | + ] |
| 44 | + |
| 45 | + @patch( |
| 46 | + "summary.api.route.tasks_v2.summarize_v2_task.apply_async", |
| 47 | + return_value=MagicMock(id="summarize-task-id-abc"), |
| 48 | + ) |
| 49 | + def test_create_summarize_task_v2_returns_task_id(self, mock_apply_async, client): |
| 50 | + """POST /async/summarize creates a task and injects tenant_id.""" |
| 51 | + response = client.post( |
| 52 | + "/api/v2/async/summarize", |
| 53 | + headers={"Authorization": "Bearer test-api-token"}, |
| 54 | + json={ |
| 55 | + "remote_id": "remote-002", |
| 56 | + "remote_owner_id": "owner-456", |
| 57 | + "content": "This is a long meeting transcript to summarize.", |
| 58 | + }, |
| 59 | + ) |
| 60 | + |
| 61 | + assert response.status_code == 200 |
| 62 | + assert response.json() == { |
| 63 | + "id": "summarize-task-id-abc", |
| 64 | + "message": "Summarize task created", |
| 65 | + } |
| 66 | + |
| 67 | + args = mock_apply_async.call_args.kwargs["args"] |
| 68 | + assert args == [ |
| 69 | + { |
| 70 | + "remote_id": "remote-002", |
| 71 | + "remote_owner_id": "owner-456", |
| 72 | + "content": "This is a long meeting transcript to summarize.", |
| 73 | + "tenant_id": "test-tenant", |
| 74 | + } |
| 75 | + ] |
| 76 | + |
| 77 | + @patch("summary.api.route.tasks_v2.AsyncResult") |
| 78 | + def test_get_transcribe_task_status_returns_status_for_same_tenant( |
| 79 | + self, mock_async_result, client |
| 80 | + ): |
| 81 | + """GET /async/transcribe/{id} returns status when tenant matches.""" |
| 82 | + mock_async_result.return_value = MagicMock( |
| 83 | + status="PENDING", |
| 84 | + args=[{"tenant_id": "test-tenant"}], |
| 85 | + ) |
| 86 | + |
| 87 | + response = client.get( |
| 88 | + "/api/v2/async/transcribe/task-id-abc", |
| 89 | + headers={"Authorization": "Bearer test-api-token"}, |
| 90 | + ) |
| 91 | + |
| 92 | + assert response.status_code == 200 |
| 93 | + assert response.json() == {"id": "task-id-abc", "status": "PENDING"} |
| 94 | + |
| 95 | + mock_async_result.assert_called_once_with("task-id-abc") |
| 96 | + |
| 97 | + @patch("summary.api.route.tasks_v2.AsyncResult") |
| 98 | + def test_get_summarize_task_status_returns_status_for_same_tenant( |
| 99 | + self, mock_async_result, client |
| 100 | + ): |
| 101 | + """GET /async/summarize/{id} returns status when tenant matches.""" |
| 102 | + mock_async_result.return_value = MagicMock( |
| 103 | + status="SUCCESS", |
| 104 | + args=[{"tenant_id": "test-tenant"}], |
| 105 | + ) |
| 106 | + |
| 107 | + response = client.get( |
| 108 | + "/api/v2/async/summarize/task-id-abc", |
| 109 | + headers={"Authorization": "Bearer test-api-token"}, |
| 110 | + ) |
| 111 | + |
| 112 | + assert response.status_code == 200 |
| 113 | + assert response.json() == {"id": "task-id-abc", "status": "SUCCESS"} |
| 114 | + |
| 115 | + mock_async_result.assert_called_once_with("task-id-abc") |
| 116 | + |
| 117 | + @patch("summary.api.route.tasks_v2.AsyncResult") |
| 118 | + def test_get_task_status_returns_404_when_args_are_missing( |
| 119 | + self, mock_async_result, client |
| 120 | + ): |
| 121 | + """GET /async/.../{id} returns 404 when task args are invalid.""" |
| 122 | + mock_async_result.return_value = MagicMock(status="SUCCESS", args=None) |
| 123 | + |
| 124 | + response = client.get( |
| 125 | + "/api/v2/async/transcribe/task-id-abc", |
| 126 | + headers={"Authorization": "Bearer test-api-token"}, |
| 127 | + ) |
| 128 | + |
| 129 | + assert response.status_code == 404 |
| 130 | + assert response.json() == {"detail": "Not found"} |
0 commit comments