1
0
mirror of synced 2026-08-05 14:47:03 +00:00

backend returns if user is already subscribed

This commit is contained in:
Rimian Perkins
2019-11-28 17:43:03 +11:00
parent c7dcc18923
commit 0fddb5e3b9
6 changed files with 93 additions and 38 deletions
+59 -31
View File
@@ -4,41 +4,69 @@ require 'rails_helper'
module DiscoursePatrons
RSpec.describe ProductsController do
let(:product) do
{
id: "prodct_23456",
name: "Very Special Product",
metadata: {
description: "Many people listened to my phone call with the Ukrainian President while it was being made"
},
otherstuff: true,
}
end
context "authenticated" do
let(:user) { Fabricate(:user) }
describe "index" do
it "gets products" do
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
get "/patrons/products.json"
expect(JSON.parse(response.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"
}])
let(:product) do
{
id: "prodct_23456",
name: "Very Special Product",
metadata: {
description: "Many people listened to my phone call with the Ukrainian President while it was being made"
},
otherstuff: true,
}
end
end
describe 'show' do
it 'retrieves the product' do
::Stripe::Product.expects(:retrieve).with('prod_walterwhite').returns(product)
get "/patrons/products/prod_walterwhite.json"
before do
sign_in(user)
end
expect(JSON.parse(response.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"
)
describe "index" do
it "gets products" do
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
get "/patrons/products.json"
expect(JSON.parse(response.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",
"subscribed" => false
}])
end
it "is subscribed" do
::DiscoursePatrons::Customer.create(product_id: product[:id], user_id: user.id, customer_id: 'x')
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
get "/patrons/products.json"
data = JSON.parse(response.body)
expect(data.first["subscribed"]).to eq true
end
it "is not subscribed" do
::DiscoursePatrons::Customer.delete_all
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
get "/patrons/products.json"
data = JSON.parse(response.body)
expect(data.first["subscribed"]).to eq false
end
end
describe 'show' do
it 'retrieves the product' do
::Stripe::Product.expects(:retrieve).with('prod_walterwhite').returns(product)
get "/patrons/products/prod_walterwhite.json"
expect(JSON.parse(response.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",
"subscribed" => false
)
end
end
end
end
+10 -3
View File
@@ -21,13 +21,20 @@ module DiscoursePatrons
describe "create" do
it "creates a subscription" do
::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: 'awesome' })
::Stripe::Plan.expects(:retrieve).returns(
product: 'product_12345',
metadata: { group_name: 'awesome' }
)
::Stripe::Subscription.expects(:create).with(
customer: 'cus_1234',
items: [ plan: 'plan_1234' ],
metadata: { user_id: user.id, username: user.username_lower },
)
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
).returns(status: 'active')
expect {
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
}.to change { DiscoursePatrons::Customer.count }
end
it "creates a customer model" do