discourse/spec/requests/admin/badges_controller_spec.rb

215 lines
6.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'rails_helper'
2014-03-05 20:52:20 +08:00
describe Admin::BadgesController do
context "while logged in as an admin" do
fab!(:admin) { Fabricate(:admin) }
fab!(:badge) { Fabricate(:badge) }
2014-03-05 20:52:20 +08:00
before do
sign_in(admin)
end
describe '#index' do
it 'returns badge index' do
get "/admin/badges.json"
expect(response.status).to eq(200)
end
end
describe '#preview' do
it 'allows preview enable_badge_sql is enabled' do
SiteSetting.enable_badge_sql = true
post "/admin/badges/preview.json", params: {
sql: 'select id as user_id, created_at granted_at from users'
}
expect(response.status).to eq(200)
expect(JSON.parse(response.body)["grant_count"]).to be > 0
end
it 'does not allow anything if enable_badge_sql is disabled' do
SiteSetting.enable_badge_sql = false
post "/admin/badges/preview.json", params: {
sql: 'select id as user_id, created_at granted_at from users'
}
expect(response.status).to eq(403)
end
end
describe '#create' do
it 'can create badges correctly' do
SiteSetting.enable_badge_sql = true
post "/admin/badges.json", params: {
name: 'test', query: 'select 1 as user_id, null as granted_at', badge_type_id: 1
}
json = JSON.parse(response.body)
expect(response.status).to eq(200)
expect(json["badge"]["name"]).to eq('test')
expect(json["badge"]["query"]).to eq('select 1 as user_id, null as granted_at')
expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:create_badge]).exists?).to eq(true)
end
end
describe '#save_badge_groupings' do
2014-07-27 16:22:01 +08:00
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-28 09:20:09 +08:00
names = groupings.map { |g| g.name }
ids = groupings.map { |g| g.id.to_s }
2014-07-27 16:22:01 +08:00
post "/admin/badges/badge_groupings.json", params: { ids: ids, names: names }
expect(response.status).to eq(200)
2014-07-27 16:22:01 +08:00
groupings2 = BadgeGrouping.all.order(:position).to_a
2017-07-28 09:20:09 +08: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-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
describe '#badge_types' do
2014-03-05 20:52:20 +08:00
it 'returns JSON' do
get "/admin/badges/types.json"
expect(response.status).to eq(200)
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
describe '#destroy' do
2014-03-05 20:52:20 +08:00
it 'deletes the badge' do
delete "/admin/badges/#{badge.id}.json"
expect(response.status).to eq(200)
expect(Badge.where(id: badge.id).exists?).to eq(false)
expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:delete_badge]).exists?).to eq(true)
2014-03-05 20:52:20 +08:00
end
end
describe '#update' do
it 'does not update the name of system badges' do
editor_badge = Badge.find(Badge::Editor)
editor_badge_name = editor_badge.name
put "/admin/badges/#{editor_badge.id}.json", params: {
name: "123456"
}
expect(response.status).to eq(200)
editor_badge.reload
expect(editor_badge.name).to eq(editor_badge_name)
expect(UserHistory.where(acting_user_id: admin.id, action: UserHistory.actions[:change_badge]).exists?).to eq(true)
end
it 'does not allow query updates if badge_sql is disabled' do
badge.query = "select 123"
badge.save
SiteSetting.enable_badge_sql = false
put "/admin/badges/#{badge.id}.json", params: {
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
}
expect(response.status).to eq(200)
badge.reload
expect(badge.name).to eq('123456')
expect(badge.query).to eq('select 123')
2014-03-05 20:52:20 +08:00
end
it 'updates the badge' do
SiteSetting.enable_badge_sql = true
sql = "select id user_id, created_at granted_at from users"
put "/admin/badges/#{badge.id}.json", params: {
name: "123456",
query: sql,
badge_type_id: badge.badge_type_id,
allow_title: false,
multiple_grant: false,
enabled: true
}
expect(response.status).to eq(200)
badge.reload
expect(badge.name).to eq('123456')
expect(badge.query).to eq(sql)
2014-03-05 20:52:20 +08:00
end
FIX: Badge and user title interaction fixes (#8282) * Fix user title logic when badge name customized * Fix an issue where a user's title was not considered a badge granted title when the user used a badge for their title and the badge name was customized. this affected the effectiveness of revoke_ungranted_titles! which only operates on badge_granted_titles. * When a user's title is set now it is considered a badge_granted_title if the badge name OR the badge custom name from TranslationOverride is the same as the title * When a user's badge is revoked we now also revoke their title if the user's title matches the badge name OR the badge custom name from TranslationOverride * Add a user history log when the title is revoked to remove confusion about why titles are revoked * Add granted_title_badge_id to user_profile, now when we set badge_granted_title on a user profile when updating a user's title based on a badge, we also remember which badge matched the title * When badge name (or custom text) changes update titles of users in a background job * When the name of a badge changes, or in the case of system badges when their custom translation text changes, then we need to update the title of all corresponding users who have a badge_granted_title and matching granted_title_badge_id. In the case of system badges we need to first get the proper badge ID based on the translation key e.g. badges.regular.name * Add migration to backfill all granted_title_badge_ids for both normal badge name titles and titles using custom badge text.
2019-11-08 13:34:24 +08:00
context 'when there is a user with a title granted using the badge' do
fab!(:user_with_badge_title) { Fabricate(:active_user) }
fab!(:badge) { Fabricate(:badge, name: 'Oathbreaker', allow_title: true) }
before do
BadgeGranter.grant(badge, user_with_badge_title)
user_with_badge_title.update(title: 'Oathbreaker')
end
it 'updates the user title in a job' do
Jobs.expects(:enqueue).with(
:bulk_user_title_update,
new_title: 'Shieldbearer',
granted_badge_id: badge.id,
action: Jobs::BulkUserTitleUpdate::UPDATE_ACTION
)
put "/admin/badges/#{badge.id}.json", params: {
name: "Shieldbearer"
}
end
end
2014-03-05 20:52:20 +08:00
end
describe '#mass_award' do
it 'does nothing when there is no file' do
post "/admin/badges/award/#{badge.id}.json", params: { file: '' }
expect(response.status).to eq(400)
end
it 'does nothing when the badge id is not valid' do
post '/admin/badges/award/fake_id.json', params: { file: fixture_file_upload(Tempfile.new) }
expect(response.status).to eq(400)
end
it 'does nothing when the file is not a csv' do
file = file_from_fixtures('cropped.png')
post "/admin/badges/award/#{badge.id}.json", params: { file: fixture_file_upload(file) }
expect(response.status).to eq(400)
end
it 'creates the badge for an existing user' do
Jobs.run_immediately!
user = Fabricate(:user, email: 'user1@test.com')
file = file_from_fixtures('user_emails.csv', 'csv')
post "/admin/badges/award/#{badge.id}.json", params: { file: fixture_file_upload(file) }
expect(UserBadge.exists?(user: user, badge: badge)).to eq(true)
end
end
2014-03-05 20:52:20 +08:00
end
end