2014-03-05 07:52:20 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe UserBadgesController do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:badge) { Fabricate(:badge) }
|
|
|
|
|
|
|
|
context 'index' do
|
2014-04-16 06:34:27 -04:00
|
|
|
let!(:user_badge) { UserBadge.create(badge: badge, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2014-04-16 10:56:11 -04:00
|
|
|
it 'requires username or badge_id to be specified' do
|
2014-03-05 07:52:20 -05:00
|
|
|
expect { xhr :get, :index }.to raise_error
|
|
|
|
end
|
|
|
|
|
2014-04-16 10:56:11 -04:00
|
|
|
it 'returns user_badges for a user' do
|
2014-03-05 07:52:20 -05:00
|
|
|
xhr :get, :index, username: user.username
|
|
|
|
|
|
|
|
response.status.should == 200
|
|
|
|
parsed = JSON.parse(response.body)
|
|
|
|
parsed["user_badges"].length.should == 1
|
|
|
|
end
|
2014-04-16 10:56:11 -04:00
|
|
|
|
|
|
|
it 'returns user_badges for a badge' do
|
|
|
|
xhr :get, :index, badge_id: badge.id
|
|
|
|
|
|
|
|
response.status.should == 200
|
|
|
|
parsed = JSON.parse(response.body)
|
|
|
|
parsed["user_badges"].length.should == 1
|
|
|
|
end
|
2014-05-21 03:22:42 -04:00
|
|
|
|
|
|
|
it 'includes counts when passed the aggregate argument' do
|
2014-06-09 21:23:18 -04:00
|
|
|
xhr :get, :index, username: user.username, grouped: true
|
2014-05-21 03:22:42 -04:00
|
|
|
|
|
|
|
response.status.should == 200
|
|
|
|
parsed = JSON.parse(response.body)
|
|
|
|
parsed["user_badges"].first.has_key?('count').should be_true
|
|
|
|
end
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'create' do
|
|
|
|
it 'requires username to be specified' do
|
|
|
|
expect { xhr :post, :create, badge_id: badge.id }.to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow regular users to grant badges' do
|
|
|
|
log_in_user Fabricate(:user)
|
|
|
|
xhr :post, :create, badge_id: badge.id, username: user.username
|
|
|
|
response.status.should == 403
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'grants badges from staff' do
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
log_in_user admin
|
2014-03-19 15:30:12 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_grant).once
|
2014-03-05 07:52:20 -05:00
|
|
|
xhr :post, :create, badge_id: badge.id, username: user.username
|
|
|
|
response.status.should == 200
|
2014-05-06 09:41:59 -04:00
|
|
|
user_badge = UserBadge.find_by(user: user, badge: badge)
|
2014-03-05 07:52:20 -05:00
|
|
|
user_badge.should be_present
|
|
|
|
user_badge.granted_by.should eq(admin)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not grant badges from regular api calls' do
|
|
|
|
Fabricate(:api_key, user: user)
|
|
|
|
xhr :post, :create, badge_id: badge.id, username: user.username, api_key: user.api_key.key
|
|
|
|
response.status.should == 403
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'grants badges from master api calls' do
|
|
|
|
api_key = Fabricate(:api_key)
|
2014-03-19 15:30:12 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_grant).never
|
2014-05-22 18:13:25 -04:00
|
|
|
xhr :post, :create, badge_id: badge.id, username: user.username, api_key: api_key.key, api_username: "system"
|
2014-03-05 07:52:20 -05:00
|
|
|
response.status.should == 200
|
2014-05-06 09:41:59 -04:00
|
|
|
user_badge = UserBadge.find_by(user: user, badge: badge)
|
2014-03-05 07:52:20 -05:00
|
|
|
user_badge.should be_present
|
|
|
|
user_badge.granted_by.should eq(Discourse.system_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'destroy' do
|
2014-04-16 06:34:27 -04:00
|
|
|
let!(:user_badge) { UserBadge.create(badge: badge, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
|
2014-03-05 07:52:20 -05:00
|
|
|
|
|
|
|
it 'checks that the user is authorized to revoke a badge' do
|
2014-04-16 06:34:27 -04:00
|
|
|
xhr :delete, :destroy, id: user_badge.id
|
2014-03-05 07:52:20 -05:00
|
|
|
response.status.should == 403
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'revokes the badge' do
|
|
|
|
log_in :admin
|
2014-03-19 15:30:12 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_revoke).once
|
2014-04-16 06:34:27 -04:00
|
|
|
xhr :delete, :destroy, id: user_badge.id
|
2014-03-05 07:52:20 -05:00
|
|
|
response.status.should == 200
|
2014-05-06 09:41:59 -04:00
|
|
|
UserBadge.find_by(id: user_badge.id).should be_nil
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|