2021-11-11 11:23:49 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SessionHelpers
|
2022-04-04 11:22:00 +05:30
|
|
|
# Stub a session in Redis, for use in request specs where we can't mock the session directly.
|
|
|
|
# This also needs the :clean_gitlab_redis_sessions tag on the spec.
|
|
|
|
def stub_session(session_hash)
|
|
|
|
unless RSpec.current_example.metadata[:clean_gitlab_redis_sessions]
|
|
|
|
raise 'Add :clean_gitlab_redis_sessions to your spec!'
|
|
|
|
end
|
|
|
|
|
|
|
|
session_id = Rack::Session::SessionId.new(SecureRandom.hex)
|
|
|
|
|
|
|
|
Gitlab::Redis::Sessions.with do |redis|
|
|
|
|
redis.set("session:gitlab:#{session_id.private_id}", Marshal.dump(session_hash))
|
|
|
|
end
|
|
|
|
|
|
|
|
cookies[Gitlab::Application.config.session_options[:key]] = session_id.public_id
|
|
|
|
end
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def expect_single_session_with_authenticated_ttl
|
|
|
|
expect_single_session_with_expiration(Settings.gitlab['session_expire_delay'] * 60)
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_single_session_with_short_ttl
|
|
|
|
expect_single_session_with_expiration(Settings.gitlab['unauthenticated_session_expire_delay'])
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_single_session_with_expiration(expiration)
|
|
|
|
session_keys = get_session_keys
|
|
|
|
|
|
|
|
expect(session_keys.size).to eq(1)
|
|
|
|
expect(get_ttl(session_keys.first)).to be_within(5).of(expiration)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_session_keys
|
2022-01-26 12:08:38 +05:30
|
|
|
Gitlab::Redis::Sessions.with { |redis| redis.scan_each(match: 'session:gitlab:*').to_a }
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def get_ttl(key)
|
2022-01-26 12:08:38 +05:30
|
|
|
Gitlab::Redis::Sessions.with { |redis| redis.ttl(key) }
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
2023-07-09 08:55:56 +05:30
|
|
|
|
|
|
|
def expire_session
|
|
|
|
get_session_keys.each do |key|
|
|
|
|
::Gitlab::Redis::Sessions.with { |redis| redis.expire(key, -1) }
|
|
|
|
end
|
|
|
|
end
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|