|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../../../spec_helper" |
| 4 | + |
| 5 | +describe "Custom Auth Plugin Integration" do |
| 6 | + let(:custom_auth_plugin_dir) { "/tmp/test_auth_plugins" } |
| 7 | + let(:plugin_file_content) do |
| 8 | + <<~RUBY |
| 9 | + module Hooks |
| 10 | + module Plugins |
| 11 | + module Auth |
| 12 | + class SomeCoolAuthPlugin < Base |
| 13 | + def self.valid?(payload:, headers:, config:) |
| 14 | + # Mock implementation: check for specific header |
| 15 | + secret = fetch_secret(config) |
| 16 | + bearer_token = headers["authorization"] |
| 17 | + bearer_token == "Bearer \#{secret}" |
| 18 | + end |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + end |
| 23 | + RUBY |
| 24 | + end |
| 25 | + |
| 26 | + let(:global_config) do |
| 27 | + { |
| 28 | + auth_plugin_dir: custom_auth_plugin_dir, |
| 29 | + handler_plugin_dir: "./spec/acceptance/handlers" |
| 30 | + } |
| 31 | + end |
| 32 | + |
| 33 | + let(:endpoint_config) do |
| 34 | + { |
| 35 | + path: "/example", |
| 36 | + handler: "DefaultHandler", |
| 37 | + auth: { |
| 38 | + type: "some_cool_auth_plugin", |
| 39 | + secret_env_key: "SUPER_COOL_SECRET", |
| 40 | + header: "Authorization" |
| 41 | + } |
| 42 | + } |
| 43 | + end |
| 44 | + |
| 45 | + before do |
| 46 | + FileUtils.mkdir_p(custom_auth_plugin_dir) |
| 47 | + File.write(File.join(custom_auth_plugin_dir, "some_cool_auth_plugin.rb"), plugin_file_content) |
| 48 | + ENV["SUPER_COOL_SECRET"] = "test-secret" |
| 49 | + end |
| 50 | + |
| 51 | + after do |
| 52 | + FileUtils.rm_rf(custom_auth_plugin_dir) if Dir.exist?(custom_auth_plugin_dir) |
| 53 | + ENV.delete("SUPER_COOL_SECRET") |
| 54 | + end |
| 55 | + |
| 56 | + it "successfully validates using a custom auth plugin" do |
| 57 | + # Create a test API class using the same pattern as the real API |
| 58 | + test_api_class = Class.new do |
| 59 | + include Hooks::App::Helpers |
| 60 | + include Hooks::App::Auth |
| 61 | + |
| 62 | + def error!(message, code) |
| 63 | + raise StandardError, "#{message} (#{code})" |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + instance = test_api_class.new |
| 68 | + payload = '{"test": "data"}' |
| 69 | + headers = { "authorization" => "Bearer test-secret" } |
| 70 | + |
| 71 | + # This should not raise any error |
| 72 | + expect do |
| 73 | + instance.validate_auth!(payload, headers, endpoint_config, global_config) |
| 74 | + end.not_to raise_error |
| 75 | + end |
| 76 | + |
| 77 | + it "rejects requests with invalid credentials using custom auth plugin" do |
| 78 | + test_api_class = Class.new do |
| 79 | + include Hooks::App::Helpers |
| 80 | + include Hooks::App::Auth |
| 81 | + |
| 82 | + def error!(message, code) |
| 83 | + raise StandardError, "#{message} (#{code})" |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + instance = test_api_class.new |
| 88 | + payload = '{"test": "data"}' |
| 89 | + headers = { "authorization" => "Bearer wrong-secret" } |
| 90 | + |
| 91 | + # This should raise authentication failed error |
| 92 | + expect do |
| 93 | + instance.validate_auth!(payload, headers, endpoint_config, global_config) |
| 94 | + end.to raise_error(StandardError, /authentication failed/) |
| 95 | + end |
| 96 | + |
| 97 | + it "works with the new configuration format" do |
| 98 | + # Test the new auth_plugin_dir configuration |
| 99 | + config = Hooks::Core::ConfigLoader.load(config_path: { |
| 100 | + auth_plugin_dir: "./custom/auth/plugins", |
| 101 | + handler_plugin_dir: "./custom/handlers" |
| 102 | + }) |
| 103 | + |
| 104 | + expect(config[:auth_plugin_dir]).to eq("./custom/auth/plugins") |
| 105 | + expect(config[:handler_plugin_dir]).to eq("./custom/handlers") |
| 106 | + expect(config[:handler_dir]).to eq("./custom/handlers") # backward compatibility |
| 107 | + end |
| 108 | + |
| 109 | + it "maintains backward compatibility with handler_dir" do |
| 110 | + # Test that old handler_dir configuration still works |
| 111 | + config = Hooks::Core::ConfigLoader.load(config_path: { |
| 112 | + handler_dir: "./legacy/handlers" |
| 113 | + }) |
| 114 | + |
| 115 | + expect(config[:handler_dir]).to eq("./legacy/handlers") |
| 116 | + expect(config[:handler_plugin_dir]).to eq("./legacy/handlers") |
| 117 | + end |
| 118 | +end |
0 commit comments