|
| 1 | +"""Integration tests for async branch deletion. |
| 2 | +
|
| 3 | +These tests verify: |
| 4 | + - DELETE /branches/{id}/ returns 202 Accepted with a Location header |
| 5 | + - A concurrent DELETE returns 400 while the first delete is in progress |
| 6 | + - The branch is eventually gone (GET returns 404) after the task completes |
| 7 | +""" |
| 8 | + |
| 9 | +import time |
| 10 | + |
| 11 | +import pytest |
| 12 | +from conftest import BRANCH_TIMEOUT_SEC, wait_for_deletion |
| 13 | + |
| 14 | +pytestmark = pytest.mark.branch |
| 15 | + |
| 16 | +_POLL_INTERVAL = 10 |
| 17 | + |
| 18 | +_state: dict = {} |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(scope="module") |
| 22 | +def org(make_org): |
| 23 | + return make_org("test-org-delete") |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="module") |
| 27 | +def project(make_project, org): |
| 28 | + return make_project(org, "test-project-delete") |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(scope="module") |
| 32 | +def branch_id(make_branch, org, project): |
| 33 | + return make_branch(org, project, "test-branch-delete") |
| 34 | + |
| 35 | + |
| 36 | +def test_delete_returns_202_with_location(client, org, project, branch_id): |
| 37 | + r = client.delete(f"organizations/{org}/projects/{project}/branches/{branch_id}/") |
| 38 | + assert r.status_code == 202 |
| 39 | + assert "Location" in r.headers |
| 40 | + _state["task_url"] = r.headers["Location"] |
| 41 | + |
| 42 | + |
| 43 | +def test_task_listed_while_deleting(client, org, project, branch_id): |
| 44 | + r = client.get(f"organizations/{org}/projects/{project}/branches/{branch_id}/tasks/") |
| 45 | + if r.status_code == 404: |
| 46 | + pytest.skip("Branch already deleted before task listing test could run") |
| 47 | + assert r.status_code == 200 |
| 48 | + tasks = r.json() |
| 49 | + assert any(t["task_type"] == "delete" for t in tasks) |
| 50 | + |
| 51 | + |
| 52 | +def test_concurrent_delete_returns_400(client, org, project, branch_id): |
| 53 | + # While the branch has delete_task_id set the endpoint must reject a second delete. |
| 54 | + # The branch may have been deleted already in fast CI environments (404 is also acceptable). |
| 55 | + r = client.delete(f"organizations/{org}/projects/{project}/branches/{branch_id}/") |
| 56 | + assert r.status_code in (400, 404) |
| 57 | + |
| 58 | + |
| 59 | +def test_branch_gone_after_delete(client, org, project, branch_id): |
| 60 | + # Poll until the branch returns 404 — the Celery task has completed and deleted it. |
| 61 | + wait_for_deletion( |
| 62 | + client, |
| 63 | + f"organizations/{org}/projects/{project}/branches/{branch_id}/", |
| 64 | + BRANCH_TIMEOUT_SEC, |
| 65 | + ) |
| 66 | + r = client.get(f"organizations/{org}/projects/{project}/branches/{branch_id}/") |
| 67 | + assert r.status_code == 404 |
| 68 | + |
| 69 | + |
| 70 | +def test_task_detail_unavailable_after_deletion(client): |
| 71 | + task_url = _state.get("task_url") |
| 72 | + if not task_url: |
| 73 | + pytest.skip("No task URL stored from delete test") |
| 74 | + # Once the branch is gone the task detail endpoint returns 404 (BranchDep lookup fails). |
| 75 | + deadline = time.monotonic() + BRANCH_TIMEOUT_SEC |
| 76 | + while True: |
| 77 | + r = client.get(task_url, timeout=30) |
| 78 | + if r.status_code == 404: |
| 79 | + return |
| 80 | + status = r.json().get("status") if r.status_code == 200 else None |
| 81 | + if status in ("COMPLETED", "FAILED"): |
| 82 | + return |
| 83 | + if time.monotonic() >= deadline: |
| 84 | + raise TimeoutError(f"Timed out waiting for task to complete; last status={status!r}") |
| 85 | + time.sleep(_POLL_INTERVAL) |
0 commit comments