From d8a3ae5554c2cf5ddeaff7ef20491a3ff509dc82 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Tue, 3 Dec 2019 14:31:15 +1100 Subject: [PATCH] fix broken unauthenticated page --- app/controllers/products_controller.rb | 2 + plugin.rb | 4 +- spec/requests/products_controller_spec.rb | 89 ++++++++++++++--------- 3 files changed, 57 insertions(+), 38 deletions(-) diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 8ab971f..8e7cc44 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -44,6 +44,8 @@ module DiscoursePatrons end def current_user_products + return [] if current_user.nil? + ::DiscoursePatrons::Customer .select(:product_id) .where(user_id: current_user.id) diff --git a/plugin.rb b/plugin.rb index 2dfd520..b9458b4 100644 --- a/plugin.rb +++ b/plugin.rb @@ -2,7 +2,7 @@ # name: discourse-subscriptions # about: Integrates Stripe into Discourse to allow visitors to subscribe -# version: 2.5.0 +# version: 2.5.1 # url: https://github.com/rimian/discourse-subscriptions # authors: Rimian Perkins @@ -44,7 +44,7 @@ after_initialize do ::Stripe.set_app_info( 'Discourse Subscriptions', - version: '2.5.0', + version: '2.5.1', url: 'https://github.com/rimian/discourse-subscriptions' ) diff --git a/spec/requests/products_controller_spec.rb b/spec/requests/products_controller_spec.rb index 6c1068e..4705ef0 100644 --- a/spec/requests/products_controller_spec.rb +++ b/spec/requests/products_controller_spec.rb @@ -4,9 +4,7 @@ require 'rails_helper' module DiscoursePatrons RSpec.describe ProductsController do - context "authenticated" do - let(:user) { Fabricate(:user) } - + describe "products" do let(:product) do { id: "prodct_23456", @@ -18,11 +16,7 @@ module DiscoursePatrons } end - before do - sign_in(user) - end - - describe "index" do + context "unauthenticated" do it "gets products" do ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) @@ -35,37 +29,60 @@ module DiscoursePatrons "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 "/s/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 "/s/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 "/s/products/prod_walterwhite.json" + context "authenticated" do + let(:user) { Fabricate(:user) } - 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 - ) + before do + sign_in(user) + end + + describe "index" do + it "gets products" do + ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) + + get "/s/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 "/s/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 "/s/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 "/s/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