Merge commit 'db971976853103017b1561f2c4f9406fa00c0a2e' into glitch-soc/merge-upstream

This commit is contained in:
Claire
2025-02-22 21:31:46 +01:00
23 changed files with 317 additions and 306 deletions

View File

@@ -69,7 +69,8 @@ RSpec.describe Admin::DomainBlocksController do
expect(DomainBlockWorker).to_not have_received(:perform_async)
expect(response).to render_template :new
expect(response.parsed_body.title)
.to match(I18n.t('admin.domain_blocks.new.title'))
end
end
@@ -84,7 +85,8 @@ RSpec.describe Admin::DomainBlocksController do
expect(DomainBlockWorker).to_not have_received(:perform_async)
expect(response).to render_template :confirm_suspension
expect(response.parsed_body.title)
.to match(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
end
end
@@ -119,7 +121,8 @@ RSpec.describe Admin::DomainBlocksController do
expect(DomainBlockWorker).to_not have_received(:perform_async)
expect(response).to render_template :confirm_suspension
expect(response.parsed_body.title)
.to match(I18n.t('admin.domain_blocks.confirm_suspension.title', domain: 'example.com'))
end
end

View File

@@ -1,235 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Admin::RolesController do
render_views
let(:permissions) { UserRole::Flags::NONE }
let(:current_role) { UserRole.create(name: 'Foo', permissions: permissions, position: 10) }
let(:current_user) { Fabricate(:user, role: current_role) }
before do
sign_in current_user, scope: :user
end
describe 'GET #index' do
before do
get :index
end
context 'when user does not have permission to manage roles' do
it 'returns http forbidden' do
expect(response).to have_http_status(403)
end
end
context 'when user has permission to manage roles' do
let(:permissions) { UserRole::FLAGS[:manage_roles] }
it 'returns http success' do
expect(response).to have_http_status(:success)
end
end
end
describe 'GET #new' do
before do
get :new
end
context 'when user does not have permission to manage roles' do
it 'returns http forbidden' do
expect(response).to have_http_status(403)
end
end
context 'when user has permission to manage roles' do
let(:permissions) { UserRole::FLAGS[:manage_roles] }
it 'returns http success' do
expect(response).to have_http_status(:success)
end
end
end
describe 'POST #create' do
let(:selected_position) { 1 }
let(:selected_permissions_as_keys) { %w(manage_roles) }
before do
post :create, params: { user_role: { name: 'Bar', position: selected_position, permissions_as_keys: selected_permissions_as_keys } }
end
context 'when user has permission to manage roles' do
let(:permissions) { UserRole::FLAGS[:manage_roles] }
context 'when new role\'s does not elevate above the user\'s role' do
let(:selected_position) { 1 }
let(:selected_permissions_as_keys) { %w(manage_roles) }
it 'redirects to roles page and creates role' do
expect(response).to redirect_to(admin_roles_path)
expect(UserRole.find_by(name: 'Bar')).to_not be_nil
end
end
context 'when new role\'s position is higher than user\'s role' do
let(:selected_position) { 100 }
let(:selected_permissions_as_keys) { %w(manage_roles) }
it 'renders new template and does not create role' do
expect(response).to render_template(:new)
expect(UserRole.find_by(name: 'Bar')).to be_nil
end
end
context 'when new role has permissions the user does not have' do
let(:selected_position) { 1 }
let(:selected_permissions_as_keys) { %w(manage_roles manage_users manage_reports) }
it 'renders new template and does not create role' do
expect(response).to render_template(:new)
expect(UserRole.find_by(name: 'Bar')).to be_nil
end
end
context 'when user has administrator permission' do
let(:permissions) { UserRole::FLAGS[:administrator] }
let(:selected_position) { 1 }
let(:selected_permissions_as_keys) { %w(manage_roles manage_users manage_reports) }
it 'redirects to roles page and creates new role' do
expect(response).to redirect_to(admin_roles_path)
expect(UserRole.find_by(name: 'Bar')).to_not be_nil
end
end
end
end
describe 'GET #edit' do
let(:role_position) { 8 }
let(:role) { UserRole.create(name: 'Bar', permissions: UserRole::FLAGS[:manage_users], position: role_position) }
before do
get :edit, params: { id: role.id }
end
context 'when user does not have permission to manage roles' do
it 'returns http forbidden' do
expect(response).to have_http_status(403)
end
end
context 'when user has permission to manage roles' do
let(:permissions) { UserRole::FLAGS[:manage_roles] }
context 'when user outranks the role' do
it 'returns http success' do
expect(response).to have_http_status(:success)
end
end
context 'when role outranks user' do
let(:role_position) { current_role.position + 1 }
it 'returns http forbidden' do
expect(response).to have_http_status(403)
end
end
end
end
describe 'PUT #update' do
let(:role_position) { 8 }
let(:role_permissions) { UserRole::FLAGS[:manage_users] }
let(:role) { UserRole.create(name: 'Bar', permissions: role_permissions, position: role_position) }
let(:selected_position) { 8 }
let(:selected_permissions_as_keys) { %w(manage_users) }
before do
put :update, params: { id: role.id, user_role: { name: 'Baz', position: selected_position, permissions_as_keys: selected_permissions_as_keys } }
end
context 'when user does not have permission to manage roles' do
it 'returns http forbidden and does not update role' do
expect(response).to have_http_status(403)
expect(role.reload.name).to eq 'Bar'
end
end
context 'when user has permission to manage roles' do
let(:permissions) { UserRole::FLAGS[:manage_roles] }
context 'when role has permissions the user doesn\'t' do
it 'renders edit template and does not update role' do
expect(response).to render_template(:edit)
expect(role.reload.name).to eq 'Bar'
end
end
context 'when user has all permissions of the role' do
let(:permissions) { UserRole::FLAGS[:manage_roles] | UserRole::FLAGS[:manage_users] }
context 'when user outranks the role' do
it 'redirects to roles page and updates role' do
expect(response).to redirect_to(admin_roles_path)
expect(role.reload.name).to eq 'Baz'
end
end
context 'when role outranks user' do
let(:role_position) { current_role.position + 1 }
it 'returns http forbidden and does not update role' do
expect(response).to have_http_status(403)
expect(role.reload.name).to eq 'Bar'
end
end
end
end
end
describe 'DELETE #destroy' do
let(:role_position) { 8 }
let(:role) { UserRole.create(name: 'Bar', permissions: UserRole::FLAGS[:manage_users], position: role_position) }
before do
delete :destroy, params: { id: role.id }
end
context 'when user does not have permission to manage roles' do
it 'returns http forbidden' do
expect(response).to have_http_status(403)
end
end
context 'when user has permission to manage roles' do
let(:permissions) { UserRole::FLAGS[:manage_roles] }
context 'when user outranks the role' do
it 'redirects to roles page' do
expect(response).to redirect_to(admin_roles_path)
end
end
context 'when role outranks user' do
let(:role_position) { current_role.position + 1 }
it 'returns http forbidden' do
expect(response).to have_http_status(403)
end
end
end
end
end

View File

@@ -5,14 +5,14 @@ require 'rails_helper'
RSpec.describe Settings::TwoFactorAuthentication::ConfirmationsController do
render_views
shared_examples 'renders :new' do
it 'renders the new view' do
shared_examples 'renders expected page' do
it 'renders the new view with QR code' do
subject
expect(response).to have_http_status(200)
expect(response).to render_template(:new)
expect(response.body)
.to include(qr_code_markup)
.and include(I18n.t('settings.two_factor_authentication'))
end
def qr_code_markup
@@ -34,7 +34,7 @@ RSpec.describe Settings::TwoFactorAuthentication::ConfirmationsController do
get :new, session: { challenge_passed_at: Time.now.utc, new_otp_secret: 'thisisasecretforthespecofnewview' }
end
include_examples 'renders :new'
include_examples 'renders expected page'
end
it 'redirects if a new otp_secret has not been set in the session' do
@@ -66,10 +66,13 @@ RSpec.describe Settings::TwoFactorAuthentication::ConfirmationsController do
expect { post_create_with_options }
.to change { user.reload.otp_secret }.to 'thisisasecretforthespecofnewview'
expect(flash[:notice]).to eq 'Two-factor authentication successfully enabled'
expect(response).to have_http_status(200)
expect(response).to render_template('settings/two_factor_authentication/recovery_codes/index')
expect(response.body).to include(*otp_backup_codes)
expect(flash[:notice])
.to eq(I18n.t('two_factor_authentication.enabled_success'))
expect(response)
.to have_http_status(200)
expect(response.body)
.to include(*otp_backup_codes)
.and include(I18n.t('settings.two_factor_authentication'))
end
end
@@ -86,10 +89,12 @@ RSpec.describe Settings::TwoFactorAuthentication::ConfirmationsController do
it 'renders page with error message' do
subject
expect(response.body).to include 'The entered code was invalid! Are server time and device time correct?'
expect(response.body)
.to include(I18n.t('otp_authentication.wrong_code'))
end
include_examples 'renders :new'
include_examples 'renders expected page'
end
private
@@ -116,18 +121,4 @@ RSpec.describe Settings::TwoFactorAuthentication::ConfirmationsController do
end
end
end
context 'when not signed in' do
it 'redirects on POST to create' do
post :create, params: { form_two_factor_confirmation: { otp_attempt: '123456' } }
expect(response).to redirect_to('/auth/sign_in')
end
it 'redirects on GET to new' do
get :new
expect(response).to redirect_to('/auth/sign_in')
end
end
end