2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-03-05 07:52:20 -05:00
|
|
|
|
|
|
|
describe UserBadgesController do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:badge) { Fabricate(:badge) }
|
2014-07-22 21:42:24 -04:00
|
|
|
|
|
|
|
context 'index' do
|
2015-02-25 19:55:17 -05:00
|
|
|
it 'does not leak private info' do
|
2014-07-22 21:42:24 -04:00
|
|
|
badge = Fabricate(:badge, target_posts: true, show_posts: false)
|
|
|
|
p = create_post
|
|
|
|
UserBadge.create(badge: badge, user: user, post_id: p.id, granted_by_id: -1, granted_at: Time.now)
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
get :index, params: { badge_id: badge.id }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2016-01-18 01:59:07 -05:00
|
|
|
|
2014-07-22 21:42:24 -04:00
|
|
|
parsed = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(parsed["topics"]).to eq(nil)
|
2016-01-18 01:59:07 -05:00
|
|
|
expect(parsed["badges"].length).to eq(1)
|
|
|
|
expect(parsed["user_badge_info"]["user_badges"][0]["post_id"]).to eq(nil)
|
2014-07-22 21:42:24 -04:00
|
|
|
end
|
|
|
|
end
|
2014-03-05 07:52:20 -05:00
|
|
|
|
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
get :index, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
2014-04-16 10:56:11 -04:00
|
|
|
it 'returns user_badges for a user' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :username, params: { username: user.username }, format: :json
|
2014-03-05 07:52:20 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2014-03-05 07:52:20 -05:00
|
|
|
parsed = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(parsed["user_badges"].length).to eq(1)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
2014-04-16 10:56:11 -04:00
|
|
|
|
|
|
|
it 'returns user_badges for a badge' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :index, params: { badge_id: badge.id }, format: :json
|
2014-04-16 10:56:11 -04:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2014-04-16 10:56:11 -04:00
|
|
|
parsed = JSON.parse(response.body)
|
2016-01-18 01:59:07 -05:00
|
|
|
expect(parsed["user_badge_info"]["user_badges"].length).to eq(1)
|
2014-04-16 10:56:11 -04:00
|
|
|
end
|
2014-05-21 03:22:42 -04:00
|
|
|
|
|
|
|
it 'includes counts when passed the aggregate argument' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :username, params: {
|
|
|
|
username: user.username, grouped: true
|
|
|
|
}, format: :json
|
2014-05-21 03:22:42 -04:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2014-05-21 03:22:42 -04:00
|
|
|
parsed = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(parsed["user_badges"].first.has_key?('count')).to eq(true)
|
2014-05-21 03:22:42 -04:00
|
|
|
end
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'create' do
|
|
|
|
it 'requires username to be specified' do
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :create, params: { badge_id: badge.id }, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not allow regular users to grant badges' do
|
|
|
|
log_in_user Fabricate(:user)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
badge_id: badge.id, username: user.username
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(403)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'grants badges from staff' do
|
|
|
|
admin = Fabricate(:admin)
|
2017-08-31 00:06:56 -04:00
|
|
|
post_1 = create_post
|
2015-02-25 20:24:21 -05:00
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
log_in_user admin
|
2015-02-25 20:24:21 -05:00
|
|
|
|
2014-03-19 15:30:12 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_badge_grant).once
|
2015-02-25 20:24:21 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
badge_id: badge.id,
|
|
|
|
username: user.username,
|
|
|
|
reason: Discourse.base_url + post_1.url
|
|
|
|
}, format: :json
|
2015-02-25 20:24:21 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2015-02-25 20:24:21 -05:00
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
user_badge = UserBadge.find_by(user: user, badge: badge)
|
2015-02-25 20:24:21 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user_badge).to be_present
|
|
|
|
expect(user_badge.granted_by).to eq(admin)
|
2017-08-31 00:06:56 -04:00
|
|
|
expect(user_badge.post_id).to eq(post_1.id)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not grant badges from regular api calls' do
|
|
|
|
Fabricate(:api_key, user: user)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
badge_id: badge.id, username: user.username, api_key: user.api_key.key
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(403)
|
2014-03-05 07:52:20 -05:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
badge_id: badge.id, username: user.username, api_key: api_key.key, api_username: "system"
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
2014-05-06 09:41:59 -04:00
|
|
|
user_badge = UserBadge.find_by(user: user, badge: badge)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(user_badge).to be_present
|
|
|
|
expect(user_badge.granted_by).to eq(Discourse.system_user)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
2015-02-25 19:55:17 -05:00
|
|
|
|
|
|
|
it 'will trigger :user_badge_granted' do
|
|
|
|
log_in :admin
|
2016-09-13 04:03:17 -04:00
|
|
|
user
|
2017-06-01 03:19:43 -04:00
|
|
|
|
|
|
|
event = DiscourseEvent.track_events do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
badge_id: badge.id, username: user.username
|
|
|
|
}, format: :json
|
2017-06-01 03:19:43 -04:00
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:user_badge_granted)
|
2015-02-25 19:55:17 -05:00
|
|
|
end
|
2014-03-05 07:52:20 -05:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { id: user_badge.id }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(403)
|
2014-03-05 07:52:20 -05:00
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { id: user_badge.id }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(UserBadge.find_by(id: user_badge.id)).to eq(nil)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
2015-02-25 19:55:17 -05:00
|
|
|
|
|
|
|
it 'will trigger :user_badge_removed' do
|
|
|
|
log_in :admin
|
2016-09-13 04:03:17 -04:00
|
|
|
|
2017-06-01 03:19:43 -04:00
|
|
|
event = DiscourseEvent.track_events do
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { id: user_badge.id }, format: :json
|
2017-06-01 03:19:43 -04:00
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:user_badge_removed)
|
2015-02-25 19:55:17 -05:00
|
|
|
end
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
end
|