Esempio n. 1
0
def test_user_edit_with_subscription_info(mock1, mock2, test_user):
    """The user has already signed up for a subscription and now the user edit
    page contains information about that from Stripe."""
    mock1.side_effect = mock_get_stripe_customer
    mock2.side_effect = mock_get_stripe_subscription_info

    # We need to fake the User.subscriber_number because the
    # 'get_stripe_subscription_info' is faked so the signals that set it are
    # never happening in this context.
    UserSubscription.set_active(test_user, "sub_123456789")
    # sanity check
    test_user.refresh_from_db()
    assert test_user.subscriber_number == 1

    client = Client()
    client.force_login(test_user)
    response = client.post(
        reverse("users.user_edit", args=[test_user.username]),
        HTTP_HOST=settings.WIKI_HOST,
    )
    assert response.status_code == 200
    page = pq(response.content)
    assert page("#subscription h2").text() == "You are MDN member number 1"
    assert not page(".stripe-error").size()
    assert "MagicCard ending in 4242" in page(".card-info p").text()
Esempio n. 2
0
def test_account_settings_delete_with_subscription(
    mocked_stripe,
    mock_create_or_update_newsletter_contact_delay,
    user_client,
    wiki_user,
):
    subscription_id = "sub_1234"
    mock_subscription = mock.MagicMock()
    mock_subscription.id = subscription_id
    mock_customer = mock.MagicMock()
    mock_customer.subscriptions.data.__iter__.return_value = [
        mock_subscription
    ]
    mocked_stripe.Customer.retrieve.return_value = mock_customer
    mocked_stripe.Subscription.retrieve.return_value = mock_subscription

    # Also, pretend that the user has a rich profile
    wiki_user.stripe_customer_id = "cus_12345"
    wiki_user.save()
    UserSubscription.set_active(wiki_user, subscription_id)

    wiki_user.is_newsletter_subscribed = False
    wiki_user.save()

    username = wiki_user.username
    response = user_client.delete(reverse("api.v1.settings"))
    assert response.status_code == 200
    assert not User.objects.filter(username=username).exists()
    assert not UserSubscription.objects.filter(
        stripe_subscription_id="sub_1234")
Esempio n. 3
0
def test_user_edit_with_subscription_info(mock1, mock2, test_user):
    """The user has already signed up for a subscription and now the user edit
    page contains information about that from Stripe."""
    mock1.side_effect = mock_get_stripe_customer
    mock2.side_effect = mock_get_stripe_subscription_info

    # We need to fake the User.subscriber_number because the
    # 'get_stripe_subscription_info' is faked so the signals that set it are
    # never happening in this context.
    UserSubscription.set_active(test_user, "sub_123456789")
    # sanity check
    test_user.refresh_from_db()
    assert test_user.subscriber_number == 1
Esempio n. 4
0
def test_whoami_is_subscriber(
    user_client,
    wiki_user,
):
    """Test responses for logged-in users and whether they have an active
    subscription."""
    url = reverse("api.v1.whoami")
    response = user_client.get(url)
    assert response.status_code == 200
    assert response.json()["is_subscriber"] is False

    UserSubscription.set_active(wiki_user, "abc123")
    response = user_client.get(url)
    assert response.status_code == 200
    assert response.json()["is_subscriber"] is True

    UserSubscription.set_canceled(wiki_user, "abc123")
    response = user_client.get(url)
    assert response.status_code == 200
    assert response.json()["is_subscriber"] is False
Esempio n. 5
0
def test_stripe_subscription_canceled(mock1, client):
    mock1.return_value = SimpleNamespace(
        type="customer.subscription.deleted",
        data=SimpleNamespace(
            object=SimpleNamespace(customer="cus_mock_testuser", id="sub_123456789")
        ),
    )

    testuser = user(
        save=True,
        username="******",
        email="*****@*****.**",
        stripe_customer_id="cus_mock_testuser",
    )
    UserSubscription.set_active(testuser, "sub_123456789")
    response = client.post(
        reverse("api.v1.stripe_hooks"), content_type="application/json", data={},
    )
    assert response.status_code == 200
    (user_subscription,) = UserSubscription.objects.filter(user=testuser)
    assert user_subscription.canceled