Skip to content

A platform for community discussion. Free, open, simple.

FIX: NULL IP for similar users caused incorrect message (#30981)

Martin Brennan08ce000647da
diff --git a/app/models/user.rb b/app/models/user.rb
index 573c29096a4..7986c189402 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1908,6 +1908,8 @@ class User < ActiveRecord::Base
end
def similar_users
+ return User.none if self.ip_address.blank?
+
User
.real
.where.not(id: self.id)
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 2eac7ec6f76..53ab2b0d68e 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -3619,4 +3619,23 @@ RSpec.describe User do
)
end
end
+
+ describe "#similar_users" do
+ fab!(:user2) { Fabricate(:user, ip_address: "1.2.3.4") }
+ fab!(:user3) { Fabricate(:user, ip_address: "1.2.3.4") }
+ fab!(:admin) { Fabricate(:admin, ip_address: "1.2.3.4") }
+ fab!(:moderator) { Fabricate(:moderator, ip_address: "1.2.3.4") }
+
+ before { user.update(ip_address: "1.2.3.4") }
+
+ it "gets users that are not admin, moderator, or current user with the same IP" do
+ expect(user.similar_users).to contain_exactly(user2, user3)
+ end
+
+ it "does not get other users with a null IP if this user has a null IP" do
+ user.update!(ip_address: nil)
+ user2.update!(ip_address: nil)
+ expect(user.similar_users).to eq([])
+ end
+ end
end