2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe UserBadgesController do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
|
|
|
fab!(:admin)
|
|
|
|
fab!(:badge)
|
2014-07-23 09:42:24 +08:00
|
|
|
|
2024-12-09 18:07:59 +08:00
|
|
|
before { user.user_stat.update!(post_count: 1) }
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "#index" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:badge) { Fabricate(:badge, target_posts: true, show_posts: false) }
|
2023-12-04 20:45:19 +08:00
|
|
|
|
2015-02-26 08:55:17 +08:00
|
|
|
it "does not leak private info" do
|
2014-07-23 09:42:24 +08:00
|
|
|
p = create_post
|
2018-06-05 10:59:01 +08:00
|
|
|
UserBadge.create!(
|
|
|
|
badge: badge,
|
|
|
|
user: user,
|
|
|
|
post_id: p.id,
|
|
|
|
granted_by_id: -1,
|
|
|
|
granted_at: Time.now,
|
|
|
|
)
|
2014-07-23 09:42:24 +08:00
|
|
|
|
2018-06-05 10:59:01 +08:00
|
|
|
get "/user_badges.json", params: { badge_id: badge.id }
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2016-01-18 14:59:07 +08:00
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
parsed = response.parsed_body
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(parsed["topics"]).to eq(nil)
|
2016-01-18 14:59:07 +08:00
|
|
|
expect(parsed["badges"].length).to eq(1)
|
|
|
|
expect(parsed["user_badge_info"]["user_badges"][0]["post_id"]).to eq(nil)
|
2014-07-23 09:42:24 +08:00
|
|
|
end
|
2017-11-22 01:22:24 +08:00
|
|
|
|
|
|
|
it "fails when badges are disabled" do
|
|
|
|
SiteSetting.enable_badges = false
|
2018-06-05 10:59:01 +08:00
|
|
|
get "/user_badges.json", params: { badge_id: badge.id }
|
2017-11-24 09:32:44 +08:00
|
|
|
expect(response.status).to eq(404)
|
2017-11-22 01:22:24 +08:00
|
|
|
end
|
2023-07-31 22:00:05 +08:00
|
|
|
|
|
|
|
it "only accepts valid offset params" do
|
|
|
|
get "/user_badges.json", params: { badge_id: badge.id, offset: -1 }
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
|
|
|
get "/user_badges.json", params: { badge_id: badge.id, offset: 100 }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2023-12-04 20:45:19 +08:00
|
|
|
|
|
|
|
it "requires username or badge_id to be specified" do
|
|
|
|
get "/user_badges.json"
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
2014-07-23 09:42:24 +08:00
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
|
2023-12-04 20:45:19 +08:00
|
|
|
describe "#show" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:post)
|
|
|
|
fab!(:private_message_post)
|
2022-10-27 11:26:14 +08:00
|
|
|
let(:topic) { post.topic }
|
|
|
|
let(:private_message_topic) { private_message_post.topic }
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
2022-10-27 11:26:14 +08:00
|
|
|
fab!(:private_category) { Fabricate(:private_category, group: group) }
|
|
|
|
fab!(:restricted_topic) { Fabricate(:topic, category: private_category) }
|
|
|
|
fab!(:restricted_post) { Fabricate(:post, topic: restricted_topic) }
|
|
|
|
fab!(:badge) { Fabricate(:badge, show_posts: true) }
|
|
|
|
fab!(:user_badge) { Fabricate(:user_badge, user: user, badge: badge, post: post) }
|
|
|
|
fab!(:user_badge_2) { Fabricate(:user_badge, badge: badge, post: private_message_post) }
|
|
|
|
fab!(:user_badge_3) { Fabricate(:user_badge, badge: badge, post: restricted_post) }
|
2014-03-05 20:52:20 +08:00
|
|
|
|
2014-04-16 22:56:11 +08:00
|
|
|
it "returns user_badges for a user" do
|
2018-06-05 10:59:01 +08:00
|
|
|
get "/user-badges/#{user.username}.json"
|
2014-03-05 20:52:20 +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
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(parsed["user_badges"].length).to eq(1)
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
2014-04-16 22:56:11 +08:00
|
|
|
|
2018-11-24 01:41:41 +08:00
|
|
|
it "returns user_badges for a user with period in username" do
|
|
|
|
user.update!(username: "myname.test")
|
|
|
|
get "/user-badges/#{user.username}", xhr: true
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
parsed = response.parsed_body
|
2018-11-24 01:41:41 +08:00
|
|
|
expect(parsed["user_badges"].length).to eq(1)
|
|
|
|
end
|
|
|
|
|
2014-04-16 22:56:11 +08:00
|
|
|
it "returns user_badges for a badge" do
|
2018-06-05 10:59:01 +08:00
|
|
|
get "/user_badges.json", params: { badge_id: badge.id }
|
2014-04-16 22:56:11 +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
|
2022-10-27 11:26:14 +08:00
|
|
|
expect(parsed["user_badge_info"]["user_badges"].length).to eq(3)
|
2014-04-16 22:56:11 +08:00
|
|
|
end
|
2014-05-21 15:22:42 +08:00
|
|
|
|
|
|
|
it "includes counts when passed the aggregate argument" do
|
2018-06-05 10:59:01 +08:00
|
|
|
get "/user-badges/#{user.username}.json", params: { grouped: true }
|
2014-05-21 15:22:42 +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
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(parsed["user_badges"].first.has_key?("count")).to eq(true)
|
2014-05-21 15:22:42 +08:00
|
|
|
end
|
2022-10-27 11:26:14 +08:00
|
|
|
|
|
|
|
context "for post and topic attributes associated with user badge" do
|
|
|
|
it "does not include the attributes for the private topic when user is anon" do
|
|
|
|
get "/user_badges.json", params: { badge_id: badge.id }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
parsed = response.parsed_body
|
|
|
|
|
|
|
|
expect(parsed["topics"].map { |t| t["id"] }).to contain_exactly(post.topic_id)
|
|
|
|
|
|
|
|
parsed_user_badges = parsed["user_badge_info"]["user_badges"]
|
|
|
|
|
|
|
|
expect(parsed_user_badges.map { |ub| ub["post_id"] }.compact).to contain_exactly(post.id)
|
|
|
|
expect(parsed_user_badges.map { |ub| ub["post_number"] }.compact).to contain_exactly(
|
|
|
|
post.post_number,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not include the attributes for topics which the current user cannot see" do
|
2023-12-06 09:26:45 +08:00
|
|
|
sign_in(user)
|
|
|
|
|
2022-10-27 11:26:14 +08:00
|
|
|
get "/user_badges.json", params: { badge_id: badge.id }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
parsed = response.parsed_body
|
|
|
|
|
|
|
|
expect(parsed["topics"].map { |t| t["id"] }).to contain_exactly(post.topic_id)
|
|
|
|
|
|
|
|
parsed_user_badges = parsed["user_badge_info"]["user_badges"]
|
|
|
|
|
|
|
|
expect(parsed_user_badges.map { |ub| ub["post_id"] }.compact).to contain_exactly(post.id)
|
|
|
|
expect(parsed_user_badges.map { |ub| ub["post_number"] }.compact).to contain_exactly(
|
|
|
|
post.post_number,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the attributes for regular topic, private messages and restricted topics which the current user can see" do
|
|
|
|
group.add(user)
|
|
|
|
private_message_topic.allowed_users << user
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
get "/user_badges.json", params: { badge_id: badge.id }
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
parsed = response.parsed_body
|
|
|
|
|
|
|
|
expect(parsed["topics"].map { |t| t["id"] }).to contain_exactly(
|
|
|
|
post.topic_id,
|
|
|
|
private_message_post.topic_id,
|
|
|
|
restricted_post.topic_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
parsed_user_badges = parsed["user_badge_info"]["user_badges"]
|
|
|
|
|
|
|
|
expect(parsed_user_badges.map { |ub| ub["post_id"] }.compact).to contain_exactly(
|
|
|
|
post.id,
|
|
|
|
private_message_post.id,
|
|
|
|
restricted_post.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(parsed_user_badges.map { |ub| ub["post_number"] }.compact).to contain_exactly(
|
|
|
|
post.post_number,
|
|
|
|
private_message_post.post_number,
|
|
|
|
restricted_post.post_number,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-10-10 05:18:44 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with hidden profiles" do
|
2024-11-13 09:22:58 +08:00
|
|
|
before { user.user_option.update_columns(hide_profile: true) }
|
2020-10-10 05:18:44 +08:00
|
|
|
|
2024-11-13 09:22:58 +08:00
|
|
|
it "returns 404 if `hide_profile` user option is checked" do
|
2020-10-10 05:18:44 +08:00
|
|
|
get "/user-badges/#{user.username}.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns user_badges if `allow_users_to_hide_profile` is false" do
|
|
|
|
SiteSetting.allow_users_to_hide_profile = false
|
|
|
|
|
|
|
|
get "/user-badges/#{user.username}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "#create" do
|
2014-03-05 20:52:20 +08:00
|
|
|
it "requires username to be specified" do
|
2018-06-05 10:59:01 +08:00
|
|
|
post "/user_badges.json", params: { badge_id: badge.id }
|
|
|
|
expect(response.status).to eq(400)
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow regular users to grant badges" do
|
2018-06-05 10:59:01 +08:00
|
|
|
sign_in(Fabricate(:user))
|
2017-08-31 12:06:56 +08:00
|
|
|
|
|
|
|
post "/user_badges.json", params: { badge_id: badge.id, username: user.username }
|
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(403)
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "grants badges from staff" do
|
2017-08-31 12:06:56 +08:00
|
|
|
post_1 = create_post
|
2015-02-26 09:24:21 +08:00
|
|
|
|
2018-06-05 10:59:01 +08:00
|
|
|
sign_in(admin)
|
2015-02-26 09:24:21 +08:00
|
|
|
|
2018-06-05 10:59:01 +08:00
|
|
|
post "/user_badges.json",
|
|
|
|
params: {
|
2017-08-31 12:06:56 +08:00
|
|
|
badge_id: badge.id,
|
|
|
|
username: user.username,
|
|
|
|
reason: Discourse.base_url + post_1.url,
|
2018-06-05 10:59:01 +08:00
|
|
|
}
|
2015-02-26 09:24:21 +08:00
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
2015-02-26 09:24:21 +08:00
|
|
|
|
2014-05-06 21:41:59 +08:00
|
|
|
user_badge = UserBadge.find_by(user: user, badge: badge)
|
2015-02-26 09:24:21 +08:00
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user_badge).to be_present
|
|
|
|
expect(user_badge.granted_by).to eq(admin)
|
2017-08-31 12:06:56 +08:00
|
|
|
expect(user_badge.post_id).to eq(post_1.id)
|
2018-06-05 10:59:01 +08:00
|
|
|
expect(UserHistory.where(acting_user: admin, target_user: user).count).to eq(1)
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not grant badges from regular api calls" do
|
2019-11-05 22:10:23 +08:00
|
|
|
api_key = Fabricate(:api_key, user: user)
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2018-06-05 10:59:01 +08:00
|
|
|
post "/user_badges.json",
|
|
|
|
params: {
|
2019-11-05 22:10:23 +08:00
|
|
|
badge_id: badge.id,
|
|
|
|
username: user.username,
|
|
|
|
api_key: api_key.key,
|
2018-06-05 10:59:01 +08:00
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(403)
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "grants badges from master api calls" do
|
|
|
|
api_key = Fabricate(:api_key)
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2020-04-07 06:55:44 +08:00
|
|
|
post "/user_badges.json",
|
|
|
|
params: {
|
|
|
|
badge_id: badge.id,
|
|
|
|
username: user.username,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
HTTP_API_KEY: api_key.key,
|
|
|
|
HTTP_API_USERNAME: "system",
|
|
|
|
}
|
2017-08-31 12:06:56 +08:00
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
2014-05-06 21:41:59 +08:00
|
|
|
user_badge = UserBadge.find_by(user: user, badge: badge)
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(user_badge).to be_present
|
|
|
|
expect(user_badge.granted_by).to eq(Discourse.system_user)
|
2018-06-05 10:59:01 +08:00
|
|
|
expect(UserHistory.where(acting_user: Discourse.system_user, target_user: user).count).to eq(
|
2023-01-09 19:18:21 +08:00
|
|
|
0,
|
2018-06-05 10:59:01 +08:00
|
|
|
)
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
2015-02-26 08:55:17 +08:00
|
|
|
|
|
|
|
it "will trigger :user_badge_granted" do
|
2018-06-05 10:59:01 +08:00
|
|
|
sign_in(Fabricate(:admin))
|
2017-06-01 15:19:43 +08:00
|
|
|
|
2018-06-05 10:59:01 +08:00
|
|
|
events =
|
|
|
|
DiscourseEvent
|
|
|
|
.track_events do
|
2017-08-31 12:06:56 +08:00
|
|
|
post "/user_badges.json", params: { badge_id: badge.id, username: user.username }
|
2018-06-05 10:59:01 +08:00
|
|
|
end
|
|
|
|
.map { |event| event[:event_name] }
|
2017-06-01 15:19:43 +08:00
|
|
|
|
2018-06-05 10:59:01 +08:00
|
|
|
expect(events).to include(:user_badge_granted)
|
2015-02-26 08:55:17 +08:00
|
|
|
end
|
2018-11-29 01:01:41 +08:00
|
|
|
|
|
|
|
it "does not grant badge when external link is used in reason" do
|
|
|
|
post = create_post
|
|
|
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
post "/user_badges.json",
|
|
|
|
params: {
|
|
|
|
badge_id: badge.id,
|
|
|
|
username: user.username,
|
|
|
|
reason: "http://example.com/" + post.url,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not grant badge if invalid discourse post/topic link is used in reason" do
|
|
|
|
post = create_post
|
|
|
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
post "/user_badges.json",
|
|
|
|
params: {
|
|
|
|
badge_id: badge.id,
|
|
|
|
username: user.username,
|
|
|
|
reason: Discourse.base_url + "/random_url/" + post.url,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "grants badge when valid post/topic link is given in reason" do
|
|
|
|
post = create_post
|
|
|
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
post "/user_badges.json",
|
|
|
|
params: {
|
|
|
|
badge_id: badge.id,
|
|
|
|
username: user.username,
|
|
|
|
reason: Discourse.base_url + post.url,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
2018-12-31 16:04:47 +08:00
|
|
|
|
|
|
|
describe "with relative_url_root" do
|
|
|
|
it "grants badge when valid post/topic link is given in reason" do
|
2019-11-15 13:48:24 +08:00
|
|
|
set_subfolder "/discuss"
|
|
|
|
|
2018-12-31 16:04:47 +08:00
|
|
|
post = create_post
|
|
|
|
|
|
|
|
sign_in(admin)
|
|
|
|
|
|
|
|
post "/user_badges.json",
|
|
|
|
params: {
|
|
|
|
badge_id: badge.id,
|
|
|
|
username: user.username,
|
|
|
|
reason: "#{Discourse.base_url}#{post.url}",
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
expect(UserBadge.exists?(badge_id: badge.id, post_id: post.id, granted_by: admin.id)).to eq(
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "#destroy" do
|
2014-04-16 18:34:27 +08:00
|
|
|
let!(:user_badge) do
|
|
|
|
UserBadge.create(
|
|
|
|
badge: badge,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
|
|
|
|
it "checks that the user is authorized to revoke a badge" do
|
2018-06-05 10:59:01 +08:00
|
|
|
delete "/user_badges/#{user_badge.id}.json"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(403)
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "revokes the badge" do
|
2018-06-05 10:59:01 +08:00
|
|
|
sign_in(admin)
|
|
|
|
delete "/user_badges/#{user_badge.id}.json"
|
|
|
|
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(UserBadge.find_by(id: user_badge.id)).to eq(nil)
|
2018-06-05 10:59:01 +08:00
|
|
|
expect(UserHistory.where(acting_user: admin, target_user: user).count).to eq(1)
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
2015-02-26 08:55:17 +08:00
|
|
|
|
|
|
|
it "will trigger :user_badge_removed" do
|
2018-06-05 10:59:01 +08:00
|
|
|
sign_in(Fabricate(:admin))
|
2016-09-13 16:03:17 +08:00
|
|
|
|
2018-06-05 10:59:01 +08:00
|
|
|
events =
|
|
|
|
DiscourseEvent
|
|
|
|
.track_events { delete "/user_badges/#{user_badge.id}.json" }
|
|
|
|
.map { |event| event[:event_name] }
|
2017-06-01 15:19:43 +08:00
|
|
|
|
2018-06-05 10:59:01 +08:00
|
|
|
expect(events).to include(:user_badge_removed)
|
2015-02-26 08:55:17 +08:00
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|
2021-06-01 16:33:40 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "#favorite" do
|
2021-06-01 16:33:40 +08:00
|
|
|
let!(:user_badge) do
|
|
|
|
UserBadge.create(
|
|
|
|
badge: badge,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2021-06-01 16:33:40 +08:00
|
|
|
|
|
|
|
it "checks that the user is authorized to favorite the badge" do
|
|
|
|
sign_in(Fabricate(:admin))
|
|
|
|
put "/user_badges/#{user_badge.id}/toggle_favorite.json"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
2021-06-22 23:58:03 +08:00
|
|
|
it "checks that the user has less than max_favorites_badges favorited badges" do
|
2021-06-01 16:33:40 +08:00
|
|
|
sign_in(user)
|
|
|
|
UserBadge.create(
|
|
|
|
badge: Fabricate(:badge),
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
is_favorite: true,
|
|
|
|
)
|
|
|
|
UserBadge.create(
|
|
|
|
badge: Fabricate(:badge),
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
2021-06-23 19:41:23 +08:00
|
|
|
is_favorite: true,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2021-06-22 23:58:03 +08:00
|
|
|
|
2021-06-01 16:33:40 +08:00
|
|
|
put "/user_badges/#{user_badge.id}/toggle_favorite.json"
|
|
|
|
expect(response.status).to eq(400)
|
2021-06-22 23:58:03 +08:00
|
|
|
|
|
|
|
SiteSetting.max_favorite_badges = 3
|
|
|
|
|
|
|
|
put "/user_badges/#{user_badge.id}/toggle_favorite.json"
|
2021-07-16 11:13:00 +08:00
|
|
|
expect(response.status).to eq(204)
|
2021-06-01 16:33:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "favorites a badge" do
|
|
|
|
sign_in(user)
|
|
|
|
put "/user_badges/#{user_badge.id}/toggle_favorite.json"
|
2021-07-15 18:51:44 +08:00
|
|
|
|
2021-07-16 11:13:00 +08:00
|
|
|
expect(response.status).to eq(204)
|
2021-06-01 16:33:40 +08:00
|
|
|
user_badge = UserBadge.find_by(user: user, badge: badge)
|
2021-07-15 18:51:44 +08:00
|
|
|
expect(user_badge.is_favorite).to eq(true)
|
2021-06-01 16:33:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "unfavorites a badge" do
|
|
|
|
sign_in(user)
|
|
|
|
user_badge.toggle!(:is_favorite)
|
|
|
|
put "/user_badges/#{user_badge.id}/toggle_favorite.json"
|
2021-07-15 18:51:44 +08:00
|
|
|
|
2021-07-16 11:13:00 +08:00
|
|
|
expect(response.status).to eq(204)
|
2021-06-01 16:33:40 +08:00
|
|
|
user_badge = UserBadge.find_by(user: user, badge: badge)
|
2021-07-15 18:51:44 +08:00
|
|
|
expect(user_badge.is_favorite).to eq(false)
|
2021-06-01 16:33:40 +08:00
|
|
|
end
|
2021-06-23 19:41:23 +08:00
|
|
|
|
|
|
|
it "works with multiple grants" do
|
|
|
|
SiteSetting.max_favorite_badges = 2
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
|
|
|
|
badge = Fabricate(:badge, multiple_grant: true)
|
|
|
|
user_badge =
|
|
|
|
UserBadge.create(
|
|
|
|
badge: badge,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
seq: 0,
|
|
|
|
is_favorite: true,
|
|
|
|
)
|
|
|
|
user_badge2 =
|
|
|
|
UserBadge.create(
|
|
|
|
badge: badge,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
seq: 1,
|
|
|
|
is_favorite: true,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2021-06-23 19:41:23 +08:00
|
|
|
other_badge = Fabricate(:badge)
|
|
|
|
other_user_badge =
|
|
|
|
UserBadge.create(
|
|
|
|
badge: other_badge,
|
|
|
|
user: user,
|
|
|
|
granted_by: Discourse.system_user,
|
|
|
|
granted_at: Time.now,
|
|
|
|
)
|
|
|
|
|
|
|
|
put "/user_badges/#{user_badge.id}/toggle_favorite.json"
|
2021-07-16 11:13:00 +08:00
|
|
|
expect(response.status).to eq(204)
|
2021-06-23 19:41:23 +08:00
|
|
|
expect(user_badge.reload.is_favorite).to eq(false)
|
|
|
|
expect(user_badge2.reload.is_favorite).to eq(false)
|
|
|
|
|
|
|
|
put "/user_badges/#{user_badge.id}/toggle_favorite.json"
|
2021-07-16 11:13:00 +08:00
|
|
|
expect(response.status).to eq(204)
|
2021-06-23 19:41:23 +08:00
|
|
|
expect(user_badge.reload.is_favorite).to eq(true)
|
|
|
|
expect(user_badge2.reload.is_favorite).to eq(true)
|
|
|
|
|
|
|
|
put "/user_badges/#{other_user_badge.id}/toggle_favorite.json"
|
2021-07-16 11:13:00 +08:00
|
|
|
expect(response.status).to eq(204)
|
2021-06-23 19:41:23 +08:00
|
|
|
expect(other_user_badge.reload.is_favorite).to eq(true)
|
|
|
|
end
|
2021-06-01 16:33:40 +08:00
|
|
|
end
|
2014-03-05 20:52:20 +08:00
|
|
|
end
|