role deprecation cleanup (#15717)

* User decorator (and spec) should use `trusted?`

Fixes a few issues seen in an rspec run that show as:

    DEPRECATION WARNING: User#trusted is deprecated, favor
    User#trusted? (called from config_body_class at
    /opt/apps/forem/app/decorators/user_decorator.rb:58)

And here:

/opt/apps/forem/spec/decorators/user_decorator_spec.rb:112
/opt/apps/forem/spec/decorators/user_decorator_spec.rb:121
/opt/apps/forem/spec/decorators/user_decorator_spec.rb:130
/opt/apps/forem/spec/decorators/user_decorator_spec.rb:139

* prefer User trusted? to trusted

DEPRECATION WARNING: User#trusted is deprecated, favor
User#trusted? (called from permissions at
/opt/apps/forem/app/models/rating_vote.rb:25)

* Prefer trusted? to trusted in user spec

* Use warned? rather than warned in admin article partial

* use trusted? rather than trusted in moderator requests spec

* Prefer trusted? to trusted in moderations controller

* Prefer trusted? to trusted in moderations view

* User auditable? should call trusted? and not trusted

Deprecations go rolling right along.

* Invert guard clause logic to be positive

The original "return unless multiple negated conditions hold" guard
was cumbersome.

Invert to return if any of the exceptions apply, namely:

- this is a comment or readinglist rating (rather than explicit),
allowed for all
- this rating is from a moderator/trusted user (allowed)
- this rating is offered by the article's author (allowed)

I had intended to also remove the safe navigation operators (since it
wasn't clear why there would be a null user or null article, as
rating_vote joins users to articles with a score), but the builtin
validation tests (is expected to validate ...) build objects with
missing attributes, and raise errors when the spec is run.
This commit is contained in:
Daniel Uber 2021-12-09 15:27:45 -06:00 committed by GitHub
parent 09eda6a0cc
commit 9f9594ccdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 19 additions and 18 deletions

View file

@ -10,7 +10,7 @@ class ModerationsController < ApplicationController
def index
skip_authorization
return unless current_user&.trusted
return unless current_user&.trusted?
articles = Article.published
.order(published_at: :desc).limit(70)

View file

@ -55,7 +55,7 @@ class UserDecorator < ApplicationDecorator
body_class = [
setting.config_theme.tr("_", "-"),
"#{setting.resolved_font_name.tr('_', '-')}-article-body",
"trusted-status-#{trusted}",
"trusted-status-#{trusted?}",
"#{setting.config_navbar.tr('_', '-')}-header",
]
body_class.join(" ")

View file

@ -22,7 +22,7 @@ class RatingVote < ApplicationRecord
end
def permissions
return unless context == "explicit" && !user&.trusted && user_id != article&.user_id
return if user == article&.user || user&.trusted? || context != "explicit"
errors.add(:user_id, "is not permitted to take this action.")
end

View file

@ -483,7 +483,7 @@ class User < ApplicationRecord
end
def auditable?
trusted || tag_moderator? || any_admin?
trusted? || tag_moderator? || any_admin?
end
def tag_moderator?

View file

@ -82,7 +82,7 @@
<% cache "admin-user-info-#{article.user_id}-#{article.user&.updated_at}", expires_in: 4.hours do %>
<% if article.user&.warned %>
<% if article.user&.warned? %>
<h2><span class="badge badge-warning">USER WARNED</span></h2>
<% end %>

View file

@ -4,7 +4,7 @@
<div id="moderation-page" aria-hidden="true"></div>
<% if current_user&.trusted %>
<% if current_user&.trusted? %>
<div class="crayons-layout crayons-layout--2-cols" id="mod-center">
<%= render "moderations/mod_sidebar_left" %>
<main class="mod-index-container articles-list crayons-layout__content">

View file

@ -109,7 +109,7 @@ RSpec.describe UserDecorator, type: :decorator do
it "creates proper body class with defaults" do
expected_result = %W[
light-theme sans-serif-article-body
trusted-status-#{user.trusted} #{user.setting.config_navbar}-header
trusted-status-#{user.trusted?} #{user.setting.config_navbar}-header
].join(" ")
expect(user.decorate.config_body_class).to eq(expected_result)
end
@ -118,7 +118,7 @@ RSpec.describe UserDecorator, type: :decorator do
user.setting.config_font = "sans_serif"
expected_result = %W[
light-theme sans-serif-article-body
trusted-status-#{user.trusted} #{user.setting.config_navbar}-header
trusted-status-#{user.trusted?} #{user.setting.config_navbar}-header
].join(" ")
expect(user.decorate.config_body_class).to eq(expected_result)
end
@ -127,7 +127,7 @@ RSpec.describe UserDecorator, type: :decorator do
user.setting.config_theme = "dark_theme"
expected_result = %W[
dark-theme sans-serif-article-body
trusted-status-#{user.trusted} #{user.setting.config_navbar}-header
trusted-status-#{user.trusted?} #{user.setting.config_navbar}-header
].join(" ")
expect(user.decorate.config_body_class).to eq(expected_result)
end
@ -136,7 +136,7 @@ RSpec.describe UserDecorator, type: :decorator do
user.setting.config_navbar = "static"
expected_result = %W[
light-theme sans-serif-article-body
trusted-status-#{user.trusted} static-header
trusted-status-#{user.trusted?} static-header
].join(" ")
expect(user.decorate.config_body_class).to eq(expected_result)
end

View file

@ -612,7 +612,7 @@ RSpec.describe User, type: :model do
it "creates proper body class with defaults" do
# rubocop:disable Layout/LineLength
classes = "light-theme sans-serif-article-body trusted-status-#{user.trusted} #{user.setting.config_navbar}-header"
classes = "light-theme sans-serif-article-body trusted-status-#{user.trusted?} #{user.setting.config_navbar}-header"
# rubocop:enable Layout/LineLength
expect(user.decorate.config_body_class).to eq(classes)
end
@ -621,7 +621,7 @@ RSpec.describe User, type: :model do
user.setting.config_font = "sans_serif"
# rubocop:disable Layout/LineLength
classes = "light-theme sans-serif-article-body trusted-status-#{user.trusted} #{user.setting.config_navbar}-header"
classes = "light-theme sans-serif-article-body trusted-status-#{user.trusted?} #{user.setting.config_navbar}-header"
# rubocop:enable Layout/LineLength
expect(user.decorate.config_body_class).to eq(classes)
end
@ -630,7 +630,7 @@ RSpec.describe User, type: :model do
user.setting.config_font = "open_dyslexic"
# rubocop:disable Layout/LineLength
classes = "light-theme open-dyslexic-article-body trusted-status-#{user.trusted} #{user.setting.config_navbar}-header"
classes = "light-theme open-dyslexic-article-body trusted-status-#{user.trusted?} #{user.setting.config_navbar}-header"
# rubocop:enable Layout/LineLength
expect(user.decorate.config_body_class).to eq(classes)
end
@ -638,7 +638,8 @@ RSpec.describe User, type: :model do
it "creates proper body class with dark theme" do
user.setting.config_theme = "dark_theme"
classes = "dark-theme sans-serif-article-body trusted-status-#{user.trusted} #{user.setting.config_navbar}-header"
classes =
"dark-theme sans-serif-article-body trusted-status-#{user.trusted?} #{user.setting.config_navbar}-header"
expect(user.decorate.config_body_class).to eq(classes)
end
end
@ -801,7 +802,7 @@ RSpec.describe User, type: :model do
end
end
describe "#trusted" do
describe "#trusted?" do
it "memoizes the result from rolify" do
allow(Rails.cache)
.to receive(:fetch)
@ -809,7 +810,7 @@ RSpec.describe User, type: :model do
.and_return(false)
.once
2.times { user.trusted }
2.times { user.trusted? }
end
end

View file

@ -11,7 +11,7 @@ RSpec.describe "/admin/content_manager/tags/:id/moderator", type: :request do
it "adds the given user as trusted and as a tag moderator" do
post admin_tag_moderator_path(tag.id), params: { tag_id: tag.id, tag: { user_id: user.id } }
expect(user.tag_moderator?).to be true
expect(user.trusted).to be true
expect(user.trusted?).to be true
end
end
@ -29,7 +29,7 @@ RSpec.describe "/admin/content_manager/tags/:id/moderator", type: :request do
it "does not remove the trusted role from the user" do
delete admin_tag_moderator_path(tag.id), params: { tag_id: tag.id, tag: { user_id: user.id } }
expect(user.trusted).to be true
expect(user.trusted?).to be true
end
end
end