Use just one action to hide users

Other than removing a redundant action, we're fixing two bugs when
blocking an author using the links in the public views:

* We were always redirecting to the debates index, even if we blocked
  the author of a proposal or an investment
* We weren't showing any kind of success message
This commit is contained in:
Javi Martín
2021-12-01 21:01:58 +01:00
parent 84c6eeae9c
commit 4c8dfb6695
9 changed files with 40 additions and 26 deletions

View File

@@ -6,16 +6,10 @@ class Moderation::UsersController < Moderation::BaseController
def index
end
def hide_in_moderation_screen
block_user
redirect_with_query_params_to({ action: :index }, { notice: I18n.t("moderation.users.notice_hide") })
end
def hide
block_user
redirect_to debates_path
redirect_with_query_params_to index_path_options, { notice: I18n.t("moderation.users.notice_hide") }
end
private
@@ -28,4 +22,17 @@ class Moderation::UsersController < Moderation::BaseController
@user.block
Activity.log(current_user, :block, @user)
end
def index_path_options
if request.referer
referer_params = Rails.application.routes.recognize_path(request.referer)
referer_params.except(:id).merge({
controller: "/#{referer_params[:controller]}",
action: :index
})
else
{ action: :index }
end
end
end

View File

@@ -2,10 +2,6 @@ class Ability
include CanCan::Ability
def initialize(user)
# If someone can hide something, he can also hide it
# from the moderation screen
alias_action :hide_in_moderation_screen, to: :hide
if user # logged-in users
merge Abilities::Valuator.new(user) if user.valuator?

View File

@@ -23,7 +23,7 @@
<% else %>
<%= render Admin::TableActionsComponent.new(user, actions: []) do |actions| %>
<%= actions.action(
:hide_in_moderation_screen,
:hide,
text: t("moderation.users.index.hide"),
method: :put,
class: "button hollow alert"

View File

@@ -4,7 +4,6 @@ namespace :moderation do
resources :users, only: :index do
member do
put :hide
put :hide_in_moderation_screen
end
end

View File

@@ -2,14 +2,32 @@ require "rails_helper"
describe Moderation::UsersController do
before { sign_in create(:moderator).user }
let(:user) { create(:user, email: "user@consul.dev") }
describe "PUT hide_in_moderation_screen" do
describe "PUT hide" do
it "keeps query parameters while using protected redirects" do
user = create(:user, email: "user@consul.dev")
get :hide_in_moderation_screen, params: { id: user, search: "user@consul.dev", host: "evil.dev" }
get :hide, params: { id: user, search: "user@consul.dev", host: "evil.dev" }
expect(response).to redirect_to "/moderation/users?search=user%40consul.dev"
end
it "redirects to the index of the section where it was called with a notice" do
proposal = create(:proposal, author: user)
request.env["HTTP_REFERER"] = proposal_path(proposal)
put :hide, params: { id: user }
expect(response).to redirect_to proposals_path
expect(flash[:notice]).to eq "User blocked. All of this user's debates and comments have been hidden."
end
it "redirects to the index with a nested resource" do
investment = create(:budget_investment, author: user)
request.env["HTTP_REFERER"] = budget_investment_path(investment.budget, investment)
put :hide, params: { id: user }
expect(response).to redirect_to budget_investments_path(investment.budget)
end
end
end

View File

@@ -52,7 +52,6 @@ describe Abilities::Moderator do
let(:ignored_proposal) { create(:proposal, :with_ignored_flag) }
it { should be_able_to(:hide, comment) }
it { should be_able_to(:hide_in_moderation_screen, comment) }
it { should_not be_able_to(:hide, hidden_comment) }
it { should be_able_to(:hide, own_comment) }
@@ -60,12 +59,10 @@ describe Abilities::Moderator do
it { should_not be_able_to(:moderate, own_comment) }
it { should be_able_to(:hide, debate) }
it { should be_able_to(:hide_in_moderation_screen, debate) }
it { should_not be_able_to(:hide, hidden_debate) }
it { should_not be_able_to(:hide, own_debate) }
it { should be_able_to(:hide, proposal) }
it { should be_able_to(:hide_in_moderation_screen, proposal) }
it { should be_able_to(:hide, own_proposal) }
it { should_not be_able_to(:hide, hidden_proposal) }

View File

@@ -219,7 +219,7 @@ describe "Admin activity" do
within("#proposal_#{proposal.id}") do
accept_confirm("Are you sure? Hide author \"#{proposal.author.name}\"") { click_link "Hide author" }
expect(page).to have_current_path(debates_path)
expect(page).to have_current_path(proposals_path)
end
visit admin_activity_path

View File

@@ -16,7 +16,7 @@ describe "Admin hidden comments", :admin do
accept_confirm("Are you sure? Hide author \"#{proposal.author.name}\"") { click_link "Hide author" }
end
expect(page).to have_current_path debates_path
expect(page).to have_current_path proposals_path
visit admin_hidden_comments_path

View File

@@ -25,10 +25,7 @@ describe "Moderate budget investments" do
accept_confirm("Are you sure? Hide author \"#{investment.author.name}\"") { click_link "Hide author" }
expect(page).to have_current_path(debates_path)
visit budget_investments_path(budget.id, heading_id: heading.id)
expect(page).to have_current_path(budget_investments_path(budget))
expect(page).not_to have_content(investment.title)
end