Fix failing tests (likely due to Discourse Rails upgrade)

This commit is contained in:
Chris Beach 2017-10-11 23:48:41 +01:00
parent 2b17c87333
commit 340959c73b
3 changed files with 33 additions and 25 deletions

View File

@ -1,6 +1,6 @@
# Discourse Donations # Discourse Donations
[![Build Status](https://travis-ci.org/choiceaustralia/discourse-donations.svg?branch=master)](https://travis-ci.org/choiceaustralia/discourse-donations) [![Build Status](https://travis-ci.org/chrisbeach/discourse-donations.svg?branch=master)](https://travis-ci.org/choiceaustralia/discourse-donations)
Accept donations in Discourse! Integrates with [Stripe](https://stripe.com). Accept donations in Discourse! Integrates with [Stripe](https://stripe.com).

View File

@ -4,21 +4,22 @@ module DiscourseDonations
class ChargesController < ActionController::Base class ChargesController < ActionController::Base
include CurrentUser include CurrentUser
skip_before_filter :verify_authenticity_token, only: [:create] protect_from_forgery prepend: true
protect_from_forgery with: :exception
skip_before_action :verify_authenticity_token, only: [:create]
def create def create
params.permit(:name, :username, :email, :password, :stripeToken, :amount, :create_account)
output = { 'messages' => [], 'rewards' => [] } output = { 'messages' => [], 'rewards' => [] }
if create_account if create_account
if !email.present? || !params[:username].present? if !email.present? || !user_params[:username].present?
output['messages'] << I18n.t('login.missing_user_field') output['messages'] << I18n.t('login.missing_user_field')
end end
if params[:password] && params[:password].length > User.max_password_length if user_params[:password] && user_params[:password].length > User.max_password_length
output['messages'] << I18n.t('login.password_too_long') output['messages'] << I18n.t('login.password_too_long')
end end
if params[:username] && ::User.reserved_username?(params[:username]) if user_params[:username] && ::User.reserved_username?(user_params[:username])
output['messages'] << I18n.t('login.reserved_username') output['messages'] << I18n.t('login.reserved_username')
end end
end end
@ -30,7 +31,7 @@ module DiscourseDonations
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options) payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
begin begin
charge = payment.charge(email, params) charge = payment.charge(email, opts: user_params)
rescue ::Stripe::CardError => e rescue ::Stripe::CardError => e
err = e.json_body[:error] err = e.json_body[:error]
@ -49,7 +50,7 @@ module DiscourseDonations
output['rewards'] << { type: :badge, name: badge_name } if badge_name output['rewards'] << { type: :badge, name: badge_name } if badge_name
if create_account && email.present? if create_account && email.present?
args = params.slice(:email, :username, :password, :name).merge(rewards: output['rewards']) args = user_params.to_h.slice(:email, :username, :password, :name).merge(rewards: output['rewards'])
Jobs.enqueue(:donation_user, args) Jobs.enqueue(:donation_user, args)
end end
end end
@ -60,7 +61,7 @@ module DiscourseDonations
private private
def create_account def create_account
params[:create_account] == 'true' && SiteSetting.discourse_donations_enable_create_accounts user_params[:create_account] == 'true' && SiteSetting.discourse_donations_enable_create_accounts
end end
def reward?(payment) def reward?(payment)
@ -86,8 +87,12 @@ module DiscourseDonations
} }
end end
def user_params
params.permit(:name, :username, :email, :password, :stripeToken, :amount, :create_account)
end
def email def email
params[:email] || current_user.try(:email) user_params[:email] || current_user.try(:email)
end end
end end
end end

View File

@ -23,20 +23,23 @@ module DiscourseDonations
SiteSetting.stubs(:discourse_donations_currency).returns('AUD') SiteSetting.stubs(:discourse_donations_currency).returns('AUD')
end end
# Workaround for rails-5 issue. See https://github.com/thoughtbot/shoulda-matchers/issues/1018#issuecomment-315876453
let(:allowed_params) { {create_account: 'true', email: 'email@example.com', password: 'secret', username: 'mr-pink', name: 'kirsten', amount: 100, stripeToken: 'rrurrrurrrrr'} }
it 'whitelists the params' do it 'whitelists the params' do
params = { create_account: 'true', email: 'email@example.com', password: 'secret', username: 'mr-pink', name: 'kirsten', amount: 100, stripeToken: 'rrurrrurrrrr' } should permit(:name, :username, :email, :password, :create_account).
should permit(:name, :username, :email, :password, :create_account).for(:create, params: params) for(:create, params: { params: allowed_params })
end end
it 'responds ok for anonymous users' do it 'responds ok for anonymous users' do
post :create, { email: 'foobar@example.com' } post :create, params: { email: 'foobar@example.com' }
expect(body['messages']).to include(I18n.t('donations.payment.success')) expect(body['messages']).to include(I18n.t('donations.payment.success'))
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'does not expect a username or email if accounts are not being created' do it 'does not expect a username or email if accounts are not being created' do
current_user = log_in(:coding_horror) current_user = log_in(:coding_horror)
post :create, { create_account: 'false' } post :create, params: { create_account: 'false' }
expect(body['messages']).to include(I18n.t('donations.payment.success')) expect(body['messages']).to include(I18n.t('donations.payment.success'))
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
@ -51,17 +54,17 @@ module DiscourseDonations
end end
it 'does not create user accounts' do it 'does not create user accounts' do
post :create, params post :create, params: params
end end
it 'does not create user accounts if the user is logged in' do it 'does not create user accounts if the user is logged in' do
log_in :coding_horror log_in :coding_horror
post :create, params post :create, params: params
end end
it 'does not create user accounts when settings are disabled and params are not' do it 'does not create user accounts when settings are disabled and params are not' do
log_in :coding_horror log_in :coding_horror
post :create, params.merge(create_account: true, email: 'email@example.com', password: 'secret', username: 'mr-brown', name: 'hacker-guy') post :create, params: params.merge(create_account: true, email: 'email@example.com', password: 'secret', username: 'mr-brown', name: 'hacker-guy')
end end
end end
@ -74,7 +77,7 @@ module DiscourseDonations
end end
it 'enqueues the user account create' do it 'enqueues the user account create' do
post :create, params post :create, params: params
end end
end end
end end
@ -85,19 +88,19 @@ module DiscourseDonations
before { SiteSetting.stubs(:discourse_donations_enable_create_accounts).returns(true) } before { SiteSetting.stubs(:discourse_donations_enable_create_accounts).returns(true) }
describe 'requires an email' do describe 'requires an email' do
before { post :create, params.merge(email: '') } before { post :create, params: params.merge(email: '') }
include_examples 'failure response', 'login.missing_user_field' include_examples 'failure response', 'login.missing_user_field'
end end
describe 'requires a username' do describe 'requires a username' do
before { post :create, params.merge(username: '') } before { post :create, params: params.merge(username: '') }
include_examples 'failure response', 'login.missing_user_field' include_examples 'failure response', 'login.missing_user_field'
end end
describe 'reserved usernames' do describe 'reserved usernames' do
before do before do
User.expects(:reserved_username?).returns(true) User.expects(:reserved_username?).returns(true)
post :create, params post :create, params: params
end end
include_examples 'failure response', 'login.reserved_username' include_examples 'failure response', 'login.reserved_username'
@ -106,7 +109,7 @@ module DiscourseDonations
describe 'minimum password length' do describe 'minimum password length' do
before do before do
User.expects(:max_password_length).returns(params[:password].length - 1) User.expects(:max_password_length).returns(params[:password].length - 1)
post :create, params post :create, params: params
end end
include_examples 'failure response', 'login.password_too_long' include_examples 'failure response', 'login.password_too_long'
@ -119,7 +122,7 @@ module DiscourseDonations
shared_examples 'no rewards' do shared_examples 'no rewards' do
it 'has no rewards' do it 'has no rewards' do
post :create, params post :create, params: params
expect(body['rewards']).to be_empty expect(body['rewards']).to be_empty
end end
end end
@ -150,7 +153,7 @@ module DiscourseDonations
end end
include_examples 'no rewards' do include_examples 'no rewards' do
let(:params) { nil } let(:params) { {} }
before do before do
stripe.stubs(:create).returns({ 'paid' => true }) stripe.stubs(:create).returns({ 'paid' => true })