From decf868823c4de6a2cba4b16f74fb0d6469fc0ca Mon Sep 17 00:00:00 2001 From: Dominik Klein Date: Tue, 31 Aug 2021 22:23:32 +0200 Subject: [PATCH] Maintenance: Ported maintenance mode test to capybara to increase the test stabilization. --- script/build/test_slice_tests.sh | 6 - spec/system/login/maintenance_mode_spec.rb | 84 +++++++++++ spec/system/system/maintenance_spec.rb | 141 +++++++++++------- test/browser/maintenance_mode_test.rb | 160 --------------------- 4 files changed, 177 insertions(+), 214 deletions(-) create mode 100644 spec/system/login/maintenance_mode_spec.rb delete mode 100644 test/browser/maintenance_mode_test.rb diff --git a/script/build/test_slice_tests.sh b/script/build/test_slice_tests.sh index 1e808534a..6592e9d78 100755 --- a/script/build/test_slice_tests.sh +++ b/script/build/test_slice_tests.sh @@ -60,7 +60,6 @@ if [ "$LEVEL" == '1' ]; then rm test/browser/integration_test.rb rm test/browser/keyboard_shortcuts_test.rb # test/browser/maintenance_app_version_test.rb - # test/browser/maintenance_mode_test.rb # test/browser/maintenance_session_message_test.rb # test/browser/manage_test.rb # test/browser/monitoring_test.rb @@ -133,7 +132,6 @@ elif [ "$LEVEL" == '2' ]; then rm test/browser/integration_test.rb rm test/browser/keyboard_shortcuts_test.rb rm test/browser/maintenance_app_version_test.rb - rm test/browser/maintenance_mode_test.rb rm test/browser/maintenance_session_message_test.rb rm test/browser/manage_test.rb rm test/browser/monitoring_test.rb @@ -206,7 +204,6 @@ elif [ "$LEVEL" == '3' ]; then rm test/browser/integration_test.rb rm test/browser/keyboard_shortcuts_test.rb rm test/browser/maintenance_app_version_test.rb - rm test/browser/maintenance_mode_test.rb rm test/browser/maintenance_session_message_test.rb rm test/browser/manage_test.rb rm test/browser/monitoring_test.rb @@ -279,7 +276,6 @@ elif [ "$LEVEL" == '4' ]; then rm test/browser/integration_test.rb rm test/browser/keyboard_shortcuts_test.rb rm test/browser/maintenance_app_version_test.rb - rm test/browser/maintenance_mode_test.rb rm test/browser/maintenance_session_message_test.rb rm test/browser/manage_test.rb rm test/browser/monitoring_test.rb @@ -351,7 +347,6 @@ elif [ "$LEVEL" == '5' ]; then rm test/browser/integration_test.rb rm test/browser/keyboard_shortcuts_test.rb rm test/browser/maintenance_app_version_test.rb - rm test/browser/maintenance_mode_test.rb rm test/browser/maintenance_session_message_test.rb rm test/browser/manage_test.rb rm test/browser/monitoring_test.rb @@ -426,7 +421,6 @@ elif [ "$LEVEL" == '6' ]; then # test/browser/integration_test.rb # test/browser/keyboard_shortcuts_test.rb rm test/browser/maintenance_app_version_test.rb - rm test/browser/maintenance_mode_test.rb rm test/browser/maintenance_session_message_test.rb rm test/browser/manage_test.rb rm test/browser/monitoring_test.rb diff --git a/spec/system/login/maintenance_mode_spec.rb b/spec/system/login/maintenance_mode_spec.rb new file mode 100644 index 000000000..9bb0bc06c --- /dev/null +++ b/spec/system/login/maintenance_mode_spec.rb @@ -0,0 +1,84 @@ +# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ + +require 'rails_helper' + +RSpec.describe 'Login Maintenance Mode', type: :system, authenticated_as: false do + context 'with maintenance_mode' do + def try_login(username, password) + within('#login') do + fill_in 'username', with: username + fill_in 'password', with: password + + click_button + end + end + + context 'with active maintenance_mode' do + before { Setting.set 'maintenance_mode', true } + + it 'shows maintenance mode' do + open_login_page + + expect(page).to have_css('.js-maintenanceMode') + + try_login('agent1@example.com', 'test') + + expect(page).to have_css('#login .alert') + + refresh + + try_login('nicole.braun@zammad.org', 'test') + + expect(page).to have_css('#login .alert') + + refresh + + try_login('admin@example.com', 'test') + + expect(find('.user-menu .user a')[:title]).to eq('admin@example.com') + end + + it 'login should work again after deactivation of maintenance mode' do + open_login_page + + expect(page).to have_css('.js-maintenanceMode') + + try_login('agent1@example.com', 'test') + + expect(page).to have_css('#login .alert') + + Setting.set 'maintenance_mode', false + + expect(page).to have_no_css('.js-maintenanceMode') + + try_login('agent1@example.com', 'test') + + expect(find('.user-menu .user a')[:title]).to eq('agent1@example.com') + end + end + + context 'without maintenance_mode' do + before { Setting.set 'maintenance_mode', false } + + it 'does not show message' do + open_login_page + + expect(page).to have_no_css('.js-maintenanceMode') + end + + it 'shows message on the go' do + open_login_page + + Setting.set 'maintenance_mode', true + + expect(page).to have_css('.js-maintenanceMode') + end + end + end + + def open_login_page + visit '/' + + ensure_websocket + end +end diff --git a/spec/system/system/maintenance_spec.rb b/spec/system/system/maintenance_spec.rb index 5177d20b3..678474d76 100644 --- a/spec/system/system/maintenance_spec.rb +++ b/spec/system/system/maintenance_spec.rb @@ -3,59 +3,104 @@ require 'rails_helper' RSpec.describe 'Manage > Maintenance', type: :system do - it 'switch maintenance_login on' do - Setting.set 'maintenance_login', false + context 'when maintenance login is used' do + context 'when maintenance login will be activated', authenticated_as: :authenticate do + def authenticate + Setting.set('maintenance_login', false) + true + end - visit 'system/maintenance' - refresh # ensure changed Setting is loaded + it 'switch maintenance_login on' do + visit 'system/maintenance' - find('.js-loginSetting label').click - find('.js-loginSetting input', visible: :all).check # required for chrome + click '.js-loginSetting label' - wait(10).until { expect(Setting.get('maintenance_login')).to be true } - end - - it 'switch maintenance_login off' do - Setting.set 'maintenance_login', true - - visit 'system/maintenance' - refresh # ensure changed Setting is loaded - - find('.js-loginSetting label').click - find('.js-loginSetting input', visible: :all).uncheck # required for chrome - - wait(10).until { expect(Setting.get('maintenance_login')).to be false } - end - - it 'shows current maintenance_login_message' do - message = "badum tssss #{rand(99_999)}" - - Setting.set 'maintenance_login_message', message - - visit 'system/maintenance' - refresh # ensure changed Setting is loaded - - expect(find('.js-loginPreview [data-name="message"]')).to have_text message - end - - it 'saves new maintenance_login_message' do - message_prefix = 'badum' - message_suffix = "tssss#{rand(99_999)}" - - Setting.set 'maintenance_login_message', message_prefix - - visit 'system/maintenance' - refresh # ensure changed Setting is loaded - - within(:active_content) do - elem = find('#maintenance-message.hero-unit') - elem.click - elem.send_keys message_suffix - elem.execute_script "$(this).trigger('blur')" # required for chrome + wait(10).until { expect(Setting.get('maintenance_login')).to be true } + end end - find('#global-search').click # unfocus + context 'when maintenance login will be deactiavted', authenticated_as: :authenticate do + def authenticate + Setting.set('maintenance_login', true) + true + end - wait(10).until { expect(Setting.get('maintenance_login_message')).to eq "#{message_prefix}#{message_suffix}" } + it 'switch maintenance_login off' do + visit 'system/maintenance' + + click '.js-loginSetting label' + + wait(10).until { expect(Setting.get('maintenance_login')).to be false } + end + end + + context 'when maintenance login message will be used', authenticated_as: :authenticate do + def message + @message ||= "badum tssss #{rand(99_999)}" + end + + def authenticate + Setting.set('maintenance_login_message', message) + true + end + + it 'shows current maintenance_login_message' do + visit 'system/maintenance' + + expect(find('.js-loginPreview [data-name="message"]')).to have_text message + end + + it 'saves new maintenance_login_message' do + message_suffix = "tssss#{rand(99_999)}" + + visit 'system/maintenance' + + within(:active_content) do + elem = find('#maintenance-message.hero-unit') + elem.click + elem.send_keys message_suffix + elem.execute_script "$(this).trigger('blur')" # required for chrome + end + + find('#global-search').click # unfocus + + wait(10).until { expect(Setting.get('maintenance_login_message')).to eq "#{message}#{message_suffix}" } + end + end + end + + context 'when maintenance mode is used' do + context 'when maintenance mode will be activated', authenticated_as: :authenticate do + def authenticate + Setting.set('maintenance_mode', false) + true + end + + it 'switch maintenance_mode on' do + visit 'system/maintenance' + + click '.js-modeSetting label' + modal_ready + click '.content.active .modal .js-submit' + modal_disappear + + wait(10).until { expect(Setting.get('maintenance_mode')).to be true } + end + end + + context 'when maintenance mode will be deactiavted', authenticated_as: :authenticate do + def authenticate + Setting.set('maintenance_mode', true) + true + end + + it 'switch maintenance_mode off' do + visit 'system/maintenance' + + click '.js-modeSetting label' + + wait(10).until { expect(Setting.get('maintenance_mode')).to be false } + end + end end end diff --git a/test/browser/maintenance_mode_test.rb b/test/browser/maintenance_mode_test.rb deleted file mode 100644 index 691a4430d..000000000 --- a/test/browser/maintenance_mode_test.rb +++ /dev/null @@ -1,160 +0,0 @@ -# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ - -require 'browser_test_helper' - -class MaintenanceModeTest < TestCase - - def test_mode - browser1 = browser_instance - login( - browser: browser1, - username: 'admin@example.com', - password: 'test', - url: browser_url, - ) - - browser2 = browser_instance - location( - browser: browser2, - url: browser_url, - ) - click( - browser: browser1, - css: 'a[href="#manage"]', - ) - click( - browser: browser1, - css: 'a[href="#system/maintenance"]', - ) - - watch_for_disappear( - browser: browser2, - css: '.js-maintenanceMode', - ) - - switch( - browser: browser1, - css: '.content.active .js-modeSetting', - type: 'on', - no_check: true, - ) - - # check warning - modal_ready(browser: browser1) - click( - browser: browser1, - css: '.content.active .modal .js-submit', - ) - modal_disappear(browser: browser1) - - watch_for( - browser: browser2, - css: '.js-maintenanceMode', - ) - - # try to logon with normal agent, should not work - login( - browser: browser2, - username: 'agent1@example.com', - password: 'test', - url: browser_url, - success: false, - ) - login( - browser: browser2, - username: 'nicole.braun@zammad.org', - password: 'test', - url: browser_url, - success: false, - ) - - # logout with admin and logon again - logout( - browser: browser1, - ) - sleep 4 - login( - browser: browser1, - username: 'admin@example.com', - password: 'test', - url: browser_url, - ) - click( - browser: browser1, - css: 'a[href="#manage"]', - ) - click( - browser: browser1, - css: 'a[href="#system/maintenance"]', - ) - - watch_for( - browser: browser1, - css: '.content.active .js-modeSetting', - ) - switch( - browser: browser1, - css: '.content.active .js-modeSetting', - type: 'off', - ) - - watch_for_disappear( - browser: browser2, - css: '.js-maintenanceMode', - ) - - # try to logon with normal agent, should work again - login( - browser: browser2, - username: 'agent1@example.com', - password: 'test', - url: browser_url, - ) - logout( - browser: browser2, - ) - sleep 4 - login( - browser: browser2, - username: 'nicole.braun@zammad.org', - password: 'test', - url: browser_url, - ) - - switch( - browser: browser1, - css: '.content.active .js-modeSetting', - type: 'on', - no_check: true, - ) - - # check warning - modal_ready(browser: browser1) - click( - browser: browser1, - css: '.content.active .modal .js-submit', - ) - modal_disappear(browser: browser1) - - watch_for( - browser: browser2, - css: '#login', - ) - watch_for( - browser: browser2, - css: '.js-maintenanceMode', - ) - - switch( - browser: browser1, - css: '.content.active .js-modeSetting', - type: 'off', - ) - - watch_for_disappear( - browser: browser2, - css: '.js-maintenanceMode', - ) - end - -end