Skip to content

Commit 42d2ade

Browse files
committed
Code cov 2
1 parent 76e5f3d commit 42d2ade

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

api/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -1137,3 +1137,10 @@ def handle(self, record: logging.LogRecord) -> None:
11371137
self.messages.append(self.format(record))
11381138

11391139
return InspectingHandler()
1140+
1141+
1142+
@pytest.fixture
1143+
def set_github_webhook_secret() -> None:
1144+
from django.conf import settings
1145+
1146+
settings.GITHUB_WEBHOOK_SECRET = "secret-key"

api/integrations/github/client.py

-3
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,6 @@ def create_flagsmith_flag_label(
281281
logger.warning("Label already exists")
282282
return {"message": "Label already exists"}, 200
283283
raise
284-
except Exception as err:
285-
logger.error(f"DEBUG: Other error occurred: {err}")
286-
raise
287284

288285

289286
def label_github_issue_pr(

api/tests/unit/integrations/github/test_unit_github_views.py

+55-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import pytest
66
import requests
77
import responses
8-
from django.conf import settings
98
from django.urls import reverse
109
from pytest_lazyfixture import lazy_fixture
1110
from pytest_mock import MockerFixture
@@ -276,6 +275,7 @@ def test_create_github_repository(
276275
"repository_owner": "repositoryowner",
277276
"repository_name": "repositoryname",
278277
"project": project.id,
278+
"tagging_enabled": True,
279279
}
280280

281281
responses.add(
@@ -297,6 +297,53 @@ def test_create_github_repository(
297297
assert GithubRepository.objects.filter(repository_owner="repositoryowner").exists()
298298

299299

300+
@responses.activate
301+
def test_create_github_repository_and_label_already_Existe(
302+
admin_client_new: APIClient,
303+
organisation: Organisation,
304+
github_configuration: GithubConfiguration,
305+
project: Project,
306+
mocker: MockerFixture,
307+
mock_github_client_generate_token: MagicMock,
308+
) -> None:
309+
# Given
310+
mocker_logger = mocker.patch("integrations.github.client.logger")
311+
312+
data = {
313+
"github_configuration": github_configuration.id,
314+
"repository_owner": "repositoryowner",
315+
"repository_name": "repositoryname",
316+
"project": project.id,
317+
"tagging_enabled": True,
318+
}
319+
320+
mock_response = {
321+
"message": "Validation Failed",
322+
"errors": [{"resource": "Label", "code": "already_exists", "field": "name"}],
323+
"documentation_url": "https://docs.github.com/rest/issues/labels#create-a-label",
324+
"status": "422",
325+
}
326+
327+
responses.add(
328+
method="POST",
329+
url=f"{GITHUB_API_URL}repos/repositoryowner/repositoryname/labels",
330+
status=status.HTTP_422_UNPROCESSABLE_ENTITY,
331+
json=mock_response,
332+
)
333+
334+
url = reverse(
335+
"api-v1:organisations:repositories-list",
336+
args=[organisation.id, github_configuration.id],
337+
)
338+
# When
339+
response = admin_client_new.post(url, data)
340+
341+
# Then
342+
mocker_logger.warning.assert_called_once_with("Label already exists")
343+
assert response.status_code == status.HTTP_201_CREATED
344+
assert GithubRepository.objects.filter(repository_owner="repositoryowner").exists()
345+
346+
300347
def test_cannot_create_github_repository_when_does_not_have_permissions(
301348
test_user_client: APIClient,
302349
organisation: Organisation,
@@ -655,9 +702,9 @@ def test_verify_github_webhook_payload_returns_false_on_no_signature_header() ->
655702
def test_github_webhook_delete_installation(
656703
api_client: APIClient,
657704
github_configuration: GithubConfiguration,
705+
set_github_webhook_secret,
658706
) -> None:
659707
# Given
660-
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
661708
url = reverse("api-v1:github-webhook")
662709

663710
# When
@@ -680,9 +727,9 @@ def test_github_webhook_merged_a_pull_request(
680727
github_configuration: GithubConfiguration,
681728
github_repository: GithubRepository,
682729
feature_external_resource: FeatureExternalResource,
730+
set_github_webhook_secret,
683731
) -> None:
684732
# Given
685-
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
686733
url = reverse("api-v1:github-webhook")
687734

688735
# When
@@ -703,9 +750,9 @@ def test_github_webhook_merged_a_pull_request(
703750
def test_github_webhook_without_installation_id(
704751
api_client: APIClient,
705752
mocker: MockerFixture,
753+
set_github_webhook_secret,
706754
) -> None:
707755
# Given
708-
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
709756
url = reverse("api-v1:github-webhook")
710757
mocker_logger = mocker.patch("integrations.github.github.logger")
711758

@@ -729,9 +776,9 @@ def test_github_webhook_with_non_existing_installation(
729776
api_client: APIClient,
730777
github_configuration: GithubConfiguration,
731778
mocker: MockerFixture,
779+
set_github_webhook_secret,
732780
) -> None:
733781
# Given
734-
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
735782
url = reverse("api-v1:github-webhook")
736783
mocker_logger = mocker.patch("integrations.github.github.logger")
737784

@@ -753,9 +800,9 @@ def test_github_webhook_with_non_existing_installation(
753800

754801
def test_github_webhook_fails_on_signature_header_missing(
755802
github_configuration: GithubConfiguration,
803+
set_github_webhook_secret,
756804
) -> None:
757805
# Given
758-
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
759806
url = reverse("api-v1:github-webhook")
760807

761808
# When
@@ -775,9 +822,9 @@ def test_github_webhook_fails_on_signature_header_missing(
775822

776823
def test_github_webhook_fails_on_bad_signature_header_missing(
777824
github_configuration: GithubConfiguration,
825+
set_github_webhook_secret,
778826
) -> None:
779827
# Given
780-
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
781828
url = reverse("api-v1:github-webhook")
782829

783830
# When
@@ -798,9 +845,9 @@ def test_github_webhook_fails_on_bad_signature_header_missing(
798845

799846
def test_github_webhook_bypass_event(
800847
github_configuration: GithubConfiguration,
848+
set_github_webhook_secret,
801849
) -> None:
802850
# Given
803-
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
804851
url = reverse("api-v1:github-webhook")
805852

806853
# When

0 commit comments

Comments
 (0)