From 8aab2253d06b03b958f1dc60943fe022565278e6 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 23 Oct 2013 17:11:11 -0400 Subject: [PATCH] Whitelisted ip addresses will not be flagged as spam by flag_sockpuppets --- app/models/screened_ip_address.rb | 10 ++++++- app/services/spam_rules_enforcer.rb | 3 +- spec/models/screened_ip_address_spec.rb | 35 +++++++++++++++++++++++ spec/services/spam_rules_enforcer_spec.rb | 10 +++++-- 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/app/models/screened_ip_address.rb b/app/models/screened_ip_address.rb index 6822e45c3..2d5ce4bf4 100644 --- a/app/models/screened_ip_address.rb +++ b/app/models/screened_ip_address.rb @@ -25,8 +25,16 @@ class ScreenedIpAddress < ActiveRecord::Base end def self.should_block?(ip_address) + exists_for_ip_address_and_action?(ip_address, actions[:block]) + end + + def self.is_whitelisted?(ip_address) + exists_for_ip_address_and_action?(ip_address, actions[:do_nothing]) + end + + def self.exists_for_ip_address_and_action?(ip_address, action_type) b = match_for_ip_address(ip_address) b.record_match! if b - !!b and b.action_type == actions[:block] + !!b and b.action_type == action_type end end diff --git a/app/services/spam_rules_enforcer.rb b/app/services/spam_rules_enforcer.rb index 2a07b2822..16475b267 100644 --- a/app/services/spam_rules_enforcer.rb +++ b/app/services/spam_rules_enforcer.rb @@ -35,7 +35,8 @@ class SpamRulesEnforcer !first_post.user.staff? and !@post.user.staff? and @post.user != first_post.user and @post.user.ip_address == first_post.user.ip_address and - @post.user.new_user? + @post.user.new_user? and + !ScreenedIpAddress.is_whitelisted?(@post.user.ip_address) end def flag_sockpuppet_users diff --git a/spec/models/screened_ip_address_spec.rb b/spec/models/screened_ip_address_spec.rb index b59d0ebe3..6f282a10b 100644 --- a/spec/models/screened_ip_address_spec.rb +++ b/spec/models/screened_ip_address_spec.rb @@ -146,4 +146,39 @@ describe ScreenedIpAddress do end end end + + describe '#is_whitelisted?' do + it 'returns false when record does not exist' do + described_class.is_whitelisted?(ip_address).should eq(false) + end + + it 'returns false when no record matches' do + Fabricate(:screened_ip_address, ip_address: '111.234.23.11', action_type: described_class.actions[:do_nothing]) + described_class.is_whitelisted?('222.12.12.12').should eq(false) + end + + context 'IPv4' do + it 'returns true when when record matches and action is :do_nothing' do + Fabricate(:screened_ip_address, ip_address: '111.234.23.11', action_type: described_class.actions[:do_nothing]) + described_class.is_whitelisted?('111.234.23.11').should eq(true) + end + + it 'returns false when when record matches and action is :block' do + Fabricate(:screened_ip_address, ip_address: '111.234.23.11', action_type: described_class.actions[:block]) + described_class.is_whitelisted?('111.234.23.11').should eq(false) + end + end + + context 'IPv6' do + it 'returns true when when record matches and action is :do_nothing' do + Fabricate(:screened_ip_address, ip_address: '2001:db8::ff00:42:8329', action_type: described_class.actions[:do_nothing]) + described_class.is_whitelisted?('2001:db8::ff00:42:8329').should eq(true) + end + + it 'returns false when when record matches and action is :block' do + Fabricate(:screened_ip_address, ip_address: '2001:db8::ff00:42:8329', action_type: described_class.actions[:block]) + described_class.is_whitelisted?('2001:db8::ff00:42:8329').should eq(false) + end + end + end end diff --git a/spec/services/spam_rules_enforcer_spec.rb b/spec/services/spam_rules_enforcer_spec.rb index b4ddfa87b..f68ca3cd5 100644 --- a/spec/services/spam_rules_enforcer_spec.rb +++ b/spec/services/spam_rules_enforcer_spec.rb @@ -20,11 +20,17 @@ describe SpamRulesEnforcer do SpamRulesEnforcer.new(post2).reply_is_from_sockpuppet?.should eq(false) end - it 'is true if users have the same IP address' do - post2 = Fabricate(:post, user: Fabricate(:user, ip_address: '182.189.119.174'), topic: post1.topic) + it 'is true if users have the same IP address and are new' do + post2 = Fabricate(:post, user: Fabricate(:user, ip_address: user1.ip_address), topic: post1.topic) SpamRulesEnforcer.new(post2).reply_is_from_sockpuppet?.should eq(true) end + it 'is false if the ip address is whitelisted' do + ScreenedIpAddress.stubs(:is_whitelisted?).with(user1.ip_address).returns(true) + post2 = Fabricate(:post, user: Fabricate(:user, ip_address: user1.ip_address), topic: post1.topic) + SpamRulesEnforcer.new(post2).reply_is_from_sockpuppet?.should eq(false) + end + it 'is false if reply and first post are from the same user' do post2 = Fabricate(:post, user: user1, topic: post1.topic) SpamRulesEnforcer.new(post2).reply_is_from_sockpuppet?.should eq(false)