add rewards to response

This commit is contained in:
Rimian Perkins 2017-04-21 14:26:53 +10:00
parent c3fe90f4d1
commit f383a0c4f9
4 changed files with 33 additions and 8 deletions

View File

@ -14,12 +14,14 @@ module DiscourseDonations
response = {} response = {}
end end
response['rewards'] = [] response['rewards'] = {}
if reward_user?(payment) if reward_user?(payment)
reward = DiscourseDonations::Rewards.new(current_user) reward = DiscourseDonations::Rewards.new(current_user)
group_name = SiteSetting.discourse_donations_reward_group 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 end
render :json => response render :json => response

View File

@ -8,7 +8,9 @@ module DiscourseDonations
def add_to_group(name) def add_to_group(name)
group = ::Group.find_by_name(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 end
end end

View File

@ -27,10 +27,26 @@ module DiscourseDonations
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'has no rewards' do describe 'rewards' do
current_user = log_in(:coding_horror) before do
post :create SiteSetting.stubs(:discourse_donations_reward_group).returns('Skimby')
expect(JSON.parse(response.body)['rewards']).to eq([]) 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 end
end end

View File

@ -9,7 +9,12 @@ module DiscourseDonations
it 'adds the user to a group' do it 'adds the user to a group' do
Group.expects(:find_by_name).with(grp.name).returns(grp) Group.expects(:find_by_name).with(grp.name).returns(grp)
grp.expects(:add).with(user) 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 end
it 'grants the user a badge' it 'grants the user a badge'