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

Commit 34c1039

Browse files
use bytes in test
1 parent ac5908b commit 34c1039

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

api/gen_ai/tests/test_gen_ai.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import hmac
2-
import json
32
from hashlib import sha256
43
from unittest.mock import patch
54

@@ -14,20 +13,19 @@
1413
VIEW_URL = reverse("auth")
1514

1615

17-
def sign_payload(payload, secret=PAYLOAD_SECRET):
18-
data = json.dumps(payload, separators=(",", ":")).encode("utf-8")
16+
def sign_payload(data: bytes, secret=PAYLOAD_SECRET):
1917
signature = "sha256=" + hmac.new(secret, data, digestmod=sha256).hexdigest()
2018
return signature, data
2119

2220

2321
class GenAIAuthViewTests(APITestCase):
2422
@patch("utils.config.get_config", return_value=PAYLOAD_SECRET)
2523
def test_missing_parameters(self, mock_config):
26-
payload = {}
24+
payload = b"{}"
2725
sig, data = sign_payload(payload)
2826
response = self.client.post(
2927
VIEW_URL,
30-
data=payload,
28+
data=data,
3129
content_type="application/json",
3230
HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=sig,
3331
)
@@ -36,11 +34,10 @@ def test_missing_parameters(self, mock_config):
3634

3735
@patch("utils.config.get_config", return_value=PAYLOAD_SECRET)
3836
def test_invalid_signature(self, mock_config):
39-
payload = {"external_owner_id": "owner1", "repo_service_id": "101"}
40-
# Create a wrong signature by altering the payload before signing
41-
wrong_sig = (
42-
"sha256=" + hmac.new(PAYLOAD_SECRET, b"{}", digestmod=sha256).hexdigest()
43-
)
37+
# Correct payload
38+
payload = b'{"external_owner_id":"owner1","repo_service_id":"101"}'
39+
# Wrong signature based on a different payload
40+
wrong_sig = "sha256=" + hmac.new(PAYLOAD_SECRET, b"{}", sha256).hexdigest()
4441
response = self.client.post(
4542
VIEW_URL,
4643
data=payload,
@@ -51,42 +48,41 @@ def test_invalid_signature(self, mock_config):
5148

5249
@patch("utils.config.get_config", return_value=PAYLOAD_SECRET)
5350
def test_owner_not_found(self, mock_config):
54-
payload = {"external_owner_id": "nonexistent_owner", "repo_service_id": "101"}
55-
sig, serialized_data = sign_payload(payload)
51+
payload = b'{"external_owner_id":"nonexistent_owner","repo_service_id":"101"}'
52+
sig, data = sign_payload(payload)
5653
response = self.client.post(
5754
VIEW_URL,
58-
HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=sig,
59-
data=serialized_data,
55+
data=data,
6056
content_type="application/json",
57+
HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=sig,
6158
)
6259
self.assertEqual(response.status_code, 404)
6360

6461
@patch("utils.config.get_config", return_value=PAYLOAD_SECRET)
6562
def test_no_installation(self, mock_config):
66-
_ = OwnerFactory(service="github", service_id="owner1", username="test1")
67-
payload = {"external_owner_id": "owner1", "repo_service_id": "101"}
63+
# Create a valid owner but no installation
64+
OwnerFactory(service="github", service_id="owner1", username="test1")
65+
payload = b'{"external_owner_id":"owner1","repo_service_id":"101"}'
6866
sig, data = sign_payload(payload)
6967
response = self.client.post(
7068
VIEW_URL,
7169
data=data,
7270
content_type="application/json",
7371
HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=sig,
7472
)
75-
7673
self.assertEqual(response.status_code, 200)
7774
self.assertEqual(response.data, {"is_valid": False})
7875

7976
@patch("utils.config.get_config", return_value=PAYLOAD_SECRET)
8077
def test_authorized(self, mock_config):
8178
owner = OwnerFactory(service="github", service_id="owner2", username="test2")
82-
app_install = GithubAppInstallation(
79+
GithubAppInstallation.objects.create(
8380
installation_id=12345,
8481
owner=owner,
8582
name="ai-features",
8683
repository_service_ids=["101", "202"],
8784
)
88-
app_install.save()
89-
payload = {"external_owner_id": "owner2", "repo_service_id": "101"}
85+
payload = b'{"external_owner_id":"owner2","repo_service_id":"101"}'
9086
sig, data = sign_payload(payload)
9187
response = self.client.post(
9288
VIEW_URL,
@@ -100,15 +96,13 @@ def test_authorized(self, mock_config):
10096
@patch("utils.config.get_config", return_value=PAYLOAD_SECRET)
10197
def test_unauthorized(self, mock_config):
10298
owner = OwnerFactory(service="github", service_id="owner3", username="test3")
103-
# Create a GithubAppInstallation where the list does not include the requested repo_service_id.
104-
app_install = GithubAppInstallation.objects.create(
99+
GithubAppInstallation.objects.create(
105100
installation_id=2,
106101
owner=owner,
107102
name="ai-features",
108103
repository_service_ids=["303", "404"],
109104
)
110-
app_install.save()
111-
payload = {"external_owner_id": "owner3", "repo_service_id": "101"}
105+
payload = b'{"external_owner_id":"owner3","repo_service_id":"101"}'
112106
sig, data = sign_payload(payload)
113107
response = self.client.post(
114108
VIEW_URL,

0 commit comments

Comments
 (0)