@@ -154,6 +154,41 @@ func TestWithOperationAuthorizationMiddleware(t *testing.T) {
154154 assert .Contains (t , err .Error (), "unable to verify operation authorization" )
155155 })
156156
157+ t .Run ("cache key includes orgID so different orgs are not conflated" , func (t * testing.T ) {
158+ var callCount atomic.Int32
159+ srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
160+ callCount .Add (1 )
161+ var req operationAuthRequest
162+ require .NoError (t , json .NewDecoder (r .Body ).Decode (& req ))
163+
164+ allowed := req .OrganizationID == "org-allowed"
165+ w .Header ().Set ("Content-Type" , "application/json" )
166+ json .NewEncoder (w ).Encode (operationAuthResponse {Allowed : allowed , Reason : "denied for this org" }) //nolint:errcheck
167+ }))
168+ defer srv .Close ()
169+
170+ cfg := & conf.OperationAuthorizationProvider {Enabled : true , Url : srv .URL }
171+ m := WithOperationAuthorizationMiddleware (cfg , logHelper )
172+
173+ baseCtx := ctxWithOperation (context .Background (), "/controlplane.v1.OrganizationService/Create" )
174+ baseCtx = entities .WithCurrentUser (baseCtx , & entities.User {ID : "user-org-test" })
175+
176+ // Call with org-allowed -> should succeed and be cached
177+ ctxAllowed := entities .WithCurrentOrg (baseCtx , & entities.Org {ID : "org-allowed" })
178+ result , err := m (passHandler )(ctxAllowed , nil )
179+ require .NoError (t , err )
180+ assert .Equal (t , "ok" , result )
181+ assert .Equal (t , int32 (1 ), callCount .Load ())
182+
183+ // Call with org-denied -> must NOT reuse the cached "allowed" result
184+ ctxDenied := entities .WithCurrentOrg (baseCtx , & entities.Org {ID : "org-denied" })
185+ result , err = m (passHandler )(ctxDenied , nil )
186+ require .Error (t , err )
187+ assert .Nil (t , result )
188+ assert .Contains (t , err .Error (), "denied for this org" )
189+ assert .Equal (t , int32 (2 ), callCount .Load (), "expected a second HTTP call for a different org" )
190+ })
191+
157192 t .Run ("cache hit skips HTTP call" , func (t * testing.T ) {
158193 var callCount atomic.Int32
159194 srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
0 commit comments