2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe TopicViewItem do
|
2014-08-05 01:14:10 -05:00
|
|
|
def add(topic_id, ip, user_id = nil)
|
|
|
|
skip_redis = true
|
|
|
|
TopicViewItem.add(topic_id, ip, user_id, nil, skip_redis)
|
|
|
|
end
|
|
|
|
|
2024-05-27 00:25:32 -05:00
|
|
|
it "correctly increase topic view stats" do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
|
|
|
freeze_time "2021-01-01 12:00"
|
|
|
|
|
|
|
|
add(topic.id, "1.1.1.1", nil)
|
|
|
|
add(topic.id, "1.1.1.1", nil)
|
|
|
|
|
|
|
|
stat = TopicViewStat.find_by(topic_id: topic.id, viewed_at: Date.today)
|
|
|
|
expect(stat.anonymous_views).to eq(2)
|
|
|
|
expect(stat.logged_in_views).to eq(0)
|
|
|
|
|
|
|
|
add(topic.id, "1.1.1.1", topic.user.id)
|
|
|
|
stat.reload
|
|
|
|
|
|
|
|
expect(stat.anonymous_views).to eq(2)
|
|
|
|
expect(stat.logged_in_views).to eq(1)
|
|
|
|
|
|
|
|
freeze_time(1.day.from_now)
|
|
|
|
|
|
|
|
add(topic.id, "1.1.1.1", nil)
|
|
|
|
stat = TopicViewStat.find_by(topic_id: topic.id, viewed_at: Date.today)
|
|
|
|
|
|
|
|
expect(stat.anonymous_views).to eq(1)
|
|
|
|
expect(stat.logged_in_views).to eq(0)
|
|
|
|
end
|
|
|
|
|
2014-08-05 01:14:10 -05:00
|
|
|
it "raises nothing for dupes" do
|
|
|
|
add(2, "1.1.1.1")
|
|
|
|
add(2, "1.1.1.1", 1)
|
|
|
|
|
|
|
|
TopicViewItem.create!(topic_id: 1, ip_address: "1.1.1.1", viewed_at: 1.day.ago)
|
|
|
|
add(1, "1.1.1.1")
|
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(TopicViewItem.count).to eq(3)
|
2014-08-05 01:14:10 -05:00
|
|
|
end
|
2014-08-04 04:07:55 -05:00
|
|
|
|
2014-08-06 23:20:42 -05:00
|
|
|
it "increases a users view count" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
add(1, "1.1.1.1", user.id)
|
|
|
|
add(1, "1.1.1.1", user.id)
|
|
|
|
|
|
|
|
user.user_stat.reload
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(user.user_stat.topics_entered).to eq(1)
|
2014-08-06 23:20:42 -05:00
|
|
|
end
|
|
|
|
|
2018-05-21 20:02:15 -05:00
|
|
|
it "does not log IP address for logged-in users" do
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
user = Fabricate(:user)
|
|
|
|
add(topic.id, "1.1.1.1", user.id)
|
|
|
|
|
|
|
|
expect(TopicViewItem.find_by(topic_id: topic.id, user_id: user.id).ip_address).to eq(nil)
|
|
|
|
add(topic.id, "1.2.3.4", nil)
|
|
|
|
expect(TopicViewItem.find_by(topic_id: topic.id, user_id: nil).ip_address).to eq("1.2.3.4")
|
|
|
|
end
|
2014-08-04 04:07:55 -05:00
|
|
|
end
|