Skip to content

Commit

Permalink
Maintenance: Simulate login instead of using the real login form all …
Browse files Browse the repository at this point in the history
…the time.
  • Loading branch information
dominikklein committed Nov 4, 2021
1 parent 35d2e01 commit 2b58a97
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/controllers/application_controller/authenticates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def authentication_check(auth_param = {})
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
Expand Down
20 changes: 18 additions & 2 deletions spec/support/capybara/authenticated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@

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)

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 '/'

login(**credentials) if credentials
wait(4).until_exists do
current_login
end
end
end
end
3 changes: 3 additions & 0 deletions spec/support/capybara/common_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +93,7 @@ def current_user
# logout
#
def logout
ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil
visit('logout')

wait.until_disappears { find('.user-menu .user a', wait: false) }
Expand Down
4 changes: 4 additions & 0 deletions spec/support/capybara/selenium_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
options[:url] = ENV['REMOTE_URL']
end

ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil

Capybara::Selenium::Driver.new(app, **options).tap do |driver|
# Selenium 4 installs a default file_detector which finds wrong files/directories such as zammad/test.
driver.browser.file_detector = nil if ENV['REMOTE_URL'].present?
Expand All @@ -51,6 +53,8 @@
options[:url] = ENV['REMOTE_URL']
end

ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil

Capybara::Selenium::Driver.new(app, **options).tap do |driver|
# Selenium 4 installs a default file_detector which finds wrong files/directories such as zammad/test.
driver.browser.file_detector = nil if ENV['REMOTE_URL'].present?
Expand Down
2 changes: 1 addition & 1 deletion spec/system/knowledge_base_public/editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/system/manage/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
4 changes: 4 additions & 0 deletions spec/system/online_notification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
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) }
Expand Down Expand Up @@ -44,6 +46,8 @@
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
Expand Down

0 comments on commit 2b58a97

Please sign in to comment.