2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe UserSummarySerializer do
|
2024-02-05 17:00:36 +08:00
|
|
|
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
|
|
|
|
fab!(:another_user) { Fabricate(:user, refresh_auto_groups: true) }
|
|
|
|
|
2019-01-12 02:09:06 +08:00
|
|
|
it "returns expected data" do
|
2019-01-04 01:03:01 +08:00
|
|
|
UserActionManager.enable
|
2023-12-13 11:50:13 +08:00
|
|
|
liked_user = Fabricate(:user, name: "John Doe", username: "john_doe", refresh_auto_groups: true)
|
2019-01-14 09:49:37 +08:00
|
|
|
liked_post = create_post(user: liked_user)
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.like(user, liked_post)
|
2019-01-12 02:09:06 +08:00
|
|
|
|
2024-02-05 17:00:36 +08:00
|
|
|
guardian = Guardian.new(another_user)
|
2019-01-12 02:09:06 +08:00
|
|
|
summary = UserSummary.new(user, guardian)
|
|
|
|
serializer = UserSummarySerializer.new(summary, scope: guardian, root: false)
|
|
|
|
json = serializer.as_json
|
|
|
|
|
2019-01-14 09:56:10 +08:00
|
|
|
expect(json[:likes_given]).to eq(1)
|
2019-01-12 02:09:06 +08:00
|
|
|
expect(json[:likes_received]).to be_present
|
|
|
|
expect(json[:posts_read_count]).to be_present
|
|
|
|
expect(json[:topic_count]).to be_present
|
|
|
|
expect(json[:time_read]).to be_present
|
2019-01-14 09:49:37 +08:00
|
|
|
expect(json[:most_liked_users][0][:count]).to eq(1)
|
|
|
|
expect(json[:most_liked_users][0][:name]).to eq("John Doe")
|
|
|
|
expect(json[:most_liked_users][0][:username]).to eq("john_doe")
|
|
|
|
expect(json[:most_liked_users][0][:avatar_template]).to eq(liked_user.avatar_template)
|
2024-02-05 17:00:36 +08:00
|
|
|
expect(json[:can_see_user_actions]).to eq(true)
|
2019-01-14 09:49:37 +08:00
|
|
|
|
|
|
|
# do not include full name if disabled
|
|
|
|
SiteSetting.enable_names = false
|
|
|
|
expect(serializer.as_json[:most_liked_users][0][:name]).to eq(nil)
|
2019-01-12 02:09:06 +08:00
|
|
|
end
|
2020-07-07 03:44:44 +08:00
|
|
|
|
2024-02-05 17:00:36 +08:00
|
|
|
it "respects hide_user_activity_tab setting" do
|
|
|
|
SiteSetting.hide_user_activity_tab = true
|
|
|
|
guardian = Guardian.new(another_user)
|
|
|
|
summary = UserSummary.new(user, guardian)
|
|
|
|
serializer = UserSummarySerializer.new(summary, scope: guardian, root: false)
|
|
|
|
|
|
|
|
expect(serializer.as_json[:can_see_user_actions]).to eq(false)
|
|
|
|
end
|
|
|
|
|
2020-07-07 03:44:44 +08:00
|
|
|
it "returns correct links data ranking" do
|
2024-01-18 14:08:40 +08:00
|
|
|
topic = Fabricate(:topic, user: Fabricate(:user, refresh_auto_groups: true))
|
2020-07-07 03:44:44 +08:00
|
|
|
post = Fabricate(:post_with_external_links, user: topic.user, topic: topic)
|
|
|
|
TopicLink.extract_from(post)
|
2020-08-13 05:46:12 +08:00
|
|
|
TopicLink
|
|
|
|
.where(topic_id: topic.id)
|
|
|
|
.order(domain: :asc, url: :asc)
|
|
|
|
.each_with_index do |link, index|
|
2020-07-07 03:44:44 +08:00
|
|
|
index.times do |i|
|
|
|
|
TopicLinkClick.create(topic_link: link, ip_address: "192.168.1.#{i + 1}")
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2020-07-07 03:44:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
guardian = Guardian.new
|
|
|
|
summary = UserSummary.new(topic.user, guardian)
|
|
|
|
serializer = UserSummarySerializer.new(summary, scope: guardian, root: false)
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:links][0][:url]).to eq("http://www.codinghorror.com/blog")
|
|
|
|
expect(json[:links][0][:clicks]).to eq(6)
|
|
|
|
expect(json[:links][1][:url]).to eq("http://twitter.com")
|
|
|
|
expect(json[:links][1][:clicks]).to eq(5)
|
|
|
|
expect(json[:links][2][:url]).to eq("https://google.com")
|
|
|
|
expect(json[:links][2][:clicks]).to eq(4)
|
|
|
|
end
|
2019-01-12 02:09:06 +08:00
|
|
|
end
|