discourse/spec/controllers/admin/badges_controller_spec.rb

77 lines
2.1 KiB
Ruby
Raw Normal View History

require 'rails_helper'
2014-03-05 20:52:20 +08:00
describe Admin::BadgesController do
context "while logged in as an admin" do
let!(:user) { log_in(:admin) }
let!(:badge) { Fabricate(:badge) }
context 'index' do
it 'returns badge index' do
xhr :get, :index
2015-01-10 01:04:02 +08:00
expect(response).to be_success
end
end
2014-07-27 16:22:01 +08: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!
names = groupings.map{|g| g.name}
ids = groupings.map{|g| g.id.to_s}
xhr :post, :save_badge_groupings, ids: ids, names: names
groupings2 = BadgeGrouping.all.order(:position).to_a
2015-01-10 01:04:02 +08:00
expect(groupings2.map{|g| g.name}).to eq(names)
expect((groupings.map(&:id) - groupings2.map{|g| g.id}).compact).to be_blank
2014-07-27 16:22:01 +08:00
2015-01-10 01:04:02 +08:00
expect(::JSON.parse(response.body)["badge_groupings"].length).to eq(groupings2.length)
2014-07-27 16:22:01 +08:00
end
end
2014-03-05 20:52:20 +08:00
context '.badge_types' do
it 'returns success' do
xhr :get, :badge_types
2015-01-10 01:04:02 +08:00
expect(response).to be_success
2014-03-05 20:52:20 +08:00
end
it 'returns JSON' do
xhr :get, :badge_types
2015-01-10 01:04:02 +08:00
expect(::JSON.parse(response.body)["badge_types"]).to be_present
2014-03-05 20:52:20 +08:00
end
end
context '.destroy' do
it 'returns success' do
xhr :delete, :destroy, id: badge.id
2015-01-10 01:04:02 +08:00
expect(response).to be_success
2014-03-05 20:52:20 +08:00
end
it 'deletes the badge' do
xhr :delete, :destroy, id: badge.id
2015-01-10 01:04:02 +08:00
expect(Badge.where(id: badge.id).count).to eq(0)
2014-03-05 20:52:20 +08:00
end
end
context '.update' do
it 'returns success' do
xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: false, enabled: true
2015-01-10 01:04:02 +08:00
expect(response).to be_success
2014-03-05 20:52:20 +08:00
end
it 'updates the badge' do
xhr :put, :update, id: badge.id, name: "123456", badge_type_id: badge.badge_type_id, allow_title: false, multiple_grant: true, enabled: true
2015-01-10 01:04:02 +08:00
expect(badge.reload.name).to eq('123456')
2014-03-05 20:52:20 +08:00
end
end
end
end