2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-09 17:17:54 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
RSpec.describe Admin::FlagsController do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
fab!(:post_1) { Fabricate(:post) }
|
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
fab!(:first_post) { Fabricate(:post, post_number: 1) }
|
2017-08-09 17:17:54 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(admin)
|
|
|
|
end
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
context '#index' do
|
2017-08-09 17:17:54 +08:00
|
|
|
it 'should return the right response when nothing is flagged' do
|
|
|
|
get '/admin/flags.json'
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-09 17:17:54 +08:00
|
|
|
|
|
|
|
data = ::JSON.parse(response.body)
|
|
|
|
expect(data["users"]).to eq([])
|
2019-01-04 01:03:01 +08:00
|
|
|
expect(data["flagged_posts"]).to eq([])
|
2017-08-09 17:17:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return the right response' do
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.create(user, post_1, :spam)
|
2017-08-09 17:17:54 +08:00
|
|
|
|
|
|
|
get '/admin/flags.json'
|
|
|
|
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-09 17:17:54 +08:00
|
|
|
|
|
|
|
data = ::JSON.parse(response.body)
|
2018-06-05 15:29:17 +08:00
|
|
|
expect(data["users"].length).to eq(2)
|
2019-01-04 01:03:01 +08:00
|
|
|
expect(data["flagged_posts"].length).to eq(1)
|
2017-08-09 17:17:54 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-24 11:01:11 +08:00
|
|
|
context '#agree' do
|
2018-09-12 11:16:45 +08:00
|
|
|
it 'should raise a reasonable error if a flag was deferred and then someone else agreed' do
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2018-09-12 11:16:45 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
reviewable = PostActionCreator.spam(user, post_1).reviewable
|
2018-09-12 11:16:45 +08:00
|
|
|
|
|
|
|
post "/admin/flags/defer/#{post_1.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
2019-01-04 01:03:01 +08:00
|
|
|
expect(reviewable.reload).to be_ignored
|
2018-09-12 11:16:45 +08:00
|
|
|
|
|
|
|
post "/admin/flags/agree/#{post_1.id}.json", params: { action_on_post: 'keep' }
|
|
|
|
# 409 means conflict which is what is happening here
|
|
|
|
expect(response.status).to eq(409)
|
|
|
|
error = JSON.parse(response.body)["errors"].first
|
|
|
|
expect(error).to eq(I18n.t("flags.errors.already_handled"))
|
|
|
|
end
|
|
|
|
|
2018-08-07 08:05:43 +08:00
|
|
|
it 'should be able to agree and keep content' do
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2018-08-07 08:05:43 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
result = PostActionCreator.spam(user, post_1)
|
|
|
|
reviewable = result.reviewable
|
2018-08-07 08:05:43 +08:00
|
|
|
|
|
|
|
post "/admin/flags/agree/#{post_1.id}.json", params: { action_on_post: 'keep' }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
expect(reviewable.reload).to be_approved
|
|
|
|
|
|
|
|
approve_history = reviewable.reviewable_histories.where(
|
|
|
|
created_by: admin,
|
|
|
|
reviewable_history_type: ReviewableHistory.types[:transitioned],
|
|
|
|
status: Reviewable.statuses[:approved]
|
|
|
|
)
|
|
|
|
expect(approve_history).to be_present
|
2018-11-01 03:35:07 +08:00
|
|
|
expect(user.user_stat.reload.flags_agreed).to eq(1)
|
2018-08-07 08:05:43 +08:00
|
|
|
|
|
|
|
post_1.reload
|
|
|
|
expect(post_1.deleted_at).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should be able to hide spam' do
|
2017-08-09 17:17:54 +08:00
|
|
|
SiteSetting.allow_user_locale = true
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2018-05-31 15:53:49 +08:00
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
post_action = PostActionCreator.new(user, post_1, PostActionType.types[:spam], message: 'bad').perform.post_action
|
2017-08-09 17:17:54 +08:00
|
|
|
admin.update!(locale: 'ja')
|
|
|
|
|
2018-11-23 21:58:04 +08:00
|
|
|
post "/admin/flags/agree/#{post_1.id}.json", params: { action_on_post: 'delete' }
|
2018-05-31 15:53:49 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-08-09 17:17:54 +08:00
|
|
|
|
|
|
|
post_action.reload
|
|
|
|
|
|
|
|
expect(post_action.agreed_by_id).to eq(admin.id)
|
2018-11-01 03:35:07 +08:00
|
|
|
expect(user.user_stat.reload.flags_agreed).to eq(1)
|
2017-08-09 17:17:54 +08:00
|
|
|
|
2018-06-05 15:29:17 +08:00
|
|
|
agree_post = Topic.joins(:topic_allowed_users).where('topic_allowed_users.user_id = ?', user.id).order(:id).last.posts.last
|
2018-11-23 21:58:04 +08:00
|
|
|
expect(agree_post.raw).to eq(I18n.with_locale(:en) { I18n.t('flags_dispositions.agreed_and_deleted') })
|
|
|
|
|
|
|
|
post_1.reload
|
|
|
|
expect(post_1.deleted_at).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not delete category topic' do
|
2019-03-14 22:47:38 +08:00
|
|
|
Jobs.run_immediately!
|
2018-11-23 21:58:04 +08:00
|
|
|
category.update_column(:topic_id, first_post.topic_id)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.new(user, first_post, PostActionType.types[:spam], message: 'bad').perform
|
2018-11-23 21:58:04 +08:00
|
|
|
|
|
|
|
post "/admin/flags/agree/#{first_post.id}.json", params: { action_on_post: 'delete' }
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
|
|
|
|
first_post.reload
|
|
|
|
expect(first_post.deleted_at).to eq(nil)
|
2017-08-09 17:17:54 +08:00
|
|
|
end
|
|
|
|
end
|
2019-02-08 02:46:05 +08:00
|
|
|
|
|
|
|
context '#disagree' do
|
|
|
|
it "unhides the post and unsilences the user if disagreed" do
|
2019-05-25 02:13:03 +08:00
|
|
|
Reviewable.set_priorities(high: 1.0)
|
|
|
|
SiteSetting.silence_new_user_sensitivity = Reviewable.sensitivity[:low]
|
2019-02-08 02:46:05 +08:00
|
|
|
SiteSetting.num_users_to_silence_new_user = 1
|
|
|
|
|
|
|
|
new_user = Fabricate(:newuser)
|
|
|
|
new_post = create_post(user: new_user)
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
PostActionCreator.spam(Fabricate(:leader), new_post)
|
2019-02-08 02:46:05 +08:00
|
|
|
|
|
|
|
post "/admin/flags/disagree/#{new_post.id}.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
new_post.reload
|
|
|
|
new_user.reload
|
|
|
|
|
|
|
|
expect(new_post).to_not be_hidden
|
|
|
|
expect(new_post.spam_count).to eq(0)
|
|
|
|
expect(new_user).to_not be_silenced
|
|
|
|
end
|
|
|
|
end
|
2017-08-09 17:17:54 +08:00
|
|
|
end
|