mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 23:03:38 +08:00
79 lines
2.6 KiB
Ruby
79 lines
2.6 KiB
Ruby
require 'spec_helper'
|
|
|
|
describe UserBadgesController do
|
|
let(:user) { Fabricate(:user) }
|
|
let(:badge) { Fabricate(:badge) }
|
|
|
|
context 'index' do
|
|
let!(:user_badge) { UserBadge.create(badge: badge, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
|
|
|
|
it 'requires username to be specified' do
|
|
expect { xhr :get, :index }.to raise_error
|
|
end
|
|
|
|
it 'returns the user\'s badges' do
|
|
xhr :get, :index, username: user.username
|
|
|
|
response.status.should == 200
|
|
parsed = JSON.parse(response.body)
|
|
parsed["user_badges"].length.should == 1
|
|
end
|
|
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
|
|
StaffActionLogger.any_instance.expects(:log_badge_grant).once
|
|
xhr :post, :create, badge_id: badge.id, username: user.username
|
|
response.status.should == 200
|
|
user_badge = UserBadge.where(user: user, badge: badge).first
|
|
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)
|
|
StaffActionLogger.any_instance.expects(:log_badge_grant).never
|
|
xhr :post, :create, badge_id: badge.id, username: user.username, api_key: api_key.key
|
|
response.status.should == 200
|
|
user_badge = UserBadge.where(user: user, badge: badge).first
|
|
user_badge.should be_present
|
|
user_badge.granted_by.should eq(Discourse.system_user)
|
|
end
|
|
end
|
|
|
|
context 'destroy' do
|
|
let!(:user_badge) { UserBadge.create(badge: badge, user: user, granted_by: Discourse.system_user, granted_at: Time.now) }
|
|
|
|
it 'checks that the user is authorized to revoke a badge' do
|
|
xhr :delete, :destroy, id: user_badge.id
|
|
response.status.should == 403
|
|
end
|
|
|
|
it 'revokes the badge' do
|
|
log_in :admin
|
|
StaffActionLogger.any_instance.expects(:log_badge_revoke).once
|
|
xhr :delete, :destroy, id: user_badge.id
|
|
response.status.should == 200
|
|
UserBadge.where(id: user_badge.id).first.should be_nil
|
|
end
|
|
end
|
|
end
|