From ad77fc749b4f2aaee526a83daa862d2d518b4bff Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Fri, 29 Nov 2019 08:15:46 +1100 Subject: [PATCH] test subcription deletes --- .../user/subscriptions_controller.rb | 5 +- config/locales/server.en.yml | 2 +- .../user/subscriptions_controller_spec.rb | 84 +++++++++++++++++-- 3 files changed, 80 insertions(+), 11 deletions(-) diff --git a/app/controllers/user/subscriptions_controller.rb b/app/controllers/user/subscriptions_controller.rb index 240ac17..14e1587 100644 --- a/app/controllers/user/subscriptions_controller.rb +++ b/app/controllers/user/subscriptions_controller.rb @@ -40,7 +40,8 @@ module DiscoursePatrons customer = Customer.find_by( user_id: current_user.id, - customer_id: subscription[:customer] + customer_id: subscription[:customer], + product_id: subscription[:plan][:product] ) if customer.present? @@ -49,7 +50,7 @@ module DiscoursePatrons render_json_dump deleted else - render_json_error I18n.t('discourse_patrons.customer_id_not_found') + render_json_error I18n.t('discourse_patrons.customer_not_found') end rescue ::Stripe::InvalidRequestError => e diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 5a99cbc..93209dd 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1,3 +1,3 @@ en: discourse_patrons: - customer_id_not_found: Customer ID not found + customer_not_found: Customer not found diff --git a/spec/requests/user/subscriptions_controller_spec.rb b/spec/requests/user/subscriptions_controller_spec.rb index 9590306..edd9079 100644 --- a/spec/requests/user/subscriptions_controller_spec.rb +++ b/spec/requests/user/subscriptions_controller_spec.rb @@ -83,12 +83,22 @@ module DiscoursePatrons before do # Users can have more than one customer id Customer.create(user_id: user.id, customer_id: 'customer_id_1', product_id: 'p_1') + Customer.create(user_id: user.id, customer_id: 'customer_id_1', product_id: 'p_2') Customer.create(user_id: user.id, customer_id: 'customer_id_2', product_id: 'p_2') end - it "does not delete a subscription" do - ::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'wrong_id') - ::Stripe::Subscription.expects(:delete).never + it "does not delete a subscription when the customer is wrong" do + ::Stripe::Subscription + .expects(:retrieve) + .with('sub_12345') + .returns( + plan: { product: 'p_1' }, + customer: 'wrong_id' + ) + + ::Stripe::Subscription + .expects(:delete) + .never expect { delete "/patrons/user/subscriptions/sub_12345.json" @@ -97,9 +107,58 @@ module DiscoursePatrons expect(response.status).to eq 422 end - it "deletes the first subscription" do - ::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'customer_id_1') - ::Stripe::Subscription.expects(:delete).with('sub_12345') + it "does not deletes the subscription when the product is wrong" do + ::Stripe::Subscription + .expects(:retrieve) + .with('sub_12345') + .returns( + plan: { product: 'p_wrong' }, + customer: 'customer_id_2' + ) + + ::Stripe::Subscription + .expects(:delete) + .never + + expect { + delete "/patrons/user/subscriptions/sub_12345.json" + }.not_to change { DiscoursePatrons::Customer.count } + + expect(response.status).to eq 422 + end + + it "deletes the first subscription product 1" do + ::Stripe::Subscription + .expects(:retrieve) + .with('sub_12345') + .returns( + plan: { product: 'p_1' }, + customer: 'customer_id_1' + ) + + ::Stripe::Subscription + .expects(:delete) + .with('sub_12345') + + expect { + delete "/patrons/user/subscriptions/sub_12345.json" + }.to change { DiscoursePatrons::Customer.count }.by(-1) + + expect(response.status).to eq 200 + end + + it "deletes the first subscription product 2" do + ::Stripe::Subscription + .expects(:retrieve) + .with('sub_12345') + .returns( + plan: { product: 'p_2' }, + customer: 'customer_id_1' + ) + + ::Stripe::Subscription + .expects(:delete) + .with('sub_12345') expect { delete "/patrons/user/subscriptions/sub_12345.json" @@ -109,8 +168,17 @@ module DiscoursePatrons end it "deletes the second subscription" do - ::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'customer_id_2') - ::Stripe::Subscription.expects(:delete).with('sub_12345') + ::Stripe::Subscription + .expects(:retrieve) + .with('sub_12345') + .returns( + plan: { product: 'p_2' }, + customer: 'customer_id_2' + ) + + ::Stripe::Subscription + .expects(:delete) + .with('sub_12345') expect { delete "/patrons/user/subscriptions/sub_12345.json"