discourse-subscriptions/spec/controllers/discourse_patrons/patrons_controller_spec.rb

164 lines
5.6 KiB
Ruby
Raw Normal View History

2019-09-11 03:57:29 -04:00
# frozen_string_literal: true
require 'rails_helper'
module DiscoursePatrons
RSpec.describe PatronsController, type: :controller do
routes { DiscoursePatrons::Engine.routes }
2019-09-11 04:32:09 -04:00
2019-09-11 05:11:02 -04:00
describe 'index' do
it 'responds ok' do
get :index, format: :json
expect(response).to have_http_status(200)
end
2019-09-12 22:35:38 -04:00
it 'has a current user email' do
user = Fabricate(:user, email: 'hello@example.com')
controller.expects(:current_user).at_least(1).returns(user)
get :index, format: :json
expect(JSON.parse(response.body)['email']).to eq 'hello@example.com'
end
it 'has no current user email' do
get :index, format: :json
2019-09-12 22:58:17 -04:00
expect(JSON.parse(response.body)['email']).to be_empty
2019-09-12 22:35:38 -04:00
end
2019-09-11 05:11:02 -04:00
end
2019-09-11 04:32:09 -04:00
2019-09-14 03:28:58 -04:00
describe 'show' do
2019-09-14 18:40:52 -04:00
let!(:admin) { Fabricate(:admin) }
let!(:user) { Fabricate(:user) }
let(:payment_intent) { { customer: user.id } }
before do
controller.stubs(:current_user).returns(user)
::Stripe::PaymentIntent.stubs(:retrieve).returns(payment_intent)
end
2019-09-14 03:28:58 -04:00
it 'responds ok' do
get :show, params: { pid: '123' }, format: :json
expect(response).to have_http_status(200)
end
2019-09-14 06:54:13 -04:00
it 'requests the payment intent' do
2019-09-14 18:40:52 -04:00
::Stripe::PaymentIntent.expects(:retrieve).with('abc-1234').returns(payment_intent)
2019-09-14 06:54:13 -04:00
get :show, params: { pid: 'abc-1234' }, format: :json
end
2019-09-14 18:40:52 -04:00
it 'allows admin to see receipts' do
controller.expects(:current_user).returns(admin)
::Stripe::PaymentIntent.expects(:retrieve).returns(customer: user.id)
get :show, params: { pid: '123' }, format: :json
expect(response).to have_http_status(200)
end
it 'does not allow another the user to see receipts' do
::Stripe::PaymentIntent.expects(:retrieve).returns(customer: 9999)
get :show, params: { pid: '123' }, format: :json
aggregate_failures do
expect(response).to have_http_status(200)
2019-09-14 22:34:36 -04:00
expect(JSON.parse(response.body)).to eq("error" => "Not found")
2019-09-14 18:40:52 -04:00
end
end
it 'does not allow anon user to see receipts' do
controller.stubs(:current_user).returns(nil)
get :show, params: { pid: '123' }, format: :json
aggregate_failures do
expect(response).to have_http_status(200)
2019-09-14 22:34:36 -04:00
expect(JSON.parse(response.body)).to eq("error" => "Not found")
2019-09-14 18:40:52 -04:00
end
end
2019-09-14 03:28:58 -04:00
end
2019-09-11 05:11:02 -04:00
describe 'create' do
2019-09-14 07:00:56 -04:00
let!(:current_user) { Fabricate(:user) }
2019-09-13 08:03:29 -04:00
let(:payment) do
{
id: 'xyz-1234',
charges: { url: '/v1/charges?payment_intent=xyz-1234' },
amount: 9000,
2019-09-14 01:55:25 -04:00
receipt_email: 'hello@example.com',
2019-09-14 07:00:56 -04:00
currency: 'aud',
customer: current_user.id
2019-09-13 08:03:29 -04:00
}
end
2019-09-11 05:55:35 -04:00
before do
SiteSetting.stubs(:discourse_patrons_currency).returns('AUD')
SiteSetting.stubs(:discourse_patrons_secret_key).returns('xyz-678')
2019-09-14 07:00:56 -04:00
controller.stubs(:current_user).returns(current_user)
2019-09-11 05:55:35 -04:00
end
2019-09-11 05:11:02 -04:00
it 'responds ok' do
2019-09-13 08:03:29 -04:00
::Stripe::PaymentIntent.expects(:create).returns(payment)
post :create, params: { receipt_email: 'hello@example.com', amount: '20.00' }, format: :json
2019-09-11 04:32:09 -04:00
expect(response).to have_http_status(200)
end
2019-09-11 21:49:52 -04:00
2019-09-13 08:03:29 -04:00
it 'creates a payment' do
::Stripe::PaymentIntent.expects(:create).returns(payment)
2019-09-13 00:34:06 -04:00
expect {
post :create, params: { receipt_email: 'hello@example.com', amount: '20.00' }, format: :json
}.to change { Payment.count }
end
2019-09-14 01:55:25 -04:00
it 'has no user' do
controller.stubs(:current_user).returns(nil)
::Stripe::PaymentIntent.expects(:create).returns(payment)
post :create, format: :json
expect(response).to have_http_status(200)
end
2019-09-11 21:49:52 -04:00
it 'has the correct amount' do
2019-09-13 08:03:29 -04:00
::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 2000)).returns(payment)
2019-09-11 21:49:52 -04:00
post :create, params: { amount: '20.00' }, format: :json
expect(response).to have_http_status(200)
end
it 'has no amount' do
2019-09-13 08:03:29 -04:00
::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 0)).returns(payment)
2019-09-11 21:49:52 -04:00
post :create, params: {}, format: :json
expect(response).to have_http_status(200)
end
2019-09-11 23:13:06 -04:00
it 'has curency' do
2019-09-13 08:03:29 -04:00
::Stripe::PaymentIntent.expects(:create).with(has_entry(:currency, 'AUD')).returns(payment)
2019-09-11 23:13:06 -04:00
post :create, params: {}, format: :json
2019-09-14 07:00:56 -04:00
expect(response).to have_http_status(200)
end
it 'has the customer id' do
::Stripe::PaymentIntent.expects(:create).with(has_entry(:customer, current_user.id)).returns(payment)
post :create, params: {}, format: :json
2019-09-11 23:13:06 -04:00
expect(response).to have_http_status(200)
end
2019-09-12 23:46:32 -04:00
it 'has a receipt email' do
2019-09-13 08:03:29 -04:00
::Stripe::PaymentIntent.expects(:create).with(has_entry(:receipt_email, 'hello@example.com')).returns(payment)
2019-09-13 00:34:06 -04:00
post :create, params: { receipt_email: 'hello@example.com' }, format: :json
expect(response).to have_http_status(200)
end
it 'has a payment method' do
2019-09-13 08:03:29 -04:00
::Stripe::PaymentIntent.expects(:create).with(has_entry(:payment_method, 'xyz-1234')).returns(payment)
2019-09-13 00:34:06 -04:00
post :create, params: { payment_method_id: 'xyz-1234' }, format: :json
2019-09-12 23:46:32 -04:00
expect(response).to have_http_status(200)
end
it 'has a description' do
SiteSetting.stubs(:discourse_patrons_payment_description).returns('hello-world')
2019-09-13 08:03:29 -04:00
::Stripe::PaymentIntent.expects(:create).with(has_entry(:description, 'hello-world')).returns(payment)
post :create, params: {}, format: :json
expect(response).to have_http_status(200)
end
2019-09-11 04:32:09 -04:00
end
2019-09-11 03:57:29 -04:00
end
end