2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe UserSummary do
|
2016-01-27 18:12:12 -06:00
|
|
|
|
|
|
|
it "produces secure summaries" do
|
|
|
|
topic = create_post.topic
|
|
|
|
user = topic.user
|
|
|
|
_reply = create_post(user: topic.user, topic: topic)
|
|
|
|
|
|
|
|
summary = UserSummary.new(user, Guardian.new)
|
|
|
|
|
|
|
|
expect(summary.topics.length).to eq(1)
|
|
|
|
expect(summary.replies.length).to eq(1)
|
2018-07-18 15:37:50 -05:00
|
|
|
expect(summary.top_categories.length).to eq(1)
|
|
|
|
expect(summary.top_categories.first[:topic_count]).to eq(1)
|
|
|
|
expect(summary.top_categories.first[:post_count]).to eq(1)
|
2016-01-27 18:12:12 -06:00
|
|
|
|
|
|
|
topic.update_columns(deleted_at: Time.now)
|
|
|
|
|
|
|
|
expect(summary.topics.length).to eq(0)
|
|
|
|
expect(summary.replies.length).to eq(0)
|
2018-07-18 15:37:50 -05:00
|
|
|
expect(summary.top_categories.length).to eq(0)
|
2016-01-27 18:12:12 -06:00
|
|
|
|
|
|
|
topic.update_columns(deleted_at: nil, visible: false)
|
|
|
|
|
|
|
|
expect(summary.topics.length).to eq(0)
|
|
|
|
expect(summary.replies.length).to eq(0)
|
2018-07-18 15:37:50 -05:00
|
|
|
expect(summary.top_categories.length).to eq(0)
|
2016-01-27 18:12:12 -06:00
|
|
|
|
|
|
|
category = Fabricate(:category)
|
|
|
|
topic.update_columns(category_id: category.id, deleted_at: nil, visible: true)
|
|
|
|
|
|
|
|
category.set_permissions(staff: :full)
|
|
|
|
category.save
|
|
|
|
|
|
|
|
expect(summary.topics.length).to eq(0)
|
|
|
|
expect(summary.replies.length).to eq(0)
|
2018-07-18 15:37:50 -05:00
|
|
|
expect(summary.top_categories.length).to eq(0)
|
2016-01-27 18:12:12 -06:00
|
|
|
end
|
|
|
|
|
2018-12-24 13:34:55 -06:00
|
|
|
it "is robust enough to handle bad data" do
|
2019-01-03 11:03:01 -06:00
|
|
|
UserActionManager.enable
|
2018-12-24 13:34:55 -06:00
|
|
|
|
|
|
|
liked_post = create_post
|
|
|
|
user = Fabricate(:user)
|
2019-01-03 11:03:01 -06:00
|
|
|
PostActionCreator.like(user, liked_post)
|
2018-12-24 13:34:55 -06:00
|
|
|
|
|
|
|
users = UserSummary.new(user, Guardian.new).most_liked_users
|
|
|
|
|
|
|
|
expect(users.map(&:id)).to eq([liked_post.user_id])
|
|
|
|
|
|
|
|
# really we should not be corrupting stuff like this
|
|
|
|
# but in production dbs this can happens sometimes I guess
|
|
|
|
liked_post.user.delete
|
|
|
|
|
|
|
|
users = UserSummary.new(user, Guardian.new).most_liked_users
|
|
|
|
expect(users).to eq([])
|
|
|
|
end
|
2021-04-15 05:05:03 -05:00
|
|
|
|
|
|
|
it "includes ordered top categories" do
|
|
|
|
u = Fabricate(:user)
|
|
|
|
|
|
|
|
UserSummary::MAX_SUMMARY_RESULTS.times do
|
|
|
|
c = Fabricate(:category)
|
|
|
|
t = Fabricate(:topic, category: c, user: u)
|
|
|
|
Fabricate(:post, user: u, topic: t)
|
|
|
|
end
|
|
|
|
|
|
|
|
top_category = Fabricate(:category)
|
|
|
|
t = Fabricate(:topic, category: top_category, user: u)
|
|
|
|
Fabricate(:post, user: u, topic: t)
|
|
|
|
Fabricate(:post, user: u, topic: t)
|
|
|
|
|
|
|
|
summary = UserSummary.new(u, Guardian.new)
|
|
|
|
|
|
|
|
expect(summary.top_categories.length).to eq(UserSummary::MAX_SUMMARY_RESULTS)
|
|
|
|
expect(summary.top_categories.first[:id]).to eq(top_category.id)
|
|
|
|
end
|
2021-11-18 02:12:03 -06:00
|
|
|
|
|
|
|
it "excludes moderator action posts" do
|
|
|
|
topic = create_post.topic
|
|
|
|
user = topic.user
|
|
|
|
create_post(user: user, topic: topic)
|
|
|
|
Fabricate(:small_action, topic: topic, user: user)
|
|
|
|
|
|
|
|
summary = UserSummary.new(user, Guardian.new)
|
|
|
|
|
|
|
|
expect(summary.topics.length).to eq(1)
|
|
|
|
expect(summary.replies.length).to eq(1)
|
|
|
|
expect(summary.top_categories.length).to eq(1)
|
|
|
|
expect(summary.top_categories.first[:topic_count]).to eq(1)
|
|
|
|
expect(summary.top_categories.first[:post_count]).to eq(1)
|
|
|
|
end
|
2016-01-27 18:12:12 -06:00
|
|
|
end
|