2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-16 21:12:06 +08:00
|
|
|
describe BadgesController do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:badge) { Fabricate(:badge) }
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
2014-06-14 15:55:06 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.enable_badges = true
|
|
|
|
end
|
2014-04-16 21:12:06 +08:00
|
|
|
|
|
|
|
context 'index' do
|
|
|
|
it 'should return a list of all badges' do
|
2018-05-22 11:40:50 +08:00
|
|
|
get "/badges.json"
|
2014-04-16 21:12:06 +08:00
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
parsed = response.parsed_body
|
2020-02-12 02:01:33 +08:00
|
|
|
expect(parsed["badges"].length).to eq(Badge.enabled.count)
|
2020-05-11 13:05:42 +08:00
|
|
|
expect(response.headers['X-Robots-Tag']).to eq('noindex')
|
2014-04-16 21:12:06 +08:00
|
|
|
end
|
|
|
|
end
|
2014-04-16 22:56:11 +08:00
|
|
|
|
|
|
|
context 'show' do
|
|
|
|
it "should return a badge" do
|
2018-05-22 11:40:50 +08:00
|
|
|
get "/badges/#{badge.id}.json"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
parsed = response.parsed_body
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(parsed["badge"]).to be_present
|
2014-04-16 22:56:11 +08:00
|
|
|
end
|
2014-06-14 15:55:06 +08:00
|
|
|
|
|
|
|
it "should mark the notification as viewed" do
|
2018-05-22 11:40:50 +08:00
|
|
|
sign_in(user)
|
2014-06-14 15:55:06 +08:00
|
|
|
user_badge = BadgeGranter.grant(badge, user)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user_badge.notification.read).to eq(false)
|
2018-05-22 11:40:50 +08:00
|
|
|
get "/badges/#{badge.id}.json"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user_badge.notification.reload.read).to eq(true)
|
2014-06-14 15:55:06 +08:00
|
|
|
end
|
2017-08-09 04:21:42 +08:00
|
|
|
|
|
|
|
it 'renders rss feed of a badge' do
|
2018-05-22 11:40:50 +08:00
|
|
|
get "/badges/#{badge.id}.rss"
|
2017-08-09 04:21:42 +08:00
|
|
|
expect(response.status).to eq(200)
|
2019-09-12 08:41:50 +08:00
|
|
|
expect(response.media_type).to eq('application/rss+xml')
|
2017-08-09 04:21:42 +08:00
|
|
|
end
|
2014-04-16 22:56:11 +08:00
|
|
|
end
|
2020-05-03 09:02:28 +08:00
|
|
|
|
|
|
|
context "user profiles" do
|
|
|
|
let(:titled_badge) { Fabricate(:badge, name: 'Protector of the Realm', allow_title: true) }
|
|
|
|
let!(:grant) { UserBadge.create!(user_id: user.id, badge_id: titled_badge.id, granted_at: 1.minute.ago, granted_by_id: -1) }
|
|
|
|
|
|
|
|
it "can be assigned as a title by the user" do
|
|
|
|
sign_in(user)
|
|
|
|
put "/u/#{user.username}/preferences/badge_title.json", params: {
|
|
|
|
user_badge_id: grant.id,
|
|
|
|
}
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
expect(user.title).to eq(titled_badge.display_name)
|
|
|
|
expect(user.user_profile.granted_title_badge_id).to eq(titled_badge.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "destroy" do
|
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
|
|
|
context "while assigned as a title" do
|
|
|
|
let(:titled_badge) { Fabricate(:badge, name: 'Protector of the Realm', allow_title: true) }
|
|
|
|
let!(:grant) { UserBadge.create!(user_id: user.id, badge_id: titled_badge.id, granted_at: 1.minute.ago, granted_by_id: -1) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
put "/u/#{user.username}/preferences/badge_title.json", params: {
|
|
|
|
user_badge_id: grant.id,
|
|
|
|
}
|
|
|
|
user.reload
|
|
|
|
sign_out
|
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds and unassigns the title from the user" do
|
|
|
|
expect(user.title).to eq(titled_badge.display_name)
|
|
|
|
|
|
|
|
sign_in(admin)
|
|
|
|
badge_id = titled_badge.id
|
|
|
|
|
|
|
|
delete "/admin/badges/#{titled_badge.id}.json"
|
|
|
|
expect(response.status).to be(200)
|
|
|
|
expect(Badge.find_by(id: badge_id)).to be(nil)
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
expect(user.title).to_not eq(titled_badge.display_name)
|
|
|
|
expect(user.user_profile.granted_title_badge_id).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-04-16 21:12:06 +08:00
|
|
|
end
|