From bbc0bc9735e11133c4969d6338592afe8ebd8882 Mon Sep 17 00:00:00 2001 From: Daniel Berglund Date: Tue, 12 Dec 2023 13:25:05 +0100 Subject: [PATCH] Fix stripe import error, fix random failing test --- api/src/shop/stripe_charge.py | 2 +- api/src/shop/stripe_customer.py | 2 +- api/src/shop/stripe_event.py | 2 +- api/src/shop/stripe_payment_intent.py | 2 +- api/src/shop/stripe_product_price.py | 4 ++-- api/src/shop/stripe_setup_intent.py | 2 +- api/src/shop/stripe_subscriptions.py | 10 +++++----- api/src/shop/stripe_util.py | 2 +- api/src/shop/test/stripe_product_price_test.py | 8 ++++---- api/src/shop/test/subscriptions_test.py | 4 ++-- api/src/systest/api/member_test.py | 4 ++-- 11 files changed, 21 insertions(+), 21 deletions(-) diff --git a/api/src/shop/stripe_charge.py b/api/src/shop/stripe_charge.py index 538a4c677..d681ffe42 100644 --- a/api/src/shop/stripe_charge.py +++ b/api/src/shop/stripe_charge.py @@ -1,7 +1,7 @@ from logging import getLogger import stripe -from stripe.error import InvalidRequestError, CardError, StripeError +from stripe import StripeError, InvalidRequestError, CardError from service.error import InternalServerError, EXCEPTION from shop.models import Transaction diff --git a/api/src/shop/stripe_customer.py b/api/src/shop/stripe_customer.py index 629792386..fcdf71fbc 100644 --- a/api/src/shop/stripe_customer.py +++ b/api/src/shop/stripe_customer.py @@ -3,7 +3,7 @@ import stripe -from stripe.error import InvalidRequestError +from stripe import InvalidRequestError from shop.stripe_util import retry, are_metadata_dicts_equivalent from service.db import db_session from service.error import NotFound, InternalServerError diff --git a/api/src/shop/stripe_event.py b/api/src/shop/stripe_event.py index 7034c80b3..96ac5a712 100644 --- a/api/src/shop/stripe_event.py +++ b/api/src/shop/stripe_event.py @@ -6,7 +6,7 @@ from typing import Any, Dict, List, Optional import stripe -from stripe.error import SignatureVerificationError, RateLimitError +from stripe import SignatureVerificationError, RateLimitError from datetime import timezone from shop import stripe_subscriptions import shop.transactions diff --git a/api/src/shop/stripe_payment_intent.py b/api/src/shop/stripe_payment_intent.py index e7d3719b4..440c19814 100644 --- a/api/src/shop/stripe_payment_intent.py +++ b/api/src/shop/stripe_payment_intent.py @@ -5,7 +5,7 @@ from typing_extensions import Never from dataclasses_json import DataClassJsonMixin import stripe -from stripe.error import InvalidRequestError, StripeError, CardError +from stripe import InvalidRequestError, CardError from stripe import PaymentIntent from membership.models import Member diff --git a/api/src/shop/stripe_product_price.py b/api/src/shop/stripe_product_price.py index a8fc385f4..cbb6b50aa 100644 --- a/api/src/shop/stripe_product_price.py +++ b/api/src/shop/stripe_product_price.py @@ -48,7 +48,7 @@ def get_stripe_product(makeradmin_product: Product) -> stripe.Product | None: id = get_stripe_product_id(makeradmin_product) try: return retry(lambda: stripe.Product.retrieve(id=id)) - except stripe.error.InvalidRequestError as e: + except stripe.InvalidRequestError as e: logger.warning( f"failed to retrive product from stripe for makeradmin product with id {makeradmin_product.id}, {e}" ) @@ -60,7 +60,7 @@ def get_stripe_prices( ) -> list[stripe.Price] | None: try: return list(retry(lambda: stripe.Price.list(product=stripe_product.stripe_id, lookup_keys=lookup_keys))) - except stripe.error.InvalidRequestError as e: + except stripe.InvalidRequestError as e: logger.warning(f"failed to retrive prices from stripe for stripe product with id {stripe_product.id}, {e}") return None diff --git a/api/src/shop/stripe_setup_intent.py b/api/src/shop/stripe_setup_intent.py index c5804bb36..21979fdf5 100644 --- a/api/src/shop/stripe_setup_intent.py +++ b/api/src/shop/stripe_setup_intent.py @@ -9,7 +9,7 @@ from shop.stripe_payment_intent import PaymentAction from shop.transactions import PaymentFailed from stripe import SetupIntent -from stripe.error import CardError +from stripe import CardError class SetupIntentResult(str, Enum): diff --git a/api/src/shop/stripe_subscriptions.py b/api/src/shop/stripe_subscriptions.py index 6cdfdc741..51e80bf36 100644 --- a/api/src/shop/stripe_subscriptions.py +++ b/api/src/shop/stripe_subscriptions.py @@ -35,7 +35,7 @@ import stripe from datetime import datetime, timezone, date, time, timedelta -from stripe.error import InvalidRequestError +from stripe import InvalidRequestError from shop.stripe_util import are_metadata_dicts_equivalent, retry, convert_from_stripe_amount from shop.stripe_customer import get_and_sync_stripe_customer from basic_types.enums import PriceLevel @@ -53,7 +53,7 @@ CURRENCY, ) from shop.stripe_constants import SubscriptionStatus -import stripe.error +import stripe class SubscriptionType(str, Enum): @@ -592,7 +592,7 @@ def pause_subscription( return True else: assert False - except stripe.error.InvalidRequestError as e: + except stripe.InvalidRequestError as e: if e.code == "resource_missing": # The subscription was already deleted # We might have missed the webhook to delete the reference from the member. @@ -649,7 +649,7 @@ def cancel_subscription( stripe.Subscription.delete(subscription_id) else: assert False - except stripe.error.InvalidRequestError as e: + except stripe.InvalidRequestError as e: if e.code == "resource_missing": # The subscription was already deleted. # We might have missed the webhook to delete the reference from the member. @@ -709,7 +709,7 @@ def get_subscription_info_from_subscription(sub_type: SubscriptionType, sub_id: amount_due=Decimal(upcoming["amount_due"]) / STRIPE_CURRENTY_BASE, ), ) - except stripe.error.InvalidRequestError as e: + except stripe.InvalidRequestError as e: if e.code == "invoice_upcoming_none": return SubscriptionInfo( type=sub_type, diff --git a/api/src/shop/stripe_util.py b/api/src/shop/stripe_util.py index 7c6ce3f55..1a2fe5c6e 100644 --- a/api/src/shop/stripe_util.py +++ b/api/src/shop/stripe_util.py @@ -117,7 +117,7 @@ def retry(f: Callable[[], T]) -> T: while True: try: return f() - except stripe.error.RateLimitError: + except stripe.RateLimitError: its += 1 if its > MAX_TRIES: raise diff --git a/api/src/shop/test/stripe_product_price_test.py b/api/src/shop/test/stripe_product_price_test.py index 6e295565b..42c35e967 100644 --- a/api/src/shop/test/stripe_product_price_test.py +++ b/api/src/shop/test/stripe_product_price_test.py @@ -301,7 +301,7 @@ def test_get_sync_product(self) -> None: makeradmin_test_product = self.db.create_product( name="test update product", price=100.0, - id=self.base_stripe_id + 6, + id=self.base_stripe_id + 8, unit="mån", smallest_multiple=1, category_id=self.subscription_category.id, @@ -319,7 +319,7 @@ def test_get_sync_price(self) -> None: makeradmin_test_product = self.db.create_product( name="test update price", price=100.0, - id=self.base_stripe_id + 7, + id=self.base_stripe_id + 9, unit="mån", smallest_multiple=1, category_id=self.not_subscription_category.id, @@ -352,7 +352,7 @@ def test_equal_product(self) -> None: makeradmin_test_eq_product = self.db.create_product( name="test eq price", price=200.0, - id=self.base_stripe_id + 8, + id=self.base_stripe_id + 10, unit="mån", smallest_multiple=1, category_id=self.subscription_category.id, @@ -381,7 +381,7 @@ def test_equal_price(self) -> None: makeradmin_test_eq_product = self.db.create_product( name="test eq price", price=200.0, - id=self.base_stripe_id + 9, + id=self.base_stripe_id + 11, unit="mån", smallest_multiple=1, category_id=self.subscription_category.id, diff --git a/api/src/shop/test/subscriptions_test.py b/api/src/shop/test/subscriptions_test.py index 24c653eb2..d251b4807 100644 --- a/api/src/shop/test/subscriptions_test.py +++ b/api/src/shop/test/subscriptions_test.py @@ -35,7 +35,7 @@ from shop.transactions import ship_orders from test_aid.test_base import FlaskTestBase import stripe -import stripe.error +import stripe from shop import stripe_event from shop import stripe_constants @@ -221,7 +221,7 @@ def test_clock_for_event(event: Any) -> Optional[str]: logger.info("Clock is ready. Waiting a bit to make sure we have received all events...") done = 1 break - except stripe.error.RateLimitError: + except stripe.RateLimitError: logger.warning("Exceeded Stripe API rate limit. Waiting a bit...") # This is most likely because we are running tests in parallel. # Add some jitter to avoid the stripe tests from running so much in parallel. diff --git a/api/src/systest/api/member_test.py b/api/src/systest/api/member_test.py index 9fefd8759..6ed9f1c36 100644 --- a/api/src/systest/api/member_test.py +++ b/api/src/systest/api/member_test.py @@ -32,14 +32,14 @@ def test_create_member_gives_new_member_numbers_and_ids(self): self.assertNotEqual(number1, number2) def test_create_password_using_unhashed_password(self): - pwd = random_str(8) + pwd = random_str(16) member = self.api.create_member(password=None, unhashed_password=pwd) self.post("/oauth/token", {"grant_type": "password", "username": member["email"], "password": pwd}).expect( code=200 ) def test_update_password_using_unhashed_password(self): - pwd = random_str(8) + pwd = random_str(16) member = self.db.create_member() self.put(f"/membership/member/{member.member_id}", dict(unhashed_password=pwd)).expect(200) self.post("/oauth/token", {"grant_type": "password", "username": member.email, "password": pwd}).expect(