pundit: convert post flags to pundit.
This commit is contained in:
@@ -3,46 +3,85 @@ require 'test_helper'
|
||||
class PostFlagsControllerTest < ActionDispatch::IntegrationTest
|
||||
context "The post flags controller" do
|
||||
setup do
|
||||
@user = create(:user, created_at: 2.weeks.ago)
|
||||
@user = create(:user)
|
||||
@flagger = create(:gold_user, created_at: 2.weeks.ago)
|
||||
@uploader = create(:mod_user, created_at: 2.weeks.ago)
|
||||
@mod = create(:mod_user)
|
||||
@post = create(:post, is_flagged: true, uploader: @uploader)
|
||||
@post_flag = create(:post_flag, post: @post, creator: @flagger)
|
||||
end
|
||||
|
||||
context "new action" do
|
||||
should "render" do
|
||||
get_auth new_post_flag_path, @user
|
||||
get_auth new_post_flag_path, @flagger
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
|
||||
context "show action" do
|
||||
should "work" do
|
||||
get post_flag_path(@post_flag), as: :json
|
||||
assert_response :success
|
||||
end
|
||||
end
|
||||
|
||||
context "index action" do
|
||||
setup do
|
||||
@post_flag = create(:post_flag, creator: @user)
|
||||
should "render" do
|
||||
get post_flags_path
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "render" do
|
||||
should "hide flagger names from regular users" do
|
||||
get_auth post_flags_path, @user
|
||||
assert_response :success
|
||||
assert_select "tr#post-flag-#{@post_flag.id} .flagged-column a.user-gold", false
|
||||
end
|
||||
|
||||
should "hide flagger names from uploaders" do
|
||||
get_auth post_flags_path, @uploader
|
||||
assert_response :success
|
||||
assert_select "tr#post-flag-#{@post_flag.id} .flagged-column a.user-gold", false
|
||||
end
|
||||
|
||||
should "show flagger names to the flagger themselves" do
|
||||
get_auth post_flags_path, @flagger
|
||||
assert_response :success
|
||||
assert_select "tr#post-flag-#{@post_flag.id} .flagged-column a.user-gold", true
|
||||
end
|
||||
|
||||
should "show flagger names to mods" do
|
||||
get_auth post_flags_path, @mod
|
||||
assert_response :success
|
||||
assert_select "tr#post-flag-#{@post_flag.id} .flagged-column a.user-gold", true
|
||||
end
|
||||
|
||||
context "with search parameters" do
|
||||
should "render" do
|
||||
get_auth post_flags_path, @user, params: {:search => {:post_id => @post_flag.post_id}}
|
||||
get_auth post_flags_path(search: { post_id: @post_flag.post_id }), @user
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
should "hide flagged posts when the searcher is the uploader" do
|
||||
get_auth post_flags_path(search: { creator_id: @flagger.id }), @uploader
|
||||
assert_response :success
|
||||
assert_select "tr#post-flag-#{@post_flag.id}", false
|
||||
end
|
||||
|
||||
should "show flagged posts when the searcher is not the uploader" do
|
||||
get_auth post_flags_path(search: { creator_id: @flagger.id }), @mod
|
||||
assert_response :success
|
||||
assert_select "tr#post-flag-#{@post_flag.id}", true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "create action" do
|
||||
setup do
|
||||
@user.as_current do
|
||||
@post = create(:post)
|
||||
end
|
||||
end
|
||||
|
||||
should "create a new flag" do
|
||||
assert_difference("PostFlag.count", 1) do
|
||||
assert_difference("PostFlag.count") do
|
||||
post_auth post_flags_path, @user, params: {:format => "js", :post_flag => {:post_id => @post.id, :reason => "xxx"}}
|
||||
end
|
||||
@post = create(:post)
|
||||
post_auth post_flags_path, @flagger, params: { post_flag: { post_id: @post.id, reason: "xxx" }}, as: :javascript
|
||||
assert_redirected_to PostFlag.last
|
||||
assert_equal(true, @post.reload.is_flagged?)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -95,22 +95,5 @@ class PostFlagTest < ActiveSupport::TestCase
|
||||
assert_equal(@alice.id, @post_flag.creator_id)
|
||||
end
|
||||
end
|
||||
|
||||
context "a moderator user" do
|
||||
should "not be able to view flags on their own uploads" do
|
||||
@dave = create(:moderator_user, created_at: 1.month.ago)
|
||||
@modpost = create(:post, :tag_string => "mmm", :uploader => @dave)
|
||||
@flag1 = create(:post_flag, post: @modpost, creator: @alice)
|
||||
|
||||
assert_equal(false, @dave.can_view_flagger_on_post?(@flag1))
|
||||
|
||||
as(@dave) do
|
||||
flag2 = PostFlag.search(:creator_id => @alice.id)
|
||||
assert_equal(0, flag2.length)
|
||||
flag3 = PostFlag.search({})
|
||||
assert_nil(JSON.parse(flag3.to_json)[0]["creator_id"])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user