1+ # frozen_string_literal: true
2+
3+ describe Hooks ::App ::Auth do
4+ # Create a test class that includes the auth module
5+ let ( :test_class ) do
6+ Class . new do
7+ include Hooks ::App ::Auth
8+
9+ def error! ( message , code )
10+ raise StandardError , "#{ message } (#{ code } )"
11+ end
12+ end
13+ end
14+
15+ let ( :auth_instance ) { test_class . new }
16+ let ( :payload ) { "test payload" }
17+ let ( :headers ) { { "Content-Type" => "application/json" } }
18+
19+ describe "#validate_auth!" do
20+ context "when auth config has secret_env_key" do
21+ let ( :endpoint_config ) do
22+ {
23+ auth : {
24+ type : "hmac" ,
25+ secret_env_key : "TEST_SECRET"
26+ }
27+ }
28+ end
29+
30+ context "when secret exists in environment" do
31+ before do
32+ ENV [ "TEST_SECRET" ] = "test-secret-value"
33+ end
34+
35+ after do
36+ ENV . delete ( "TEST_SECRET" )
37+ end
38+
39+ context "with HMAC auth type" do
40+ it "validates with HMAC plugin when authentication succeeds" do
41+ allow ( Hooks ::Plugins ::Auth ::HMAC ) . to receive ( :valid? ) . and_return ( true )
42+
43+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
44+ . not_to raise_error
45+
46+ expect ( Hooks ::Plugins ::Auth ::HMAC ) . to have_received ( :valid? ) . with (
47+ payload : payload ,
48+ headers : headers ,
49+ secret : "test-secret-value" ,
50+ config : endpoint_config
51+ )
52+ end
53+
54+ it "raises authentication failed error when HMAC validation fails" do
55+ allow ( Hooks ::Plugins ::Auth ::HMAC ) . to receive ( :valid? ) . and_return ( false )
56+
57+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
58+ . to raise_error ( StandardError , "authentication failed (401)" )
59+ end
60+ end
61+
62+ context "with shared_secret auth type" do
63+ let ( :endpoint_config ) do
64+ {
65+ auth : {
66+ type : "shared_secret" ,
67+ secret_env_key : "TEST_SECRET"
68+ }
69+ }
70+ end
71+
72+ it "validates with SharedSecret plugin when authentication succeeds" do
73+ allow ( Hooks ::Plugins ::Auth ::SharedSecret ) . to receive ( :valid? ) . and_return ( true )
74+
75+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
76+ . not_to raise_error
77+
78+ expect ( Hooks ::Plugins ::Auth ::SharedSecret ) . to have_received ( :valid? ) . with (
79+ payload : payload ,
80+ headers : headers ,
81+ secret : "test-secret-value" ,
82+ config : endpoint_config
83+ )
84+ end
85+
86+ it "raises authentication failed error when SharedSecret validation fails" do
87+ allow ( Hooks ::Plugins ::Auth ::SharedSecret ) . to receive ( :valid? ) . and_return ( false )
88+
89+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
90+ . to raise_error ( StandardError , "authentication failed (401)" )
91+ end
92+ end
93+
94+ context "with unsupported auth type" do
95+ let ( :endpoint_config ) do
96+ {
97+ auth : {
98+ type : "custom" ,
99+ secret_env_key : "TEST_SECRET"
100+ }
101+ }
102+ end
103+
104+ it "raises custom validators not implemented error" do
105+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
106+ . to raise_error ( StandardError , "Custom validators not implemented in POC (500)" )
107+ end
108+ end
109+
110+ context "with case variations in auth type" do
111+ let ( :endpoint_config ) do
112+ {
113+ auth : {
114+ type : "HMAC" ,
115+ secret_env_key : "TEST_SECRET"
116+ }
117+ }
118+ end
119+
120+ it "handles uppercase auth type" do
121+ allow ( Hooks ::Plugins ::Auth ::HMAC ) . to receive ( :valid? ) . and_return ( true )
122+
123+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
124+ . not_to raise_error
125+ end
126+ end
127+ end
128+
129+ context "when secret does not exist in environment" do
130+ let ( :endpoint_config ) do
131+ {
132+ auth : {
133+ type : "hmac" ,
134+ secret_env_key : "NONEXISTENT_SECRET"
135+ }
136+ }
137+ end
138+
139+ it "raises secret not found error" do
140+ ENV . delete ( "NONEXISTENT_SECRET" ) # Ensure it's not set
141+
142+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
143+ . to raise_error ( StandardError , "secret 'NONEXISTENT_SECRET' not found in environment (500)" )
144+ end
145+ end
146+ end
147+
148+ context "when auth config has no secret_env_key" do
149+ let ( :endpoint_config ) do
150+ {
151+ auth : {
152+ type : "hmac"
153+ }
154+ }
155+ end
156+
157+ it "returns without validation" do
158+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
159+ . not_to raise_error
160+
161+ # No auth plugins should be called
162+ expect ( Hooks ::Plugins ::Auth ::HMAC ) . not_to receive ( :valid? )
163+ expect ( Hooks ::Plugins ::Auth ::SharedSecret ) . not_to receive ( :valid? )
164+ end
165+ end
166+
167+ context "when auth config has nil secret_env_key" do
168+ let ( :endpoint_config ) do
169+ {
170+ auth : {
171+ type : "hmac" ,
172+ secret_env_key : nil
173+ }
174+ }
175+ end
176+
177+ it "returns without validation" do
178+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
179+ . not_to raise_error
180+ end
181+ end
182+
183+ context "when auth config has empty secret_env_key" do
184+ let ( :endpoint_config ) do
185+ {
186+ auth : {
187+ type : "hmac" ,
188+ secret_env_key : ""
189+ }
190+ }
191+ end
192+
193+ it "raises secret not found error for empty string" do
194+ ENV . delete ( "" ) # Ensure empty string key is not set
195+
196+ expect { auth_instance . validate_auth! ( payload , headers , endpoint_config ) }
197+ . to raise_error ( StandardError , "secret '' not found in environment (500)" )
198+ end
199+ end
200+ end
201+ end
0 commit comments