Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 28457dd

Browse files
Lint
1 parent 5bdbff6 commit 28457dd

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

api/gen_ai/tests/test_gen_ai.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
import hmac, json
1+
import hmac
2+
import json
23
from hashlib import sha256
34
from unittest.mock import patch
45

56
from django.urls import reverse
6-
from django.utils.crypto import constant_time_compare
77
from rest_framework import status
88
from 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

1413
PAYLOAD_SECRET = b"testixik8qdauiab1yiffydimvi72ekq"
1514
VIEW_URL = reverse("auth")
1615

16+
1717
def 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+
2223
class 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,

api/gen_ai/views.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import hmac
22
import logging
33
from hashlib import sha256
4+
45
from django.utils.crypto import constant_time_compare
5-
from graphql_api.types.owner.owner import AI_FEATURES_GH_APP_ID
66
from rest_framework.exceptions import NotFound, PermissionDenied
77
from rest_framework.permissions import AllowAny
88
from rest_framework.response import Response
99
from rest_framework.views import APIView
10-
from codecov_auth.models import Owner
11-
from core.models import Repository
10+
11+
from codecov_auth.models import GithubAppInstallation, Owner
12+
from graphql_api.types.owner.owner import AI_FEATURES_GH_APP_ID
1213
from utils.config import get_config
13-
from codecov_auth.models import GithubAppInstallation
1414

1515
log = logging.getLogger(__name__)
1616

17+
1718
class GenAIAuthView(APIView):
1819
permission_classes = [AllowAny]
1920

@@ -24,7 +25,9 @@ def validate_signature(self, request):
2425
if isinstance(key, str):
2526
key = key.encode("utf-8")
2627
expected_sig = request.META.get("HTTP_X_GEN_AI_AUTH_SIGNATURE")
27-
computed_sig = "sha256=" + hmac.new(key, request.body, digestmod=sha256).hexdigest()
28+
computed_sig = (
29+
"sha256=" + hmac.new(key, request.body, digestmod=sha256).hexdigest()
30+
)
2831
if not (expected_sig and constant_time_compare(computed_sig, expected_sig)):
2932
raise PermissionDenied("Invalid signature")
3033

@@ -38,23 +41,24 @@ def post(self, request, *args, **kwargs):
3841
owner = Owner.objects.get(service_id=external_owner_id)
3942
except Owner.DoesNotExist:
4043
raise NotFound("Owner not found")
41-
44+
4245
is_authorized = True
4346

44-
app_install = GithubAppInstallation.objects.filter(owner_id=owner.ownerid, app_id=AI_FEATURES_GH_APP_ID).first()
47+
app_install = GithubAppInstallation.objects.filter(
48+
owner_id=owner.ownerid, app_id=AI_FEATURES_GH_APP_ID
49+
).first()
4550

4651
if not app_install:
4752
print("FAILED")
4853
is_authorized = False
49-
54+
5055
else:
5156
repo_ids = app_install.repository_service_ids
5257
if repo_ids and repo_service_id not in repo_ids:
5358
print("HERE")
5459
is_authorized = False
5560

56-
5761
return Response({"is_valid": is_authorized})
5862

5963

60-
# api/gen_ai/tests/test_gen_ai.py
64+
# api/gen_ai/tests/test_gen_ai.py

0 commit comments

Comments
 (0)