2016-01-28 08:12:12 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe UserSummary do
|
|
|
|
|
|
|
|
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-19 04:37:50 +08: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-28 08:12:12 +08:00
|
|
|
|
|
|
|
topic.update_columns(deleted_at: Time.now)
|
|
|
|
|
|
|
|
expect(summary.topics.length).to eq(0)
|
|
|
|
expect(summary.replies.length).to eq(0)
|
2018-07-19 04:37:50 +08:00
|
|
|
expect(summary.top_categories.length).to eq(0)
|
2016-01-28 08:12:12 +08: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-19 04:37:50 +08:00
|
|
|
expect(summary.top_categories.length).to eq(0)
|
2016-01-28 08:12:12 +08: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-19 04:37:50 +08:00
|
|
|
expect(summary.top_categories.length).to eq(0)
|
2016-01-28 08:12:12 +08:00
|
|
|
end
|
|
|
|
|
2018-12-25 03:34:55 +08:00
|
|
|
it "is robust enough to handle bad data" do
|
2019-01-04 01:03:01 +08:00
|
|
|
UserActionManager.enable
|
2018-12-25 03:34:55 +08:00
|
|
|
|
|
|
|
liked_post = create_post
|
|
|
|
user = Fabricate(:user)
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(user, liked_post)
|
2018-12-25 03:34:55 +08: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
|
2016-01-28 08:12:12 +08:00
|
|
|
end
|