From f383a0c4f9ef4094e53e760b022e09ada928df67 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Fri, 21 Apr 2017 14:26:53 +1000 Subject: [PATCH] add rewards to response --- .../discourse_donations/charges_controller.rb | 6 +++-- app/services/discourse_donations/rewards.rb | 4 +++- .../charges_controller_spec.rb | 24 +++++++++++++++---- .../discourse_donations/rewards_spec.rb | 7 +++++- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/controllers/discourse_donations/charges_controller.rb b/app/controllers/discourse_donations/charges_controller.rb index c7a04f8..18919e5 100644 --- a/app/controllers/discourse_donations/charges_controller.rb +++ b/app/controllers/discourse_donations/charges_controller.rb @@ -14,12 +14,14 @@ module DiscourseDonations response = {} end - response['rewards'] = [] + response['rewards'] = {} if reward_user?(payment) reward = DiscourseDonations::Rewards.new(current_user) group_name = SiteSetting.discourse_donations_reward_group - reward.add_to_group(group_name) if group_name.present? + if reward.add_to_group(group_name) + response['rewards']['groups'] = [SiteSetting.discourse_donations_reward_group] + end end render :json => response diff --git a/app/services/discourse_donations/rewards.rb b/app/services/discourse_donations/rewards.rb index 8e73448..0e5adbb 100644 --- a/app/services/discourse_donations/rewards.rb +++ b/app/services/discourse_donations/rewards.rb @@ -8,7 +8,9 @@ module DiscourseDonations def add_to_group(name) group = ::Group.find_by_name(name) - group.add(@user) if group.present? + return false if group.nil? + group.add(@user) + group.present? end end end diff --git a/spec/controllers/discourse_donations/charges_controller_spec.rb b/spec/controllers/discourse_donations/charges_controller_spec.rb index 978c23b..a3777f2 100644 --- a/spec/controllers/discourse_donations/charges_controller_spec.rb +++ b/spec/controllers/discourse_donations/charges_controller_spec.rb @@ -27,10 +27,26 @@ module DiscourseDonations expect(response).to have_http_status(200) end - it 'has no rewards' do - current_user = log_in(:coding_horror) - post :create - expect(JSON.parse(response.body)['rewards']).to eq([]) + describe 'rewards' do + before do + SiteSetting.stubs(:discourse_donations_reward_group).returns('Skimby') + Fabricate(:group, name: SiteSetting.discourse_donations_reward_group) + log_in :coding_horror + end + + let(:response_rewards) { JSON.parse(response.body)['rewards'] } + let(:stripe) { ::Stripe::Charge } + + it 'has no rewards' do + stripe.expects(:create).returns({ bummer: true }) + post :create + expect(response_rewards).to eq({}) + end + + it 'awards a group' do + post :create + expect(response_rewards['groups']).to eq([SiteSetting.discourse_donations_reward_group]) + end end end end diff --git a/spec/services/discourse_donations/rewards_spec.rb b/spec/services/discourse_donations/rewards_spec.rb index 7c40afa..ee3fbc6 100644 --- a/spec/services/discourse_donations/rewards_spec.rb +++ b/spec/services/discourse_donations/rewards_spec.rb @@ -9,7 +9,12 @@ module DiscourseDonations it 'adds the user to a group' do Group.expects(:find_by_name).with(grp.name).returns(grp) grp.expects(:add).with(user) - subject.add_to_group(grp.name) + expect(subject.add_to_group(grp.name)).to eq true + end + + it 'does not add the user to a group' do + Group.expects(:find_by_name).with(grp.name).returns(nil) + expect(subject.add_to_group(grp.name)).to eq false end it 'grants the user a badge'