REFACTOR: admin badges controller specs to requests (#5960)

This commit is contained in:
OsamaSayegh 2018-06-11 07:50:56 +03:00 committed by Guo Xiang Tan
parent bf8d392a51
commit 1fe092da0a
1 changed files with 34 additions and 35 deletions

View File

@ -1,60 +1,62 @@
require 'rails_helper' require 'rails_helper'
describe Admin::BadgesController do describe Admin::BadgesController do
context "while logged in as an admin" do context "while logged in as an admin" do
let!(:user) { log_in(:admin) } let(:admin) { Fabricate(:admin) }
let!(:badge) { Fabricate(:badge) } let!(:badge) { Fabricate(:badge) }
context 'index' do before do
sign_in(admin)
end
describe '#index' do
it 'returns badge index' do it 'returns badge index' do
get :index, format: :json get "/admin/badges.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
end end
end end
context 'preview' do describe '#preview' do
it 'allows preview enable_badge_sql is enabled' do it 'allows preview enable_badge_sql is enabled' do
SiteSetting.enable_badge_sql = true SiteSetting.enable_badge_sql = true
get :preview, params: { post "/admin/badges/preview.json", params: {
sql: 'select id as user_id, created_at granted_at from users' sql: 'select id as user_id, created_at granted_at from users'
}, format: :json }
expect(response.status).to eq(200)
expect(JSON.parse(response.body)["grant_count"]).to be > 0 expect(JSON.parse(response.body)["grant_count"]).to be > 0
end end
it 'does not allow anything if enable_badge_sql is disabled' do it 'does not allow anything if enable_badge_sql is disabled' do
SiteSetting.enable_badge_sql = false SiteSetting.enable_badge_sql = false
get :preview, params: { post "/admin/badges/preview.json", params: {
sql: 'select id as user_id, created_at granted_at from users' sql: 'select id as user_id, created_at granted_at from users'
}, format: :json }
expect(response.status).to eq(403) expect(response.status).to eq(403)
end end
end end
describe '.create' do describe '#create' do
render_views
it 'can create badges correctly' do it 'can create badges correctly' do
SiteSetting.enable_badge_sql = true SiteSetting.enable_badge_sql = true
post :create, params: { post "/admin/badges.json", params: {
name: 'test', query: 'select 1 as user_id, null as granted_at', badge_type_id: 1 name: 'test', query: 'select 1 as user_id, null as granted_at', badge_type_id: 1
}, format: :json }
json = JSON.parse(response.body) json = JSON.parse(response.body)
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json["badge"]["name"]).to eq('test') expect(json["badge"]["name"]).to eq('test')
expect(json["badge"]["query"]).to eq('select 1 as user_id, null as granted_at') expect(json["badge"]["query"]).to eq('select 1 as user_id, null as granted_at')
expect(UserHistory.where(acting_user_id: user.id, action: UserHistory.actions[:create_badge]).exists?).to eq(true) expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:create_badge]).exists?).to eq(true)
end end
end end
context '.save_badge_groupings' do describe '#save_badge_groupings' do
it 'can save badge groupings' do it 'can save badge groupings' do
groupings = BadgeGrouping.all.order(:position).to_a groupings = BadgeGrouping.all.order(:position).to_a
groupings << BadgeGrouping.new(name: 'Test 1') groupings << BadgeGrouping.new(name: 'Test 1')
@ -65,7 +67,8 @@ describe Admin::BadgesController do
names = groupings.map { |g| g.name } names = groupings.map { |g| g.name }
ids = groupings.map { |g| g.id.to_s } ids = groupings.map { |g| g.id.to_s }
post :save_badge_groupings, params: { ids: ids, names: names }, format: :json post "/admin/badges/badge_groupings.json", params: { ids: ids, names: names }
expect(response.status).to eq(200)
groupings2 = BadgeGrouping.all.order(:position).to_a groupings2 = BadgeGrouping.all.order(:position).to_a
@ -75,40 +78,38 @@ describe Admin::BadgesController do
end end
end end
context '.badge_types' do describe '#badge_types' do
it 'returns JSON' do it 'returns JSON' do
get :badge_types, format: :json get "/admin/badges/types.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(::JSON.parse(response.body)["badge_types"]).to be_present expect(::JSON.parse(response.body)["badge_types"]).to be_present
end end
end end
context '.destroy' do describe '#destroy' do
it 'deletes the badge' do it 'deletes the badge' do
delete :destroy, params: { id: badge.id }, format: :json delete "/admin/badges/#{badge.id}.json"
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(Badge.where(id: badge.id).exists?).to eq(false) 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) expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:delete_badge]).exists?).to eq(true)
end end
end end
context '.update' do describe '#update' do
it 'does not update the name of system badges' do it 'does not update the name of system badges' do
editor_badge = Badge.find(Badge::Editor) editor_badge = Badge.find(Badge::Editor)
editor_badge_name = editor_badge.name editor_badge_name = editor_badge.name
put :update, params: { put "/admin/badges/#{editor_badge.id}.json", params: {
id: editor_badge.id,
name: "123456" name: "123456"
}, format: :json }
expect(response.status).to eq(200) expect(response.status).to eq(200)
editor_badge.reload editor_badge.reload
expect(editor_badge.name).to eq(editor_badge_name) expect(editor_badge.name).to eq(editor_badge_name)
expect(UserHistory.where(acting_user_id: user.id, action: UserHistory.actions[:change_badge]).exists?).to eq(true) expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:change_badge]).exists?).to eq(true)
end end
it 'does not allow query updates if badge_sql is disabled' do it 'does not allow query updates if badge_sql is disabled' do
@ -117,15 +118,14 @@ describe Admin::BadgesController do
SiteSetting.enable_badge_sql = false SiteSetting.enable_badge_sql = false
put :update, params: { put "/admin/badges/#{badge.id}.json", params: {
id: badge.id,
name: "123456", name: "123456",
query: "select id user_id, created_at granted_at from users", query: "select id user_id, created_at granted_at from users",
badge_type_id: badge.badge_type_id, badge_type_id: badge.badge_type_id,
allow_title: false, allow_title: false,
multiple_grant: false, multiple_grant: false,
enabled: true enabled: true
}, format: :json }
expect(response.status).to eq(200) expect(response.status).to eq(200)
badge.reload badge.reload
@ -137,15 +137,14 @@ describe Admin::BadgesController do
SiteSetting.enable_badge_sql = true SiteSetting.enable_badge_sql = true
sql = "select id user_id, created_at granted_at from users" sql = "select id user_id, created_at granted_at from users"
put :update, params: { put "/admin/badges/#{badge.id}.json", params: {
id: badge.id,
name: "123456", name: "123456",
query: sql, query: sql,
badge_type_id: badge.badge_type_id, badge_type_id: badge.badge_type_id,
allow_title: false, allow_title: false,
multiple_grant: false, multiple_grant: false,
enabled: true enabled: true
}, format: :json }
expect(response.status).to eq(200) expect(response.status).to eq(200)
badge.reload badge.reload