|
| 1 | +import hmac |
| 2 | +from hashlib import sha256 |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from django.urls import reverse |
| 6 | +from rest_framework import status |
| 7 | +from rest_framework.test import APITestCase |
| 8 | +from shared.django_apps.core.tests.factories import OwnerFactory |
| 9 | + |
| 10 | +from codecov_auth.models import GithubAppInstallation |
| 11 | + |
| 12 | +PAYLOAD_SECRET = b"testixik8qdauiab1yiffydimvi72ekq" |
| 13 | +VIEW_URL = reverse("auth") |
| 14 | + |
| 15 | + |
| 16 | +def sign_payload(data: bytes, secret=PAYLOAD_SECRET): |
| 17 | + signature = "sha256=" + hmac.new(secret, data, digestmod=sha256).hexdigest() |
| 18 | + return signature, data |
| 19 | + |
| 20 | + |
| 21 | +class GenAIAuthViewTests(APITestCase): |
| 22 | + @patch("api.gen_ai.views.get_config", return_value=PAYLOAD_SECRET) |
| 23 | + def test_missing_parameters(self, mock_config): |
| 24 | + payload = b"{}" |
| 25 | + sig, data = sign_payload(payload) |
| 26 | + response = self.client.post( |
| 27 | + VIEW_URL, |
| 28 | + data=data, |
| 29 | + content_type="application/json", |
| 30 | + HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=sig, |
| 31 | + ) |
| 32 | + self.assertEqual(response.status_code, 400) |
| 33 | + self.assertIn("Missing required parameters", response.data) |
| 34 | + |
| 35 | + @patch("api.gen_ai.views.get_config", return_value=PAYLOAD_SECRET) |
| 36 | + def test_invalid_signature(self, mock_config): |
| 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() |
| 41 | + response = self.client.post( |
| 42 | + VIEW_URL, |
| 43 | + data=payload, |
| 44 | + content_type="application/json", |
| 45 | + HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=wrong_sig, |
| 46 | + ) |
| 47 | + self.assertEqual(response.status_code, 403) |
| 48 | + |
| 49 | + @patch("api.gen_ai.views.get_config", return_value=PAYLOAD_SECRET) |
| 50 | + def test_owner_not_found(self, mock_config): |
| 51 | + payload = b'{"external_owner_id":"nonexistent_owner","repo_service_id":"101"}' |
| 52 | + sig, data = sign_payload(payload) |
| 53 | + response = self.client.post( |
| 54 | + VIEW_URL, |
| 55 | + data=data, |
| 56 | + content_type="application/json", |
| 57 | + HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=sig, |
| 58 | + ) |
| 59 | + self.assertEqual(response.status_code, 404) |
| 60 | + |
| 61 | + @patch("api.gen_ai.views.get_config", return_value=PAYLOAD_SECRET) |
| 62 | + def test_no_installation(self, mock_config): |
| 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"}' |
| 66 | + sig, data = sign_payload(payload) |
| 67 | + response = self.client.post( |
| 68 | + VIEW_URL, |
| 69 | + data=data, |
| 70 | + content_type="application/json", |
| 71 | + HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=sig, |
| 72 | + ) |
| 73 | + self.assertEqual(response.status_code, 200) |
| 74 | + self.assertEqual(response.data, {"is_valid": False}) |
| 75 | + |
| 76 | + @patch("api.gen_ai.views.get_config", return_value=PAYLOAD_SECRET) |
| 77 | + def test_authorized(self, mock_config): |
| 78 | + owner = OwnerFactory(service="github", service_id="owner2", username="test2") |
| 79 | + GithubAppInstallation.objects.create( |
| 80 | + installation_id=12345, |
| 81 | + owner=owner, |
| 82 | + name="ai-features", |
| 83 | + repository_service_ids=["101", "202"], |
| 84 | + ) |
| 85 | + payload = b'{"external_owner_id":"owner2","repo_service_id":"101"}' |
| 86 | + sig, data = sign_payload(payload) |
| 87 | + response = self.client.post( |
| 88 | + VIEW_URL, |
| 89 | + data=data, |
| 90 | + content_type="application/json", |
| 91 | + HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=sig, |
| 92 | + ) |
| 93 | + self.assertEqual(response.status_code, status.HTTP_200_OK) |
| 94 | + self.assertEqual(response.data, {"is_valid": True}) |
| 95 | + |
| 96 | + @patch("api.gen_ai.views.get_config", return_value=PAYLOAD_SECRET) |
| 97 | + def test_unauthorized(self, mock_config): |
| 98 | + owner = OwnerFactory(service="github", service_id="owner3", username="test3") |
| 99 | + GithubAppInstallation.objects.create( |
| 100 | + installation_id=2, |
| 101 | + owner=owner, |
| 102 | + name="ai-features", |
| 103 | + repository_service_ids=["303", "404"], |
| 104 | + ) |
| 105 | + payload = b'{"external_owner_id":"owner3","repo_service_id":"101"}' |
| 106 | + sig, data = sign_payload(payload) |
| 107 | + response = self.client.post( |
| 108 | + VIEW_URL, |
| 109 | + data=data, |
| 110 | + content_type="application/json", |
| 111 | + HTTP_HTTP_X_GEN_AI_AUTH_SIGNATURE=sig, |
| 112 | + ) |
| 113 | + self.assertEqual(response.status_code, 200) |
| 114 | + self.assertEqual(response.data, {"is_valid": False}) |
0 commit comments