5
5
import pytest
6
6
import requests
7
7
import responses
8
- from django .conf import settings
9
8
from django .urls import reverse
10
9
from pytest_lazyfixture import lazy_fixture
11
10
from pytest_mock import MockerFixture
@@ -276,6 +275,7 @@ def test_create_github_repository(
276
275
"repository_owner" : "repositoryowner" ,
277
276
"repository_name" : "repositoryname" ,
278
277
"project" : project .id ,
278
+ "tagging_enabled" : True ,
279
279
}
280
280
281
281
responses .add (
@@ -297,6 +297,53 @@ def test_create_github_repository(
297
297
assert GithubRepository .objects .filter (repository_owner = "repositoryowner" ).exists ()
298
298
299
299
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
+
300
347
def test_cannot_create_github_repository_when_does_not_have_permissions (
301
348
test_user_client : APIClient ,
302
349
organisation : Organisation ,
@@ -655,9 +702,9 @@ def test_verify_github_webhook_payload_returns_false_on_no_signature_header() ->
655
702
def test_github_webhook_delete_installation (
656
703
api_client : APIClient ,
657
704
github_configuration : GithubConfiguration ,
705
+ set_github_webhook_secret ,
658
706
) -> None :
659
707
# Given
660
- settings .GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
661
708
url = reverse ("api-v1:github-webhook" )
662
709
663
710
# When
@@ -680,9 +727,9 @@ def test_github_webhook_merged_a_pull_request(
680
727
github_configuration : GithubConfiguration ,
681
728
github_repository : GithubRepository ,
682
729
feature_external_resource : FeatureExternalResource ,
730
+ set_github_webhook_secret ,
683
731
) -> None :
684
732
# Given
685
- settings .GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
686
733
url = reverse ("api-v1:github-webhook" )
687
734
688
735
# When
@@ -703,9 +750,9 @@ def test_github_webhook_merged_a_pull_request(
703
750
def test_github_webhook_without_installation_id (
704
751
api_client : APIClient ,
705
752
mocker : MockerFixture ,
753
+ set_github_webhook_secret ,
706
754
) -> None :
707
755
# Given
708
- settings .GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
709
756
url = reverse ("api-v1:github-webhook" )
710
757
mocker_logger = mocker .patch ("integrations.github.github.logger" )
711
758
@@ -729,9 +776,9 @@ def test_github_webhook_with_non_existing_installation(
729
776
api_client : APIClient ,
730
777
github_configuration : GithubConfiguration ,
731
778
mocker : MockerFixture ,
779
+ set_github_webhook_secret ,
732
780
) -> None :
733
781
# Given
734
- settings .GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
735
782
url = reverse ("api-v1:github-webhook" )
736
783
mocker_logger = mocker .patch ("integrations.github.github.logger" )
737
784
@@ -753,9 +800,9 @@ def test_github_webhook_with_non_existing_installation(
753
800
754
801
def test_github_webhook_fails_on_signature_header_missing (
755
802
github_configuration : GithubConfiguration ,
803
+ set_github_webhook_secret ,
756
804
) -> None :
757
805
# Given
758
- settings .GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
759
806
url = reverse ("api-v1:github-webhook" )
760
807
761
808
# When
@@ -775,9 +822,9 @@ def test_github_webhook_fails_on_signature_header_missing(
775
822
776
823
def test_github_webhook_fails_on_bad_signature_header_missing (
777
824
github_configuration : GithubConfiguration ,
825
+ set_github_webhook_secret ,
778
826
) -> None :
779
827
# Given
780
- settings .GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
781
828
url = reverse ("api-v1:github-webhook" )
782
829
783
830
# When
@@ -798,9 +845,9 @@ def test_github_webhook_fails_on_bad_signature_header_missing(
798
845
799
846
def test_github_webhook_bypass_event (
800
847
github_configuration : GithubConfiguration ,
848
+ set_github_webhook_secret ,
801
849
) -> None :
802
850
# Given
803
- settings .GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
804
851
url = reverse ("api-v1:github-webhook" )
805
852
806
853
# When
0 commit comments