diff --git a/spec/controllers/discourse_donations/charges_controller_spec.rb b/spec/controllers/discourse_donations/charges_controller_spec.rb index 1d8c97c..728c153 100644 --- a/spec/controllers/discourse_donations/charges_controller_spec.rb +++ b/spec/controllers/discourse_donations/charges_controller_spec.rb @@ -35,42 +35,14 @@ module DiscourseDonations it 'responds ok for anonymous users' do controller.expects(:current_user).at_least(1).returns(user) - customer = Fabricate(:customer) + customer = Fabricate(:stripe_customer).to_json - stub_request(:get, /v1\/customers/).to_return(status: 200, body: customer.to_json) + stub_request(:get, /v1\/customers/).to_return(status: 200, body: customer) - plans = { - "object": "list", - "url": "/v1/plans", - "has_more": false, - "data": [ - { - "id": "plan_EeE4ns3bvb34ZP", - "object": "plan", - "active": true, - "aggregate_usage": "null", - "amount": 3000, - "amount_decimal": "3000", - "billing_scheme": "per_unit", - "created": 1551862832, - "currency": "usd", - "interval": "month", - "interval_count": 1, - "livemode": false, - "metadata": {}, - "nickname": "Pro Plan", - "product": "prod_BT942zL7VcClrn", - "tiers": "null", - "tiers_mode": "null", - "transform_usage": "null", - "trial_period_days": "null", - "usage_type": "licensed" - }, - ] - } + plans = Fabricate(:stripe_plans).to_json - stub_request(:get, "https://api.stripe.com/v1/plans").to_return(status: 200, body: plans.to_json) - stub_request(:post, "https://api.stripe.com/v1/plans").to_return(status: 200, body: plans.to_json) + stub_request(:get, "https://api.stripe.com/v1/plans").to_return(status: 200, body: plans) + stub_request(:post, "https://api.stripe.com/v1/plans").to_return(status: 200, body: plans) products = { "object": "list", @@ -103,7 +75,7 @@ module DiscourseDonations stub_request(:get, "https://api.stripe.com/v1/products?type=service").to_return(status: 200, body: products.to_json) stub_request(:post, "https://api.stripe.com/v1/products").to_return(status: 200, body: products.to_json) - stub_request(:post, "https://api.stripe.com/v1/customers").to_return(status: 200, body: customer.to_json) + stub_request(:post, "https://api.stripe.com/v1/customers").to_return(status: 200, body: customer) subscription = { "id": "sub_8epEF0PuRhmltU", diff --git a/spec/fabricators/customer_fabricator.rb b/spec/fabricators/customer_fabricator.rb index 65c28e8..e306e69 100644 --- a/spec/fabricators/customer_fabricator.rb +++ b/spec/fabricators/customer_fabricator.rb @@ -1,10 +1,5 @@ - -class Customer - attr_accessor :to_json -end - -Fabricator(:customer) do +Fabricator(:stripe_customer, from: "DiscourseDonations::StripeResponse") do response = { "id": "cus_FhHJDzf0OxYtb8", "object": "customer", diff --git a/spec/fabricators/plans_fabricator.rb b/spec/fabricators/plans_fabricator.rb new file mode 100644 index 0000000..a88f19a --- /dev/null +++ b/spec/fabricators/plans_fabricator.rb @@ -0,0 +1,34 @@ + +Fabricator(:stripe_plans, from: "DiscourseDonations::StripeResponse") do + response = { + "object": "list", + "url": "/v1/plans", + "has_more": false, + "data": [ + { + "id": "plan_EeE4ns3bvb34ZP", + "object": "plan", + "active": true, + "aggregate_usage": "null", + "amount": 3000, + "amount_decimal": "3000", + "billing_scheme": "per_unit", + "created": 1551862832, + "currency": "usd", + "interval": "month", + "interval_count": 1, + "livemode": false, + "metadata": {}, + "nickname": "Pro Plan", + "product": "prod_BT942zL7VcClrn", + "tiers": "null", + "tiers_mode": "null", + "transform_usage": "null", + "trial_period_days": "null", + "usage_type": "licensed" + }, + ] + }.to_json + + to_json response +end diff --git a/spec/fabricators/stripe_response.rb b/spec/fabricators/stripe_response.rb new file mode 100644 index 0000000..5e68f95 --- /dev/null +++ b/spec/fabricators/stripe_response.rb @@ -0,0 +1,10 @@ + +# This is for building http responses with Fabricate +# Usage: Fabricate(:customer).to_json +# See: https://stripe.com/docs/api + +module DiscourseDonations + class StripeResponse + attr_accessor :to_json + end +end diff --git a/spec/plugin_helper.rb b/spec/plugin_helper.rb index e3ed7b0..88bec3b 100644 --- a/spec/plugin_helper.rb +++ b/spec/plugin_helper.rb @@ -1,2 +1,4 @@ +require_relative './fabricators/stripe_response.rb' require_relative './fabricators/customer_fabricator.rb' +require_relative './fabricators/plans_fabricator.rb'