|
6 | 6 | RSpec.describe SubscriptionsController, type: :controller do |
7 | 7 | before do |
8 | 8 | WebMock.disable_net_connect!(allow_localhost: true) |
| 9 | + |
| 10 | + # Enable request logging to debug what requests are being made |
| 11 | + # WebMock::Config.instance.show_stubbing_instructions = true |
9 | 12 |
|
10 | 13 | # Need to stub .save on StripeObject, but this doesn't seem to work |
11 | 14 | #Stripe::StripeObject.any_instance.stub(:save).and_return(true) |
|
48 | 51 | headers: {} |
49 | 52 | ) |
50 | 53 |
|
51 | | - # Stub creating subscription with price instead of plan |
| 54 | + # Stub PaymentMethod.list for SubscriptionService (without payment methods) |
| 55 | + stub_request(:get, "https://api.stripe.com/v1/payment_methods") |
| 56 | + .with(query: {customer: 'stripe-id', type: 'card'}) |
| 57 | + .to_return( |
| 58 | + status: 200, |
| 59 | + body: { |
| 60 | + data: [] |
| 61 | + }.to_json, |
| 62 | + headers: {} |
| 63 | + ) |
| 64 | + |
| 65 | + # Stub creating subscription with items array (no payment method) |
52 | 66 | stub_request(:post, "https://api.stripe.com/v1/subscriptions") |
53 | | - .with(body: { customer: "stripe-id", price: 'starter' }) |
| 67 | + .with(body: { customer: "stripe-id", items: [{price: 'starter'}] }) |
54 | 68 | .to_return(status: 200, body: {id: 'sub_starter'}.to_json, headers: {}) |
55 | 69 |
|
| 70 | + # Stub creating subscription with items array (with payment method) |
| 71 | + stub_request(:post, "https://api.stripe.com/v1/subscriptions") |
| 72 | + .with(body: { customer: "stripe-id", items: [{price: 'premium'}], default_payment_method: 'pm_123' }) |
| 73 | + .to_return(status: 200, body: {id: 'sub_premium'}.to_json, headers: {}) |
| 74 | + |
56 | 75 | # Stub updating subscription with Subscription.modify |
57 | 76 | stub_request(:post, "https://api.stripe.com/v1/subscriptions/sub_123") |
58 | 77 | .to_return(status: 200, body: {id: 'sub_123'}.to_json, headers: {}) |
|
143 | 162 | end |
144 | 163 |
|
145 | 164 | it "allows upgrading to Premium when they have a payment method saved" do |
146 | | - # Re-stub list_payment_methods to include a payment method |
| 165 | + # Re-stub list_payment_methods to include a payment method (for controller check) |
147 | 166 | stub_request(:get, "https://api.stripe.com/v1/customers/stripe-id/payment_methods") |
148 | 167 | .with(query: {type: 'card'}) |
149 | 168 | .to_return( |
|
160 | 179 | headers: {} |
161 | 180 | ) |
162 | 181 |
|
| 182 | + # Re-stub PaymentMethod.list for SubscriptionService (with payment method) |
| 183 | + stub_request(:get, "https://api.stripe.com/v1/payment_methods") |
| 184 | + .with(query: {customer: 'stripe-id', type: 'card'}) |
| 185 | + .to_return( |
| 186 | + status: 200, |
| 187 | + body: { |
| 188 | + data: [{ |
| 189 | + id: 'pm_123', |
| 190 | + card: { |
| 191 | + brand: 'visa', |
| 192 | + last4: '4242' |
| 193 | + } |
| 194 | + }] |
| 195 | + }.to_json, |
| 196 | + headers: {} |
| 197 | + ) |
| 198 | + |
163 | 199 | expect(@user.selected_billing_plan_id).to eq(@free_plan.id) |
164 | 200 | expect(@user.active_billing_plans).not_to eq([@premium_plan]) |
165 | 201 |
|
|
0 commit comments