|
| 1 | +import pytest |
| 2 | +import respx |
| 3 | +from core.integrations.github import ( |
| 4 | + GITHUB_API_URL, |
| 5 | + GithubProjectV2Item, |
| 6 | + fetch_github_item_details, |
| 7 | + parse_github_webhook, |
| 8 | +) |
| 9 | +from httpx import Response |
| 10 | + |
| 11 | + |
| 12 | +def test_parse_github_webhook_raises_value_error_for_unsupported(): |
| 13 | + headers = {"X-Github-Event": "random_event"} |
| 14 | + content = {} |
| 15 | + |
| 16 | + with pytest.raises(ValueError): |
| 17 | + parse_github_webhook(headers, content) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def mocked_github_response(): |
| 22 | + def _mock_response(item_type, content_data): |
| 23 | + return {"data": {"node": {"content": content_data}}} |
| 24 | + |
| 25 | + return _mock_response |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def sample_content(): |
| 30 | + return { |
| 31 | + "sender": {"login": "testuser", "html_url": "https://github.com/testuser"}, |
| 32 | + "projects_v2_item": { |
| 33 | + "content_type": "Issue", |
| 34 | + "node_id": "test_node_id", |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | +@respx.mock |
| 40 | +def test_fetch_github_item_details(mocked_github_response): |
| 41 | + mocked_response = mocked_github_response( |
| 42 | + "Issue", |
| 43 | + { |
| 44 | + "id": "test_issue_id", |
| 45 | + "title": "Test Issue", |
| 46 | + "url": "https://github.com/test/repo/issues/1", |
| 47 | + }, |
| 48 | + ) |
| 49 | + respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) |
| 50 | + |
| 51 | + result = fetch_github_item_details("test_node_id") |
| 52 | + |
| 53 | + assert result["id"] == "test_issue_id" |
| 54 | + assert result["title"] == "Test Issue" |
| 55 | + assert result["url"] == "https://github.com/test/repo/issues/1" |
| 56 | + |
| 57 | + |
| 58 | +@respx.mock |
| 59 | +def test_github_project_created_event(mocked_github_response, sample_content): |
| 60 | + sample_content["action"] = "created" |
| 61 | + mocked_response = mocked_github_response( |
| 62 | + "Issue", |
| 63 | + { |
| 64 | + "id": "test_issue_id", |
| 65 | + "title": "Test Issue", |
| 66 | + "url": "https://github.com/test/repo/issues/1", |
| 67 | + }, |
| 68 | + ) |
| 69 | + respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) |
| 70 | + |
| 71 | + parser = GithubProjectV2Item(sample_content) |
| 72 | + message = parser.as_str() |
| 73 | + |
| 74 | + assert message == ( |
| 75 | + "[@testuser](https://github.com/testuser) created " |
| 76 | + "[Test Issue](https://github.com/test/repo/issues/1)" |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +@respx.mock |
| 81 | +def test_github_project_edited_event_for_status_change( |
| 82 | + mocked_github_response, sample_content |
| 83 | +): |
| 84 | + sample_content["action"] = "edited" |
| 85 | + sample_content["changes"] = { |
| 86 | + "field_value": { |
| 87 | + "field_name": "Status", |
| 88 | + "field_type": "single_select", |
| 89 | + "from": {"name": "To Do"}, |
| 90 | + "to": {"name": "In Progress"}, |
| 91 | + } |
| 92 | + } |
| 93 | + mocked_response = mocked_github_response( |
| 94 | + "Issue", |
| 95 | + { |
| 96 | + "id": "test_issue_id", |
| 97 | + "title": "Test Issue", |
| 98 | + "url": "https://github.com/test/repo/issues/1", |
| 99 | + }, |
| 100 | + ) |
| 101 | + respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) |
| 102 | + |
| 103 | + parser = GithubProjectV2Item(sample_content) |
| 104 | + message = parser.as_str() |
| 105 | + |
| 106 | + assert message == ( |
| 107 | + "[@testuser](https://github.com/testuser) changed **Status** of " |
| 108 | + "**[Test Issue](https://github.com/test/repo/issues/1)** " |
| 109 | + "from **To Do** to **In Progress**" |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +@respx.mock |
| 114 | +def test_github_project_edited_event_for_date_change( |
| 115 | + mocked_github_response, sample_content |
| 116 | +): |
| 117 | + sample_content["action"] = "edited" |
| 118 | + sample_content["changes"] = { |
| 119 | + "field_value": { |
| 120 | + "field_name": "Deadline", |
| 121 | + "field_type": "date", |
| 122 | + "from": "2024-01-01T10:20:30", |
| 123 | + "to": "2025-01-05T20:30:10", |
| 124 | + } |
| 125 | + } |
| 126 | + mocked_response = mocked_github_response( |
| 127 | + "Issue", |
| 128 | + { |
| 129 | + "id": "test_issue_id", |
| 130 | + "title": "Test Issue", |
| 131 | + "url": "https://github.com/test/repo/issues/1", |
| 132 | + }, |
| 133 | + ) |
| 134 | + respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) |
| 135 | + |
| 136 | + parser = GithubProjectV2Item(sample_content) |
| 137 | + message = parser.as_str() |
| 138 | + |
| 139 | + assert message == ( |
| 140 | + "[@testuser](https://github.com/testuser) changed **Deadline** of " |
| 141 | + "**[Test Issue](https://github.com/test/repo/issues/1)** " |
| 142 | + "from **2024-01-01** to **2025-01-05**" |
| 143 | + ) |
| 144 | + |
| 145 | + |
| 146 | +@respx.mock |
| 147 | +def test_github_project_draft_issue_event(mocked_github_response, sample_content): |
| 148 | + sample_content["action"] = "created" |
| 149 | + sample_content["projects_v2_item"]["content_type"] = "DraftIssue" |
| 150 | + mocked_response = mocked_github_response( |
| 151 | + "DraftIssue", |
| 152 | + { |
| 153 | + "id": "draft_issue_id", |
| 154 | + "title": "Draft Title", |
| 155 | + }, |
| 156 | + ) |
| 157 | + respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) |
| 158 | + |
| 159 | + parser = GithubProjectV2Item(sample_content) |
| 160 | + message = parser.as_str() |
| 161 | + |
| 162 | + assert message == "[@testuser](https://github.com/testuser) created Draft Title" |
| 163 | + |
| 164 | + |
| 165 | +def test_github_project_unsupported_action(sample_content): |
| 166 | + sample_content["action"] = "unsupported_action" |
| 167 | + |
| 168 | + parser = GithubProjectV2Item(sample_content) |
| 169 | + |
| 170 | + with pytest.raises(ValueError, match="Action unsupported unsupported_action"): |
| 171 | + parser.action() |
| 172 | + |
| 173 | + |
| 174 | +@respx.mock |
| 175 | +def test_github_project_edited_event_no_changes(mocked_github_response, sample_content): |
| 176 | + sample_content["action"] = "edited" |
| 177 | + mocked_response = mocked_github_response( |
| 178 | + "Issue", |
| 179 | + { |
| 180 | + "id": "test_issue_id", |
| 181 | + "title": "Test Issue", |
| 182 | + "url": "https://github.com/test/repo/issues/1", |
| 183 | + }, |
| 184 | + ) |
| 185 | + respx.post(GITHUB_API_URL).mock(return_value=Response(200, json=mocked_response)) |
| 186 | + |
| 187 | + parser = GithubProjectV2Item(sample_content) |
| 188 | + message = parser.as_str() |
| 189 | + |
| 190 | + assert message == ( |
| 191 | + "[@testuser](https://github.com/testuser) changed " |
| 192 | + "[Test Issue](https://github.com/test/repo/issues/1)" |
| 193 | + ) |
| 194 | + |
| 195 | + |
| 196 | +@respx.mock |
| 197 | +def test_fetch_github_item_details_api_error(): |
| 198 | + respx.post(GITHUB_API_URL).mock( |
| 199 | + return_value=Response(500, json={"message": "Internal Server Error"}) |
| 200 | + ) |
| 201 | + |
| 202 | + with pytest.raises(Exception, match="GitHub API error: 500 - .*"): |
| 203 | + fetch_github_item_details("test_node_id") |
0 commit comments