From df9c087e85524d4cccc21ad7f208914940c71bd2 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Fri, 2 Aug 2013 16:55:30 -0700 Subject: [PATCH] fixes #826 --- app/models/post_flag.rb | 14 ++++++-------- test/unit/post_flag_test.rb | 23 ++++++++++++++++++++--- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/app/models/post_flag.rb b/app/models/post_flag.rb index 80e1ef9df..987578510 100644 --- a/app/models/post_flag.rb +++ b/app/models/post_flag.rb @@ -61,21 +61,19 @@ class PostFlag < ActiveRecord::Base def validate_creator_is_not_limited if CurrentUser.is_janitor? - false - elsif flag_count_for_creator >= 10 + # do nothing + elsif creator.created_at > 1.week.ago + errors[:creator] << "cannot flag within the first week of sign up" + elsif creator.is_gold? && flag_count_for_creator >= 10 errors[:creator] << "can flag 10 posts a day" - false - else - true + elsif !creator.is_gold? && flag_count_for_creator >= 1 + errors[:creator] << "can flag 1 post a day" end end def validate_post_is_active if post.is_deleted? errors[:post] << "is deleted" - false - else - true end end diff --git a/test/unit/post_flag_test.rb b/test/unit/post_flag_test.rb index be1e75bb1..9e4afa602 100644 --- a/test/unit/post_flag_test.rb +++ b/test/unit/post_flag_test.rb @@ -3,10 +3,13 @@ require 'test_helper' class PostFlagTest < ActiveSupport::TestCase context "In all cases" do setup do - @alice = FactoryGirl.create(:user) + Timecop.travel(2.weeks.ago) do + @alice = FactoryGirl.create(:gold_user) + end CurrentUser.user = @alice CurrentUser.ip_addr = "127.0.0.1" MEMCACHE.flush_all + @post = FactoryGirl.create(:post, :tag_string => "aaa") end teardown do @@ -14,11 +17,25 @@ class PostFlagTest < ActiveSupport::TestCase CurrentUser.ip_addr = nil end - context "a user" do + context "a basic user" do setup do - @post = FactoryGirl.create(:post, :tag_string => "aaa") + Timecop.travel(2.weeks.ago) do + @bob = FactoryGirl.create(:user) + end + CurrentUser.user = @bob end + should "not be able to flag more than 1 post in 24 hours" do + @post_flag = PostFlag.new(:post => @post, :reason => "aaa", :is_resolved => false) + @post_flag.expects(:flag_count_for_creator).returns(1) + assert_difference("PostFlag.count", 0) do + @post_flag.save + end + assert_equal(["You can flag 1 post a day"], @post_flag.errors.full_messages) + end + end + + context "a gold user" do should "not be able to flag a post more than twice" do assert_difference("PostFlag.count", 1) do @post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)