* Refactoring questions asked of user In this pull request, I'm extracting and normalizing role-based questions asked of the user. Prior to this commit, our codebase has asked two very similar questions of our user model: - `user.has_role?(:admin)` - `user.admin?` In asking `has_role?(:admin)` we are relying on implementation details of the rolify gem. In addition, the `has_role?` question asked throughout controllers or views means that it's harder to create hieararchies of permissions. In favoring `user.admin?` as our question, we can use that indirection as an opportunity to discuss and decide "Should someone with the `:super_admin` role be `user.admin? == true`?" The details of this commit is to do three primary things: 1. Ask the `has_role?` questions in "one place" in the code (e.g. the `Authorizer` module) 2. Extract the role based questions that are on the `User` model and provde backwards compatable delegation. 3. Structure the code so that it's harder to accidentally call `user.has_role?` (e.g., make `User#has_role?` and `User#has_any_role?` private). This is related to #15624 and the updates are informed by discussion in PR #15691. This commit supplants #15691. * Refactoring the liquid tag policy tests * Fixing typo * Bump for travis
76 lines
2 KiB
Ruby
76 lines
2 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe "/admin/moderation/mods", type: :request do
|
|
let!(:admin) { create(:user, :admin) }
|
|
let!(:regular_user) { create(:user) }
|
|
let!(:moderator) { create(:user, :trusted) }
|
|
|
|
describe "GET /admin/moderation/mods" do
|
|
before do
|
|
sign_in admin
|
|
end
|
|
|
|
context "when the user is a single resource admin" do
|
|
let(:single_resource_admin) { create(:user, :single_resource_admin, resource: Mod) }
|
|
|
|
before do
|
|
sign_in single_resource_admin
|
|
get admin_mods_path
|
|
end
|
|
|
|
it "allows the request" do
|
|
expect(response).to have_http_status(:ok)
|
|
end
|
|
end
|
|
|
|
context "when the user is a not an admin" do
|
|
before do
|
|
sign_in regular_user
|
|
end
|
|
|
|
it "blocks the request" do
|
|
expect do
|
|
get admin_mods_path
|
|
end.to raise_error(Pundit::NotAuthorizedError)
|
|
end
|
|
end
|
|
|
|
context "when the are no matching mods" do
|
|
it "displays an warning" do
|
|
get "#{admin_mods_path}?search=no-results&state=tag_moderator"
|
|
expect(response.body).to include("There are no mods matching your search criteria")
|
|
end
|
|
end
|
|
|
|
it "displays mod user" do
|
|
get admin_mods_path
|
|
expect(response.body).to include(moderator.username)
|
|
end
|
|
|
|
it "does not display non-mod" do
|
|
get admin_mods_path
|
|
expect(response.body).not_to include(regular_user.username)
|
|
end
|
|
|
|
it "lists regular users as potential mods" do
|
|
get "#{admin_mods_path}?state=potential"
|
|
expect(response.body).to include(regular_user.username)
|
|
end
|
|
|
|
it "does not list mods as potential mods" do
|
|
get "#{admin_mods_path}?state=potential"
|
|
expect(response.body).not_to include(moderator.username)
|
|
end
|
|
end
|
|
|
|
describe "PUT /admin/moderation/mods" do
|
|
before do
|
|
sign_in admin
|
|
end
|
|
|
|
it "displays mod user" do
|
|
put admin_mod_path(regular_user.id)
|
|
expect(regular_user.reload.trusted?).to eq true
|
|
end
|
|
end
|
|
end
|