2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-14 02:51:17 -05:00
|
|
|
RSpec.describe UserProfileView do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:other_user) { Fabricate(:user) }
|
2015-09-14 02:51:17 -05:00
|
|
|
let(:user_profile_id) { user.user_profile.id }
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def add(user_profile_id, ip, user_id = nil, at = nil)
|
2015-09-14 02:51:17 -05:00
|
|
|
described_class.add(user_profile_id, ip, user_id, at, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should increase user's profile view count" do
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { add(user_profile_id, "1.1.1.1") }.to change { described_class.count }.by(1)
|
2015-09-14 02:51:17 -05:00
|
|
|
expect(user.user_profile.reload.views).to eq(1)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect { add(user_profile_id, "1.1.1.1", other_user.id) }.to change {
|
|
|
|
described_class.count
|
|
|
|
}.by(1)
|
2015-09-14 02:51:17 -05:00
|
|
|
|
|
|
|
user_profile = user.user_profile.reload
|
|
|
|
expect(user_profile.views).to eq(2)
|
|
|
|
expect(user_profile.user_profile_views).to eq(described_class.all)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not create duplicated profile view for anon user" do
|
|
|
|
time = Time.zone.now
|
|
|
|
|
|
|
|
2.times do
|
2023-01-09 05:18:21 -06:00
|
|
|
add(user_profile_id, "1.1.1.1", nil, time)
|
2015-09-14 02:51:17 -05:00
|
|
|
expect(described_class.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-21 20:04:54 -05:00
|
|
|
it "should not create duplicated profile view or log IP for signed in user" do
|
2015-09-14 02:51:17 -05:00
|
|
|
time = Time.zone.now
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
%w[1.1.1.1 2.2.2.2].each do |ip|
|
2015-09-14 02:51:17 -05:00
|
|
|
add(user_profile_id, ip, other_user.id, time)
|
|
|
|
expect(described_class.count).to eq(1)
|
2018-05-21 20:04:54 -05:00
|
|
|
expect(UserProfileView.find_by(user_id: other_user.id).ip_address).to eq(nil)
|
2015-09-14 02:51:17 -05:00
|
|
|
end
|
|
|
|
end
|
2016-08-23 03:25:31 -05:00
|
|
|
|
|
|
|
it "should not create a profile view for the system user" do
|
2023-01-09 05:18:21 -06:00
|
|
|
add(user_profile_id, "1.1.1.1", Discourse::SYSTEM_USER_ID)
|
2016-08-23 03:25:31 -05:00
|
|
|
expect(described_class.count).to eq(0)
|
|
|
|
end
|
2015-09-14 02:51:17 -05:00
|
|
|
end
|