|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +from django.test import TransactionTestCase |
| 4 | +from shared.django_apps.core.tests.factories import OwnerFactory |
| 5 | + |
| 6 | +from graphql_api.tests.helper import GraphQLTestHelper |
| 7 | + |
| 8 | +query = """ |
| 9 | +mutation($input: CreateStripeSetupIntentInput!) { |
| 10 | + createStripeSetupIntent(input: $input) { |
| 11 | + error { |
| 12 | + __typename |
| 13 | + } |
| 14 | + clientSecret |
| 15 | + } |
| 16 | +} |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +class CreateStripeSetupIntentTestCase(GraphQLTestHelper, TransactionTestCase): |
| 21 | + def setUp(self): |
| 22 | + self.owner = OwnerFactory(username="codecov-user") |
| 23 | + |
| 24 | + def test_when_unauthenticated(self): |
| 25 | + data = self.gql_request(query, variables={"input": {"owner": "somename"}}) |
| 26 | + assert ( |
| 27 | + data["createStripeSetupIntent"]["error"]["__typename"] |
| 28 | + == "UnauthenticatedError" |
| 29 | + ) |
| 30 | + |
| 31 | + def test_when_unauthorized(self): |
| 32 | + other_owner = OwnerFactory(username="other-user") |
| 33 | + data = self.gql_request( |
| 34 | + query, |
| 35 | + owner=self.owner, |
| 36 | + variables={"input": {"owner": other_owner.username}}, |
| 37 | + ) |
| 38 | + assert ( |
| 39 | + data["createStripeSetupIntent"]["error"]["__typename"] |
| 40 | + == "UnauthorizedError" |
| 41 | + ) |
| 42 | + |
| 43 | + @patch("services.billing.stripe.SetupIntent.create") |
| 44 | + def test_when_validation_error(self, setup_intent_create_mock): |
| 45 | + setup_intent_create_mock.side_effect = Exception("Some error") |
| 46 | + data = self.gql_request( |
| 47 | + query, owner=self.owner, variables={"input": {"owner": self.owner.username}} |
| 48 | + ) |
| 49 | + assert ( |
| 50 | + data["createStripeSetupIntent"]["error"]["__typename"] == "ValidationError" |
| 51 | + ) |
| 52 | + |
| 53 | + def test_when_owner_not_found(self): |
| 54 | + data = self.gql_request( |
| 55 | + query, owner=self.owner, variables={"input": {"owner": "nonexistent-user"}} |
| 56 | + ) |
| 57 | + assert ( |
| 58 | + data["createStripeSetupIntent"]["error"]["__typename"] == "ValidationError" |
| 59 | + ) |
| 60 | + |
| 61 | + @patch("services.billing.stripe.SetupIntent.create") |
| 62 | + def test_success(self, setup_intent_create_mock): |
| 63 | + setup_intent_create_mock.return_value = {"client_secret": "test-client-secret"} |
| 64 | + data = self.gql_request( |
| 65 | + query, owner=self.owner, variables={"input": {"owner": self.owner.username}} |
| 66 | + ) |
| 67 | + assert data["createStripeSetupIntent"]["clientSecret"] == "test-client-secret" |
0 commit comments