Skip to content

Commit

Permalink
Code cov 2
Browse files Browse the repository at this point in the history
  • Loading branch information
novakzaballa committed Aug 9, 2024
1 parent 76e5f3d commit 07292e4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
7 changes: 7 additions & 0 deletions api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1137,3 +1137,10 @@ def handle(self, record: logging.LogRecord) -> None:
self.messages.append(self.format(record))

return InspectingHandler()


@pytest.fixture
def set_github_webhook_secret() -> None:
from django.conf import settings

settings.GITHUB_WEBHOOK_SECRET = "secret-key"
3 changes: 0 additions & 3 deletions api/integrations/github/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,6 @@ def create_flagsmith_flag_label(
logger.warning("Label already exists")
return {"message": "Label already exists"}, 200
raise
except Exception as err:
logger.error(f"DEBUG: Other error occurred: {err}")
raise


def label_github_issue_pr(
Expand Down
65 changes: 56 additions & 9 deletions api/tests/unit/integrations/github/test_unit_github_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest
import requests
import responses
from django.conf import settings
from django.urls import reverse
from pytest_lazyfixture import lazy_fixture
from pytest_mock import MockerFixture
Expand Down Expand Up @@ -276,6 +275,7 @@ def test_create_github_repository(
"repository_owner": "repositoryowner",
"repository_name": "repositoryname",
"project": project.id,
"tagging_enabled": True,
}

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


@responses.activate
def test_create_github_repository_and_label_already_Existe(
admin_client_new: APIClient,
organisation: Organisation,
github_configuration: GithubConfiguration,
project: Project,
mocker: MockerFixture,
mock_github_client_generate_token: MagicMock,
) -> None:
# Given
mocker_logger = mocker.patch("integrations.github.client.logger")

data = {
"github_configuration": github_configuration.id,
"repository_owner": "repositoryowner",
"repository_name": "repositoryname",
"project": project.id,
"tagging_enabled": True,
}

mock_response = {
"message": "Validation Failed",
"errors": [{"resource": "Label", "code": "already_exists", "field": "name"}],
"documentation_url": "https://docs.github.com/rest/issues/labels#create-a-label",
"status": "422",
}

responses.add(
method="POST",
url=f"{GITHUB_API_URL}repos/repositoryowner/repositoryname/labels",
status=status.HTTP_422_UNPROCESSABLE_ENTITY,
json=mock_response,
)

url = reverse(
"api-v1:organisations:repositories-list",
args=[organisation.id, github_configuration.id],
)
# When
response = admin_client_new.post(url, data)

# Then
mocker_logger.warning.assert_called_once_with("Label already exists")
assert response.status_code == status.HTTP_201_CREATED
assert GithubRepository.objects.filter(repository_owner="repositoryowner").exists()


def test_cannot_create_github_repository_when_does_not_have_permissions(
test_user_client: APIClient,
organisation: Organisation,
Expand Down Expand Up @@ -655,9 +702,9 @@ def test_verify_github_webhook_payload_returns_false_on_no_signature_header() ->
def test_github_webhook_delete_installation(
api_client: APIClient,
github_configuration: GithubConfiguration,
set_github_webhook_secret,
) -> None:
# Given
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
url = reverse("api-v1:github-webhook")

# When
Expand All @@ -680,9 +727,9 @@ def test_github_webhook_merged_a_pull_request(
github_configuration: GithubConfiguration,
github_repository: GithubRepository,
feature_external_resource: FeatureExternalResource,
set_github_webhook_secret,
) -> None:
# Given
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
url = reverse("api-v1:github-webhook")

# When
Expand All @@ -703,11 +750,11 @@ def test_github_webhook_merged_a_pull_request(
def test_github_webhook_without_installation_id(
api_client: APIClient,
mocker: MockerFixture,
set_github_webhook_secret,
) -> None:
# Given
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
url = reverse("api-v1:github-webhook")
mocker_logger = mocker.patch("integrations.github.github.logger")
mocker_logger = mocker.patch("integrations.github.github.loggers")

# When
response = api_client.post(
Expand All @@ -729,9 +776,9 @@ def test_github_webhook_with_non_existing_installation(
api_client: APIClient,
github_configuration: GithubConfiguration,
mocker: MockerFixture,
set_github_webhook_secret,
) -> None:
# Given
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
url = reverse("api-v1:github-webhook")
mocker_logger = mocker.patch("integrations.github.github.logger")

Expand All @@ -753,9 +800,9 @@ def test_github_webhook_with_non_existing_installation(

def test_github_webhook_fails_on_signature_header_missing(
github_configuration: GithubConfiguration,
set_github_webhook_secret,
) -> None:
# Given
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
url = reverse("api-v1:github-webhook")

# When
Expand All @@ -775,9 +822,9 @@ def test_github_webhook_fails_on_signature_header_missing(

def test_github_webhook_fails_on_bad_signature_header_missing(
github_configuration: GithubConfiguration,
set_github_webhook_secret,
) -> None:
# Given
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
url = reverse("api-v1:github-webhook")

# When
Expand All @@ -798,9 +845,9 @@ def test_github_webhook_fails_on_bad_signature_header_missing(

def test_github_webhook_bypass_event(
github_configuration: GithubConfiguration,
set_github_webhook_secret,
) -> None:
# Given
settings.GITHUB_WEBHOOK_SECRET = WEBHOOK_SECRET
url = reverse("api-v1:github-webhook")

# When
Expand Down

0 comments on commit 07292e4

Please sign in to comment.