# frozen_string_literal: true require "curb" require_relative "jwt_test_helper" module NulogySSO module TestUtilities # This provides a simple mock implementation of Auth0 endpoints, via mockserver class Auth0Mock def initialize @jwt_test_helper = NulogySSO::TestUtilities::JwtTestHelper.new end def setup_jwks mockserver_expectation( httpRequest: { method: "GET", path: "/.well-known/jwks.json" }, httpResponse: { body: jwt_test_helper.jwks_json } ) end 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}/nulogy_sso/code?#{redirect_query_params}"] } } ) setup_jwks mockserver_expectation( httpRequest: { method: "POST", path: "/oauth/token" }, httpResponse: { statusCode: status_code, body: signed_jwt_response(email) } ) end def mockserver_reset Curl.put(mockserver_url("reset")) end private attr_reader :jwt_test_helper def mockserver_expectation(body) Curl.put(mockserver_url("expectation"), body.to_json) end def mockserver_url(path = "") "#{ENV.fetch("NULOGY_SSO_MOCKSERVER_HOST")}:#{ENV.fetch("NULOGY_SSO_MOCKSERVER_PORT")}/mockserver/#{path}" end def signed_jwt_response(email, overrides = {}) jwt = jwt_test_helper.jwt(email, overrides) { access_token: jwt }.to_json end def capybara_current_host @capybara_current_host ||= "http://#{Capybara.current_session.server.host}:#{Capybara.current_session.server.port}" end end end end