discourse-subscriptions/spec/requests/admin/plans_controller_spec.rb

175 lines
5.8 KiB
Ruby
Raw Normal View History

2019-10-10 12:08:52 +11:00
# frozen_string_literal: true
require "rails_helper"
2019-10-10 12:08:52 +11:00
2019-12-04 11:23:45 +11:00
module DiscourseSubscriptions
2019-10-10 12:08:52 +11:00
module Admin
RSpec.describe PlansController do
it "is a subclass of AdminController" do
expect(DiscourseSubscriptions::Admin::PlansController < ::Admin::AdminController).to eq(
true,
)
2019-10-10 12:08:52 +11:00
end
context "when not authenticated" do
2019-10-10 12:31:32 +11:00
describe "index" do
it "does not get the plans" do
::Stripe::Price.expects(:list).never
2019-12-03 11:48:12 +11:00
get "/s/admin/plans.json"
2019-10-10 12:31:32 +11:00
end
2019-10-10 12:08:52 +11:00
2019-10-10 12:31:32 +11:00
it "not ok" do
2019-12-03 11:48:12 +11:00
get "/s/admin/plans.json"
expect(response.status).to eq 404
2019-10-10 12:31:32 +11:00
end
2019-10-10 12:08:52 +11:00
end
2019-10-10 12:31:32 +11:00
describe "create" do
it "does not create a plan" do
::Stripe::Price.expects(:create).never
post "/s/admin/plans.json", params: { name: "Rick Astley", amount: 1, interval: "week" }
2019-10-10 12:31:32 +11:00
end
it "is not ok" do
post "/s/admin/plans.json", params: { name: "Rick Astley", amount: 1, interval: "week" }
expect(response.status).to eq 404
2019-10-10 12:31:32 +11:00
end
2019-10-10 12:08:52 +11:00
end
2019-10-17 20:34:26 +11:00
describe "show" do
it "does not show the plan" do
::Stripe::Price.expects(:retrieve).never
2019-12-03 11:48:12 +11:00
get "/s/admin/plans/plan_12345.json"
2019-10-17 20:34:26 +11:00
end
it "is not ok" do
2019-12-03 11:48:12 +11:00
get "/s/admin/plans/plan_12345.json"
expect(response.status).to eq 404
2019-10-17 20:34:26 +11:00
end
end
2019-10-23 15:55:06 +11:00
describe "update" do
it "does not update a plan" do
::Stripe::Price.expects(:update).never
2019-12-03 11:48:12 +11:00
delete "/s/admin/plans/plan_12345.json"
2019-10-23 15:55:06 +11:00
end
2019-10-10 12:08:52 +11:00
end
2019-10-10 12:31:32 +11:00
end
2019-10-10 12:08:52 +11:00
context "when authenticated" do
2019-10-10 12:31:32 +11:00
let(:admin) { Fabricate(:admin) }
before { sign_in(admin) }
describe "index" do
2019-10-10 13:52:55 +11:00
it "lists the plans" do
::Stripe::Price.expects(:list).with(nil)
2019-12-03 11:48:12 +11:00
get "/s/admin/plans.json"
2019-10-10 12:31:32 +11:00
end
2019-10-21 15:28:45 +11:00
it "lists the plans for the product" do
::Stripe::Price.expects(:list).with({ product: "prod_id123" })
get "/s/admin/plans.json", params: { product_id: "prod_id123" }
2019-10-21 15:28:45 +11:00
end
2019-10-10 12:08:52 +11:00
end
2019-11-30 16:39:28 +11:00
describe "show" do
it "shows a plan" do
::Stripe::Price.expects(:retrieve).with("plan_12345").returns(currency: "aud")
2019-12-03 11:48:12 +11:00
get "/s/admin/plans/plan_12345.json"
2019-11-30 16:39:28 +11:00
expect(response.status).to eq 200
end
it "upcases the currency" do
::Stripe::Price
.expects(:retrieve)
.with("plan_12345")
.returns(currency: "aud", recurring: { interval: "year" })
2019-12-03 11:48:12 +11:00
get "/s/admin/plans/plan_12345.json"
plan = response.parsed_body
expect(plan["currency"]).to eq "AUD"
expect(plan["interval"]).to eq "year"
2019-11-30 16:39:28 +11:00
end
end
2019-10-10 12:31:32 +11:00
describe "create" do
2019-10-17 20:34:26 +11:00
it "creates a plan with a nickname" do
::Stripe::Price.expects(:create).with(has_entry(:nickname, "Veg"))
post "/s/admin/plans.json", params: { nickname: "Veg", metadata: { group_name: "" } }
2019-10-17 20:34:26 +11:00
end
2019-10-10 12:31:32 +11:00
it "creates a plan with a currency" do
::Stripe::Price.expects(:create).with(has_entry(:currency, "AUD"))
post "/s/admin/plans.json", params: { currency: "AUD", metadata: { group_name: "" } }
2019-10-10 12:31:32 +11:00
end
it "creates a plan with an interval" do
::Stripe::Price.expects(:create).with(has_entry(recurring: { interval: "week" }))
post "/s/admin/plans.json",
params: {
type: "recurring",
interval: "week",
metadata: {
group_name: "",
},
}
end
it "creates a plan as a one-time purchase" do
::Stripe::Price.expects(:create).with(Not(has_key(:recurring)))
post "/s/admin/plans.json", params: { metadata: { group_name: "" } }
2019-10-10 12:31:32 +11:00
end
it "creates a plan with an amount" do
::Stripe::Price.expects(:create).with(has_entry(:unit_amount, "102"))
post "/s/admin/plans.json", params: { amount: "102", metadata: { group_name: "" } }
2019-10-10 12:31:32 +11:00
end
2019-10-17 12:07:06 +11:00
it "creates a plan with a product" do
::Stripe::Price.expects(:create).with(has_entry(product: "prod_walterwhite"))
post "/s/admin/plans.json",
params: {
product: "prod_walterwhite",
metadata: {
group_name: "",
},
}
2019-10-23 11:50:54 +11:00
end
2019-10-24 10:02:31 +11:00
it "creates a plan with an active status" do
::Stripe::Price.expects(:create).with(has_entry(:active, "false"))
post "/s/admin/plans.json", params: { active: "false", metadata: { group_name: "" } }
2019-10-24 10:02:31 +11:00
end
# TODO: Need to fix the metadata tests
# I think mocha has issues with the metadata fields here.
#it 'has metadata' do
# ::Stripe::Price.expects(:create).with(has_entry(:group_name, "discourse-user-group-name"))
# post "/s/admin/plans.json", params: { amount: "100", metadata: { group_name: 'discourse-user-group-name' } }
#end
#it "creates a plan with a trial period" do
# ::Stripe::Price.expects(:create).with(has_entry(trial_period_days: '14'))
# post "/s/admin/plans.json", params: { trial_period_days: '14' }
#end
2019-10-10 12:08:52 +11:00
end
2019-10-23 15:55:06 +11:00
describe "update" do
it "updates a plan" do
::Stripe::Price.expects(:update)
patch "/s/admin/plans/plan_12345.json",
params: {
trial_period_days: "14",
metadata: {
group_name: "discourse-user-group-name",
},
}
2019-10-10 12:31:32 +11:00
end
2019-10-10 12:08:52 +11:00
end
end
end
end
end