@@ -197,6 +197,49 @@ func TestInlineAuthCacheKeyDeduplicatesAbsoluteAuthCodeEndpoints(t *testing.T) {
197197 }
198198}
199199
200+ func TestCachedOAuthTokenEntryMigratesLegacyInlineAuthCodeCacheKey (t * testing.T ) {
201+ cacheFile := filepath .Join (t .TempDir (), "tokens.cbor" )
202+ c := New ()
203+ c .Hooks ().TokenCachePath = cacheFile
204+
205+ ac := & config.AuthConfig {
206+ Type : "oauth-authorization-code" ,
207+ Params : map [string ]string {
208+ "client_id" : "restish" ,
209+ "authorize_url" : "https://dex.example.com/auth" ,
210+ "token_url" : "https://dex.example.com/token" ,
211+ },
212+ }
213+ cacheKey := inlineAuthCacheKey ("demo:default" , ac , "https://api.example.com" )
214+ if cacheKey == "" || cacheKey == "demo:default" {
215+ t .Fatalf ("inline auth code cache key = %q, want hashed oauth key" , cacheKey )
216+ }
217+
218+ tc := auth .NewTokenCache (cacheFile )
219+ if err := tc .Set ("demo:default" , auth.CachedToken {AccessToken : "legacy-token" }); err != nil {
220+ t .Fatalf ("seed legacy token: %v" , err )
221+ }
222+
223+ got := c .cachedOAuthTokenEntry ("oauth-authorization-code" , cacheKey , "demo" , "default" )
224+ if got == nil || got .AccessToken != "legacy-token" {
225+ t .Fatalf ("migrated token = %+v, want legacy-token" , got )
226+ }
227+ migrated , err := tc .Get (cacheKey )
228+ if err != nil {
229+ t .Fatalf ("read migrated token: %v" , err )
230+ }
231+ if migrated == nil || migrated .AccessToken != "legacy-token" {
232+ t .Fatalf ("migrated cache entry = %+v, want legacy-token" , migrated )
233+ }
234+ legacy , err := tc .Get ("demo:default" )
235+ if err != nil {
236+ t .Fatalf ("read legacy token: %v" , err )
237+ }
238+ if legacy != nil {
239+ t .Fatalf ("legacy cache entry still present: %+v" , legacy )
240+ }
241+ }
242+
200243func TestInlineAuthCacheKeyDeduplicatesIssuerAuthCodeEndpoints (t * testing.T ) {
201244 ac := & config.AuthConfig {
202245 Type : "oauth-authorization-code" ,
@@ -363,3 +406,50 @@ func TestAuthHandlerForOAuthUsesThemeCallbackColors(t *testing.T) {
363406 t .Fatalf ("failure color = %q, want #ff0000" , oauthHandler .CallbackFailureColor )
364407 }
365408}
409+
410+ func TestOperationSetForAPIHintsAPISync_WhenCacheEmpty (t * testing.T ) {
411+ var stderr bytes.Buffer
412+ c := New ()
413+ c .Stderr = & stderr
414+ c .Hooks ().SpecCachePath = t .TempDir () // empty cache dir
415+
416+ apiCfg := & config.APIConfig {BaseURL : "https://api.example.com" }
417+ set , ok , err := c .operationSetForAPI (context .Background (), "example" , apiCfg , "default" , false )
418+ if err != nil {
419+ t .Fatalf ("unexpected error: %v" , err )
420+ }
421+ if ok || len (set .Operations ) > 0 {
422+ t .Fatal ("expected empty operation set from cold cache" )
423+ }
424+ if ! strings .Contains (stderr .String (), "api sync example" ) {
425+ t .Fatalf ("expected api sync hint on stderr, got: %q" , stderr .String ())
426+ }
427+ }
428+
429+ func TestOperationSetForAPINoHintWhenForceRefresh (t * testing.T ) {
430+ // forceRefresh=true means an explicit sync is already in progress; no hint.
431+ var stderr bytes.Buffer
432+ c := New ()
433+ c .Stderr = & stderr
434+ c .Hooks ().SpecCachePath = t .TempDir ()
435+
436+ apiCfg := & config.APIConfig {BaseURL : "https://203.0.113.1" }
437+ _ , _ , _ = c .operationSetForAPI (context .Background (), "example" , apiCfg , "default" , true )
438+ if strings .Contains (stderr .String (), "api sync" ) {
439+ t .Fatalf ("expected no api sync hint during forceRefresh, got: %q" , stderr .String ())
440+ }
441+ }
442+
443+ func TestOperationSetForAPINoHintWhenNoSpecSource (t * testing.T ) {
444+ // An API with no BaseURL, no SpecURL, no SpecFiles configured has nothing to sync.
445+ var stderr bytes.Buffer
446+ c := New ()
447+ c .Stderr = & stderr
448+ c .Hooks ().SpecCachePath = t .TempDir ()
449+
450+ apiCfg := & config.APIConfig {}
451+ _ , _ , _ = c .operationSetForAPI (context .Background (), "bare" , apiCfg , "default" , false )
452+ if strings .Contains (stderr .String (), "api sync" ) {
453+ t .Fatalf ("expected no api sync hint for API with no spec source, got: %q" , stderr .String ())
454+ }
455+ }
0 commit comments