test subcription deletes
This commit is contained in:
parent
2eab43d4a0
commit
ad77fc749b
|
@ -40,7 +40,8 @@ module DiscoursePatrons
|
||||||
|
|
||||||
customer = Customer.find_by(
|
customer = Customer.find_by(
|
||||||
user_id: current_user.id,
|
user_id: current_user.id,
|
||||||
customer_id: subscription[:customer]
|
customer_id: subscription[:customer],
|
||||||
|
product_id: subscription[:plan][:product]
|
||||||
)
|
)
|
||||||
|
|
||||||
if customer.present?
|
if customer.present?
|
||||||
|
@ -49,7 +50,7 @@ module DiscoursePatrons
|
||||||
|
|
||||||
render_json_dump deleted
|
render_json_dump deleted
|
||||||
else
|
else
|
||||||
render_json_error I18n.t('discourse_patrons.customer_id_not_found')
|
render_json_error I18n.t('discourse_patrons.customer_not_found')
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue ::Stripe::InvalidRequestError => e
|
rescue ::Stripe::InvalidRequestError => e
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
en:
|
en:
|
||||||
discourse_patrons:
|
discourse_patrons:
|
||||||
customer_id_not_found: Customer ID not found
|
customer_not_found: Customer not found
|
||||||
|
|
|
@ -83,12 +83,22 @@ module DiscoursePatrons
|
||||||
before do
|
before do
|
||||||
# Users can have more than one customer id
|
# 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_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')
|
Customer.create(user_id: user.id, customer_id: 'customer_id_2', product_id: 'p_2')
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not delete a subscription" do
|
it "does not delete a subscription when the customer is wrong" do
|
||||||
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'wrong_id')
|
::Stripe::Subscription
|
||||||
::Stripe::Subscription.expects(:delete).never
|
.expects(:retrieve)
|
||||||
|
.with('sub_12345')
|
||||||
|
.returns(
|
||||||
|
plan: { product: 'p_1' },
|
||||||
|
customer: 'wrong_id'
|
||||||
|
)
|
||||||
|
|
||||||
|
::Stripe::Subscription
|
||||||
|
.expects(:delete)
|
||||||
|
.never
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
delete "/patrons/user/subscriptions/sub_12345.json"
|
delete "/patrons/user/subscriptions/sub_12345.json"
|
||||||
|
@ -97,9 +107,58 @@ module DiscoursePatrons
|
||||||
expect(response.status).to eq 422
|
expect(response.status).to eq 422
|
||||||
end
|
end
|
||||||
|
|
||||||
it "deletes the first subscription" do
|
it "does not deletes the subscription when the product is wrong" do
|
||||||
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'customer_id_1')
|
::Stripe::Subscription
|
||||||
::Stripe::Subscription.expects(:delete).with('sub_12345')
|
.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 {
|
expect {
|
||||||
delete "/patrons/user/subscriptions/sub_12345.json"
|
delete "/patrons/user/subscriptions/sub_12345.json"
|
||||||
|
@ -109,8 +168,17 @@ module DiscoursePatrons
|
||||||
end
|
end
|
||||||
|
|
||||||
it "deletes the second subscription" do
|
it "deletes the second subscription" do
|
||||||
::Stripe::Subscription.expects(:retrieve).with('sub_12345').returns(customer: 'customer_id_2')
|
::Stripe::Subscription
|
||||||
::Stripe::Subscription.expects(:delete).with('sub_12345')
|
.expects(:retrieve)
|
||||||
|
.with('sub_12345')
|
||||||
|
.returns(
|
||||||
|
plan: { product: 'p_2' },
|
||||||
|
customer: 'customer_id_2'
|
||||||
|
)
|
||||||
|
|
||||||
|
::Stripe::Subscription
|
||||||
|
.expects(:delete)
|
||||||
|
.with('sub_12345')
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
delete "/patrons/user/subscriptions/sub_12345.json"
|
delete "/patrons/user/subscriptions/sub_12345.json"
|
||||||
|
|
Loading…
Reference in New Issue