Class: NulogySSO::TestUtilities::Auth0Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/nulogy_sso/test_utilities/auth0_mock.rb

Overview

This provides a simple mock implementation of Auth0 endpoints, via mockserver

Instance Method Summary collapse

Constructor Details

#initialize(engine_path: "/nulogy_sso", mockserver_host: ENV.fetch("NULOGY_SSO_MOCKSERVER_HOST"), mockserver_port: ENV.fetch("NULOGY_SSO_MOCKSERVER_PORT")) ⇒ Auth0Mock

Returns a new instance of Auth0Mock.



10
11
12
13
14
15
16
17
18
19
# File 'lib/nulogy_sso/test_utilities/auth0_mock.rb', line 10

def initialize(
  engine_path: "/nulogy_sso",
  mockserver_host: ENV.fetch("NULOGY_SSO_MOCKSERVER_HOST"),
  mockserver_port: ENV.fetch("NULOGY_SSO_MOCKSERVER_PORT")
)
  @jwt_test_helper = NulogySSO::TestUtilities::JwtTestHelper.new
  @engine_path = engine_path
  @mockserver_host = mockserver_host
  @mockserver_port = mockserver_port
end

Instance Method Details

#mockserver_resetObject



67
68
69
70
71
72
73
# File 'lib/nulogy_sso/test_utilities/auth0_mock.rb', line 67

def mockserver_reset
  uri = URI(mockserver_url("reset"))
  req = Net::HTTP::Put.new(uri)
  Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(req)
  end
end

#setup(email:, redirect_path:, status_code: 200) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nulogy_sso/test_utilities/auth0_mock.rb', line 33

def setup(email:, redirect_path:, status_code: 200)
  mockserver_reset

  redirect_query_params = {
    code: "FAKE_CODE",
    origin: "#{capybara_current_host}#{redirect_path}"
  }.to_query
  mockserver_expectation(
    httpRequest: {
      method: "GET",
      path: "/authorize"
    },
    httpResponse: {
      statusCode: 302,
      headers: {
        Location: ["#{capybara_current_host}#{engine_path}/verify_authentication_code?#{redirect_query_params}"]
      }
    }
  )

  setup_jwks

  mockserver_expectation(
    httpRequest: {
      method: "POST",
      path: "/oauth/token"
    },
    httpResponse: {
      statusCode: status_code,
      body: signed_jwt_response(email)
    }
  )
end

#setup_jwksObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nulogy_sso/test_utilities/auth0_mock.rb', line 21

def setup_jwks
  mockserver_expectation(
    httpRequest: {
      method: "GET",
      path: "/.well-known/jwks.json"
    },
    httpResponse: {
      body: jwt_test_helper.jwks_json
    }
  )
end