REFACTOR: Use models to store data (#11)

* REFACTOR: Use api to add subscribe link

* FIX: I18n subscribe link

* REFACTOR: Use models to store some data

This enables the plugin to show only subscription information which was
generated on Discourse. Subscription data storage is limited to the
external identifiers Stripe generates so we can interact with the API.

* DEV: Test/linting fixes/rake task
This commit is contained in:
Justin DiRose
2020-05-22 11:20:05 -05:00
committed by GitHub
parent 7ba08ab7da
commit fb4fac197b
47 changed files with 308 additions and 225 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
# frozen_string_literal: true
Fabricator(:customer, from: "DiscourseSubscriptions::Customer")
Fabricator(:product, from: "DiscourseSubscriptions::Product")
+3
View File
@@ -0,0 +1,3 @@
# frozen_string_literal: true
Fabricator(:customer, from: "DiscourseSubscriptions::Customer")
@@ -0,0 +1,3 @@
# frozen_string_literal: true
Fabricator(:subscription, from: "DiscourseSubscriptions::Subscription")
+1 -1
View File
@@ -98,7 +98,7 @@ module DiscourseSubscriptions
it "upcases the currency" do
::Stripe::Plan.expects(:retrieve).with('plan_12345').returns(currency: 'aud')
get "/s/admin/plans/plan_12345.json"
expect(response.body).to eq '{"currency":"AUD"}'
expect(response.parsed_body["currency"]).to eq 'AUD'
end
end
@@ -48,8 +48,8 @@ module DiscourseSubscriptions
describe 'index' do
it "gets the empty products" do
::Stripe::Product.expects(:list)
get "/s/admin/products.json"
expect(response.parsed_body).to be_empty
end
end
@@ -8,6 +8,13 @@ module DiscourseSubscriptions
expect(DiscourseSubscriptions::Admin::SubscriptionsController < ::Admin::AdminController).to eq(true)
end
let(:user) { Fabricate(:user) }
let(:customer) { Fabricate(:customer, user_id: user.id, customer_id: 'c_123', product_id: 'pr_34578') }
before do
Fabricate(:subscription, external_id: "sub_12345", customer_id: customer.id)
end
context 'unauthenticated' do
it "does nothing" do
::Stripe::Subscription.expects(:list).never
@@ -22,16 +29,23 @@ module DiscourseSubscriptions
end
context 'authenticated' do
let(:user) { Fabricate(:user) }
let(:admin) { Fabricate(:admin) }
before { sign_in(admin) }
describe "index" do
it "gets the subscriptions and products" do
::Stripe::Subscription.expects(:list).with(expand: ['data.plan.product'])
::Stripe::Subscription.expects(:list).with(expand: ['data.plan.product']).returns(
[
{ id: "sub_12345" },
{ id: "sub_nope" }
]
)
get "/s/admin/subscriptions.json"
subscriptions = response.parsed_body[0]["id"]
expect(response.status).to eq(200)
expect(subscriptions).to eq("sub_12345")
end
end
@@ -39,12 +53,6 @@ module DiscourseSubscriptions
let(:group) { Fabricate(:group, name: 'subscribers') }
before do
DiscourseSubscriptions::Customer.create(
user_id: user.id,
customer_id: 'c_123',
product_id: 'pr_34578'
)
group.add(user)
end
+1 -1
View File
@@ -26,7 +26,7 @@ module DiscourseSubscriptions
get "/s/plans.json"
expect(JSON.parse(response.body)).to eq([
expect(response.parsed_body).to eq([
{ "amount" => 1000, "currency" => "aud", "id" => "plan_id678", "interval" => "week" },
{ "amount" => 1220, "currency" => "aud", "id" => "plan_id123", "interval" => "year" },
{ "amount" => 1399, "currency" => "usd", "id" => "plan_id234", "interval" => "year" }
+15 -10
View File
@@ -15,14 +15,19 @@ module DiscourseSubscriptions
otherstuff: true,
}
end
let(:product_ids) { ["prodct_23456"] }
before do
Fabricate(:product, external_id: "prodct_23456")
end
context "unauthenticated" do
it "gets products" do
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
::Stripe::Product.expects(:list).with(ids: product_ids, active: true).returns(data: [product])
get "/s/products.json"
expect(JSON.parse(response.body)).to eq([{
expect(response.parsed_body).to eq([{
"id" => "prodct_23456",
"name" => "Very Special Product",
"description" => "Many people listened to my phone call with the Ukrainian President while it was being made",
@@ -40,11 +45,11 @@ module DiscourseSubscriptions
describe "index" do
it "gets products" do
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
::Stripe::Product.expects(:list).with(ids: product_ids, active: true).returns(data: [product])
get "/s/products.json"
expect(JSON.parse(response.body)).to eq([{
expect(response.parsed_body).to eq([{
"id" => "prodct_23456",
"name" => "Very Special Product",
"description" => "Many people listened to my phone call with the Ukrainian President while it was being made",
@@ -53,20 +58,20 @@ module DiscourseSubscriptions
end
it "is subscribed" do
::DiscourseSubscriptions::Customer.create(product_id: product[:id], user_id: user.id, customer_id: 'x')
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
Fabricate(:customer, product_id: product[:id], user_id: user.id, customer_id: 'x')
::Stripe::Product.expects(:list).with(ids: product_ids, active: true).returns(data: [product])
get "/s/products.json"
data = JSON.parse(response.body)
data = response.parsed_body
expect(data.first["subscribed"]).to eq true
end
it "is not subscribed" do
::DiscourseSubscriptions::Customer.delete_all
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
::Stripe::Product.expects(:list).with(ids: product_ids, active: true).returns(data: [product])
get "/s/products.json"
data = JSON.parse(response.body)
data = response.parsed_body
expect(data.first["subscribed"]).to eq false
end
end
@@ -76,7 +81,7 @@ module DiscourseSubscriptions
::Stripe::Product.expects(:retrieve).with('prod_walterwhite').returns(product)
get "/s/products/prod_walterwhite.json"
expect(JSON.parse(response.body)).to eq(
expect(response.parsed_body).to eq(
"id" => "prodct_23456",
"name" => "Very Special Product",
"description" => "Many people listened to my phone call with the Ukrainian President while it was being made",
+28 -1
View File
@@ -12,6 +12,7 @@ module DiscourseSubscriptions
it "does not get the payment intents" do
::Stripe::PaymentIntent.expects(:list).never
get "/s/user/payments.json"
expect(response.status).to eq(403)
end
end
@@ -21,14 +22,40 @@ module DiscourseSubscriptions
before do
sign_in(user)
Fabricate(:customer, customer_id: 'c_345678', user_id: user.id)
Fabricate(:product, external_id: 'prod_8675309')
end
it "gets payment intents" do
::Stripe::PaymentIntent.expects(:list).with(
::Stripe::Invoice.expects(:list).with(
customer: 'c_345678'
).returns(
data: [
id: "inv_900007",
lines: {
data: [
plan: {
product: "prod_8675309"
}
]
},
]
)
::Stripe::PaymentIntent.expects(:list).with(
customer: 'c_345678',
).returns(
data: [
{ invoice: "inv_900007" },
{ invoice: "inv_007" }
]
)
get "/s/user/payments.json"
invoice = response.parsed_body[0]["invoice"]
expect(invoice).to eq("inv_900007")
end
end
@@ -22,9 +22,11 @@ module DiscourseSubscriptions
context "authenticated" do
let(:user) { Fabricate(:user, email: '[email protected]') }
let(:customer) { Fabricate(:customer, user_id: user.id, customer_id: "cus_23456", product_id: "prod_123") }
before do
sign_in(user)
Fabricate(:subscription, customer_id: customer.id, external_id: "sub_1234")
end
describe "index" do
@@ -69,7 +71,7 @@ module DiscourseSubscriptions
get "/s/user/subscriptions.json"
subscription = JSON.parse(response.body).first
subscription = response.parsed_body.first
expect(subscription).to eq(
"id" => "sub_1234",