test subcription deletes

This commit is contained in:
Rimian Perkins 2019-11-29 08:15:46 +11:00
parent 2eab43d4a0
commit ad77fc749b
3 changed files with 80 additions and 11 deletions

View File

@ -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

View File

@ -1,3 +1,3 @@
en:
discourse_patrons:
customer_id_not_found: Customer ID not found
customer_not_found: Customer not found

View File

@ -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"