The personal finance app for everyone (by everyone)
Ruby
require "test_helper"
class OmniauthProviderRegistryTest < ActiveSupport::TestCase
setup do
@previous_test_mode = OmniAuth.config.test_mode
@previous_mock_auth = OmniAuth.config.mock_auth.dup
@previous_sso_providers = Rails.configuration.x.auth.sso_providers
OmniAuth.config.test_mode = true
end
teardown do
OmniAuth.config.test_mode = @previous_test_mode
OmniAuth.config.mock_auth = @previous_mock_auth
Rails.configuration.x.auth.sso_providers = @previous_sso_providers
end
test "registers custom database OIDC provider callbacks dynamically" do
FeatureFlags.stubs(:db_sso_providers?).returns(true)
ProviderLoader.stubs(:load_providers).returns([ custom_oidc_provider ])
OmniAuth.config.mock_auth[:authentik] = OmniAuth::AuthHash.new(
provider: "authentik",
uid: "uid-123",
info: { email: "[email protected]" }
)
response = Rack::MockRequest.new(dynamic_omniauth_app).get("/auth/authentik/callback")
assert_equal 200, response.status
assert_equal "authentik", response.body
end
test "dynamic OIDC setup uses the custom provider name and redirect URI" do
registration = OmniauthProviderRegistry.registration_for(custom_oidc_provider)
assert_equal :openid_connect, registration.strategy
assert_equal :authentik, registration.options[:name]
assert_equal "https://app.example.com/auth/authentik/callback", registration.options.dig(:client_options, :redirect_uri)
assert_equal "login", registration.options[:prompt]
assert registration.options.dig(:extra_authorize_params, :claims).present?
assert_equal "authentik", registration.config[:name]
end
test "database OIDC configuration is not also registered statically" do
FeatureFlags.stubs(:db_sso_providers?).returns(true)
builder = mock("omniauth builder")
builder.expects(:provider).never
config = OmniauthProviderRegistry.register(builder, custom_oidc_provider)
assert_equal "authentik", config[:name]
end
test "a disabled provider is no longer intercepted by a builder created while it was enabled" do
FeatureFlags.stubs(:db_sso_providers?).returns(true)
providers = [ custom_oidc_provider ]
ProviderLoader.stubs(:load_providers).returns(providers)
app = Rack::Builder.new do
use Rack::Session::Cookie, secret: "a" * 64
use OmniAuth::Builder do
OmniauthProviderRegistry.register_dynamic_database_oidc_provider(self)
OmniauthProviderRegistry.register(self, providers.first)
end
run ->(env) { [ 200, { "content-type" => "text/plain" }, [ env["omniauth.auth"] ? "authenticated" : "no auth" ] ] }
end.to_app
OmniAuth.config.mock_auth[:authentik] = OmniAuth::AuthHash.new(provider: "authentik", uid: "uid-123")
assert_equal "authenticated", Rack::MockRequest.new(app).get("/auth/authentik/callback").body
ProviderLoader.stubs(:load_providers).returns([])
assert_equal "no auth", Rack::MockRequest.new(app).get("/auth/authentik/callback").body
end
test "does not intercept incomplete database OIDC providers" do
Rails.env.stubs(:test?).returns(false)
FeatureFlags.stubs(:db_sso_providers?).returns(true)
ProviderLoader.stubs(:load_providers).returns([
custom_oidc_provider.except(:client_secret)
])
response = Rack::MockRequest.new(dynamic_omniauth_app).get("/auth/authentik/callback")
assert_equal 200, response.status
assert_equal "no auth", response.body
end
test "an incomplete provider is reported once until its configuration changes" do
Rails.env.stubs(:test?).returns(false)
OmniauthProviderRegistry::SKIPPED_PROVIDERS.clear
incomplete = custom_oidc_provider.except(:client_secret)
warning = "[OmniAuth] Skipping OIDC provider 'authentik' - missing required configuration"
Rails.logger.expects(:warn).with(warning).twice
2.times { assert_nil OmniauthProviderRegistry.registration_for(incomplete) }
assert OmniauthProviderRegistry.registration_for(custom_oidc_provider)
assert_nil OmniauthProviderRegistry.registration_for(incomplete)
ensure
OmniauthProviderRegistry::SKIPPED_PROVIDERS.clear
end
test "auth config returns current database providers instead of stale boot providers" do
FeatureFlags.stubs(:db_sso_providers?).returns(true)
Rails.configuration.x.auth.sso_providers = [
{ id: "oidc", strategy: "openid_connect", name: "openid_connect" }
]
provider = custom_oidc_provider.deep_stringify_keys
provider["settings"]["default_role"] = "super_admin"
ProviderLoader.stubs(:load_providers).returns([ provider ])
providers = AuthConfig.sso_providers
assert_equal [ "authentik" ], providers.map { |provider| provider[:name] }
assert_equal "super_admin", providers.first.dig(:settings, :default_role)
end
test "non-OIDC providers are listed only after their named strategy is registered" do
FeatureFlags.stubs(:db_sso_providers?).returns(true)
providers = [
{ name: "new_google", strategy: "google_oauth2", client_id: "client-id", client_secret: "client-secret" },
{ name: "new_github", strategy: "github", client_id: "client-id", client_secret: "client-secret" },
{ name: "new_saml", strategy: "saml", settings: { idp_sso_url: "https://idp.example.com/saml" } }
]
providers.each do |provider|
ProviderLoader.stubs(:load_providers).returns([ provider ])
Rails.configuration.x.auth.sso_providers = [ custom_oidc_provider ]
assert_empty AuthConfig.sso_providers, "#{provider[:strategy]} needs a registered route"
Rails.configuration.x.auth.sso_providers = [ provider ]
assert_equal [ provider[:name] ], AuthConfig.sso_providers.map { |config| config[:name] }
end
end
test "auth config excludes unconfigured strategies from database YAML fallback" do
Rails.env.stubs(:test?).returns(false)
FeatureFlags.stubs(:db_sso_providers?).returns(true)
SsoProvider.stubs(:enabled).returns(SsoProvider.none)
Rails.application.stubs(:config_for).with(:auth).returns(
"providers" => [
custom_oidc_provider.deep_stringify_keys,
{ "name" => "google_oauth2", "strategy" => "google_oauth2" },
{ "name" => "github", "strategy" => "github" }
]
)
ProviderLoader.clear_cache
with_env_overrides(
GOOGLE_OAUTH_CLIENT_ID: nil, GOOGLE_OAUTH_CLIENT_SECRET: nil,
GITHUB_CLIENT_ID: nil, GITHUB_CLIENT_SECRET: nil
) do
assert_equal [ "authentik" ], AuthConfig.sso_providers.map { |provider| provider[:name] }
end
ensure
ProviderLoader.clear_cache
end
test "dynamic OIDC route matches a provider configured with only an id" do
FeatureFlags.stubs(:db_sso_providers?).returns(true)
ProviderLoader.stubs(:load_providers).returns([ custom_oidc_provider.except(:name) ])
OmniAuth.config.mock_auth[:authentik] = OmniAuth::AuthHash.new(provider: "authentik", uid: "uid-123")
response = Rack::MockRequest.new(dynamic_omniauth_app).get("/auth/authentik/callback")
assert_equal "authentik", response.body
end
test "dynamic OIDC route accepts a callback with a trailing slash" do
FeatureFlags.stubs(:db_sso_providers?).returns(true)
ProviderLoader.stubs(:load_providers).returns([ custom_oidc_provider ])
OmniAuth.config.mock_auth[:authentik] = OmniAuth::AuthHash.new(provider: "authentik", uid: "uid-123")
response = Rack::MockRequest.new(dynamic_omniauth_app).get("/auth/authentik/callback/")
assert_equal "authentik", response.body
end
test "dynamic OIDC route supports YAML providers returned by the active provider loader" do
FeatureFlags.stubs(:db_sso_providers?).returns(true)
ProviderLoader.stubs(:load_providers).returns([ custom_oidc_provider.deep_stringify_keys ])
OmniAuth.config.mock_auth[:authentik] = OmniAuth::AuthHash.new(
provider: "authentik",
uid: "uid-123",
info: { email: "[email protected]" }
)
response = Rack::MockRequest.new(dynamic_omniauth_app).get("/auth/authentik/callback")
assert_equal 200, response.status
assert_equal "authentik", response.body
end
test "dynamic OIDC route uses YAML providers when no database providers are enabled" do
Rails.env.stubs(:test?).returns(false)
FeatureFlags.stubs(:db_sso_providers?).returns(true)
SsoProvider.stubs(:enabled).returns(SsoProvider.none)
Rails.application.stubs(:config_for).with(:auth).returns(
"providers" => [ custom_oidc_provider.deep_stringify_keys ]
)
ProviderLoader.clear_cache
OmniAuth.config.mock_auth[:authentik] = OmniAuth::AuthHash.new(
provider: "authentik",
uid: "uid-123",
info: { email: "[email protected]" }
)
response = Rack::MockRequest.new(dynamic_omniauth_app).get("/auth/authentik/callback")
assert_equal 200, response.status
assert_equal "authentik", response.body
ensure
ProviderLoader.clear_cache
end
private
# Build one Rack app to verify provider changes take effect without rebuilding middleware.
def dynamic_omniauth_app
Rack::Builder.new do
use Rack::Session::Cookie, secret: "a" * 64
use OmniAuth::Builder do
OmniauthProviderRegistry.register_dynamic_database_oidc_provider(self)
end
run lambda { |env|
auth = env["omniauth.auth"]
[ 200, { "content-type" => "text/plain" }, [ auth&.provider || "no auth" ] ]
}
end.to_app
end
# Create an enabled custom-named database OIDC provider for dynamic route tests.
def custom_oidc_provider
{
id: "authentik",
strategy: "openid_connect",
name: "authentik",
label: "Sign in with Authentik",
issuer: "https://idp.example.com",
client_id: "client-id",
client_secret: "client-secret",
redirect_uri: "https://app.example.com/auth/authentik/callback",
settings: {
scopes: "openid email profile groups",
prompt: "login"
}
}
end
end