Ensure super moderator actions Note & AuditLog (#18569)

* Specs - before including changes requires

* Include AuditLog Subscribe listen/forget directives

* Ensure Notes (w/ default content) + AuditLog

* Remove old spec - no longer needed
This commit is contained in:
Fernando Valverde 2022-10-12 09:36:41 -06:00 committed by GitHub
parent d230eb97c1
commit 0a0e2b0a7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 14 deletions

View file

@ -151,7 +151,6 @@ module Admin
end
end
Credits::Manage.call(@user, credit_params)
add_note if user_params[:new_note]
end
def export_data
@ -195,10 +194,12 @@ module Admin
def unpublish_all_articles
target_user = User.find(params[:id].to_i)
Moderator::UnpublishAllArticlesWorker.perform_async(target_user.id, current_user.id, "moderator")
if params[:note] && params[:note][:content]
Note.create(noteable: target_user, reason: "unpublish_all_articles",
content: params[:note][:content], author: current_user)
end
note_content = params.dig(:note, :content).presence
note_content ||= "#{current_user.username} unpublished all articles"
Note.create(noteable: target_user, reason: "unpublish_all_articles",
content: note_content, author: current_user)
message = I18n.t("admin.users_controller.unpublished")
respond_to do |format|
format.html do

View file

@ -215,6 +215,14 @@ class ArticlesController < ApplicationController
result = Articles::Unpublish.call(current_user, @article)
if result.success
Audit::Logger.log(:moderator, current_user, params.dup)
Note.create(
noteable: @article.user,
reason: "unpublish_article",
content: "#{current_user.username} unpublished post with ID #{@article.id}",
author: current_user,
)
render json: { message: "success", path: @article.current_state_path }, status: :ok
else
render json: { message: @article.errors.full_messages }, status: :unprocessable_entity

View file

@ -10,7 +10,7 @@ const suspendOrUnsuspendUser = async ({
btnAction,
userId,
username,
suspendOrUnsuspendReason,
actionReason,
}) => {
event.preventDefault();
closeModal();
@ -23,7 +23,7 @@ const suspendOrUnsuspendUser = async ({
body: JSON.stringify({
id: userId,
user: {
note_for_current_role: suspendOrUnsuspendReason,
note_for_current_role: actionReason,
user_status: btnAction == 'suspend' ? 'Suspended' : 'Good standing',
},
}),

View file

@ -54,7 +54,7 @@ module Moderator
noteable_id: @user.id,
noteable_type: "User",
reason: reason,
content: content,
content: content || "#{@admin.username} updated #{@user.username}",
)
end

View file

@ -313,12 +313,6 @@ RSpec.describe "/admin/member_manager/users", type: :request do
expect(note.author_id).to eq(admin.id)
end
it "doesn't create a note if note content was not passed" do
expect do
post unpublish_all_articles_admin_user_path(target_user.id, note: { content: "" })
end.not_to change(Note, :count)
end
it "unpublishes all articles" do
allow(Moderator::UnpublishAllArticlesWorker).to receive(:perform_async)
post unpublish_all_articles_admin_user_path(target_user.id)

View file

@ -172,4 +172,87 @@ RSpec.describe "Moderations", type: :request do
end
end
end
describe "Super moderator Note & AuditLog" do
let(:super_mod) { create(:user, :super_moderator) }
before do
sign_in super_mod
Audit::Subscribe.listen(:moderator)
end
after { Audit::Subscribe.forget(:moderator) }
context "when unpublish a post of user" do
it "creates a note on a user" do
expected_note = "#{super_mod.username} unpublished post with ID #{article.id}"
expect do
patch "/articles/#{article.id}/admin_unpublish",
params: { slug: article.slug, username: article.user.username }
end.to change(Note, :count).by(1)
expect(Note.last.content).to eq(expected_note)
end
it "creates an AuditLog for the action taken" do
expect do
patch "/articles/#{article.id}/admin_unpublish",
params: { slug: article.slug, username: article.user.username }
end.to change(AuditLog, :count).by(1)
end
end
context "when unpublish all posts of user" do
it "creates a note on a user when note content is provided" do
note_content = "Unpublish due to Spam"
expect do
post "/admin/member_manager/users/#{article.user_id}/unpublish_all_articles",
params: { note: { content: note_content } }
end.to change(Note, :count).by(1)
expect(Note.last.content).to eq(note_content)
end
it "creates a default note on a user when note content isn't provided" do
expected_note = "#{super_mod.username} unpublished all articles"
expect do
post "/admin/member_manager/users/#{article.user_id}/unpublish_all_articles"
end.to change(Note, :count).by(1)
expect(Note.last.content).to eq(expected_note)
end
it "creates an AuditLog for the action taken" do
expect do
post "/admin/member_manager/users/#{article.user_id}/unpublish_all_articles",
params: { note: { content: "Test note" } }
sidekiq_perform_enqueued_jobs
end.to change(AuditLog, :count).by(1)
end
end
context "when suspend user" do
it "creates a note on a user when note content is provided" do
note_content = "Unpublish due to Spam"
expect do
patch "/admin/member_manager/users/#{article.user_id}/user_status",
params: { user: { user_status: "Suspend", note_for_current_role: note_content } }
end.to change(Note, :count).by(1)
expect(Note.last.content).to eq(note_content)
end
it "creates a default note on a user when note content isn't provided" do
expected_note = "#{super_mod.username} updated #{article.user.username}"
expect do
patch "/admin/member_manager/users/#{article.user_id}/user_status",
params: { user: { user_status: "Suspend" } }
end.to change(Note, :count).by(1)
expect(Note.last.content).to eq(expected_note)
end
it "creates an AuditLog for the action taken" do
expect do
patch "/admin/member_manager/users/#{article.user_id}/user_status",
params: { user: { user_status: "Suspend", new_note: "Test note" } }
end.to change(AuditLog, :count).by(1)
end
end
end
end