-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: #328 Fix sign-up returns validation error on installation withou…
…t env variables (#374)
- Loading branch information
1 parent
08c28c2
commit 9b65dd9
Showing
12 changed files
with
90 additions
and
9 deletions.
There are no files selected for viewing
7 changes: 0 additions & 7 deletions
7
packages/backend/apps/finances/management/commands/init_subscriptions.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
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 |
---|---|---|
@@ -1,13 +1,25 @@ | ||
import logging | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from stripe.error import AuthenticationError | ||
|
||
from .services import subscriptions | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
User = get_user_model() | ||
|
||
|
||
@receiver(post_save, sender=User) | ||
def create_free_plan_subscription(sender, instance, created, **kwargs): | ||
if created: | ||
subscriptions.initialize_user(user=instance) | ||
try: | ||
subscriptions.initialize_user(user=instance) | ||
except AuthenticationError as e: | ||
logger.error(msg=e._message, exc_info=e) | ||
return | ||
except Exception as e: | ||
raise e |
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions
13
packages/backend/apps/users/management/commands/init_customers_plans.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,13 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from ...models import User | ||
from apps.finances.services import subscriptions | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Creates stripe customer and schedule subscription plan' | ||
|
||
def handle(self, *args, **options): | ||
users = User.objects.filter(djstripe_customers__isnull=True, is_superuser=False) | ||
for user in users: | ||
subscriptions.initialize_user(user=user) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
import factory | ||
from django.db.models import signals | ||
|
||
from apps.users.management.commands.init_customers_plans import Command | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
class TestInitCustomerCommand: | ||
@factory.django.mute_signals(signals.post_save) | ||
def test_command_run_for_users_without_customer(self, stripe_customer_factory, user_factory, mocker): | ||
mock = mocker.patch("apps.finances.services.subscriptions.initialize_user") | ||
user = user_factory.create() | ||
stripe_customer_factory.create() | ||
|
||
Command().handle() | ||
|
||
mock.assert_called_once_with(user=user) | ||
|
||
@factory.django.mute_signals(signals.post_save) | ||
def test_command_do_not_run_for_superusers(self, user_factory, mocker): | ||
mock = mocker.patch("apps.finances.services.subscriptions.initialize_user") | ||
user_factory.create(is_superuser=True) | ||
|
||
Command().handle() | ||
|
||
mock.assert_not_called() |
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 @@ | ||
import pytest | ||
from stripe.error import AuthenticationError | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
class TestUserPostSaveSignal: | ||
def test_signal_is_not_raising_exception_on_auth_error(self, user_factory, mocker): | ||
mock = mocker.patch("apps.finances.services.subscriptions.initialize_user", side_effect=AuthenticationError()) | ||
sentry_mock = mocker.patch("apps.finances.signals.logger.error") | ||
|
||
user_factory.create() | ||
|
||
mock.assert_called_once() | ||
sentry_mock.assert_called_once() | ||
|
||
def test_reraise_stripe_error(self, user_factory, totp_mock, mocker): | ||
initial_error = Exception | ||
mocker.patch("apps.finances.services.subscriptions.initialize_user", side_effect=initial_error()) | ||
|
||
with pytest.raises(initial_error) as error: | ||
user_factory.create() | ||
|
||
assert initial_error == 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
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