|
1 | 1 | import json |
| 2 | +from datetime import datetime, timezone |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 |
|
| 6 | +from pgcommitfest.commitfest.models import ( |
| 7 | + MailThread, |
| 8 | + Patch, |
| 9 | + PatchOnCommitFest, |
| 10 | +) |
| 11 | + |
5 | 12 | pytestmark = pytest.mark.django_db |
6 | 13 |
|
7 | 14 |
|
@@ -44,3 +51,112 @@ def test_needs_ci_endpoint(client, commitfests): |
44 | 51 | } |
45 | 52 |
|
46 | 53 | assert data == expected |
| 54 | + |
| 55 | + |
| 56 | +def test_commitfest_patches_endpoint(client, open_cf, alice, bob): |
| 57 | + """Test the /api/v1/commitfests/<id>/patches endpoint.""" |
| 58 | + # Create test patches |
| 59 | + patch1 = Patch.objects.create(name="Add feature X") |
| 60 | + patch1.authors.add(alice) |
| 61 | + patch1.lastmail = datetime(2025, 1, 15, 10, 30, 0, tzinfo=timezone.utc) |
| 62 | + patch1.save() |
| 63 | + |
| 64 | + patch2 = Patch.objects.create(name="Fix bug Y") |
| 65 | + patch2.authors.add(alice, bob) |
| 66 | + patch2.save() |
| 67 | + |
| 68 | + # Link patches to commitfest |
| 69 | + PatchOnCommitFest.objects.create( |
| 70 | + patch=patch1, |
| 71 | + commitfest=open_cf, |
| 72 | + enterdate=datetime.now(), |
| 73 | + status=PatchOnCommitFest.STATUS_REVIEW, |
| 74 | + ) |
| 75 | + PatchOnCommitFest.objects.create( |
| 76 | + patch=patch2, |
| 77 | + commitfest=open_cf, |
| 78 | + enterdate=datetime.now(), |
| 79 | + status=PatchOnCommitFest.STATUS_AUTHOR, |
| 80 | + ) |
| 81 | + |
| 82 | + response = client.get(f"/api/v1/commitfests/{open_cf.id}/patches") |
| 83 | + |
| 84 | + assert response.status_code == 200 |
| 85 | + assert response["Content-Type"] == "application/json" |
| 86 | + assert response["Access-Control-Allow-Origin"] == "*" |
| 87 | + |
| 88 | + data = json.loads(response.content) |
| 89 | + |
| 90 | + assert data["commitfest_id"] == open_cf.id |
| 91 | + assert len(data["patches"]) == 2 |
| 92 | + |
| 93 | + # Patches are ordered by id |
| 94 | + p1 = data["patches"][0] |
| 95 | + assert p1["id"] == patch1.id |
| 96 | + assert p1["name"] == "Add feature X" |
| 97 | + assert p1["status"] == "Needs review" |
| 98 | + assert p1["authors"] == ["Alice Anderson"] |
| 99 | + assert p1["last_email_time"] == "2025-01-15T10:30:00+00:00" |
| 100 | + |
| 101 | + p2 = data["patches"][1] |
| 102 | + assert p2["id"] == patch2.id |
| 103 | + assert p2["name"] == "Fix bug Y" |
| 104 | + assert p2["status"] == "Waiting on Author" |
| 105 | + assert sorted(p2["authors"]) == ["Alice Anderson", "Bob Brown"] |
| 106 | + assert p2["last_email_time"] is None |
| 107 | + |
| 108 | + |
| 109 | +def test_commitfest_patches_endpoint_not_found(client, commitfests): |
| 110 | + """Test the patches endpoint returns 404 for non-existent commitfest.""" |
| 111 | + response = client.get("/api/v1/commitfests/99999/patches") |
| 112 | + assert response.status_code == 404 |
| 113 | + |
| 114 | + |
| 115 | +def test_patch_threads_endpoint(client, open_cf, alice): |
| 116 | + """Test the /api/v1/patches/<id>/threads endpoint.""" |
| 117 | + patch = Patch.objects.create(name="Test patch") |
| 118 | + patch.authors.add(alice) |
| 119 | + |
| 120 | + PatchOnCommitFest.objects.create( |
| 121 | + patch=patch, |
| 122 | + commitfest=open_cf, |
| 123 | + enterdate=datetime.now(), |
| 124 | + status=PatchOnCommitFest.STATUS_REVIEW, |
| 125 | + ) |
| 126 | + |
| 127 | + # Create mail threads |
| 128 | + thread1 = MailThread.objects.create( |
| 129 | + messageid="abc123@example.com", |
| 130 | + subject="[PATCH] Test patch v1", |
| 131 | + firstmessage=datetime(2025, 1, 10, 9, 0, 0, tzinfo=timezone.utc), |
| 132 | + firstauthor="alice@example.com", |
| 133 | + latestmessage=datetime(2025, 1, 12, 14, 30, 0, tzinfo=timezone.utc), |
| 134 | + latestauthor="bob@example.com", |
| 135 | + latestsubject="Re: [PATCH] Test patch v1", |
| 136 | + latestmsgid="def456@example.com", |
| 137 | + ) |
| 138 | + patch.mailthread_set.add(thread1) |
| 139 | + |
| 140 | + response = client.get(f"/api/v1/patches/{patch.id}/threads") |
| 141 | + |
| 142 | + assert response.status_code == 200 |
| 143 | + assert response["Content-Type"] == "application/json" |
| 144 | + |
| 145 | + data = json.loads(response.content) |
| 146 | + |
| 147 | + assert data["patch_id"] == patch.id |
| 148 | + assert data["name"] == "Test patch" |
| 149 | + assert len(data["threads"]) == 1 |
| 150 | + |
| 151 | + t = data["threads"][0] |
| 152 | + assert t["messageid"] == "abc123@example.com" |
| 153 | + assert t["subject"] == "[PATCH] Test patch v1" |
| 154 | + assert t["latest_message_id"] == "def456@example.com" |
| 155 | + assert t["latest_message_time"] == "2025-01-12T14:30:00+00:00" |
| 156 | + assert t["has_attachment"] is False |
| 157 | + |
| 158 | + |
| 159 | +def test_patch_threads_endpoint_not_found(client, commitfests): |
| 160 | + """Test the threads endpoint returns 404 for non-existent patch.""" |
| 161 | + response = client.get("/api/v1/patches/99999/threads") |
| 162 | + assert response.status_code == 404 |
0 commit comments