def test_keep_active_trials(self): """Recent trials are not expired.""" stdout = StringIO() trialing = Account.AccountStatus.TRIALING account = AccountFactory(status=trialing, user__date_joined=timezone.now()) command = Command(stdout=stdout) command.handle() account.refresh_from_db() assert account.status == Account.AccountStatus.TRIALING
def test_account_active(self): """An account is set to the active state.""" account = AccountFactory(status=Account.AccountStatus.TRIALING) event = EventFactory( data={"object": { "client_reference_id": str(account.id) }}) handle_checkout_session_completed(event) account.refresh_from_db() assert account.status == Account.AccountStatus.ACTIVE
def test_other_statuses_not_expired(self): """Only TRIALING is marked as expired.""" stdout = StringIO() active = Account.AccountStatus.ACTIVE account = AccountFactory( status=active, user__date_joined=timezone.now() - datetime.timedelta(days=constants.TRIAL_DAYS + 5), ) command = Command(stdout=stdout) command.handle() account.refresh_from_db() assert account.status == Account.AccountStatus.ACTIVE
def test_expires_trials(self): """Old trials are marked as expired.""" stdout = StringIO() trialing = Account.AccountStatus.TRIALING account = AccountFactory( status=trialing, user__date_joined=timezone.now() - datetime.timedelta(days=constants.TRIAL_DAYS + 5), ) command = Command(stdout=stdout) command.handle() account.refresh_from_db() assert account.status == Account.AccountStatus.TRIAL_EXPIRED
def test_non_trialing(self): """Non-TRIALING accounts do not see the banner.""" account = AccountFactory(status=Account.AccountStatus.ACTIVE) request = self.rf.get("/") request.account = account context = {"request": request} context = accounts_tags.trial_banner(context) assert not context["display_banner"]
def test_creates_session(self, mock_stripe): """The gateway creates a checkout session for the price.""" account = AccountFactory() mock_stripe.checkout.Session.create.return_value = {"id": "fake_session_id"} gateway = StripeGateway() session_id = gateway.create_checkout_session("price_fake_id", account) assert session_id == "fake_session_id" kwargs = mock_stripe.checkout.Session.create.call_args.kwargs assert kwargs["customer_email"] == account.email assert kwargs["client_reference_id"] == str(account.id) assert "subscription_data" in kwargs
def test_display_banner(self): """An old TRIALING account displays the trial banner.""" old_trial_start = timezone.now() - datetime.timedelta(days=constants.TRIAL_DAYS) account = AccountFactory( status=Account.AccountStatus.TRIALING, user__date_joined=old_trial_start ) request = self.rf.get("/") request.account = account context = {"request": request} context = accounts_tags.trial_banner(context) assert context["display_banner"]
def test_creates_billing_portal_session(self, mock_stripe): """The gateway creates a URL to a billing portal session.""" account = AccountFactory() customer = CustomerFactory(email=account.email) mock_session = mock.Mock() mock_session.url = "/portal" mock_stripe.billing_portal.Session.create.return_value = mock_session gateway = StripeGateway() url = gateway.create_billing_portal_session(account) assert url == "/portal" kwargs = mock_stripe.billing_portal.Session.create.call_args.kwargs assert kwargs["customer"] == customer.id assert self.reverse("settings:dashboard") in kwargs["return_url"]
def test_no_trial_in_stripe_limits(self, mock_stripe): """A trial is not added to the session within Stripe's limit. Stripe does not permit a trial unless there are at least 48 hours from now. """ too_close_date = timezone.now() - datetime.timedelta( days=constants.TRIAL_DAYS - 1 # Set the user 24 hours from trial ending. ) account = AccountFactory(user__date_joined=too_close_date) gateway = StripeGateway() gateway.create_checkout_session("price_fake_id", account) kwargs = mock_stripe.checkout.Session.create.call_args.kwargs assert "subscription_data" not in kwargs
def test_factory(self): account = AccountFactory() assert account.user is not None assert account.status == Account.AccountStatus.TRIALING
def test_trial_end(self): """The account has a trial end date.""" account = AccountFactory() assert account.trial_end == account.user.date_joined + datetime.timedelta( days=constants.TRIAL_DAYS)
def test_email(self): """The account proxies the account holder's email address.""" account = AccountFactory() assert account.email == account.user.email