File: session_helpers.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (49 lines) | stat: -rw-r--r-- 1,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true

module SessionHelpers
  # 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_data:, user_id: nil)
    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_data))
      redis.sadd("session:lookup:user:gitlab:#{user_id}", [session_id.private_id]) if user_id
    end

    defined?(cookies) && cookies[Gitlab::Application.config.session_options[:key]] = session_id.public_id
  end

  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
    Gitlab::Redis::Sessions.with { |redis| redis.scan_each(match: 'session:gitlab:*').to_a }
  end

  def get_ttl(key)
    Gitlab::Redis::Sessions.with { |redis| redis.ttl(key) }
  end

  def expire_session
    get_session_keys.each do |key|
      ::Gitlab::Redis::Sessions.with { |redis| redis.expire(key, -1) }
    end
  end
end