2019-11-01 13:43:09 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 12:35:06 +00:00
|
|
|
require "rails_helper"
|
2019-11-01 13:43:09 +11:00
|
|
|
|
2019-12-04 11:23:45 +11:00
|
|
|
module DiscourseSubscriptions
|
2019-11-01 13:43:09 +11:00
|
|
|
RSpec.describe User::SubscriptionsController do
|
2022-12-29 12:35:06 +00:00
|
|
|
it "is a subclass of ApplicationController" do
|
|
|
|
expect(DiscourseSubscriptions::User::SubscriptionsController < ::ApplicationController).to eq(
|
|
|
|
true,
|
|
|
|
)
|
2019-11-01 13:43:09 +11:00
|
|
|
end
|
|
|
|
|
2022-10-04 09:18:34 +01:00
|
|
|
context "when not authenticated" do
|
2019-11-01 13:43:09 +11:00
|
|
|
it "does not get the subscriptions" do
|
|
|
|
::Stripe::Customer.expects(:list).never
|
2019-12-03 11:48:12 +11:00
|
|
|
get "/s/user/subscriptions.json"
|
2019-11-01 13:43:09 +11:00
|
|
|
end
|
2019-11-04 16:37:21 +11:00
|
|
|
|
|
|
|
it "does not destroy a subscription" do
|
|
|
|
::Stripe::Subscription.expects(:delete).never
|
2019-12-03 11:48:12 +11:00
|
|
|
patch "/s/user/subscriptions/sub_12345.json"
|
2019-11-04 16:37:21 +11:00
|
|
|
end
|
2022-07-06 07:23:27 +05:30
|
|
|
|
|
|
|
it "doesn't update payment method for subscription" do
|
|
|
|
::Stripe::Subscription.expects(:update).never
|
|
|
|
::Stripe::PaymentMethod.expects(:attach).never
|
|
|
|
put "/s/user/subscriptions/sub_12345.json", params: { payment_method: "pm_abc123abc" }
|
|
|
|
end
|
2019-11-01 13:43:09 +11:00
|
|
|
end
|
|
|
|
|
2022-10-04 09:18:34 +01:00
|
|
|
context "when authenticated" do
|
2022-12-29 12:35:06 +00:00
|
|
|
let(:user) { Fabricate(:user, email: "beanie@example.com") }
|
|
|
|
let(:customer) do
|
|
|
|
Fabricate(:customer, user_id: user.id, customer_id: "cus_23456", product_id: "prod_123")
|
|
|
|
end
|
2019-11-01 13:43:09 +11:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
2020-05-22 11:20:05 -05:00
|
|
|
Fabricate(:subscription, customer_id: customer.id, external_id: "sub_1234")
|
2019-11-01 13:43:09 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "index" do
|
2019-11-27 10:48:30 +11:00
|
|
|
let(:plans) do
|
|
|
|
{
|
|
|
|
data: [
|
2022-12-29 12:35:06 +00:00
|
|
|
{ id: "plan_1", product: { name: "ACME Subscriptions" } },
|
|
|
|
{ id: "plan_2", product: { name: "ACME Other Subscriptions" } },
|
|
|
|
],
|
2019-11-27 10:48:30 +11:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-11-01 13:43:09 +11:00
|
|
|
let(:customers) do
|
|
|
|
{
|
2022-12-29 12:35:06 +00:00
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: "cus_23456",
|
|
|
|
subscriptions: {
|
|
|
|
data: [
|
|
|
|
{ id: "sub_1234", items: { data: [price: { id: "plan_1" }] } },
|
|
|
|
{ id: "sub_4567", items: { data: [price: { id: "plan_2" }] } },
|
|
|
|
],
|
|
|
|
},
|
2019-11-01 13:43:09 +11:00
|
|
|
},
|
2022-12-29 12:35:06 +00:00
|
|
|
],
|
2019-11-01 13:43:09 +11:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gets subscriptions" do
|
2022-12-29 12:35:06 +00:00
|
|
|
::Stripe::Price.expects(:list).with(expand: ["data.product"], limit: 100).returns(plans)
|
2019-11-27 10:48:30 +11:00
|
|
|
|
2022-12-29 12:35:06 +00:00
|
|
|
::Stripe::Customer
|
|
|
|
.expects(:list)
|
|
|
|
.with(email: user.email, expand: ["data.subscriptions"])
|
|
|
|
.returns(customers)
|
2019-11-01 13:43:09 +11:00
|
|
|
|
2019-12-03 11:48:12 +11:00
|
|
|
get "/s/user/subscriptions.json"
|
2019-11-01 13:43:09 +11:00
|
|
|
|
2020-05-22 11:20:05 -05:00
|
|
|
subscription = response.parsed_body.first
|
2019-11-27 10:48:30 +11:00
|
|
|
|
|
|
|
expect(subscription).to eq(
|
|
|
|
"id" => "sub_1234",
|
2022-12-29 12:35:06 +00:00
|
|
|
"items" => {
|
|
|
|
"data" => [{ "price" => { "id" => "plan_1" } }],
|
|
|
|
},
|
|
|
|
"plan" => {
|
|
|
|
"id" => "plan_1",
|
|
|
|
"product" => {
|
|
|
|
"name" => "ACME Subscriptions",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"product" => {
|
|
|
|
"name" => "ACME Subscriptions",
|
|
|
|
},
|
2019-11-27 10:48:30 +11:00
|
|
|
)
|
2019-11-01 13:43:09 +11:00
|
|
|
end
|
|
|
|
end
|
2022-07-06 07:23:27 +05:30
|
|
|
|
|
|
|
describe "update" do
|
|
|
|
it "updates the payment method for subscription" do
|
|
|
|
::Stripe::Subscription.expects(:update).once
|
|
|
|
::Stripe::PaymentMethod.expects(:attach).once
|
|
|
|
put "/s/user/subscriptions/sub_1234.json", params: { payment_method: "pm_abc123abc" }
|
|
|
|
end
|
|
|
|
end
|
2019-11-01 13:43:09 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|