Module: Brevio::Session::Testing

Extended by:
Testing
Included in:
Testing
Defined in:
lib/brevio/session/testing.rb

Overview

Module used to enable testing for controllers using the Brevio session. Mocks a login to a shared redis, with keys generated by the Brevio::Session::CookieJar.

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



57
58
59
# File 'lib/brevio/session/testing.rb', line 57

def config
  @config
end

#sessionsObject

Returns the value of attribute sessions.



57
58
59
# File 'lib/brevio/session/testing.rb', line 57

def sessions
  @sessions
end

Instance Method Details

#brevio_login(user, updated_at: Time.current.yesterday) ⇒ Object

Simulates a user with a shared Brevio session. We can specify which brevio_id the mocked ID service should return (as well as a custom last updated_at timestamp).

The user needs to have the following methods defined: #brevio_id, #audit_company



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/brevio/session/testing.rb', line 13

def (user, updated_at: Time.current.yesterday)
  redis_key = SecureRandom.hex(6)

  session_hash = { user_id: user.brevio_id,
                   audit_company_id: user.audit_company.brevio_id,
                   user_stamp: updated_at }

  Testing.config.logger.info "setting Brevio session to #{session_hash}"

  Testing.config.gem_config.redis.set(
    redis_key,
    session_hash
  )
  set_cookie(redis_key)
end

#brevio_login_new_user(user_brevio_id, audit_company:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/brevio/session/testing.rb', line 29

def (user_brevio_id, audit_company:)
  redis_key = SecureRandom.hex(6)

  session_hash = { user_id: user_brevio_id,
                   audit_company_id: audit_company.brevio_id,
                   user_stamp: Time.current.yesterday }

  Testing.config.logger.info "setting Brevio session to #{session_hash}"

  Testing.config.gem_config.redis.set(
    redis_key,
    session_hash
  )
  set_cookie(redis_key)
end

#brevio_logoutObject



45
46
47
# File 'lib/brevio/session/testing.rb', line 45

def brevio_logout
  cookies[Testing.config.gem_config.session_cookie] = nil
end

rubocop:disable Naming/AccessorMethodName (set makes sense here)



50
51
52
# File 'lib/brevio/session/testing.rb', line 50

def set_cookie(value)
  cookies[Testing.config.gem_config.session_cookie] = value
end

#setup!(logger:) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/brevio/session/testing.rb', line 59

def setup!(logger:)
  self.config = Config.new
  config.logger = logger
  config.gem_config = Brevio::Session::Config.config
  config.logger.info '--- 👨‍🔬 Setting up Brevio Session gem for testing 👨‍🔬 ---'

  # Ensures we return a mocked value of the session, rather than something which depends on
  # the cryptographically signed value.
  #
  Brevio::Session::Cookies::Parse.send(:define_method, :perform!) do |cookie|
    raise NilSession if cookie.nil?
    cookie
  end
end