예제 #1
0
 def test_user_subscribed_is_run_when_user_tier_is_changed_to_paid(
         self, user_subscribed, session):
     free_user = UserFactory(tier="free")
     free_user.plan = Plan.paid()
     session.add(free_user)
     session.commit()
     user_subscribed.assert_called_with(free_user.id, Plan.paid().id)
예제 #2
0
    def test_after_commit_hooks_are_specific_to_a_session(
            self, user_subscribed, session, app):

        # What does this test prove? Well - we start by showing
        # the thing works and by setting up a an "after_commit" hook
        # on the session.  (There SHOULD BE one session per request.)
        # The next request should NOT fire the "after_commit" hook
        # because it has a new session.  IE - we are not leaking
        # Model instances across requests, and not firing subscribe
        # events for instances we didn't change

        free_user = UserFactory(tier="free")
        free_user.plan = Plan.paid()

        session.add(free_user)
        session.commit()
        session.expire_all()

        user_subscribed.assert_called_with(free_user.id, Plan.paid().id)
        user_subscribed.reset_mock()

        sess = db.create_scoped_session()

        free_user2 = UserFactory(tier="free", email="*****@*****.**")
        session.expunge(free_user2.plan)
        sess.add(free_user2)
        sess.commit()
        user_subscribed.assert_not_called()

        sess.remove()
    def test_user_unlink_from_plan_via_subscription(self):
        """Unlink a User from a plan based on change in their stripe subscription"""
        user = StripedUserFactory(tier="paid",
                                  stripe__payment_method="pm_card_visa")
        subscription = UserStripe(user)._retrieve_users_subscriptions(
            Plan.paid())

        UserStripe(user).unlink_from_plan_via_subscription(subscription.id)

        assert UserStripe(user)._retrieve_users_subscriptions(
            Plan.paid()) is None
        assert user.plan == Plan.free()
예제 #4
0
    def test_user_subscribed_is_run_when_user_tier_is_changed_to_paid(
        self, user_subscribed, session
    ):
        free_user = UserFactory(tier="free")

        UserUpdate(
            user=free_user,
            rels={"plan": {"data": {"type": "plan", "id": str(Plan.paid().id)}}},
        ).update()

        db.session.commit()

        user_subscribed.assert_called_with(free_user.id, Plan.paid().id)
예제 #5
0
def test_locate():
    """ Convert a Json-API style locator ref to a python object """
    p = Plan.paid()

    obj = locate({"type": "plan", "id": str(p.id)})

    assert obj == p
    def test_user_link_to_plan_via_subscription(self):
        """Link a User to a Plan based on a change in their stripe subscription"""
        user = StripedUserFactory(
            tier="paid",
            stripe__payment_method="pm_card_threeDSecure2Required")

        subscriptions = stripe.Subscription.list(plan=Plan.paid().stripe_id,
                                                 customer=user.stripe_id)

        # this subscription is _NOT_ actually paid now, but we check in the event
        # listener - this method just makes sure the User's plan reflects the
        # subscription they'r paying for
        UserStripe(user).link_to_plan_via_subscription(
            subscriptions.data[0].id)

        assert user.plan == Plan.paid()
예제 #7
0
    def test_striped_user_can_modify_plan(self, app, session):
        """ A user with a valid stripe_id is able to change their payment method """

        app.test_client_class = TestClient
        user = StripedUserFactory(tier="free", stripe__payment_method="pm_card_visa")
        session.add(user)
        session.flush()
        plan_id = Plan.paid().id

        with app.test_client(user=user) as client:
            res = client.patch(
                "/account",
                json={
                    "data": {
                        "type": "user",
                        "attributes": {},
                        "relationships": {
                            "plan": {"data": {"type": "plan", "id": str(plan_id)}}
                        },
                    }
                },
            )

            assert res.status_code == 200
            user = User.query.filter_by(uuid=user.uuid).first()
            assert user.tier == "paid"
예제 #8
0
    def test_user_async_unsubscribe(self, unlink_from_plan, session):
        user = UserFactory(tier="free", stripe_id="cust_12345")
        session.add(user)
        session.flush()
        plan = Plan.paid()

        user_unsubscribed(user.id, plan.id)

        assert unlink_from_plan.called_once_with(user, plan)
    def test_user_fails_to_subscribe_no_card(self, _sf, _ss, _sfa, session):
        """ User couldn't subscribe because they didn't setup a
            card or stripe account"""
        user = UnstripedUserFactory()
        plan = Plan.paid()
        uss = UserStripe(user)
        session.add(user)
        session.flush()

        with pytest.raises(AssertionError):
            uss.link_to_plan(plan)
예제 #10
0
    def test_user_can_change_relationship(self, session):
        free_user = UserFactory(tier="free")
        plan = Plan.paid()
        session.add(free_user)
        session.flush()

        user = UserUpdate(
            user=free_user,
            rels={"plan": {"data": {"type": "plan", "id": str(plan.id)}}},
        ).update()

        assert user.plan == plan
    def test_user_unsubscribed(self, unsubscribe_successful,
                               unsubscribe_not_required, session):
        """ User unsubscribes successfully """
        user = SubscribedUserFactory(tier="paid",
                                     stripe__payment_method="pm_card_visa")
        user.plan = Plan.free()
        session.add(user)
        session.flush()

        UserStripe(user).unlink_from_plan(Plan.paid())

        assert unsubscribe_successful.called_once
        assert not unsubscribe_not_required.called
예제 #12
0
    def test_user_unsubscribed_is_run_when_user_tier_is_changed_to_free(
        self, user_unsubscribed, session
    ):
        user = UserFactory()
        db.session.add(user)
        db.session.flush()

        UserUpdate(
            user=user,
            rels={"plan": {"data": {"type": "plan", "id": str(Plan.free().id)}}},
        ).update()

        db.session.commit()

        user_unsubscribed.assert_called_with(user.id, Plan.paid().id)
    def test_user_unsubscribed_but_didnt_need_to(self, unsubscribe_successful,
                                                 unsubscribe_not_required,
                                                 session):
        """User can't unsubscribe if they're not subscribed in the first place
        <taps forehead>"""

        user = StripedUserFactory(tier="free",
                                  stripe__payment_method="pm_card_visa")
        user.plan = Plan.free()
        session.add(user)
        session.flush()

        UserStripe(user).unlink_from_plan(Plan.paid())

        assert not unsubscribe_successful.called
        assert unsubscribe_not_required.called_once
    def test_delete_customer(self, app):
        """ Delete a Stripe Customer """

        user = StripedUserFactory(tier="paid",
                                  email="*****@*****.**",
                                  stripe__payment_method="pm_card_visa")
        uss = UserStripe(user)
        uss.link_to_plan(Plan.paid())

        customer_id = user.stripe_id

        UserStripe(user).delete_stripe_customer()

        cus = stripe.Customer.retrieve(customer_id)
        assert cus.deleted is True
        assert user.plan == Plan.free()
    def test_user_subscribed_successfully(
        self,
        subscribed_successfully,
        subscribed_failed,
        subscription_requires_action,
        session,
    ):
        """ User subscribed sucessfully """
        user = StripedUserFactory(tier="paid",
                                  stripe__payment_method="pm_card_visa")
        session.add(user)
        session.flush()
        # the user is a paid user when it is submitted to this job
        # immediately after a commit which changes it's plan_id
        # NOTE: this test does not actually change the user's plan
        UserStripe(user).link_to_plan(user.plan)

        assert not subscribed_failed.called
        assert not subscription_requires_action.called
        assert subscribed_successfully.called_once
        assert user.plan == Plan.paid()
예제 #16
0
    def test_unstriped_user_cannot_modify_plan(self, app, session):
        """ The user can update plan id only if paymenet method and customer id
        are set. They cannot be set in the same request."""

        app.test_client_class = TestClient
        user = UnstripedUserFactory()
        session.add(user)
        session.flush()
        plan_id = Plan.paid().id
        with app.test_client(user=user) as client:
            res = client.patch(
                "/account",
                json={
                    "data": {
                        "type": "user",
                        "relationships": {
                            "plan": {"data": {"type": "plan", "id": str(plan_id)}}
                        },
                    }
                },
            )

        assert res.status_code == 422