Skip to content

Commit

Permalink
Couple more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Undo1 committed May 14, 2017
1 parent cbd2e6b commit 91e4ba6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Post < ApplicationRecord
end

def autoflag
return unless Post.where(:link => link).count == 1
return unless FlagSetting['flagging_enabled'] == '1'
return "Duplicate post" unless Post.where(:link => link).count == 1
return "Flagging disabled" unless FlagSetting['flagging_enabled'] == '1'

dry_run = FlagSetting['dry_run'] == '1'
post = self
Expand All @@ -51,13 +51,13 @@ def autoflag
users = User.where(:id => uids, :flags_enabled => true).where.not(:encrypted_api_token => nil)
unless users.present?
post.send_not_autoflagged
Thread.exit
return "No users eligible to flag"
end

post.fetch_revision_count
unless post.revision_count == 1
post.send_not_autoflagged
Thread.exit
return "More than one revision"
end

max_flags = [post.site.max_flags_per_post, (FlagSetting['max_flags'] || '3').to_i].min
Expand Down
19 changes: 16 additions & 3 deletions test/integration/flagging_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ def setup
@stack_id = 1234
@multi_rev_stack_id = 4321

@post = Post.new({
@post = Post.create({
link: "//#{@site.site_domain}/questions/#{@stack_id}",
site: @site
})

@multi_rev_post = Post.new({
@multi_rev_post = Post.create({
link: "//#{@site.site_domain}/questions/#{@multi_rev_stack_id}",
site: @site
})

@limited_post = Post.new({
@limited_post = Post.create({
link: "//#{@limited_site.site_domain}/questions/#{@stack_id}",
site: @limited_site
})
Expand All @@ -46,6 +46,8 @@ def setup
end

def setup_webmock
WebMock.reset!

stub_request(:get, /https:\/\/api.stackexchange.com\/2\.2\/me\/associated/).
to_return(:status => 200, :body => webmock_file("mod_sites_response"), :headers => {})

Expand Down Expand Up @@ -99,4 +101,15 @@ def webmock_file(name)
assert_requested @multi_rev_stub
assert_operator @multi_rev_post.revision_count, :>, 1
end

test "shouldn't flag if flagging is disabled" do
FlagSetting.find_by_name('flagging_enabled').update(value: '0')
assert_equal @post.autoflag, "Flagging disabled"
end

test "shouldn't flag if there are no eligible users" do
@user.update(flags_enabled: false)

assert_equal @post.autoflag, "No users eligible to flag"
end
end

0 comments on commit 91e4ba6

Please sign in to comment.