Esempio n. 1
0
def gen_collabs(n, plan=None):
    for _ in range(n):
        key = secrets.token_hex(3)
        u = User.objects.create_user(
            f"collab-{key}", f"collab-{key}@example.com", "heyhey2222"
        )
        create_profile_from_user(u)
        profile = Profile.objects.get(user=u)
        if plan is not None and USE_STRIPE:
            stripe_customer = stripe.Customer.create(
                email=u.email, source="tok_bypassPending"
            )
            Customer.get_or_construct(stripe_customer.id, user=u)
            profile.refresh_from_db()
            product = Product.objects.get(name="Compute Studio Subscription")
            plan_instance = product.plans.get(nickname=f"Monthly {plan.title()} Plan")
            profile.user.customer.update_plan(plan_instance)
            profile.refresh_from_db()
            yield profile
        elif plan is not None:
            Customer.objects.create(
                stripe_id=f"collab-{u.pk}",
                livemode=False,
                user=u,
                default_source="123",
                metadata={},
            )
            profile.refresh_from_db()
            mock_cp = lambda: {"name": plan, "plan_duration": plan.lower()}
            profile.user.customer.current_plan = mock_cp
            yield profile
        else:
            yield profile
def test_authors_sorted_alphabetically(db, get_inputs, meta_param_dict):
    modeler = User.objects.get(username="******").profile
    inputs = _submit_inputs("Used-for-testing", get_inputs, meta_param_dict,
                            modeler)

    _, submit_sim = _submit_sim(inputs)
    sim = submit_sim.submit()
    sim.status = "SUCCESS"
    sim.is_public = True
    sim.save()

    factory = APIRequestFactory()
    req = factory.get("/")
    req.user = modeler.user

    u = User.objects.create_user("aaaa", "*****@*****.**", "heyhey2222")
    create_profile_from_user(u)
    profile = Profile.objects.get(user__username="******")

    pp, created = PendingPermission.objects.get_or_create(
        sim=sim, profile=profile, permission_name="add_author")
    assert created
    pp.add_author()

    data = SimulationSerializer(instance=sim, context={"request": req}).data
    data2 = copy.deepcopy(data)
    assert sorted(data2["authors"]) == data["authors"]

    data = SimulationSerializer(instance=sim, context={"request": req}).data
    data2 = copy.deepcopy(data)
    assert sorted(data2["authors"]) == data["authors"]

    data = InputsSerializer(instance=sim.inputs, context={"request": req}).data
    data2 = copy.deepcopy(data)
    assert sorted(data2["sim"]["authors"]) == data["sim"]["authors"]
Esempio n. 3
0
    def test_list_invoices(self, db, client, customer):
        """
        Test list invoice view:
        - Unauthenticated redirects to login.
        - List of invoices in context matches list in model.
        - Still matches after updating subscription.
        - Test with authenticated user without customer object.
        """
        resp = client.get("/billing/invoices/")
        assert resp.status_code == 302

        client.force_login(customer.user)
        resp = client.get("/billing/invoices/")
        assert resp.status_code == 200
        assert len(resp.context["invoices"]) == len(list(customer.invoices()))

        resp = client.get(f"/billing/upgrade/monthly/?upgrade_plan=pro")
        assert resp.status_code == 302, f"Expected 302: got {resp.status_code}"
        next_url = resp.url
        assert next_url == reverse("upgrade_plan_duration_done",
                                   kwargs=dict(plan_duration="monthly"))

        resp = client.get("/billing/invoices/")
        assert resp.status_code == 200
        assert len(resp.context["invoices"]) == len(list(customer.invoices()))

        newuser = User.objects.create_user("nocust", f"*****@*****.**",
                                           "heyhey2222")
        create_profile_from_user(newuser)
        newuser.refresh_from_db()
        client.force_login(newuser)
        resp = client.get("/billing/invoices/")
        assert resp.status_code == 200
        assert len(resp.context["invoices"]) == 0
Esempio n. 4
0
    def test_auto_upgrade_after_trial_no_customer(self, client, plan_duration,
                                                  profile):
        """
        Test auto upgrade after trial page with no customer.
        """
        user = User.objects.create_user(f"test-no-cust",
                                        f"*****@*****.**",
                                        "heyhey2222")
        create_profile_from_user(user)

        resp = client.get(
            f"/billing/upgrade/{plan_duration.lower()}/aftertrial/")
        assert resp.status_code == 302

        client.force_login(user)
        resp = client.get(
            f"/billing/upgrade/{plan_duration.lower()}/aftertrial/")
        assert resp.status_code == 302
        assert resp.url == f"/billing/upgrade/monthly/"
    def test_subscription_with_trial_and_coupon(self, db, has_pmt_info, customer):
        """
        Create a subscription with a coupon and trial that expire in 3 months:
        1. User has payment info.
        2. User has no payment info.
        """
        if not has_pmt_info:
            user = User.objects.create_user(
                f"test-coupon", f"*****@*****.**", "heyhey2222"
            )
            create_profile_from_user(user)
        else:
            user = customer.user

        create_three_month_pro_subscription(user)

        now = datetime.utcnow().replace(tzinfo=pytz.UTC)
        if now.month <= 9:
            three_months = now.replace(month=now.month + 3)
        else:
            three_months = now.replace(year=now.year + 1, month=now.month + 3 - 12)

        user.refresh_from_db()
        customer = getattr(user, "customer", None)
        assert customer is not None

        current_plan = customer.current_plan()
        assert current_plan["name"] == "pro"

        si = customer.current_plan(as_dict=False)

        assert time_is_close(si.subscription.cancel_at, three_months)
        assert time_is_close(si.subscription.trial_end, three_months)

        sub = stripe.Subscription.retrieve(si.subscription.stripe_id)
        assert abs(sub.cancel_at - int(three_months.timestamp())) < 15
        assert abs(sub.trial_end - int(three_months.timestamp())) < 15
        assert current_plan["cancel_at"] == si.subscription.cancel_at.date()
        assert current_plan["trial_end"] == si.subscription.trial_end.date()

        assert si.subscription.is_trial() is True
Esempio n. 6
0
    def test_auto_upgrade_after_trial_no_pmt_info(self, client, plan_duration):
        """
        Test opt-in for subscription after trial for user without payment info.

        - get page returns 200, even without login information.
        - confirm returns 302 with redirect to payment page and next url back to
          opt-in page.
        - selected_plan is set to pro to trigger modal on opt-in page.
        """
        user = User.objects.create_user(f"test-auto-upgrade",
                                        f"*****@*****.**",
                                        "heyhey2222")
        create_profile_from_user(user)

        create_three_month_pro_subscription(user)

        user.refresh_from_db()
        si = user.customer.current_plan(as_dict=False)
        assert si.subscription.is_trial() is True

        client.force_login(user)
        resp = client.get(
            f"/billing/upgrade/{plan_duration.lower()}/aftertrial/")
        assert resp.status_code == 200
        si = user.customer.current_plan(as_dict=False)
        exp = {
            "plan_duration": plan_duration.lower(),
            "current_plan": {
                "plan_duration": "monthly",
                "name": "pro",
                "cancel_at": si.subscription.cancel_at.date(),
                "trial_end": si.subscription.trial_end.date(),
            },
            "card_info": None,
            "selected_plan": None,
            "next": None,
            "banner_msg": None,
            "trial_end": si.subscription.trial_end.date(),
            "cancel_at": si.subscription.cancel_at.date(),
        }
        for key, value in exp.items():
            assert resp.context[key] == value, f"key: {key}"

        resp = client.get(
            f"/billing/upgrade/{plan_duration.lower()}/aftertrial/confirm/")
        assert resp.status_code == 302, f"Expected 302: got {resp.status_code}"
        assert (
            resp.url ==
            f"/billing/update/?next=/billing/upgrade/{plan_duration.lower()}/aftertrial/?selected_plan=pro"
        )

        data = {"stripeToken": ["tok_bypassPending"]}

        resp = client.post(
            f"/billing/update/?next=/billing/upgrade/{plan_duration.lower()}/aftertrial/?selected_plan=pro",
            data=data,
        )
        assert resp.status_code == 302
        assert (
            resp.url ==
            f"/billing/upgrade/{plan_duration.lower()}/aftertrial/?selected_plan=pro"
        )

        resp = client.get(
            f"/billing/upgrade/{plan_duration.lower()}/aftertrial/?selected_plan=pro"
        )
        assert resp.status_code == 200
        assert resp.context["selected_plan"] == "pro"