2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe ReviewableUser, type: :model do
|
|
|
|
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
2019-04-03 12:04:05 -04:00
|
|
|
let(:user) do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.activate
|
|
|
|
user
|
|
|
|
end
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
context "actions_for" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:reviewable) { Fabricate(:reviewable) }
|
2019-04-17 11:26:43 -04:00
|
|
|
it "returns correct actions in the pending state" do
|
2019-01-03 12:03:01 -05:00
|
|
|
actions = reviewable.actions_for(Guardian.new(moderator))
|
2019-04-17 11:26:43 -04:00
|
|
|
expect(actions.has?(:approve_user)).to eq(true)
|
|
|
|
expect(actions.has?(:reject_user_delete)).to eq(true)
|
|
|
|
expect(actions.has?(:reject_user_block)).to eq(true)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return anything in the approved state" do
|
|
|
|
reviewable.status = Reviewable.statuses[:approved]
|
|
|
|
actions = reviewable.actions_for(Guardian.new(moderator))
|
2019-04-17 11:26:43 -04:00
|
|
|
expect(actions.has?(:approve_user)).to eq(false)
|
|
|
|
expect(actions.has?(:reject_user_delete)).to eq(false)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "#update_fields" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:moderator) { Fabricate(:moderator) }
|
|
|
|
fab!(:reviewable) { Fabricate(:reviewable) }
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
it "doesn't raise errors with an empty update" do
|
|
|
|
expect(reviewable.update_fields(nil, moderator)).to eq(true)
|
|
|
|
expect(reviewable.update_fields({}, moderator)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-03 16:41:04 -04:00
|
|
|
context "when a user is deleted" do
|
|
|
|
it "should reject the reviewable" do
|
2019-04-11 11:11:35 -04:00
|
|
|
SiteSetting.must_approve_users = true
|
2019-04-03 16:41:04 -04:00
|
|
|
Jobs::CreateUserReviewable.new.execute(user_id: user.id)
|
|
|
|
reviewable = Reviewable.find_by(target: user)
|
|
|
|
expect(reviewable.pending?).to eq(true)
|
|
|
|
|
|
|
|
UserDestroyer.new(Discourse.system_user).destroy(user)
|
|
|
|
expect(reviewable.reload.rejected?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
context "perform" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:reviewable) { Fabricate(:reviewable) }
|
2019-01-03 12:03:01 -05:00
|
|
|
context "approve" do
|
|
|
|
it "allows us to approve a user" do
|
2019-04-17 11:26:43 -04:00
|
|
|
result = reviewable.perform(moderator, :approve_user)
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(result.success?).to eq(true)
|
|
|
|
|
|
|
|
expect(reviewable.pending?).to eq(false)
|
|
|
|
expect(reviewable.approved?).to eq(true)
|
|
|
|
expect(reviewable.target.approved?).to eq(true)
|
|
|
|
expect(reviewable.target.approved_by_id).to eq(moderator.id)
|
|
|
|
expect(reviewable.target.approved_at).to be_present
|
|
|
|
expect(reviewable.version > 0).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows us to reject a user" do
|
2019-04-17 11:26:43 -04:00
|
|
|
result = reviewable.perform(moderator, :reject_user_delete)
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(result.success?).to eq(true)
|
|
|
|
|
|
|
|
expect(reviewable.pending?).to eq(false)
|
|
|
|
expect(reviewable.rejected?).to eq(true)
|
|
|
|
|
|
|
|
# Rejecting deletes the user record
|
|
|
|
reviewable.reload
|
|
|
|
expect(reviewable.target).to be_blank
|
|
|
|
end
|
2019-03-29 13:52:53 -04:00
|
|
|
|
2019-04-17 11:26:43 -04:00
|
|
|
it "allows us to reject and block a user" do
|
|
|
|
email = reviewable.target.email
|
|
|
|
ip = reviewable.target.ip_address
|
|
|
|
|
|
|
|
result = reviewable.perform(moderator, :reject_user_block)
|
|
|
|
expect(result.success?).to eq(true)
|
|
|
|
|
|
|
|
expect(reviewable.pending?).to eq(false)
|
|
|
|
expect(reviewable.rejected?).to eq(true)
|
|
|
|
|
|
|
|
# Rejecting deletes the user record
|
|
|
|
reviewable.reload
|
|
|
|
expect(reviewable.target).to be_blank
|
|
|
|
|
|
|
|
expect(ScreenedEmail.should_block?(email)).to eq(true)
|
|
|
|
expect(ScreenedIpAddress.should_block?(ip)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2019-03-29 13:52:53 -04:00
|
|
|
it "allows us to reject a user who has posts" do
|
|
|
|
Fabricate(:post, user: reviewable.target)
|
2019-04-17 11:26:43 -04:00
|
|
|
result = reviewable.perform(moderator, :reject_user_delete)
|
2019-03-29 13:52:53 -04:00
|
|
|
expect(result.success?).to eq(true)
|
|
|
|
|
|
|
|
expect(reviewable.pending?).to eq(false)
|
|
|
|
expect(reviewable.rejected?).to eq(true)
|
|
|
|
|
|
|
|
# Rejecting deletes the user record
|
|
|
|
reviewable.reload
|
|
|
|
expect(reviewable.target).to be_present
|
|
|
|
expect(reviewable.target.approved).to eq(false)
|
|
|
|
end
|
2019-04-10 11:00:14 -04:00
|
|
|
|
|
|
|
it "allows us to reject a user who has been deleted" do
|
|
|
|
reviewable.target.destroy!
|
|
|
|
reviewable.reload
|
2019-04-17 11:26:43 -04:00
|
|
|
result = reviewable.perform(moderator, :reject_user_delete)
|
2019-04-10 11:00:14 -04:00
|
|
|
expect(result.success?).to eq(true)
|
|
|
|
expect(reviewable.rejected?).to eq(true)
|
|
|
|
expect(reviewable.target).to be_blank
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-16 14:42:47 -04:00
|
|
|
describe "changing must_approve_users" do
|
|
|
|
it "will approve any existing users" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
expect(user).not_to be_approved
|
|
|
|
SiteSetting.must_approve_users = true
|
|
|
|
expect(user.reload).to be_approved
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
describe 'when must_approve_users is true' do
|
|
|
|
before do
|
|
|
|
SiteSetting.must_approve_users = true
|
2019-04-03 12:04:05 -04:00
|
|
|
Jobs.run_immediately!
|
2020-07-24 05:16:52 -04:00
|
|
|
@reviewable = ReviewableUser.find_by(target: user)
|
|
|
|
Jobs.run_later!
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates the ReviewableUser for a user, with moderator access" do
|
2020-07-24 05:16:52 -04:00
|
|
|
expect(@reviewable.reviewable_by_moderator).to eq(true)
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "email jobs" do
|
|
|
|
it "enqueues a 'signup after approval' email if must_approve_users is true" do
|
2020-07-24 05:16:52 -04:00
|
|
|
expect_enqueued_with(job: :critical_user_email, args: { type: :signup_after_approval }) do
|
|
|
|
@reviewable.perform(admin, :approve_user)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't enqueue a 'signup after approval' email if must_approve_users is false" do
|
|
|
|
SiteSetting.must_approve_users = false
|
2020-07-24 05:16:52 -04:00
|
|
|
|
|
|
|
expect_not_enqueued_with(job: :critical_user_email, args: { type: :signup_after_approval }) do
|
|
|
|
@reviewable.perform(admin, :approve_user)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'triggers a extensibility event' do
|
|
|
|
user && admin # bypass the user_created event
|
|
|
|
event = DiscourseEvent.track_events {
|
2019-04-17 11:26:43 -04:00
|
|
|
ReviewableUser.find_by(target: user).perform(admin, :approve_user)
|
2019-01-03 12:03:01 -05:00
|
|
|
}.first
|
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:user_approved)
|
|
|
|
expect(event[:params].first).to eq(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'triggers a extensibility event' do
|
|
|
|
user && admin # bypass the user_created event
|
|
|
|
event = DiscourseEvent.track_events {
|
2019-04-17 11:26:43 -04:00
|
|
|
ReviewableUser.find_by(target: user).perform(admin, :approve_user)
|
2019-01-03 12:03:01 -05:00
|
|
|
}.first
|
|
|
|
|
|
|
|
expect(event[:event_name]).to eq(:user_approved)
|
|
|
|
expect(event[:params].first).to eq(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|