2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-03-05 07:52:20 -05:00
|
|
|
|
|
|
|
describe Admin::BadgesController do
|
|
|
|
|
|
|
|
context "while logged in as an admin" do
|
|
|
|
let!(:user) { log_in(:admin) }
|
|
|
|
let!(:badge) { Fabricate(:badge) }
|
|
|
|
|
2014-07-29 18:35:15 -04:00
|
|
|
context 'index' do
|
|
|
|
it 'returns badge index' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :index, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2014-07-29 18:35:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-27 19:03:00 -04:00
|
|
|
context 'preview' do
|
|
|
|
it 'allows preview enable_badge_sql is enabled' do
|
|
|
|
SiteSetting.enable_badge_sql = true
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :preview, params: {
|
|
|
|
sql: 'select id as user_id, created_at granted_at from users'
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(JSON.parse(response.body)["grant_count"]).to be > 0
|
2016-07-27 19:03:00 -04:00
|
|
|
end
|
|
|
|
it 'does not allow anything if enable_badge_sql is disabled' do
|
|
|
|
SiteSetting.enable_badge_sql = false
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
get :preview, params: {
|
|
|
|
sql: 'select id as user_id, created_at granted_at from users'
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
expect(response.status).to eq(403)
|
2016-07-27 19:03:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-09 09:25:57 -04:00
|
|
|
describe '.create' do
|
|
|
|
render_views
|
|
|
|
|
|
|
|
it 'can create badges correctly' do
|
|
|
|
SiteSetting.enable_badge_sql = true
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :create, params: {
|
|
|
|
name: 'test', query: 'select 1 as user_id, null as granted_at', badge_type_id: 1
|
|
|
|
}, format: :json
|
|
|
|
|
|
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(response.status).to eq(200)
|
2017-05-09 09:25:57 -04:00
|
|
|
expect(json["badge"]["name"]).to eq('test')
|
|
|
|
expect(json["badge"]["query"]).to eq('select 1 as user_id, null as granted_at')
|
2018-05-17 12:09:27 -04:00
|
|
|
|
|
|
|
expect(UserHistory.where(acting_user_id: user.id, action: UserHistory.actions[:create_badge]).exists?).to eq(true)
|
2017-05-09 09:25:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-27 04:22:01 -04:00
|
|
|
context '.save_badge_groupings' do
|
|
|
|
|
|
|
|
it 'can save badge groupings' do
|
|
|
|
groupings = BadgeGrouping.all.order(:position).to_a
|
|
|
|
groupings << BadgeGrouping.new(name: 'Test 1')
|
|
|
|
groupings << BadgeGrouping.new(name: 'Test 2')
|
|
|
|
|
|
|
|
groupings.shuffle!
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
names = groupings.map { |g| g.name }
|
|
|
|
ids = groupings.map { |g| g.id.to_s }
|
2014-07-27 04:22:01 -04:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
post :save_badge_groupings, params: { ids: ids, names: names }, format: :json
|
2014-07-27 04:22:01 -04:00
|
|
|
|
|
|
|
groupings2 = BadgeGrouping.all.order(:position).to_a
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
expect(groupings2.map { |g| g.name }).to eq(names)
|
|
|
|
expect((groupings.map(&:id) - groupings2.map { |g| g.id }).compact).to be_blank
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)["badge_groupings"].length).to eq(groupings2.length)
|
2014-07-27 04:22:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-05 07:52:20 -05:00
|
|
|
context '.badge_types' do
|
|
|
|
it 'returns JSON' do
|
2017-08-31 00:06:56 -04:00
|
|
|
get :badge_types, format: :json
|
|
|
|
|
|
|
|
expect(response).to be_success
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(::JSON.parse(response.body)["badge_types"]).to be_present
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.destroy' do
|
|
|
|
it 'deletes the badge' do
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { id: badge.id }, format: :json
|
2018-05-17 12:09:27 -04:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(Badge.where(id: badge.id).exists?).to eq(false)
|
|
|
|
expect(UserHistory.where(acting_user_id: user.id, action: UserHistory.actions[:delete_badge]).exists?).to eq(true)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.update' do
|
2016-07-27 19:03:00 -04:00
|
|
|
|
2017-02-20 08:35:05 -05:00
|
|
|
it 'does not update the name of system badges' do
|
|
|
|
editor_badge = Badge.find(Badge::Editor)
|
|
|
|
editor_badge_name = editor_badge.name
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: editor_badge.id,
|
|
|
|
name: "123456"
|
|
|
|
}, format: :json
|
2017-02-20 08:35:05 -05:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
editor_badge.reload
|
|
|
|
expect(editor_badge.name).to eq(editor_badge_name)
|
2018-05-17 12:09:27 -04:00
|
|
|
|
|
|
|
expect(UserHistory.where(acting_user_id: user.id, action: UserHistory.actions[:change_badge]).exists?).to eq(true)
|
2017-02-20 08:35:05 -05:00
|
|
|
end
|
|
|
|
|
2016-07-27 19:03:00 -04:00
|
|
|
it 'does not allow query updates if badge_sql is disabled' do
|
|
|
|
badge.query = "select 123"
|
|
|
|
badge.save
|
|
|
|
|
|
|
|
SiteSetting.enable_badge_sql = false
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: badge.id,
|
|
|
|
name: "123456",
|
|
|
|
query: "select id user_id, created_at granted_at from users",
|
|
|
|
badge_type_id: badge.badge_type_id,
|
|
|
|
allow_title: false,
|
|
|
|
multiple_grant: false,
|
|
|
|
enabled: true
|
|
|
|
}, format: :json
|
2016-07-27 19:03:00 -04:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2016-07-27 19:03:00 -04:00
|
|
|
badge.reload
|
|
|
|
expect(badge.name).to eq('123456')
|
|
|
|
expect(badge.query).to eq('select 123')
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates the badge' do
|
2016-07-27 19:03:00 -04:00
|
|
|
SiteSetting.enable_badge_sql = true
|
|
|
|
sql = "select id user_id, created_at granted_at from users"
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
put :update, params: {
|
|
|
|
id: badge.id,
|
|
|
|
name: "123456",
|
|
|
|
query: sql,
|
|
|
|
badge_type_id: badge.badge_type_id,
|
|
|
|
allow_title: false,
|
|
|
|
multiple_grant: false,
|
|
|
|
enabled: true
|
|
|
|
}, format: :json
|
2016-07-27 19:03:00 -04:00
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
badge.reload
|
|
|
|
expect(badge.name).to eq('123456')
|
|
|
|
expect(badge.query).to eq(sql)
|
2014-03-05 07:52:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|