Skip to content

Commit 54e6fd4

Browse files
committed
Add unit tests for the integration Model methods #349
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent 15095f4 commit 54e6fd4

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

workflow/tests/test_integrations.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,21 @@ def test_update_issue_calls_patch(self, mock_session_patch):
103103

104104
self.assertEqual(response["state"], "closed")
105105
mock_session_patch.assert_called_once()
106+
107+
@mock.patch("requests.Session.post")
108+
def test_post_comment_calls_post(self, mock_session_post):
109+
mock_session_post.return_value.json.return_value = {"id": 77, "body": "Test comment"}
110+
mock_session_post.return_value.raise_for_status.return_value = None
111+
112+
response = self.github.post_comment(
113+
repo_id="user/repo",
114+
issue_id=42,
115+
comment_body="Test comment",
116+
)
117+
118+
self.assertEqual(response["body"], "Test comment")
119+
mock_session_post.assert_called_once_with(
120+
"https://api.github.com/repos/user/repo/issues/42/comments",
121+
json={"body": "Test comment"},
122+
timeout=self.github.default_timeout,
123+
)

workflow/tests/test_models.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88

99
import json
10+
from unittest import mock
1011

1112
from django.contrib.contenttypes.models import ContentType
1213
from django.test import TestCase
@@ -350,6 +351,30 @@ def test_request_queryset_status_methods(self):
350351
self.assertIn(self.request2, qs)
351352
self.assertNotIn(request3, qs)
352353

354+
def test_request_model_close_method(self):
355+
self.request1.close(user=self.super_user, reason="Reason")
356+
self.request1.refresh_from_db()
357+
self.assertEqual(Request.Status.CLOSED, self.request1.status)
358+
self.assertEqual(self.super_user, self.request1.last_modified_by)
359+
close_event = self.request1.events.get(event_type=RequestEvent.CLOSED)
360+
self.assertEqual(self.super_user, close_event.user)
361+
self.assertEqual("Reason", close_event.text)
362+
363+
def test_request_model_link_external_issue(self):
364+
external_issue = self.request1.link_external_issue(
365+
platform="github",
366+
repo="org/repo",
367+
issue_id="1",
368+
)
369+
self.assertEqual(self.nexb_dataspace, external_issue.dataspace)
370+
self.assertEqual("github", external_issue.platform)
371+
self.assertEqual("org/repo", external_issue.repo)
372+
self.assertEqual("1", external_issue.issue_id)
373+
self.assertEqual("https://github.com/org/repo/issues/1", external_issue.issue_url)
374+
375+
self.request1.refresh_from_db()
376+
self.assertEqual(external_issue, self.request1.external_issue)
377+
353378
def test_request_model_is_draft_property(self):
354379
self.request1.status = Request.Status.OPEN
355380
self.assertFalse(self.request1.is_draft)
@@ -564,3 +589,21 @@ def test_request_content_object_update_request_count_on_delete(self):
564589

565590
component1.delete()
566591
request1.delete()
592+
593+
@mock.patch("workflow.integrations.github.GitHubIntegration.sync")
594+
def test_sync_called_on_save_when_issue_tracker_is_github(self, mock_sync):
595+
mock_sync.return_value = None
596+
self.nexb_dataspace.set_configuration("github_token", "fake-token")
597+
598+
self.request_template1.create_request(
599+
title="Example Request",
600+
requester=self.super_user,
601+
)
602+
mock_sync.assert_not_called()
603+
604+
self.request_template1.update(issue_tracker_id="https://github.com/org/repo")
605+
request = self.request_template1.create_request(
606+
title="Example Request",
607+
requester=self.super_user,
608+
)
609+
mock_sync.assert_called_once_with(request=request)

0 commit comments

Comments
 (0)