164 lines
5.6 KiB
Ruby
164 lines
5.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'rails_helper'
|
|
|
|
module DiscoursePatrons
|
|
RSpec.describe PatronsController, type: :controller do
|
|
routes { DiscoursePatrons::Engine.routes }
|
|
|
|
describe 'index' do
|
|
it 'responds ok' do
|
|
get :index, format: :json
|
|
expect(response).to have_http_status(200)
|
|
end
|
|
|
|
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
|
|
expect(JSON.parse(response.body)['email']).to be_empty
|
|
end
|
|
end
|
|
|
|
describe 'show' do
|
|
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
|
|
|
|
it 'responds ok' do
|
|
get :show, params: { pid: '123' }, format: :json
|
|
expect(response).to have_http_status(200)
|
|
end
|
|
|
|
it 'requests the payment intent' do
|
|
::Stripe::PaymentIntent.expects(:retrieve).with('abc-1234').returns(payment_intent)
|
|
get :show, params: { pid: 'abc-1234' }, format: :json
|
|
end
|
|
|
|
it 'allows admin to see receipts' do
|
|
controller.expects(:current_user).returns(admin)
|
|
::Stripe::PaymentIntent.expects(:retrieve).returns(metadata: { user_id: 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(metadata: { user_id: 9999 })
|
|
get :show, params: { pid: '123' }, format: :json
|
|
|
|
aggregate_failures do
|
|
expect(response).to have_http_status(200)
|
|
expect(JSON.parse(response.body)).to eq("error" => "Not found")
|
|
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)
|
|
expect(JSON.parse(response.body)).to eq("error" => "Not found")
|
|
end
|
|
end
|
|
end
|
|
|
|
describe 'create' do
|
|
let!(:current_user) { Fabricate(:user) }
|
|
|
|
let(:payment) do
|
|
{
|
|
id: 'xyz-1234',
|
|
charges: { url: '/v1/charges?payment_intent=xyz-1234' },
|
|
amount: 9000,
|
|
receipt_email: 'hello@example.com',
|
|
currency: 'aud',
|
|
metadata: { user_id: current_user.id }
|
|
}
|
|
end
|
|
|
|
before do
|
|
SiteSetting.stubs(:discourse_patrons_currency).returns('AUD')
|
|
SiteSetting.stubs(:discourse_patrons_secret_key).returns('xyz-678')
|
|
controller.stubs(:current_user).returns(current_user)
|
|
end
|
|
|
|
it 'responds ok' do
|
|
::Stripe::PaymentIntent.expects(:create).returns(payment)
|
|
post :create, params: { receipt_email: 'hello@example.com', amount: '20.00' }, format: :json
|
|
expect(response).to have_http_status(200)
|
|
end
|
|
|
|
it 'creates a payment' do
|
|
::Stripe::PaymentIntent.expects(:create).returns(payment)
|
|
|
|
expect {
|
|
post :create, params: { receipt_email: 'hello@example.com', amount: '20.00' }, format: :json
|
|
}.to change { Payment.count }
|
|
end
|
|
|
|
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
|
|
|
|
it 'has the correct amount' do
|
|
::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 2000)).returns(payment)
|
|
post :create, params: { amount: '20.00' }, format: :json
|
|
expect(response).to have_http_status(200)
|
|
end
|
|
|
|
it 'has no amount' do
|
|
::Stripe::PaymentIntent.expects(:create).with(has_entry(:amount, 0)).returns(payment)
|
|
post :create, params: {}, format: :json
|
|
expect(response).to have_http_status(200)
|
|
end
|
|
|
|
it 'has curency' do
|
|
::Stripe::PaymentIntent.expects(:create).with(has_entry(:currency, 'AUD')).returns(payment)
|
|
post :create, params: {}, format: :json
|
|
expect(response).to have_http_status(200)
|
|
end
|
|
|
|
it 'has the user id' do
|
|
::Stripe::PaymentIntent.expects(:create).with(has_entry(:metadata, user_id: current_user.id)).returns(payment)
|
|
post :create, params: {}, format: :json
|
|
expect(response).to have_http_status(200)
|
|
end
|
|
|
|
it 'has a receipt email' do
|
|
::Stripe::PaymentIntent.expects(:create).with(has_entry(:receipt_email, 'hello@example.com')).returns(payment)
|
|
post :create, params: { receipt_email: 'hello@example.com' }, format: :json
|
|
expect(response).to have_http_status(200)
|
|
end
|
|
|
|
it 'has a payment method' do
|
|
::Stripe::PaymentIntent.expects(:create).with(has_entry(:payment_method, 'xyz-1234')).returns(payment)
|
|
post :create, params: { payment_method_id: 'xyz-1234' }, format: :json
|
|
expect(response).to have_http_status(200)
|
|
end
|
|
|
|
it 'has a description' do
|
|
SiteSetting.stubs(:discourse_patrons_payment_description).returns('hello-world')
|
|
::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
|
|
end
|
|
end
|
|
end
|