add the user to a group

This commit is contained in:
Rimian Perkins 2017-04-24 11:44:55 +10:00
parent 1c87a87bd6
commit 061ea69434
5 changed files with 43 additions and 10 deletions

View File

@ -18,10 +18,12 @@ module DiscourseDonations
if reward_user?(payment)
reward = DiscourseDonations::Rewards.new(current_user)
group_name = SiteSetting.discourse_donations_reward_group_name
if reward.add_to_group(group_name)
response['rewards'] << { type: :group, name: group_name }
end
if reward.grant_badge(badge_name)
response['rewards'] << { type: :badge, name: badge_name }
end
end
render :json => response
@ -33,6 +35,14 @@ module DiscourseDonations
payment.present? && payment.successful? && current_user.present?
end
def group_name
SiteSetting.discourse_donations_reward_group_name
end
def badge_name
SiteSetting.discourse_donations_reward_badge_name
end
def secret_key
SiteSetting.discourse_donations_secret_key
end

View File

@ -7,10 +7,15 @@ module DiscourseDonations
end
def add_to_group(name)
group = ::Group.find_by_name(name)
return false if group.nil?
group.add(@user)
group.present?
grp = ::Group.find_by_name(name)
return if grp.nil?
grp.add(@user)
end
def grant_badge(name)
badge = ::Badge.find_by_name(name)
return if badge.nil?
BadgeGranter.grant(badge, @user)
end
end
end

View File

@ -15,7 +15,7 @@ plugins:
discourse_donations_hide_zip_code:
default: true
client: true
discourse_donations_reward_badge:
discourse_donations_reward_badge_name:
client: false
default: 'Donation'
discourse_donations_reward_group_name:

View File

@ -29,12 +29,15 @@ module DiscourseDonations
describe 'rewards' do
let(:group_name) { 'Zasch' }
let(:badge_name) { 'Beanie' }
let(:response_rewards) { JSON.parse(response.body)['rewards'] }
let(:stripe) { ::Stripe::Charge }
before do
SiteSetting.stubs(:discourse_donations_reward_group_name).returns(group_name)
SiteSetting.stubs(:discourse_donations_reward_badge_name).returns(badge_name)
Fabricate(:group, name: SiteSetting.discourse_donations_reward_group_name)
Fabricate(:badge, name: SiteSetting.discourse_donations_reward_badge_name)
log_in :coding_horror
end
@ -46,7 +49,12 @@ module DiscourseDonations
it 'awards a group' do
post :create
expect(response_rewards.first).to eq({'type' => 'group', 'name' => group_name})
expect(response_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})
end
end
end

View File

@ -9,14 +9,24 @@ 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)
expect(subject.add_to_group(grp.name)).to eq true
subject.add_to_group(grp.name)
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
grp.expects(:add).never
expect(subject.add_to_group(grp.name)).to be_falsy
end
it 'grants the user a badge'
it 'grants the user a badge' do
badge = Fabricate(:badge)
BadgeGranter.expects(:grant).with(badge, user)
subject.grant_badge(badge.name)
end
it 'does not grant the user a badge' do
BadgeGranter.expects(:grant).never
expect(subject.grant_badge('does not exist')).to be_falsy
end
end
end