Commit e042473dc49e5403d031a88697ac4f91e92361ff
1 parent
2fd03497
Exists in
master
and in
22 other branches
Spammer logger
Also logging spammer when comment is marked as spam
Showing
4 changed files
with
59 additions
and
0 deletions
Show diff stats
app/models/comment.rb
| @@ -224,6 +224,7 @@ class Comment < ActiveRecord::Base | @@ -224,6 +224,7 @@ class Comment < ActiveRecord::Base | ||
| 224 | def spam! | 224 | def spam! |
| 225 | self.spam = true | 225 | self.spam = true |
| 226 | self.save! | 226 | self.save! |
| 227 | + SpammerLogger.log(ip_address, self) | ||
| 227 | Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam)) | 228 | Delayed::Job.enqueue(CommentHandler.new(self.id, :marked_as_spam)) |
| 228 | self | 229 | self |
| 229 | end | 230 | end |
| @@ -0,0 +1,24 @@ | @@ -0,0 +1,24 @@ | ||
| 1 | +class SpammerLogger < Logger | ||
| 2 | + @logpath = File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_spammers.log") | ||
| 3 | + @logger = new(@logpath) | ||
| 4 | + | ||
| 5 | + def self.log(spammer_ip, object=nil) | ||
| 6 | + if object | ||
| 7 | + if object.kind_of?(Comment) | ||
| 8 | + @logger << "[#{Time.now.strftime("%F %T %z")}] Comment-id: #{object.id} IP: #{spammer_ip}\n" | ||
| 9 | + end | ||
| 10 | + else | ||
| 11 | + @logger << "[#{Time.now.strftime("%F %T %z")}] IP: #{spammer_ip}\n" | ||
| 12 | + end | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | + def self.clean_log | ||
| 16 | + File.delete(@logpath) if File.exists?(@logpath) | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + def self.reload_log | ||
| 20 | + clean_log | ||
| 21 | + @logger = new(@logpath) | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | +end |
test/unit/comment_test.rb
| @@ -563,6 +563,14 @@ class CommentTest < ActiveSupport::TestCase | @@ -563,6 +563,14 @@ class CommentTest < ActiveSupport::TestCase | ||
| 563 | assert_equal Environment.default, comment.environment | 563 | assert_equal Environment.default, comment.environment |
| 564 | end | 564 | end |
| 565 | 565 | ||
| 566 | + should 'log spammer ip after marking comment as spam' do | ||
| 567 | + comment = create_comment(:ip_address => '192.168.0.1') | ||
| 568 | + comment.spam! | ||
| 569 | + log = File.open('log/test_spammers.log') | ||
| 570 | + assert_match "Comment-id: #{comment.id} IP: 192.168.0.1", log.read | ||
| 571 | + SpammerLogger.clean_log | ||
| 572 | + end | ||
| 573 | + | ||
| 566 | private | 574 | private |
| 567 | 575 | ||
| 568 | def create_comment(args = {}) | 576 | def create_comment(args = {}) |
| @@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | + | ||
| 3 | +class SpammerLoggerTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + def setup | ||
| 6 | + SpammerLogger.reload_log | ||
| 7 | + end | ||
| 8 | + | ||
| 9 | + def teardown | ||
| 10 | + SpammerLogger.clean_log | ||
| 11 | + end | ||
| 12 | + | ||
| 13 | + should 'log the spammer ip' do | ||
| 14 | + SpammerLogger.log('192.168.0.1') | ||
| 15 | + log = File.open('log/test_spammers.log') | ||
| 16 | + assert_match 'IP: 192.168.0.1', log.read | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + should 'log the spammer ip with comment associated' do | ||
| 20 | + comment = fast_create(Comment) | ||
| 21 | + SpammerLogger.log('192.168.0.1', comment) | ||
| 22 | + log = File.open('log/test_spammers.log') | ||
| 23 | + assert_match "Comment-id: #{comment.id} IP: 192.168.0.1", log.read | ||
| 24 | + end | ||
| 25 | + | ||
| 26 | +end |