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.



61
62
63
# File 'lib/brevio/session/testing.rb', line 61

def config
  @config
end

#sessionsObject

Returns the value of attribute sessions.



61
62
63
# File 'lib/brevio/session/testing.rb', line 61

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
28
29
30
# 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_patch: user.patch,
                   user_stamp: updated_at }

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

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

  Testing.config.gem_config.redis.set("brevio-id:audit-company:#{user.audit_company.brevio_id}:patch", user.audit_company.patch)
  set_cookie(redis_key)
end

#brevio_login_new_user(user_brevio_id, patch: nil, audit_company:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/brevio/session/testing.rb', line 32

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

  session_hash = { user_id: user_brevio_id,
                   audit_company_id: audit_company.brevio_id,
                   user_patch: patch || SecureRandom.hex(12),
                   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



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

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

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



54
55
56
# File 'lib/brevio/session/testing.rb', line 54

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

#setup!(logger:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/brevio/session/testing.rb', line 63

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