From 690ee1d25bfb4fd8dad57660546135b680948ff0 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Thu, 4 May 2017 12:39:45 +1000 Subject: [PATCH] fix up name space collisions --- .../discourse_donations/charges_controller.rb | 20 +++++++++++-------- .../charges_controller_spec.rb | 15 +++++++------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/app/controllers/discourse_donations/charges_controller.rb b/app/controllers/discourse_donations/charges_controller.rb index 8865871..c26d96c 100644 --- a/app/controllers/discourse_donations/charges_controller.rb +++ b/app/controllers/discourse_donations/charges_controller.rb @@ -7,26 +7,30 @@ module DiscourseDonations skip_before_filter :verify_authenticity_token, only: [:create] def create + output = { 'messages' => [], 'rewards' => [] } + 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? - response = {'messages' => ['Please enter a username']} + output['messages'] << 'Please enter a username' else payment = DiscourseDonations::Stripe.new(secret_key, stripe_options) - response = payment.charge(email, params) - response['messages'] = [response['outcome']['seller_message']] + charge = payment.charge(email, params) + output['messages'] = [charge['outcome']['seller_message']] end - response['rewards'] = [] + if payment.nil? + render(:json => output) and return + end if reward?(payment) if current_user.present? reward = DiscourseDonations::Rewards.new(current_user) if reward.add_to_group(group_name) - response['rewards'] << { type: :group, name: group_name } + output['rewards'] << { type: :group, name: group_name } end if reward.grant_badge(badge_name) - response['rewards'] << { type: :badge, name: badge_name } + output['rewards'] << { type: :badge, name: badge_name } end elsif email.present? if group_name.present? @@ -40,7 +44,7 @@ module DiscourseDonations end end - render :json => response + render :json => output end private diff --git a/spec/controllers/discourse_donations/charges_controller_spec.rb b/spec/controllers/discourse_donations/charges_controller_spec.rb index 87a8fb3..72e5320 100644 --- a/spec/controllers/discourse_donations/charges_controller_spec.rb +++ b/spec/controllers/discourse_donations/charges_controller_spec.rb @@ -14,14 +14,13 @@ module DiscourseDonations it 'responds ok for anonymous users' do 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) 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: '' } expect(body['messages']).to include('Please enter your email address') - expect(response).to have_http_status(200) end it 'responds ok when the email is empty' do @@ -46,7 +45,7 @@ module DiscourseDonations describe 'rewards' do let(:group_name) { 'Zasch' } let(:badge_name) { 'Beanie' } - let(:response_rewards) { JSON.parse(response.body)['rewards'] } + let(:body) { JSON.parse(response.body) } let(:stripe) { ::Stripe::Charge } let!(:grp) { Fabricate(:group, name: group_name) } let!(:badge) { Fabricate(:badge, name: badge_name) } @@ -61,7 +60,7 @@ module DiscourseDonations it 'has no rewards' do post :create - expect(response_rewards).to be_empty + expect(body['rewards']).to be_empty end 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 stripe.expects(:create).returns({ 'outcome' => { 'seller_message' => 'bummer' } }) post :create - expect(response_rewards).to be_empty + expect(body['rewards']).to be_empty end it 'awards a group' do post :create - expect(response_rewards).to include({'type' => 'group', 'name' => group_name}) + expect(body['rewards']).to include({'type' => 'group', 'name' => group_name}) end it 'awards a badge' do 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