1- import hmac, json
1+ import hmac
2+ import json
23from hashlib import sha256
34from unittest.mock import patch
45
56from django.urls import reverse
6- from django.utils.crypto import constant_time_compare
77from rest_framework import status
88from rest_framework.test import APITestCase
9+ from shared.django_apps.core.tests.factories import OwnerFactory
910
10- from codecov_auth.models import Owner, GithubAppInstallation
11- from utils.config import get_config
12- from shared.django_apps.core.tests.factories import OwnerFactory, RepositoryFactory
11+ from codecov_auth.models import GithubAppInstallation
1312
1413PAYLOAD_SECRET = b"testixik8qdauiab1yiffydimvi72ekq"
1514VIEW_URL = reverse("auth")
1615
16+
1717def sign_payload(payload, secret=PAYLOAD_SECRET):
1818 data = json.dumps(payload, separators=(",", ":")).encode("utf-8")
1919 signature = "sha256=" + hmac.new(secret, data, digestmod=sha256).hexdigest()
2020 return signature, data
2121
22+
2223class GenAIAuthViewTests(APITestCase):
2324 @patch("utils.config.get_config", return_value=PAYLOAD_SECRET)
2425 def test_missing_parameters(self, mock_config):
@@ -37,7 +38,9 @@ def test_missing_parameters(self, mock_config):
3738 def test_invalid_signature(self, mock_config):
3839 payload = {"external_owner_id": "owner1", "repo_service_id": "101"}
3940 # Create a wrong signature by altering the payload before signing
40- wrong_sig = "sha256=" + hmac.new(PAYLOAD_SECRET, b"{}", digestmod=sha256).hexdigest()
41+ wrong_sig = (
42+ "sha256=" + hmac.new(PAYLOAD_SECRET, b"{}", digestmod=sha256).hexdigest()
43+ )
4144 response = self.client.post(
4245 VIEW_URL,
4346 data=payload,
@@ -48,21 +51,20 @@ def test_invalid_signature(self, mock_config):
4851
4952 @patch("utils.config.get_config", return_value=PAYLOAD_SECRET)
5053 def test_owner_not_found(self, mock_config):
51- payload = {' external_owner_id': ' nonexistent_owner', ' repo_service_id': ' 101' }
54+ payload = {" external_owner_id": " nonexistent_owner", " repo_service_id": " 101" }
5255 sig, serialized_data = sign_payload(payload)
5356 response = self.client.post(
5457 VIEW_URL,
5558 HTTP_X_GEN_AI_AUTH_SIGNATURE=sig,
5659 data=serialized_data,
5760 content_type="application/json",
58-
5961 )
6062 self.assertEqual(response.status_code, 404)
6163
6264 @patch("utils.config.get_config", return_value=PAYLOAD_SECRET)
6365 def test_no_installation(self, mock_config):
6466 _ = OwnerFactory(service="github", service_id="owner1", username="test1")
65- payload = {' external_owner_id': ' owner1', ' repo_service_id': ' 101' }
67+ payload = {" external_owner_id": " owner1", " repo_service_id": " 101" }
6668 sig, data = sign_payload(payload)
6769 response = self.client.post(
6870 VIEW_URL,
@@ -81,10 +83,10 @@ def test_authorized(self, mock_config):
8183 installation_id=12345,
8284 owner=owner,
8385 name="ai-features",
84- repository_service_ids=[' 101', ' 202']
86+ repository_service_ids=[" 101", " 202"],
8587 )
8688 app_install.save()
87- payload = {"external_owner_id": "owner2", "repo_service_id": ' 101' }
89+ payload = {"external_owner_id": "owner2", "repo_service_id": " 101" }
8890 sig, data = sign_payload(payload)
8991 response = self.client.post(
9092 VIEW_URL,
@@ -103,10 +105,10 @@ def test_unauthorized(self, mock_config):
103105 installation_id=2,
104106 owner=owner,
105107 name="ai-features",
106- repository_service_ids=["303", "404"]
108+ repository_service_ids=["303", "404"],
107109 )
108110 app_install.save()
109- payload = {' external_owner_id': ' owner3', ' repo_service_id': ' 101' }
111+ payload = {" external_owner_id": " owner3", " repo_service_id": " 101" }
110112 sig, data = sign_payload(payload)
111113 response = self.client.post(
112114 VIEW_URL,
0 commit comments