|
| 1 | +from sentry.tempest.models import TempestCredentials |
1 | 2 | from sentry.testutils.cases import APITestCase |
2 | 3 | from sentry.testutils.helpers.features import Feature |
3 | 4 |
|
4 | 5 |
|
5 | 6 | class TestTempestCredentials(APITestCase): |
6 | 7 | endpoint = "sentry-api-0-project-tempest-credentials" |
7 | 8 |
|
8 | | - def test_create_tempest_credentials(self): |
| 9 | + valid_credentials_data = {"clientId": "test", "clientSecret": "test"} |
| 10 | + |
| 11 | + def test_get_tempest_credentials(self): |
9 | 12 | with Feature({"organizations:tempest-access": True}): |
10 | 13 | credentials = [self.create_tempest_credentials(self.project) for _ in range(5)] |
11 | 14 |
|
@@ -34,3 +37,88 @@ def test_client_secret_is_obfuscated(self): |
34 | 37 |
|
35 | 38 | def test_unauthenticated_user_cant_access_endpoint(self): |
36 | 39 | self.get_error_response(self.project.organization.slug, self.project.slug) |
| 40 | + |
| 41 | + def test_create_tempest_credentials(self): |
| 42 | + with Feature({"organizations:tempest-access": True}): |
| 43 | + self.login_as(self.user) |
| 44 | + response = self.get_success_response( |
| 45 | + self.project.organization.slug, |
| 46 | + self.project.slug, |
| 47 | + method="POST", |
| 48 | + **self.valid_credentials_data, |
| 49 | + ) |
| 50 | + assert response.status_code == 201 |
| 51 | + creds_obj = TempestCredentials.objects.get(project=self.project) |
| 52 | + assert creds_obj.client_id == self.valid_credentials_data["clientId"] |
| 53 | + assert creds_obj.client_secret == self.valid_credentials_data["clientSecret"] |
| 54 | + assert creds_obj.project == self.project |
| 55 | + assert creds_obj.created_by_id == self.user.id |
| 56 | + |
| 57 | + def test_create_tempest_credentials_without_feature_flag(self): |
| 58 | + self.login_as(self.user) |
| 59 | + response = self.get_error_response( |
| 60 | + self.project.organization.slug, |
| 61 | + self.project.slug, |
| 62 | + method="POST", |
| 63 | + **self.valid_credentials_data, |
| 64 | + ) |
| 65 | + assert response.status_code == 404 |
| 66 | + |
| 67 | + def test_create_tempest_credentials_as_unauthenticated_user(self): |
| 68 | + response = self.get_error_response( |
| 69 | + self.project.organization.slug, |
| 70 | + self.project.slug, |
| 71 | + method="POST", |
| 72 | + **self.valid_credentials_data, |
| 73 | + ) |
| 74 | + assert response.status_code == 401 |
| 75 | + |
| 76 | + def test_non_admin_cant_create_tempest_credentials(self): |
| 77 | + non_admin_user = self.create_user() |
| 78 | + self.create_member( |
| 79 | + user=non_admin_user, organization=self.project.organization, role="member" |
| 80 | + ) |
| 81 | + with Feature({"organizations:tempest-access": True}): |
| 82 | + self.login_as(non_admin_user) |
| 83 | + response = self.get_error_response( |
| 84 | + self.project.organization.slug, |
| 85 | + self.project.slug, |
| 86 | + method="POST", |
| 87 | + **self.valid_credentials_data, |
| 88 | + ) |
| 89 | + assert response.status_code == 403 |
| 90 | + |
| 91 | + def test_create_tempest_credentials_with_invalid_data(self): |
| 92 | + with Feature({"organizations:tempest-access": True}): |
| 93 | + self.login_as(self.user) |
| 94 | + response = self.get_error_response( |
| 95 | + self.project.organization.slug, |
| 96 | + self.project.slug, |
| 97 | + method="POST", |
| 98 | + **{"clientId": "test"}, |
| 99 | + ) |
| 100 | + assert response.status_code == 400 |
| 101 | + |
| 102 | + response2 = self.get_error_response( |
| 103 | + self.project.organization.slug, |
| 104 | + self.project.slug, |
| 105 | + method="POST", |
| 106 | + **{"clientSecret": "test"}, |
| 107 | + ) |
| 108 | + assert response2.status_code == 400 |
| 109 | + |
| 110 | + def test_cant_create_tempest_credentials_with_duplicate_client_id(self): |
| 111 | + with Feature({"organizations:tempest-access": True}): |
| 112 | + self.login_as(self.user) |
| 113 | + self.create_tempest_credentials( |
| 114 | + self.project, client_id=self.valid_credentials_data["clientId"] |
| 115 | + ) |
| 116 | + response = self.get_error_response( |
| 117 | + self.project.organization.slug, |
| 118 | + self.project.slug, |
| 119 | + method="POST", |
| 120 | + **self.valid_credentials_data, |
| 121 | + ) |
| 122 | + # database constraint violation |
| 123 | + assert response.status_code == 400 |
| 124 | + assert response.data["detail"] == "A credential with this client ID already exists." |
0 commit comments