discourse-subscriptions/spec/requests/products_controller_spec.rb

91 lines
2.9 KiB
Ruby
Raw Normal View History

2019-10-24 17:18:16 -04:00
# frozen_string_literal: true
require 'rails_helper'
2019-12-03 19:23:45 -05:00
module DiscourseSubscriptions
2019-10-24 17:18:16 -04:00
RSpec.describe ProductsController do
2019-12-02 22:31:15 -05:00
describe "products" 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
2019-12-02 22:31:15 -05:00
context "unauthenticated" do
it "gets products" do
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
2019-11-04 00:37:21 -05:00
2019-12-02 19:48:12 -05:00
get "/s/products.json"
2019-11-04 00:37:21 -05:00
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
2019-12-02 22:31:15 -05:00
end
2019-12-02 22:31:15 -05:00
context "authenticated" do
let(:user) { Fabricate(:user) }
2019-12-02 22:31:15 -05:00
before do
sign_in(user)
end
2019-12-02 22:31:15 -05:00
describe "index" do
it "gets products" do
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
2019-12-02 22:31:15 -05:00
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
2019-12-03 19:23:45 -05:00
::DiscourseSubscriptions::Customer.create(product_id: product[:id], user_id: user.id, customer_id: 'x')
2019-12-02 22:31:15 -05:00
::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
2019-12-03 19:23:45 -05:00
::DiscourseSubscriptions::Customer.delete_all
2019-12-02 22:31:15 -05:00
::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
2019-11-04 00:37:21 -05:00
2019-12-02 22:31:15 -05:00
describe 'show' do
it 'retrieves the product' do
::Stripe::Product.expects(:retrieve).with('prod_walterwhite').returns(product)
get "/s/products/prod_walterwhite.json"
2019-11-04 00:37:21 -05:00
2019-12-02 22:31:15 -05:00
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
2019-10-24 17:18:16 -04:00
end
end
end
end