File size: 12,141 Bytes
4c94294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import pytest
from fastapi.testclient import TestClient
from main import app
from unittest.mock import patch, MagicMock


client = TestClient(app)

def test_all_api_endpoints_return_proper_status_codes():
    """Verify all API endpoints return proper status codes"""
    
    user_id = "status_test_user"
    
    # Test GET /api/tasks - should return 200 when authenticated
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.get(
            "/api/tasks",
            headers={"Authorization": "Bearer valid_token"}
        )
        # Should return 200 OK (might be 204 No Content if no tasks exist)
        assert response.status_code in [200, 204, 404]
        
        # Check the response structure
        if response.status_code == 200:
            data = response.json()
            assert "data" in data  # Should return proper response format
            assert isinstance(data["data"], list)  # Should return list of tasks
    
    # Test POST /api/tasks - should return 200 on success
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.post(
            "/api/tasks",
            headers={"Authorization": "Bearer valid_token"},
            json={
                "title": "Status Code Test Task",
                "description": "Testing proper status codes",
                "priority": "medium"
            }
        )
        # Should return 200 OK on success
        assert response.status_code == 200

        # Check response structure
        data = response.json()
        assert "data" in data  # Should return proper response format
        task_data = data["data"]
        assert "id" in task_data
        assert task_data["user_id"] == user_id
        assert task_data["title"] == "Status Code Test Task"
        assert task_data["completed"] is False

        task_id = task_data["id"]
    
    # Test GET /api/tasks/{id} - should return 200 for existing task
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.get(
            f"/api/tasks/{task_id}",
            headers={"Authorization": "Bearer valid_token"}
        )
        # Should return 200 for existing task
        assert response.status_code == 200
        
        # Check response structure
        data = response.json()
        assert "data" in data
        task_data = data["data"]
        assert task_data["id"] == task_id
        assert task_data["user_id"] == user_id
    
    # Test PUT /api/tasks/{id} - should return 200 on success
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.put(
            f"/api/tasks/{task_id}",
            headers={"Authorization": "Bearer valid_token"},
            json={
                "title": "Updated Status Code Test Task",
                "description": "Updated description for status code test",
                "completed": True
            }
        )
        # Should return 200 OK on success
        assert response.status_code == 200
        
        # Check response structure
        data = response.json()
        assert "data" in data
        updated_task = data["data"]
        assert updated_task["id"] == task_id
        assert updated_task["title"] == "Updated Status Code Test Task"
        assert updated_task["completed"] is True
    
    # Test PATCH /api/tasks/{id}/complete - should return 200 on success
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.patch(
            f"/api/tasks/{task_id}/complete",
            headers={"Authorization": "Bearer valid_token"}
        )
        # Should return 200 OK on success
        assert response.status_code == 200
        
        # Check response structure
        data = response.json()
        assert "data" in data
        toggled_task = data["data"]
        assert toggled_task["id"] == task_id
        assert toggled_task["completed"] is False  # Toggled back to False
    
    # Test DELETE /api/tasks/{id} - should return 200 on success
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.delete(
            f"/api/tasks/{task_id}",
            headers={"Authorization": "Bearer valid_token"}
        )
        # Should return 200 OK on success
        assert response.status_code == 200
        
        # Check response structure
        data = response.json()
        assert "data" in data
        delete_result = data["data"]
        assert delete_result["ok"] is True


def test_error_status_codes():
    """Test that endpoints return proper error status codes"""
    
    # Test unauthenticated access - should return 401
    response = client.get("/api/tasks")
    assert response.status_code == 401
    
    response = client.post("/api/tasks", json={"title": "Unauthorized task"})
    assert response.status_code == 401
    
    response = client.put("/api/tasks/1", json={"title": "Unauthorized update"})
    assert response.status_code == 401
    
    response = client.delete("/api/tasks/1")
    assert response.status_code == 401
    
    response = client.patch("/api/tasks/1/complete")
    assert response.status_code == 401
    
    # Test authenticated access to non-existent task - should return 404
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = "test_user"
        
        response = client.get(
            "/api/tasks/999999",  # Non-existent task ID
            headers={"Authorization": "Bearer valid_token"}
        )
        # Should return 404 for non-existent resource
        assert response.status_code in [404, 422]  # 422 for validation errors
        
        # Test updating non-existent task
        response = client.put(
            "/api/tasks/999999",  # Non-existent task ID
            headers={"Authorization": "Bearer valid_token"},
            json={"title": "Updated non-existent task"}
        )
        # Should return 404 for non-existent resource
        assert response.status_code in [404, 422]
        
        # Test deleting non-existent task
        response = client.delete(
            "/api/tasks/999999",  # Non-existent task ID
            headers={"Authorization": "Bearer valid_token"}
        )
        # Should return 404 for non-existent resource
        assert response.status_code in [404, 422]
        
        # Test completing non-existent task
        response = client.patch(
            "/api/tasks/999999/complete",  # Non-existent task ID
            headers={"Authorization": "Bearer valid_token"}
        )
        # Should return 404 for non-existent resource
        assert response.status_code in [404, 422]


def test_api_endpoint_responses_structure():
    """Test that all API endpoints return consistent response structures"""
    
    user_id = "response_structure_user"
    
    # Test consistent response structure for POST /api/tasks
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.post(
            "/api/tasks",
            headers={"Authorization": "Bearer valid_token"},
            json={
                "title": "Response Structure Test",
                "priority": "high"
            }
        )
        
        assert response.status_code == 200
        data = response.json()
        assert "data" in data  # All successful responses should have data wrapper
        
        task = data["data"]
        required_fields = ["id", "user_id", "title", "description", "completed", "priority", "category", "tags", "created_at", "updated_at"]
        for field in required_fields:
            assert field in task  # All tasks should have required fields
    
    # Test consistent response structure for GET /api/tasks
    task_id = data["data"]["id"]
    
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.get(
            "/api/tasks",
            headers={"Authorization": "Bearer valid_token"}
        )
        
        assert response.status_code == 200
        data = response.json()
        assert "data" in data  # All successful responses should have data wrapper
        assert isinstance(data["data"], list)  # Collection endpoints should return arrays
        # Check that each task in the list has the correct structure
        for task in data["data"]:
            required_fields = ["id", "user_id", "title", "description", "completed", "priority", "category", "tags", "created_at", "updated_at"]
            for field in required_fields:
                assert field in task  # All tasks should have required fields
    
    # Test consistent response structure for GET /api/tasks/{id}
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.get(
            f"/api/tasks/{task_id}",
            headers={"Authorization": "Bearer valid_token"}
        )
        
        assert response.status_code == 200
        data = response.json()
        assert "data" in data  # All successful responses should have data wrapper
        
        task = data["data"]
        required_fields = ["id", "user_id", "title", "description", "completed", "priority", "category", "tags", "created_at", "updated_at"]
        for field in required_fields:
            assert field in task  # All tasks should have required fields
    
    # Test consistent response structure for PUT /api/tasks/{id}
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.put(
            f"/api/tasks/{task_id}",
            headers={"Authorization": "Bearer valid_token"},
            json={"title": "Updated with Consistent Response Structure"}
        )
        
        assert response.status_code == 200
        data = response.json()
        assert "data" in data  # All successful responses should have data wrapper
        
        task = data["data"]
        required_fields = ["id", "user_id", "title", "description", "completed", "priority", "category", "tags", "created_at", "updated_at"]
        for field in required_fields:
            assert field in task  # All tasks should have required fields
    
    # Test consistent response structure for PATCH /api/tasks/{id}/complete
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.patch(
            f"/api/tasks/{task_id}/complete",
            headers={"Authorization": "Bearer valid_token"}
        )
        
        assert response.status_code == 200
        data = response.json()
        assert "data" in data  # All successful responses should have data wrapper
        
        task = data["data"]
        required_fields = ["id", "user_id", "title", "description", "completed", "priority", "category", "tags", "created_at", "updated_at"]
        for field in required_fields:
            assert field in task  # All tasks should have required fields
    
    # Test consistent response structure for DELETE /api/tasks/{id}
    with patch("auth.jwt.get_current_user_id") as mock_get_user:
        mock_get_user.return_value = user_id
        
        response = client.delete(
            f"/api/tasks/{task_id}",
            headers={"Authorization": "Bearer valid_token"}
        )
        
        assert response.status_code == 200
        data = response.json()
        assert "data" in data  # All successful responses should have data wrapper
        
        result = data["data"]
        assert "ok" in result  # Delete should return ok status
        assert result["ok"] is True