Ejemplo n.º 1
0
def test_get_subscription_info_view_subscription_overage(mocker: MockFixture) -> None:
    """
    check the API response when a user's account has exceeded the number of
    seats
    """
    user, account = create_account()
    account.stripe_customer_id = create_stripe_customer_info().customer_id
    account.github_account_type = AccountType.organization
    account.save()
    for i in range(1, 6):
        create_active_user(account=account, user_id=i, pr_number=i)

    assert account.github_account_type != AccountType.user
    assert account.active_trial() is False
    stripe_customer_info = account.stripe_customer_info()
    assert stripe_customer_info is not None
    assert stripe_customer_info.subscription_quantity < len(
        UserPullRequestActivity.get_active_users_in_last_30_days(account=account)
    )

    client = Client()
    client.login(user)
    res = client.get(f"/v1/t/{account.id}/subscription_info")
    assert res.status_code == 200
    assert res.json() == {
        "type": "SUBSCRIPTION_OVERAGE",
        "activeUserCount": 5,
        "licenseCount": 3,
    }
Ejemplo n.º 2
0
def test_stripe_self_serve_redirect_view(mocker: MockFixture) -> None:
    """
    Smoke test to ensure the redirect URL for the stripe billing portal works
    in the success case.
    """
    fake_billing_portal_session = stripe.billing_portal.Session.construct_from(
        {
            "created": 1588895592,
            "customer": "cus_HEmOJM4AntPHdz",
            "id": "bps_1GgJWeCoyKa1V9Y6Bfnab1L3",
            "livemode": False,
            "object": "billing_portal.session",
            "return_url": "http://app.localhost.kodiakhq.com:3000/t/134f9ff9-327b-4cb3-a0b3-edf63f23a96e/usage",
            "url": "https://billing.stripe.com/session/O4pTob2jXrlVdYdh1grBH1mXJiOIDgwS",
        },
        "fake-key",
    )

    mocker.patch(
        "web_api.views.stripe.billing_portal.Session.create",
        return_value=fake_billing_portal_session,
    )

    user, account = create_account()

    client = Client()
    client.login(user)
    res = client.get(f"/v1/t/{account.id}/stripe_self_serve_redirect")

    assert res.status_code == 302
    assert res["Location"] == fake_billing_portal_session.url
Ejemplo n.º 3
0
def test_get_subscription_info_view_subscription_expired(
        mocker: MockFixture) -> None:
    """
    response when user's account subscription has expired
    """
    user, account = create_account()
    stripe_customer_info = create_stripe_customer_info()
    account.stripe_customer_id = stripe_customer_info.customer_id
    account.github_account_type = AccountType.organization
    account.save()

    # bunch of checks to ensure we don't hit the other subscription cases
    customer_info = account.stripe_customer_info()
    assert customer_info is not None
    assert customer_info.expired is True
    assert account.active_trial() is False
    assert account.github_account_type != AccountType.user

    client = Client()
    client.login(user)
    res = client.get(f"/v1/t/{account.id}/subscription_info")
    assert res.status_code == 200
    assert res.json() == {
        "type": "SUBSCRIPTION_EXPIRED",
    }
Ejemplo n.º 4
0
def test_get_subscription_info_view_valid_account_personal_user(
    mocker: MockFixture, ) -> None:
    """
    all personal accounts have valid subscriptions
    """
    user, account = create_account()
    account.github_account_type = AccountType.user
    account.save()
    create_active_user(account=account, user_id=user.github_id, pr_number=2)

    assert account.github_account_type == AccountType.user

    client = Client()
    client.login(user)
    res = client.get(f"/v1/t/{account.id}/subscription_info")
    assert res.status_code == 200
    assert res.json() == {
        "type": "VALID_SUBSCRIPTION",
    }
Ejemplo n.º 5
0
def test_get_subscription_info_view_trial_expired(mocker: MockFixture) -> None:
    """
    ensure trial expiration is handled explicitly in the response
    """
    user, account = create_account()
    account.github_account_type = AccountType.organization
    account.trial_expiration = timezone.now() - timedelta(days=10)
    account.save()

    assert account.github_account_type != AccountType.user
    assert account.active_trial() is False
    assert account.trial_expired() is True

    client = Client()
    client.login(user)
    res = client.get(f"/v1/t/{account.id}/subscription_info")
    assert res.status_code == 200
    assert res.json() == {
        "type": "TRIAL_EXPIRED",
    }
Ejemplo n.º 6
0
def test_get_subscription_info_view_valid_account_trial_user(
    mocker: MockFixture, ) -> None:
    """
    trial users should have a valid subscription
    """
    user, account = create_account()
    account.github_account_type = AccountType.organization
    account.trial_expiration = timezone.now() + timedelta(days=10)
    account.save()

    assert account.github_account_type != AccountType.user
    assert account.active_trial() is True

    client = Client()
    client.login(user)
    res = client.get(f"/v1/t/{account.id}/subscription_info")
    assert res.status_code == 200
    assert res.json() == {
        "type": "VALID_SUBSCRIPTION",
    }
Ejemplo n.º 7
0
def test_get_subscription_info_view_valid_account_subscription_user(
    mocker: MockFixture, ) -> None:
    """
    valid subscription user, aka they aren't over their subscribed number of seats
    """
    user, account = create_account()
    account.github_account_type = AccountType.organization
    account.trial_expiration = None
    account.save()

    assert account.github_account_type != AccountType.user
    assert account.active_trial() is False
    assert account.trial_expired() is False

    client = Client()
    client.login(user)
    res = client.get(f"/v1/t/{account.id}/subscription_info")
    assert res.status_code == 200
    assert res.json() == {
        "type": "VALID_SUBSCRIPTION",
    }