|
1 | 1 | import logging
|
| 2 | +from django.conf import settings |
2 | 3 |
|
3 | 4 | import pytest
|
| 5 | +import respx |
| 6 | +from core.integrations.github import GITHUB_API_URL |
4 | 7 | from core.models import DiscordMessage, Webhook
|
5 | 8 | from core.tasks import process_github_webhook, process_internal_webhook, process_webhook
|
| 9 | +from django.utils import timezone |
6 | 10 | from django_tasks.task import ResultStatus
|
| 11 | +from httpx import Response |
7 | 12 |
|
8 | 13 |
|
9 | 14 | @pytest.mark.django_db
|
@@ -69,3 +74,96 @@ def test_process_github_webhook_logs_unsupported_event(caplog):
|
69 | 74 | caplog.records[0].message
|
70 | 75 | == f"Not processing Github Webhook {wh.uuid}: Event `testrandom` not supported"
|
71 | 76 | )
|
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.django_db |
| 80 | +@respx.mock |
| 81 | +def test_process_github_webhook_skips_a_message_when_unsupported_project( |
| 82 | + github_data, |
| 83 | +): |
| 84 | + wh = Webhook.objects.create( |
| 85 | + source="github", |
| 86 | + event="", |
| 87 | + meta={"X-Github-Event": "projects_v2_item"}, |
| 88 | + content=github_data["project_v2_item.edited"], |
| 89 | + extra={}, |
| 90 | + ) |
| 91 | + node = { |
| 92 | + "project": { |
| 93 | + "id": "PVT_Random_Project", |
| 94 | + "title": "Random Project", |
| 95 | + "url": "https://github.com/europython", |
| 96 | + }, |
| 97 | + "content": { |
| 98 | + "__typename": "Issue", |
| 99 | + "id": "I_randomIssueID", |
| 100 | + "title": "Test Issue", |
| 101 | + "url": "https://github.com/test-issue", |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + mocked_response = { |
| 106 | + "data": { |
| 107 | + "node": node, |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) |
| 112 | + process_github_webhook(wh) |
| 113 | + |
| 114 | + # Skip the message but mark as processed |
| 115 | + assert DiscordMessage.objects.count() == 0 |
| 116 | + assert wh.processed_at is not None |
| 117 | + assert wh.processed_at < timezone.now() |
| 118 | + assert wh.event == "projects_v2_item.edited" |
| 119 | + |
| 120 | + |
| 121 | +@pytest.mark.django_db |
| 122 | +@respx.mock |
| 123 | +def test_process_github_webhook_creates_a_message_from_supported( |
| 124 | + github_data, |
| 125 | +): |
| 126 | + wh = Webhook.objects.create( |
| 127 | + source="github", |
| 128 | + event="", |
| 129 | + meta={"X-Github-Event": "projects_v2_item"}, |
| 130 | + content=github_data["project_v2_item.edited"], |
| 131 | + extra={}, |
| 132 | + ) |
| 133 | + node = { |
| 134 | + "project": { |
| 135 | + "id": "PVT_Test_Board_Project", |
| 136 | + "title": "Test Board Project", |
| 137 | + "url": "https://github.com/europython", |
| 138 | + }, |
| 139 | + "content": { |
| 140 | + "__typename": "Issue", |
| 141 | + "id": "I_randomIssueID", |
| 142 | + "title": "Test Issue", |
| 143 | + "url": "https://github.com/test-issue", |
| 144 | + }, |
| 145 | + } |
| 146 | + |
| 147 | + mocked_response = { |
| 148 | + "data": { |
| 149 | + "node": node, |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) |
| 154 | + process_github_webhook(wh) |
| 155 | + |
| 156 | + dm = DiscordMessage.objects.get() |
| 157 | + assert wh.processed_at is not None |
| 158 | + assert wh.processed_at < timezone.now() |
| 159 | + assert wh.event == "projects_v2_item.edited" |
| 160 | + assert dm.channel_id == settings.DISCORD_BOARD_CHANNEL_ID |
| 161 | + assert dm.channel_name == settings.DISCORD_BOARD_CHANNEL_NAME |
| 162 | + assert dm.content == ( |
| 163 | + "GitHub: [@github-project-automation[bot]]" |
| 164 | + "(https://github.com/apps/github-project-automation)" |
| 165 | + " projects_v2_item.edited **Status** of " |
| 166 | + "**[Test Issue](https://github.com/test-issue)**" |
| 167 | + " from **Done** to **In progress**" |
| 168 | + ) |
| 169 | + assert dm.sent_at is None |
0 commit comments