def test_subscribe_customer_existing(create_customer_for_processing):
    """
    GIVEN create a subscription
    WHEN provided a customer and plan
    THEN validate subscription is created
    """
    uid = uuid.uuid4()
    subscription, code = payments.subscribe_to_plan(
        "validcustomer",
        {
            "pmt_token": "tok_visa",
            "plan_id": "plan_EtMcOlFMNWW4nd",
            "email": f"valid@{uid}customer.com",
            "orig_system": "Test_system",
        },
    )
    subscription2, code2 = payments.subscribe_to_plan(
        "validcustomer",
        {
            "pmt_token": "tok_visa",
            "plan_id": "plan_EtMcOlFMNWW4nd",
            "email": f"valid@{uid}customer.com",
            "orig_system": "Test_system",
        },
    )
    assert 409 == code2
    payments.cancel_subscription(
        "validcustomer", subscription["subscriptions"][0]["subscription_id"])
    g.subhub_account.remove_from_db("validcustomer")
Exemple #2
0
def test_create_subscription_with_invalid_plan_id(app):
    """
    GIVEN a api_token, userid, pmt_token, plan_id, email
    WHEN the plan_id provided is invalid
    THEN a StripeError is raised
    """
    exception = None
    try:
        plan, code = payments.subscribe_to_plan(
            "invalid_plan",
            {
                "pmt_token": "tok_visa",
                "plan_id": "plan_abc123",
                "email": "*****@*****.**",
                "orig_system": "Test_system",
                "display_name": "Jon Tester",
            },
        )
    except Exception as e:
        exception = e

    g.subhub_account.remove_from_db("invalid_plan")

    assert isinstance(exception, InvalidRequestError)
    assert "No such plan:" in exception.user_message
Exemple #3
0
def test_create_subscription_with_invalid_payment_token():
    """
    GIVEN a api_token, userid, invalid pmt_token, plan_id, email
    WHEN the pmt_token is invalid
    THEN a StripeError should be raised
    """
    exception = None
    try:
        subscription, code = payments.subscribe_to_plan(
            "invalid_test",
            {
                "pmt_token": "tok_invalid",
                "plan_id": "plan_EtMcOlFMNWW4nd",
                "email": "*****@*****.**",
                "orig_system": "Test_system",
                "display_name": "Jon Tester",
            },
        )
    except Exception as e:
        exception = e

    g.subhub_account.remove_from_db("invalid_test")

    assert isinstance(exception, InvalidRequestError)
    assert "Unable to create customer." in exception.user_message
Exemple #4
0
def test_cancel_subscription_with_valid_data_multiple_subscriptions_remove_second(
):
    """
    GIVEN a user with multiple subscriptions
    WHEN the second subscription is cancelled
    THEN the specified subscription is cancelled
    """
    uid = uuid.uuid4()
    subscription1, code1 = payments.subscribe_to_plan(
        "valid_customer",
        {
            "pmt_token": "tok_visa",
            "plan_id": "plan_EtMcOlFMNWW4nd",
            "email": f"valid@{uid}customer.com",
            "orig_system": "Test_system",
            "display_name": "Jon Tester",
        },
    )
    subscription2, code2 = payments.subscribe_to_plan(
        "valid_customer",
        {
            "pmt_token": "tok_visa",
            "plan_id": "plan_F4G9jB3x5i6Dpj",
            "email": f"valid@{uid}customer.com",
            "orig_system": "Test_system",
            "display_name": "Jon Tester",
        },
    )

    # cancel the second subscription created
    cancelled, code = payments.cancel_subscription(
        "valid_customer", subscription2["subscriptions"][0]["subscription_id"])
    assert cancelled["message"] == "Subscription cancellation successful"
    assert 201 == code

    # clean up test data created
    cancelled, code = payments.cancel_subscription(
        "valid_customer", subscription1["subscriptions"][0]["subscription_id"])
    g.subhub_account.remove_from_db("valid_customer")
    assert cancelled["message"] == "Subscription cancellation successful"
    assert 201 == code
Exemple #5
0
def create_subscription_for_processing():
    uid = uuid.uuid4()
    subscription = payments.subscribe_to_plan(
        "process_test",
        {
            "pmt_token": "tok_visa",
            "plan_id": "plan_EtMcOlFMNWW4nd",
            "orig_system": "Test_system",
            "email": "subtest@{}tester.com".format(uid),
        },
    )
    yield subscription
def test_create_subscription_with_invalid_plan_id(app):
    """
    GIVEN should not create a subscription
    WHEN provided a api_token, userid, pmt_token, invalid plan_id, email
    THEN validate subscription is created
    """
    plan, code = payments.subscribe_to_plan(
        "invalid_plan",
        {
            "pmt_token": "tok_visa",
            "plan_id": "plan_abc123",
            "email": "*****@*****.**",
            "orig_system": "Test_system",
        },
    )
    g.subhub_account.remove_from_db("invalid_plan")
    assert "Unable to subscribe:" in plan["message"]
def test_create_subscription_with_invalid_payment_token():
    """
    GIVEN should not create a subscription
    WHEN provided a api_token, userid, invalid pmt_token, plan_id, email
    THEN validate subscription is created
    """
    subscription, code = payments.subscribe_to_plan(
        "invalid_test",
        {
            "pmt_token": "tok_invalid",
            "plan_id": "plan_EtMcOlFMNWW4nd",
            "email": "*****@*****.**",
            "orig_system": "Test_system",
        },
    )
    g.subhub_account.remove_from_db("invalid_test")
    assert "Unable to subscribe." in subscription["message"]
def test_subscribe_to_plan_returns_newest(monkeypatch):
    subhub_account = MagicMock()

    get_user = MagicMock()
    user_id = PropertyMock(return_value=UID)
    cust_id = PropertyMock(return_value="cust123")
    type(get_user).user_id = user_id
    type(get_user).cust_id = cust_id

    subhub_account.get_user = get_user

    customer = Mock(return_value=MockCustomer())
    none = Mock(return_value=None)
    updated_customer = Mock(
        return_value={
            "subscriptions": {
                "data": [
                    get_file("subscription1.json"),
                    get_file("subscription2.json"),
                    get_file("subscription3.json"),
                ]
            }
        })

    monkeypatch.setattr("flask.g.subhub_account", subhub_account)
    monkeypatch.setattr("subhub.api.payments.existing_or_new_customer",
                        customer)
    monkeypatch.setattr("subhub.api.payments.has_existing_plan", none)
    monkeypatch.setattr("stripe.Subscription.create", Mock)
    monkeypatch.setattr("stripe.Customer.retrieve", updated_customer)

    data = json.dumps({
        "pmt_token": "tok_visa",
        "plan_id": "plan_EtMcOlFMNWW4nd",
        "orig_system": "Test_system",
        "email": "*****@*****.**",
        "display_name": "John Tester",
    })

    test_customer = subscribe_to_plan(UID, json.loads(data))

    assert test_customer[0]["subscriptions"][0][
        "current_period_start"] == 1_516_229_999
    updated_customer.assert_called()
def test_create_subscription_with_valid_data():
    """
    GIVEN create a subscription
    WHEN provided a api_token, userid, pmt_token, plan_id, cust_id
    THEN validate subscription is created
    """
    uid = uuid.uuid4()
    subscription, code = payments.subscribe_to_plan(
        "validcustomer",
        {
            "pmt_token": "tok_visa",
            "plan_id": "plan_EtMcOlFMNWW4nd",
            "email": "valid@{}customer.com".format(uid),
            "orig_system": "Test_system",
        },
    )
    assert 201 == code
    payments.cancel_subscription(
        "validcustomer", subscription["subscriptions"][0]["subscription_id"])
    g.subhub_account.remove_from_db("validcustomer")