2023-02-22 01:55:31 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-05 02:41:40 +02:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2024-09-04 07:12:25 +02:00
|
|
|
RSpec.describe Admin::Users::RolesController do
|
2022-07-05 02:41:40 +02:00
|
|
|
render_views
|
|
|
|
|
|
|
|
let(:current_role) { UserRole.create(name: 'Foo', permissions: UserRole::FLAGS[:manage_roles], position: 10) }
|
|
|
|
let(:current_user) { Fabricate(:user, role: current_role) }
|
|
|
|
|
|
|
|
let(:previous_role) { nil }
|
|
|
|
let(:user) { Fabricate(:user, role: previous_role) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in current_user, scope: :user
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET #show' do
|
|
|
|
before do
|
|
|
|
get :show, params: { user_id: user.id }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns http success' do
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when target user is higher ranked than current user' do
|
|
|
|
let(:previous_role) { UserRole.create(name: 'Baz', permissions: UserRole::FLAGS[:administrator], position: 100) }
|
|
|
|
|
|
|
|
it 'returns http forbidden' do
|
2023-02-20 03:16:40 +01:00
|
|
|
expect(response).to have_http_status(403)
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'PUT #update' do
|
|
|
|
let(:selected_role) { UserRole.create(name: 'Bar', permissions: permissions, position: position) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
put :update, params: { user_id: user.id, user: { role_id: selected_role.id } }
|
|
|
|
end
|
|
|
|
|
2023-06-06 15:51:42 +02:00
|
|
|
context 'with manage roles permissions' do
|
2022-07-05 02:41:40 +02:00
|
|
|
let(:permissions) { UserRole::FLAGS[:manage_roles] }
|
|
|
|
let(:position) { 1 }
|
|
|
|
|
|
|
|
it 'updates user role' do
|
|
|
|
expect(user.reload.role_id).to eq selected_role&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'redirects back to account page' do
|
|
|
|
expect(response).to redirect_to(admin_account_path(user.account_id))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when selected role has higher position than current user\'s role' do
|
|
|
|
let(:permissions) { UserRole::FLAGS[:administrator] }
|
|
|
|
let(:position) { 100 }
|
|
|
|
|
|
|
|
it 'does not update user role' do
|
|
|
|
expect(user.reload.role_id).to eq previous_role&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders edit form' do
|
|
|
|
expect(response).to render_template(:show)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when target user is higher ranked than current user' do
|
|
|
|
let(:previous_role) { UserRole.create(name: 'Baz', permissions: UserRole::FLAGS[:administrator], position: 100) }
|
|
|
|
let(:permissions) { UserRole::FLAGS[:manage_roles] }
|
|
|
|
let(:position) { 1 }
|
|
|
|
|
|
|
|
it 'does not update user role' do
|
|
|
|
expect(user.reload.role_id).to eq previous_role&.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns http forbidden' do
|
2023-02-20 03:16:40 +01:00
|
|
|
expect(response).to have_http_status(403)
|
2022-07-05 02:41:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|