|
| 1 | +from src.const import GithubHeaders |
| 2 | + |
| 3 | +HEADERS = { |
| 4 | + GithubHeaders.EVENT.value: "workflow_job", |
| 5 | + "Content-Type": "application/json", |
| 6 | + "User-Agent": "GitHub-Hookshot/123", |
| 7 | +} |
| 8 | +BODY = { |
| 9 | + "action": "", |
| 10 | + "workflow_job": { |
| 11 | + "id": 0, |
| 12 | + "workflow_name": "CI", |
| 13 | + "started_at": "2023-01-27T14:00:00Z", |
| 14 | + }, |
| 15 | + "repository": { |
| 16 | + "full_name": "foo/foo", |
| 17 | + }, |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def test_method_not_allowed(client): |
| 22 | + assert client.get("/github-webhook").status_code == 401 |
| 23 | + |
| 24 | + |
| 25 | +def test_headers_not_correct(client, caplog): |
| 26 | + response = client.post("/github-webhook") |
| 27 | + assert response.status_code == 401 |
| 28 | + assert caplog.messages == [ |
| 29 | + "User-Agent is werkzeug/2.2.2", |
| 30 | + "Content is not JSON", |
| 31 | + "No GitHub Event received!", |
| 32 | + ] |
| 33 | + |
| 34 | + |
| 35 | +def test_no_body_bad_request(client): |
| 36 | + response = client.post("/github-webhook", headers=HEADERS) |
| 37 | + assert response.status_code == 400 |
| 38 | + |
| 39 | + |
| 40 | +def test_unknown_event(client, caplog): |
| 41 | + headers = HEADERS.copy() |
| 42 | + headers[GithubHeaders.EVENT.value] = "foo" |
| 43 | + response = client.post("/github-webhook", headers=headers, json={}) |
| 44 | + assert response.status_code == 405 |
| 45 | + assert caplog.messages == ["Unknown event type foo, can't handle"] |
| 46 | + |
| 47 | + |
| 48 | +def test_started_job_not_stored(client, caplog): |
| 49 | + body_started = BODY.copy() |
| 50 | + body_started["action"] = "in_progress" |
| 51 | + body_started["workflow_job"]["id"] = 2 |
| 52 | + response = client.post("/github-webhook", headers=HEADERS, json=body_started) |
| 53 | + assert response.status_code == 200 |
| 54 | + assert caplog.messages == [ |
| 55 | + "Job 2 is in_progress but not stored!", |
| 56 | + 'action=in_progress repository=foo/foo job_id=2 workflow="CI" time_to_start=0', |
| 57 | + ] |
| 58 | + |
| 59 | + |
| 60 | +def test_finished_job_not_stored(client, caplog): |
| 61 | + body_finished = BODY.copy() |
| 62 | + body_finished["action"] = "completed" |
| 63 | + body_finished["workflow_job"]["id"] = 3 |
| 64 | + response = client.post("/github-webhook", headers=HEADERS, json=body_finished) |
| 65 | + assert response.status_code == 200 |
| 66 | + assert caplog.messages == [ |
| 67 | + "Job 3 is completed but not stored!", |
| 68 | + 'action=completed repository=foo/foo job_id=3 workflow="CI" time_to_finish=0', |
| 69 | + ] |
| 70 | + |
| 71 | + |
| 72 | +def test_unknown_action(client, caplog): |
| 73 | + body_started = BODY.copy() |
| 74 | + body_started["action"] = "queued" |
| 75 | + body_started["workflow_job"]["id"] = 4 |
| 76 | + response = client.post("/github-webhook", headers=HEADERS, json=body_started) |
| 77 | + body_failed = body_started.copy() |
| 78 | + body_failed["action"] = "failed" |
| 79 | + response = client.post("/github-webhook", headers=HEADERS, json=body_failed) |
| 80 | + assert response.status_code == 200 |
| 81 | + assert caplog.messages == [ |
| 82 | + 'action=queued repository=foo/foo job_id=4 workflow="CI"', |
| 83 | + "Unknown action failed, removing from memory", |
| 84 | + ] |
| 85 | + |
| 86 | + |
| 87 | +def test_queued_job(client, caplog): |
| 88 | + body_queued = BODY.copy() |
| 89 | + body_queued["action"] = "queued" |
| 90 | + body_queued["workflow_job"]["id"] = 1 |
| 91 | + response = client.post("/github-webhook", headers=HEADERS, json=body_queued) |
| 92 | + assert response.status_code == 200 |
| 93 | + assert caplog.messages == [ |
| 94 | + 'action=queued repository=foo/foo job_id=1 workflow="CI"' |
| 95 | + ] |
| 96 | + |
| 97 | + |
| 98 | +def test_logging_flow(client, caplog): |
| 99 | + body_queued = BODY.copy() |
| 100 | + body_queued["action"] = "queued" |
| 101 | + body_queued["workflow_job"]["id"] = 5 |
| 102 | + |
| 103 | + response = client.post("/github-webhook", headers=HEADERS, json=body_queued) |
| 104 | + assert response.status_code == 200 |
| 105 | + assert ( |
| 106 | + caplog.messages[0] == 'action=queued repository=foo/foo job_id=5 workflow="CI"' |
| 107 | + ) |
| 108 | + |
| 109 | + body_started = BODY.copy() |
| 110 | + body_started["action"] = "in_progress" |
| 111 | + body_started["workflow_job"]["started_at"] = "2023-01-27T14:00:05Z" |
| 112 | + response = client.post("/github-webhook", headers=HEADERS, json=body_started) |
| 113 | + assert response.status_code == 200 |
| 114 | + assert ( |
| 115 | + caplog.messages[1] |
| 116 | + == 'action=in_progress repository=foo/foo job_id=5 workflow="CI" time_to_start=5' |
| 117 | + ) |
| 118 | + |
| 119 | + body_completed = BODY.copy() |
| 120 | + body_completed["action"] = "completed" |
| 121 | + body_completed["workflow_job"]["completed_at"] = "2023-01-27T14:05:00Z" |
| 122 | + response = client.post("/github-webhook", headers=HEADERS, json=body_completed) |
| 123 | + assert response.status_code == 200 |
| 124 | + assert ( |
| 125 | + caplog.messages[2] |
| 126 | + == 'action=completed repository=foo/foo job_id=5 workflow="CI" time_to_finish=295' |
| 127 | + ) |
0 commit comments