From dc5ba3cbaa870e9bb11d82e72582c55a9ad35f26 Mon Sep 17 00:00:00 2001 From: Dominik Klein Date: Thu, 4 Nov 2021 14:40:58 +0100 Subject: [PATCH] Maintenance: Simulate login instead of using the real login form all the time. --- .../application_controller/authenticates.rb | 4 ++++ spec/support/capybara/authenticated.rb | 20 +++++++++++++++++-- spec/support/capybara/common_actions.rb | 3 +++ spec/support/capybara/selenium_driver.rb | 4 ++++ .../knowledge_base_public/editor_spec.rb | 2 +- spec/system/manage/users_spec.rb | 2 +- spec/system/online_notification_spec.rb | 4 ++++ 7 files changed, 35 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller/authenticates.rb b/app/controllers/application_controller/authenticates.rb index 503649b2f..54a72668a 100644 --- a/app/controllers/application_controller/authenticates.rb +++ b/app/controllers/application_controller/authenticates.rb @@ -43,6 +43,10 @@ module ApplicationController::Authenticates end def authentication_check_only(auth_param = {}) + if Rails.env.test? && ENV['FAKE_SELENIUM_LOGIN_USER_ID'].present? && session[:user_id].blank? + session[:user_id] = ENV['FAKE_SELENIUM_LOGIN_USER_ID'].to_i + end + # logger.debug 'authentication_check' # logger.debug params.inspect # logger.debug session.inspect diff --git a/spec/support/capybara/authenticated.rb b/spec/support/capybara/authenticated.rb index 020ffabe2..bf60a0bf3 100644 --- a/spec/support/capybara/authenticated.rb +++ b/spec/support/capybara/authenticated.rb @@ -12,12 +12,28 @@ RSpec.configure do |config| config.before(:each, type: :system) do |example| + ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil + # there is no way to authenticated in a not set up system next if !example.metadata.fetch(:set_up, true) authenticated = example.metadata.fetch(:authenticated_as, true) - credentials = authenticated_as_get_user(authenticated, return_type: :credentials) + credentials = authenticated_as_get_user(authenticated, return_type: :credentials) - login(**credentials) if credentials + authentication_type = example.metadata.fetch(:authentication_type, :auto) + + next if credentials.nil? + + if authentication_type == :form + login(**credentials) + else + ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = User.find_by(email: credentials[:username]).id.to_s + + visit '/' + + wait(4).until_exists do + current_login + end + end end end diff --git a/spec/support/capybara/common_actions.rb b/spec/support/capybara/common_actions.rb index d9e640221..6405961af 100644 --- a/spec/support/capybara/common_actions.rb +++ b/spec/support/capybara/common_actions.rb @@ -22,6 +22,8 @@ module CommonActions # # return [nil] def login(username:, password:, remember_me: false) + ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil + visit '/' within('#login') do @@ -91,6 +93,7 @@ module CommonActions # logout # def logout + ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil visit('logout') end diff --git a/spec/support/capybara/selenium_driver.rb b/spec/support/capybara/selenium_driver.rb index ea016806a..cfe5f6804 100644 --- a/spec/support/capybara/selenium_driver.rb +++ b/spec/support/capybara/selenium_driver.rb @@ -29,6 +29,8 @@ Capybara.register_driver(:zammad_chrome) do |app| options[:url] = ENV['REMOTE_URL'] end + ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil + Capybara::Selenium::Driver.new(app, **options) end @@ -54,5 +56,7 @@ Capybara.register_driver(:zammad_firefox) do |app| options[:url] = ENV['REMOTE_URL'] end + ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil + Capybara::Selenium::Driver.new(app, **options) end diff --git a/spec/system/knowledge_base_public/editor_spec.rb b/spec/system/knowledge_base_public/editor_spec.rb index f215e8e2e..87d630ceb 100644 --- a/spec/system/knowledge_base_public/editor_spec.rb +++ b/spec/system/knowledge_base_public/editor_spec.rb @@ -2,7 +2,7 @@ require 'rails_helper' -RSpec.describe 'Public Knowledge Base for editor', type: :system do +RSpec.describe 'Public Knowledge Base for editor', authentication_type: :form, type: :system do include_context 'basic Knowledge Base' before do diff --git a/spec/system/manage/users_spec.rb b/spec/system/manage/users_spec.rb index fe10293a9..be2cd1e2d 100644 --- a/spec/system/manage/users_spec.rb +++ b/spec/system/manage/users_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' RSpec.describe 'Manage > Users', type: :system do - describe 'switching to an alternative user', authenticated_as: -> { original_user } do + describe 'switching to an alternative user', authentication_type: :form, authenticated_as: -> { original_user } do let(:original_user) { create(:admin) } let(:alternative_one_user) { create(:admin) } let(:alternative_two_user) { create(:admin) } diff --git a/spec/system/online_notification_spec.rb b/spec/system/online_notification_spec.rb index ca65801f6..6ebe81e2f 100644 --- a/spec/system/online_notification_spec.rb +++ b/spec/system/online_notification_spec.rb @@ -15,6 +15,8 @@ RSpec.describe 'Online notification', type: :system do context 'when pending time is reached soon' do before do visit "ticket/zoom/#{ticket.id}" + + wait.until_exists { find("a[data-key='Ticket-#{ticket.id}']", wait: 0) } end let(:ticket) { create(:ticket, owner: session_user, group: Group.first, state_name: 'pending reminder', pending_time: 4.seconds.from_now) } @@ -44,6 +46,8 @@ RSpec.describe 'Online notification', type: :system do before do ensure_websocket do visit "ticket/zoom/#{ticket.id}" + + wait.until_exists { find("a[data-key='Ticket-#{ticket.id}']", wait: 0) } end ticket.update! state: Ticket::State.lookup(name: 'pending reminder'), pending_time: 5.seconds.from_now