Skip to content

Commit 15095f4

Browse files
committed
Add test suite for the GitHub integration #349
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent a83502e commit 15095f4

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# DejaCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: AGPL-3.0-only
5+
# See https://github.com/aboutcode-org/dejacode for support or download.
6+
# See https://aboutcode.org for more information about AboutCode FOSS projects.
7+
#
8+
9+
from unittest import mock
10+
11+
from django.contrib.contenttypes.models import ContentType
12+
from django.test import TestCase
13+
14+
from dje.models import Dataspace
15+
from dje.tests import create_superuser
16+
from workflow.integrations.github import GitHubIntegration
17+
from workflow.models import Question
18+
from workflow.models import RequestTemplate
19+
20+
21+
class GitHubIntegrationTestCase(TestCase):
22+
def setUp(self):
23+
patcher = mock.patch("workflow.models.Request.handle_integrations", return_value=None)
24+
self.mock_handle_integrations = patcher.start()
25+
self.addCleanup(patcher.stop)
26+
27+
self.dataspace = Dataspace.objects.create(name="nexB")
28+
self.dataspace.set_configuration("github_token", "fake-token")
29+
self.super_user = create_superuser("nexb_user", self.dataspace)
30+
self.component_ct = ContentType.objects.get(
31+
app_label="component_catalog", model="component"
32+
)
33+
self.request_template = RequestTemplate.objects.create(
34+
name="GitHub Template",
35+
description="Integration test template",
36+
content_type=self.component_ct,
37+
dataspace=self.dataspace,
38+
issue_tracker_id="https://github.com/nexB/repo",
39+
)
40+
self.question = Question.objects.create(
41+
template=self.request_template,
42+
label="Example Question",
43+
input_type="TextField",
44+
position=0,
45+
dataspace=self.dataspace,
46+
)
47+
self.request = self.request_template.create_request(
48+
title="Example Request",
49+
requester=self.super_user,
50+
serialized_data='{"Example Question": "Some value"}',
51+
)
52+
self.github = GitHubIntegration(dataspace=self.dataspace)
53+
54+
def test_extract_github_repo_path_valid_url(self):
55+
url = "https://github.com/user/repo"
56+
result = GitHubIntegration.extract_github_repo_path(url)
57+
self.assertEqual(result, "user/repo")
58+
59+
def test_extract_github_repo_path_invalid_url(self):
60+
with self.assertRaises(ValueError):
61+
GitHubIntegration.extract_github_repo_path("https://example.com/user/repo")
62+
63+
def test_get_headers_returns_auth_header(self):
64+
headers = self.github.get_headers()
65+
self.assertEqual(headers, {"Authorization": "token fake-token"})
66+
67+
def test_make_issue_title(self):
68+
title = GitHubIntegration.make_issue_title(self.request)
69+
self.assertEqual(title, "[DEJACODE] Example Request")
70+
71+
def test_make_issue_body_contains_question(self):
72+
body = GitHubIntegration.make_issue_body(self.request)
73+
self.assertIn("### Example Question", body)
74+
self.assertIn("Some value", body)
75+
76+
@mock.patch("requests.Session.post")
77+
def test_create_issue_calls_post(self, mock_session_post):
78+
mock_session_post.return_value.json.return_value = {"number": 10}
79+
mock_session_post.return_value.raise_for_status.return_value = None
80+
81+
issue = self.github.create_issue(
82+
repo_id="user/repo",
83+
title="Issue Title",
84+
body="Issue Body",
85+
labels=["High"],
86+
)
87+
88+
self.assertEqual(issue["number"], 10)
89+
mock_session_post.assert_called_once()
90+
91+
@mock.patch("requests.Session.patch")
92+
def test_update_issue_calls_patch(self, mock_session_patch):
93+
mock_session_patch.return_value.json.return_value = {"state": "closed"}
94+
mock_session_patch.return_value.raise_for_status.return_value = None
95+
96+
response = self.github.update_issue(
97+
repo_id="user/repo",
98+
issue_id=123,
99+
title="Updated title",
100+
body="Updated body",
101+
state="closed",
102+
)
103+
104+
self.assertEqual(response["state"], "closed")
105+
mock_session_patch.assert_called_once()

0 commit comments

Comments
 (0)