simplify the http stubs

This commit is contained in:
Rimian Perkins 2019-08-27 12:53:20 +10:00
parent 4b41756cfa
commit 1315d240e6

View File

@ -1,5 +1,4 @@
require 'rails_helper' require 'rails_helper'
require_relative '../../support/dd_helper'
shared_examples 'failure response' do |message_key| shared_examples 'failure response' do |message_key|
let(:body) { JSON.parse(response.body) } let(:body) { JSON.parse(response.body) }
@ -87,32 +86,9 @@ module DiscourseDonations
"tax_info_verification": "null" "tax_info_verification": "null"
} }
stub_request(:get, "https://api.stripe.com/v1/customers?email=foobar@example.com"). stub_request(:get, /v1\/customers/).to_return(status: 200, body: customer.to_json)
with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Bearer secret-key-yo',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0',
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}'
}).
to_return(status: 200, body: customer.to_json, headers: {})
stub_request(:post, "https://api.stripe.com/v1/customers"). plans = {
with(
body: { "email" => "foobar@example.com" },
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Bearer secret-key-yo',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0',
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}'
}).
to_return(status: 200, body: customer.to_json, headers: {})
plans = {
"object": "list", "object": "list",
"url": "/v1/plans", "url": "/v1/plans",
"has_more": false, "has_more": false,
@ -142,30 +118,8 @@ module DiscourseDonations
] ]
} }
stub_request(:get, "https://api.stripe.com/v1/plans"). stub_request(:get, "https://api.stripe.com/v1/plans").to_return(status: 200, body: plans.to_json)
with( stub_request(:post, "https://api.stripe.com/v1/plans").to_return(status: 200, body: plans.to_json)
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Bearer secret-key-yo',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0',
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}'
}).
to_return(status: 200, body: plans.to_json, headers: {})
stub_request(:post, "https://api.stripe.com/v1/plans").
with(
body: { "amount" => "0", "currency" => "AUD", "id" => "discourse_donation_recurring__", "nickname" => "Discourse Donation Recurring " },
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Bearer secret-key-yo',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0',
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}'
}).
to_return(status: 200, body: plans.to_json, headers: {})
products = { products = {
"object": "list", "object": "list",
@ -196,300 +150,234 @@ module DiscourseDonations
] ]
} }
stub_request(:get, "https://api.stripe.com/v1/products?type=service"). stub_request(:get, "https://api.stripe.com/v1/products?type=service").to_return(status: 200, body: products.to_json)
with( stub_request(:post, "https://api.stripe.com/v1/products").to_return(status: 200, body: products.to_json)
headers: { stub_request(:post, "https://api.stripe.com/v1/customers").to_return(status: 200, body: customer.to_json)
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Bearer secret-key-yo',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0',
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}'
}).
to_return(status: 200, body: products.to_json, headers: {})
stub_request(:post, "https://api.stripe.com/v1/products"). subscription = {
with( "id": "sub_8epEF0PuRhmltU",
body: { "id" => "Discourse_recurring_donation", "name" => "Discourse Recurring Donation", "type" => "service" }, "object": "subscription",
headers: { "application_fee_percent": "null",
'Accept' => '*/*', "billing": "charge_automatically",
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', "billing_cycle_anchor": 1466202990,
'Authorization' => 'Bearer secret-key-yo', "billing_thresholds": "null",
'Content-Type' => 'application/x-www-form-urlencoded', "cancel_at": "null",
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0', "cancel_at_period_end": false,
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}' "canceled_at": 1517528245,
}). "collection_method": "charge_automatically",
to_return(status: 200, body: products.to_json, headers: {}) "created": 1466202990,
"current_period_end": 1518906990,
stub_request(:post, "https://api.stripe.com/v1/customers"). "current_period_start": 1516228590,
with( "customer": "cus_FhHJDzf0OxYtb8",
body: { "email" => "foobar@example.com", "metadata" => { "discourse_user_id" => "1" } }, "days_until_due": "null",
headers: { "default_payment_method": "null",
'Accept' => '*/*', "default_source": "null",
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', "default_tax_rates": [],
'Authorization' => 'Bearer secret-key-yo', "discount": "null",
'Content-Type' => 'application/x-www-form-urlencoded', "ended_at": 1517528245,
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0', "items": {
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}' "object": "list",
}). "data": [
to_return(status: 200, body: customer.to_json, headers: {}) {
"id": "si_18NVZi2eZvKYlo2CUtBNGL9x",
subscription = { "object": "subscription_item",
"id": "sub_8epEF0PuRhmltU", "billing_thresholds": "null",
"object": "subscription", "created": 1466202990,
"application_fee_percent": "null",
"billing": "charge_automatically",
"billing_cycle_anchor": 1466202990,
"billing_thresholds": "null",
"cancel_at": "null",
"cancel_at_period_end": false,
"canceled_at": 1517528245,
"collection_method": "charge_automatically",
"created": 1466202990,
"current_period_end": 1518906990,
"current_period_start": 1516228590,
"customer": "cus_FhHJDzf0OxYtb8",
"days_until_due": "null",
"default_payment_method": "null",
"default_source": "null",
"default_tax_rates": [],
"discount": "null",
"ended_at": 1517528245,
"items": {
"object": "list",
"data": [
{
"id": "si_18NVZi2eZvKYlo2CUtBNGL9x",
"object": "subscription_item",
"billing_thresholds": "null",
"created": 1466202990,
"metadata": {},
"plan": {
"id": "ivory-freelance-040",
"object": "plan",
"active": true,
"aggregate_usage": "null",
"amount": 999,
"amount_decimal": "999",
"billing_scheme": "per_unit",
"created": 1466202980,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {}, "metadata": {},
"nickname": "null", "plan": {
"product": "prod_BUthVRQ7KdFfa7", "id": "ivory-freelance-040",
"tiers": "null", "object": "plan",
"tiers_mode": "null", "active": true,
"transform_usage": "null", "aggregate_usage": "null",
"trial_period_days": "null",
"usage_type": "licensed"
},
"quantity": 1,
"subscription": "sub_8epEF0PuRhmltU",
"tax_rates": []
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/subscription_items?subscription=sub_8epEF0PuRhmltU"
},
"latest_invoice": "null",
"livemode": false,
"metadata": {},
"pending_setup_intent": "null",
"plan": {
"id": "ivory-freelance-040",
"object": "plan",
"active": true,
"aggregate_usage": "null",
"amount": 999,
"amount_decimal": "999",
"billing_scheme": "per_unit",
"created": 1466202980,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": "null",
"product": "prod_BUthVRQ7KdFfa7",
"tiers": "null",
"tiers_mode": "null",
"transform_usage": "null",
"trial_period_days": "null",
"usage_type": "licensed"
},
"quantity": 1,
"schedule": "null",
"start": 1466202990,
"start_date": 1466202990,
"status": "active",
"tax_percent": "null",
"trial_end": "null",
"trial_start": "null"
}
stub_request(:post, "https://api.stripe.com/v1/subscriptions").
with(
body: { "customer" => "cus_FhHJDzf0OxYtb8", "metadata" => { "discourse_user_id" => "1" } },
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Bearer secret-key-yo',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0',
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}'
}).
to_return(status: 200, body: subscription.to_json, headers: {})
stub_request(:get, "https://api.stripe.com/v1/customers/cus_FhHJDzf0OxYtb8").
with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Bearer secret-key-yo',
'Content-Type' => 'application/x-www-form-urlencoded',
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0',
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}'
}).
to_return(status: 200, body: customer.to_json, headers: {})
invoices = {
"object": "list",
"url": "/v1/invoices",
"has_more": false,
"data": [
{
"id": "in_1Cc9wc2eZvKYlo2ClBzJbDQz",
"object": "invoice",
"account_country": "US",
"account_name": "Stripe.com",
"amount_due": 20,
"amount_paid": 0,
"amount_remaining": 20,
"application_fee_amount": "null",
"attempt_count": 0,
"attempted": false,
"auto_advance": false,
"billing": "send_invoice",
"billing_reason": "subscription_update",
"charge": "null",
"collection_method": "send_invoice",
"created": 1528800106,
"currency": "usd",
"custom_fields": "null",
"customer": "cus_FhHJDzf0OxYtb8",
"customer_address": "null",
"customer_email": "ziad+123@elysian.team",
"customer_name": "null",
"customer_phone": "null",
"customer_shipping": "null",
"customer_tax_exempt": "none",
"customer_tax_ids": [],
"default_payment_method": "null",
"default_source": "null",
"default_tax_rates": [],
"description": "null",
"discount": "null",
"due_date": 1529059306,
"ending_balance": "null",
"footer": "null",
"hosted_invoice_url": "null",
"invoice_pdf": "null",
"lines": {
"data": [
{
"id": "sli_42e8bf79bec714",
"object": "line_item",
"amount": 999, "amount": 999,
"amount_decimal": "999",
"billing_scheme": "per_unit",
"created": 1466202980,
"currency": "usd", "currency": "usd",
"description": "1 × Ivory Freelance (at $9.99 / month)", "interval": "month",
"discountable": true, "interval_count": 1,
"livemode": false, "livemode": false,
"metadata": {}, "metadata": {},
"period": { "nickname": "null",
"end": 1521326190, "product": "prod_BUthVRQ7KdFfa7",
"start": 1518906990 "tiers": "null",
}, "tiers_mode": "null",
"plan": { "transform_usage": "null",
"id": "ivory-freelance-040", "trial_period_days": "null",
"object": "plan", "usage_type": "licensed"
"active": true, },
"aggregate_usage": "null", "quantity": 1,
"amount": 999, "subscription": "sub_8epEF0PuRhmltU",
"amount_decimal": "999", "tax_rates": []
"billing_scheme": "per_unit", }
"created": 1466202980, ],
"currency": "usd", "has_more": false,
"interval": "month", "total_count": 1,
"interval_count": 1, "url": "/v1/subscription_items?subscription=sub_8epEF0PuRhmltU"
"livemode": false, },
"metadata": {}, "latest_invoice": "null",
"nickname": "null", "livemode": false,
"product": "prod_BUthVRQ7KdFfa7", "metadata": {},
"tiers": "null", "pending_setup_intent": "null",
"tiers_mode": "null", "plan": {
"transform_usage": "null", "id": "ivory-freelance-040",
"trial_period_days": "null", "object": "plan",
"usage_type": "licensed" "active": true,
}, "aggregate_usage": "null",
"proration": false, "amount": 999,
"quantity": 1, "amount_decimal": "999",
"subscription": "sub_8epEF0PuRhmltU", "billing_scheme": "per_unit",
"subscription_item": "si_18NVZi2eZvKYlo2CUtBNGL9x", "created": 1466202980,
"tax_amounts": [], "currency": "usd",
"tax_rates": [], "interval": "month",
"type": "subscription" "interval_count": 1,
}
],
"has_more": false,
"object": "list",
"url": "/v1/invoices/in_1Cc9wc2eZvKYlo2ClBzJbDQz/lines"
},
"livemode": false, "livemode": false,
"metadata": {}, "metadata": {},
"next_payment_attempt": "null", "nickname": "null",
"number": "8B36FE9-0005", "product": "prod_BUthVRQ7KdFfa7",
"paid": false, "tiers": "null",
"payment_intent": "null", "tiers_mode": "null",
"period_end": 1528800106, "transform_usage": "null",
"period_start": 1528800106, "trial_period_days": "null",
"post_payment_credit_notes_amount": 0, "usage_type": "licensed"
"pre_payment_credit_notes_amount": 0,
"receipt_number": "null",
"starting_balance": 10,
"statement_descriptor": "null",
"status": "draft",
"status_transitions": {
"finalized_at": "null",
"marked_uncollectible_at": "null",
"paid_at": "null",
"voided_at": "null"
},
"subscription": "sub_D2ECXpuEnnXkWU",
"subtotal": 10,
"tax": "null",
"tax_percent": "null",
"total": 10,
"total_tax_amounts": [],
"webhooks_delivered_at": 1528800106
}, },
] "quantity": 1,
} "schedule": "null",
"start": 1466202990,
"start_date": 1466202990,
"status": "active",
"tax_percent": "null",
"trial_end": "null",
"trial_start": "null"
}
stub_request(:get, "https://api.stripe.com/v1/invoices?customer=cus_FhHJDzf0OxYtb8&subscription=sub_8epEF0PuRhmltU"). stub_request(:post, "https://api.stripe.com/v1/subscriptions").to_return(status: 200, body: subscription.to_json)
with(
headers: { invoices = {
'Accept' => '*/*', "object": "list",
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', "url": "/v1/invoices",
'Authorization' => 'Bearer secret-key-yo', "has_more": false,
'Content-Type' => 'application/x-www-form-urlencoded', "data": [
'User-Agent' => 'Stripe/v1 RubyBindings/2.8.0', {
'X-Stripe-Client-User-Agent' => '{"bindings_version":"2.8.0","lang":"ruby","lang_version":"2.6.2 p47 (2019-03-13)","platform":"x86_64-darwin18","engine":"ruby","publisher":"stripe","uname":"Darwin 192-168-1-102.tpgi.com.au 18.7.0 Darwin Kernel Version 18.7.0: Thu Jun 20 18:42:21 PDT 2019; root:xnu-4903.270.47~4/RELEASE_X86_64 x86_64","hostname":"192-168-1-102.tpgi.com.au"}' "id": "in_1Cc9wc2eZvKYlo2ClBzJbDQz",
}). "object": "invoice",
to_return(status: 200, body: invoices.to_json, headers: {}) "account_country": "US",
"account_name": "Stripe.com",
"amount_due": 20,
"amount_paid": 0,
"amount_remaining": 20,
"application_fee_amount": "null",
"attempt_count": 0,
"attempted": false,
"auto_advance": false,
"billing": "send_invoice",
"billing_reason": "subscription_update",
"charge": "null",
"collection_method": "send_invoice",
"created": 1528800106,
"currency": "usd",
"custom_fields": "null",
"customer": "cus_FhHJDzf0OxYtb8",
"customer_address": "null",
"customer_email": "ziad+123@elysian.team",
"customer_name": "null",
"customer_phone": "null",
"customer_shipping": "null",
"customer_tax_exempt": "none",
"customer_tax_ids": [],
"default_payment_method": "null",
"default_source": "null",
"default_tax_rates": [],
"description": "null",
"discount": "null",
"due_date": 1529059306,
"ending_balance": "null",
"footer": "null",
"hosted_invoice_url": "null",
"invoice_pdf": "null",
"lines": {
"data": [
{
"id": "sli_42e8bf79bec714",
"object": "line_item",
"amount": 999,
"currency": "usd",
"description": "1 × Ivory Freelance (at $9.99 / month)",
"discountable": true,
"livemode": false,
"metadata": {},
"period": {
"end": 1521326190,
"start": 1518906990
},
"plan": {
"id": "ivory-freelance-040",
"object": "plan",
"active": true,
"aggregate_usage": "null",
"amount": 999,
"amount_decimal": "999",
"billing_scheme": "per_unit",
"created": 1466202980,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": "null",
"product": "prod_BUthVRQ7KdFfa7",
"tiers": "null",
"tiers_mode": "null",
"transform_usage": "null",
"trial_period_days": "null",
"usage_type": "licensed"
},
"proration": false,
"quantity": 1,
"subscription": "sub_8epEF0PuRhmltU",
"subscription_item": "si_18NVZi2eZvKYlo2CUtBNGL9x",
"tax_amounts": [],
"tax_rates": [],
"type": "subscription"
}
],
"has_more": false,
"object": "list",
"url": "/v1/invoices/in_1Cc9wc2eZvKYlo2ClBzJbDQz/lines"
},
"livemode": false,
"metadata": {},
"next_payment_attempt": "null",
"number": "8B36FE9-0005",
"paid": false,
"payment_intent": "null",
"period_end": 1528800106,
"period_start": 1528800106,
"post_payment_credit_notes_amount": 0,
"pre_payment_credit_notes_amount": 0,
"receipt_number": "null",
"starting_balance": 10,
"statement_descriptor": "null",
"status": "draft",
"status_transitions": {
"finalized_at": "null",
"marked_uncollectible_at": "null",
"paid_at": "null",
"voided_at": "null"
},
"subscription": "sub_D2ECXpuEnnXkWU",
"subtotal": 10,
"tax": "null",
"tax_percent": "null",
"total": 10,
"total_tax_amounts": [],
"webhooks_delivered_at": 1528800106
},
]
}
stub_request(:get, "https://api.stripe.com/v1/invoices?customer=cus_FhHJDzf0OxYtb8&subscription=sub_8epEF0PuRhmltU")
.to_return(status: 200, body: invoices.to_json)
post :create, params: { email: 'foobar@example.com' }, format: :json post :create, params: { email: 'foobar@example.com' }, format: :json
expect(body['messages'][0]).to end_with(I18n.t('donations.payment.success')) expect(body['messages'][0]).to end_with(I18n.t('donations.payment.success'))