From 9ca0f099166af2d7f0d56bf61279eac06ff77f35 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Mon, 8 May 2017 13:38:14 +1000 Subject: [PATCH] job grants badge --- app/jobs/regular/donation_user.rb | 5 ++++- spec/jobs/award_group_spec.rb | 1 + spec/jobs/grant_badge_spec.rb | 1 + spec/jobs/regular/donation_user_spec.rb | 22 ++++++++++++++++++---- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/app/jobs/regular/donation_user.rb b/app/jobs/regular/donation_user.rb index b8f4943..031b63f 100644 --- a/app/jobs/regular/donation_user.rb +++ b/app/jobs/regular/donation_user.rb @@ -2,7 +2,10 @@ module Jobs class DonationUser < ::Jobs::Base def execute(args) - User.create(args) + user = User.create!(args) + if args[:rewards].present? + DiscourseDonations::Rewards.new(user).grant_badge(args[:rewards][:name]) + end end end end diff --git a/spec/jobs/award_group_spec.rb b/spec/jobs/award_group_spec.rb index 965d9bc..c1af29a 100644 --- a/spec/jobs/award_group_spec.rb +++ b/spec/jobs/award_group_spec.rb @@ -1,3 +1,4 @@ +require 'rails_helper' RSpec.describe Jobs::AwardGroup, type: :job do let(:users) { [Fabricate(:user), Fabricate(:user)] } diff --git a/spec/jobs/grant_badge_spec.rb b/spec/jobs/grant_badge_spec.rb index d9b75f2..2e880da 100644 --- a/spec/jobs/grant_badge_spec.rb +++ b/spec/jobs/grant_badge_spec.rb @@ -1,3 +1,4 @@ +require 'rails_helper' RSpec.describe Jobs::GrantBadge, type: :job do let(:users) { [Fabricate(:user), Fabricate(:user)] } diff --git a/spec/jobs/regular/donation_user_spec.rb b/spec/jobs/regular/donation_user_spec.rb index 7e1fde7..934e147 100644 --- a/spec/jobs/regular/donation_user_spec.rb +++ b/spec/jobs/regular/donation_user_spec.rb @@ -1,10 +1,24 @@ +require 'rails_helper' RSpec.describe Jobs::DonationUser, type: :job do - it { should respond_to(:execute).with(1).arguments } + let(:args) { { email: 'fsfs4@example.com', username: 'sxsomething', name: 'ssbrian', password: 'ssecret-yo' } } + + before do + SiteSetting.stubs(:enable_badges).returns(true) + end it 'creates a new user' do - args = { email: 'foo@example.com', username: 'something' } - User.expects(:create).with(args) - subject.execute(args) + expect{ subject.execute(args) }.to change{ User.count }.by(1) + end + + describe 'rewards' do + let(:user) { Fabricate(:user, args) } + let(:badge) { Fabricate(:badge) } + + it 'has the badge' do + User.expects(:create!).returns(user) + subject.execute(args.merge(rewards: { type: 'badge', name: badge.name })) + expect(user.badges).to include(badge) + end end end