fix up name space collisions

This commit is contained in:
Rimian Perkins 2017-05-04 12:39:45 +10:00
parent 256b44ad60
commit 690ee1d25b
2 changed files with 19 additions and 16 deletions

View File

@ -7,26 +7,30 @@ module DiscourseDonations
skip_before_filter :verify_authenticity_token, only: [:create] skip_before_filter :verify_authenticity_token, only: [:create]
def create def create
output = { 'messages' => [], 'rewards' => [] }
if create_account && (email.nil? || email.empty?) if create_account && (email.nil? || email.empty?)
response = {'messages' => ['Please enter your email address']} output['messages'] << 'Please enter your email address'
elsif create_account && params[:username].nil? elsif create_account && params[:username].nil?
response = {'messages' => ['Please enter a username']} output['messages'] << 'Please enter a username'
else else
payment = DiscourseDonations::Stripe.new(secret_key, stripe_options) payment = DiscourseDonations::Stripe.new(secret_key, stripe_options)
response = payment.charge(email, params) charge = payment.charge(email, params)
response['messages'] = [response['outcome']['seller_message']] output['messages'] = [charge['outcome']['seller_message']]
end end
response['rewards'] = [] if payment.nil?
render(:json => output) and return
end
if reward?(payment) if reward?(payment)
if current_user.present? if current_user.present?
reward = DiscourseDonations::Rewards.new(current_user) reward = DiscourseDonations::Rewards.new(current_user)
if reward.add_to_group(group_name) if reward.add_to_group(group_name)
response['rewards'] << { type: :group, name: group_name } output['rewards'] << { type: :group, name: group_name }
end end
if reward.grant_badge(badge_name) if reward.grant_badge(badge_name)
response['rewards'] << { type: :badge, name: badge_name } output['rewards'] << { type: :badge, name: badge_name }
end end
elsif email.present? elsif email.present?
if group_name.present? if group_name.present?
@ -40,7 +44,7 @@ module DiscourseDonations
end end
end end
render :json => response render :json => output
end end
private private

View File

@ -14,14 +14,13 @@ module DiscourseDonations
it 'responds ok for anonymous users' do it 'responds ok for anonymous users' do
post :create, { email: 'foobar@example.com' } post :create, { email: 'foobar@example.com' }
expect(body['messages']).to include(body['outcome']['seller_message']) expect(body['messages']).to include('Payment complete.')
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'responds ok when the email is empty' do it 'responds with a message when the email is empty' do
post :create, { create_account: 'true', email: '' } post :create, { create_account: 'true', email: '' }
expect(body['messages']).to include('Please enter your email address') expect(body['messages']).to include('Please enter your email address')
expect(response).to have_http_status(200)
end end
it 'responds ok when the email is empty' do it 'responds ok when the email is empty' do
@ -46,7 +45,7 @@ module DiscourseDonations
describe 'rewards' do describe 'rewards' do
let(:group_name) { 'Zasch' } let(:group_name) { 'Zasch' }
let(:badge_name) { 'Beanie' } let(:badge_name) { 'Beanie' }
let(:response_rewards) { JSON.parse(response.body)['rewards'] } let(:body) { JSON.parse(response.body) }
let(:stripe) { ::Stripe::Charge } let(:stripe) { ::Stripe::Charge }
let!(:grp) { Fabricate(:group, name: group_name) } let!(:grp) { Fabricate(:group, name: group_name) }
let!(:badge) { Fabricate(:badge, name: badge_name) } let!(:badge) { Fabricate(:badge, name: badge_name) }
@ -61,7 +60,7 @@ module DiscourseDonations
it 'has no rewards' do it 'has no rewards' do
post :create post :create
expect(response_rewards).to be_empty expect(body['rewards']).to be_empty
end end
it 'stores the email in group:add and badge:grant and adds them' do it 'stores the email in group:add and badge:grant and adds them' do
@ -81,17 +80,17 @@ module DiscourseDonations
it 'has no rewards' do it 'has no rewards' do
stripe.expects(:create).returns({ 'outcome' => { 'seller_message' => 'bummer' } }) stripe.expects(:create).returns({ 'outcome' => { 'seller_message' => 'bummer' } })
post :create post :create
expect(response_rewards).to be_empty expect(body['rewards']).to be_empty
end end
it 'awards a group' do it 'awards a group' do
post :create post :create
expect(response_rewards).to include({'type' => 'group', 'name' => group_name}) expect(body['rewards']).to include({'type' => 'group', 'name' => group_name})
end end
it 'awards a badge' do it 'awards a badge' do
post :create post :create
expect(response_rewards).to include({'type' => 'badge', 'name' => badge_name}) expect(body['rewards']).to include({'type' => 'badge', 'name' => badge_name})
end end
end end
end end