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-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',
|
2019-09-15 07:44:10 -04:00
|
|
|
metadata: { user_id: 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-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
|
|
|
|
|
2019-09-15 07:44:10 -04:00
|
|
|
it 'has the user id' do
|
2019-09-15 08:11:39 -04:00
|
|
|
::Stripe::PaymentIntent.expects(:create).with(has_entry(:metadata, user_id: current_user.id)).returns(payment)
|
2019-09-14 07:00:56 -04:00
|
|
|
post :create, params: {}, format: :json
|
2019-09-11 23:13:06 -04:00
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
2019-09-12 05:17:08 -04:00
|
|
|
|
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
|
|
|
|
|
2019-09-12 05:17:08 -04:00
|
|
|
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)
|
2019-09-12 05:17:08 -04:00
|
|
|
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
|