-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move create-setup-intent to graphql api
- Loading branch information
1 parent
0d80364
commit 198f23e
Showing
17 changed files
with
207 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
codecov_auth/commands/owner/interactors/create_stripe_setup_intent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import logging | ||
|
||
import stripe | ||
|
||
from codecov.commands.base import BaseInteractor | ||
from codecov.commands.exceptions import Unauthenticated, Unauthorized, ValidationError | ||
from codecov.db import sync_to_async | ||
from codecov_auth.helpers import current_user_part_of_org | ||
from codecov_auth.models import Owner | ||
from services.billing import BillingService | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
class CreateStripeSetupIntentInteractor(BaseInteractor): | ||
def validate(self, owner_obj: Owner) -> None: | ||
if not self.current_user.is_authenticated: | ||
raise Unauthenticated() | ||
if not owner_obj: | ||
raise ValidationError("Owner not found") | ||
if not current_user_part_of_org(self.current_owner, owner_obj): | ||
raise Unauthorized() | ||
|
||
def create_setup_intent(self, owner_obj: Owner) -> stripe.SetupIntent: | ||
try: | ||
billing = BillingService(requesting_user=self.current_owner) | ||
return billing.create_setup_intent(owner_obj) | ||
except Exception as e: | ||
log.error( | ||
f"Error getting setup intent for owner {owner_obj.ownerid}", | ||
extra={"error": str(e)}, | ||
) | ||
raise ValidationError("Unable to create setup intent") | ||
|
||
@sync_to_async | ||
def execute(self, owner: str) -> stripe.SetupIntent: | ||
owner_obj = Owner.objects.filter(username=owner, service=self.service).first() | ||
self.validate(owner_obj) | ||
return self.create_setup_intent(owner_obj) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
graphql_api/tests/mutation/test_create_stripe_setup_intent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from unittest.mock import patch | ||
from django.test import TransactionTestCase | ||
from shared.django_apps.core.tests.factories import OwnerFactory | ||
|
||
from codecov_auth.models import Session | ||
from graphql_api.tests.helper import GraphQLTestHelper | ||
|
||
query = """ | ||
mutation($input: CreateStripeSetupIntentInput!) { | ||
createStripeSetupIntent(input: $input) { | ||
error { | ||
__typename | ||
} | ||
clientSecret | ||
} | ||
} | ||
""" | ||
|
||
|
||
class CreateStripeSetupIntentTestCase(GraphQLTestHelper, TransactionTestCase): | ||
def setUp(self): | ||
self.owner = OwnerFactory(username="codecov-user") | ||
|
||
def test_when_unauthenticated(self): | ||
data = self.gql_request(query, variables={"input": {"owner": "somename"}}) | ||
assert data["createStripeSetupIntent"]["error"]["__typename"] == "UnauthenticatedError" | ||
|
||
@patch("services.billing.stripe.SetupIntent.create") | ||
def test_when_authenticated(self, setup_intent_create_mock): | ||
setup_intent_create_mock.return_value = {"client_secret": "test-client-secret"} | ||
data = self.gql_request( | ||
query, owner=self.owner, variables={"input": {"owner": self.owner.username}} | ||
) | ||
assert data["createStripeSetupIntent"]["clientSecret"] == "test-client-secret" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
input CreateStripeSetupIntentInput { | ||
owner: String! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
graphql_api/types/mutation/create_stripe_setup_intent/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from graphql_api.helpers.ariadne import ariadne_load_local_graphql | ||
|
||
from .create_stripe_setup_intent import ( | ||
error_create_stripe_setup_intent, | ||
resolve_create_stripe_setup_intent, | ||
) | ||
|
||
gql_create_stripe_setup_intent = ariadne_load_local_graphql( | ||
__file__, "create_stripe_setup_intent.graphql" | ||
) | ||
|
||
__all__ = ["error_create_stripe_setup_intent", "resolve_create_stripe_setup_intent"] |
6 changes: 6 additions & 0 deletions
6
graphql_api/types/mutation/create_stripe_setup_intent/create_stripe_setup_intent.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
union CreateStripeSetupIntentError = UnauthenticatedError | ValidationError | ||
|
||
type CreateStripeSetupIntentPayload { | ||
error: CreateStripeSetupIntentError | ||
clientSecret: String | ||
} |
24 changes: 24 additions & 0 deletions
24
graphql_api/types/mutation/create_stripe_setup_intent/create_stripe_setup_intent.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Dict | ||
|
||
from ariadne import UnionType | ||
from ariadne.types import GraphQLResolveInfo | ||
|
||
from graphql_api.helpers.mutation import ( | ||
resolve_union_error_type, | ||
wrap_error_handling_mutation, | ||
) | ||
|
||
|
||
@wrap_error_handling_mutation | ||
async def resolve_create_stripe_setup_intent( | ||
_, info: GraphQLResolveInfo, input: Dict[str, any] | ||
) -> Dict[str, any]: | ||
command = info.context["executor"].get_command("owner") | ||
resp = await command.create_stripe_setup_intent(input.get("owner")) | ||
return { | ||
"client_secret": resp["client_secret"], | ||
} | ||
|
||
|
||
error_create_stripe_setup_intent = UnionType("CreateStripeSetupIntentError") | ||
error_create_stripe_setup_intent.type_resolver(resolve_union_error_type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.