diff --git a/config/routes.rb b/config/routes.rb index ac6c27ae..8cabfc85 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,7 @@ Rails.application.routes.draw do mount ActionCable.server => '/cable' mount Rswag::Api::Engine => '/api-docs' mount Rswag::Ui::Engine => '/api-docs' - authenticate :user, ->(u) { u.admin? } do + authenticate :user, ->(u) { u.admin? && DawarichSettings.self_hosted? } do mount Sidekiq::Web => '/sidekiq' end diff --git a/spec/requests/sidekiq_spec.rb b/spec/requests/sidekiq_spec.rb index a8440e1a..b1dbca16 100644 --- a/spec/requests/sidekiq_spec.rb +++ b/spec/requests/sidekiq_spec.rb @@ -3,39 +3,71 @@ require 'rails_helper' RSpec.describe '/sidekiq', type: :request do - context 'when user is not authenticated' do - it 'redirects to sign in page' do - get sidekiq_url + context 'when Dawarich is in self-hosted mode' do + before do + allow(DawarichSettings).to receive(:self_hosted?).and_return(true) + end - expect(response).to redirect_to('/users/sign_in') + context 'when user is not authenticated' do + it 'redirects to sign in page' do + get sidekiq_url + + expect(response).to redirect_to('/users/sign_in') + end + end + + context 'when user is authenticated' do + context 'when user is not admin' do + before { sign_in create(:user) } + + it 'redirects to root page' do + get sidekiq_url + + expect(response).to redirect_to(root_url) + end + + it 'shows flash message' do + get sidekiq_url + + expect(flash[:error]).to eq('You are not authorized to perform this action.') + end + end + + context 'when user is admin' do + before { sign_in create(:user, :admin) } + + it 'renders a successful response' do + get sidekiq_url + + expect(response).to be_successful + end + end end end - context 'when user is authenticated' do - context 'when user is not admin' do - before { sign_in create(:user) } + context 'when Dawarich is not in self-hosted mode' do + before do + allow(DawarichSettings).to receive(:self_hosted?).and_return(false) + Rails.application.reload_routes! + end + + context 'when user is not authenticated' do + it 'redirects to sign in page' do + get sidekiq_url + + expect(response).to redirect_to('/users/sign_in') + end + end + + context 'when user is authenticated' do + before { sign_in create(:user, :admin) } it 'redirects to root page' do get sidekiq_url expect(response).to redirect_to(root_url) - end - - it 'shows flash message' do - get sidekiq_url - expect(flash[:error]).to eq('You are not authorized to perform this action.') end end - - context 'when user is admin' do - before { sign_in create(:user, :admin) } - - it 'renders a successful response' do - get sidekiq_url - - expect(response).to be_successful - end - end end end