@@ -141,10 +141,13 @@ func TestAuthorize_CEMode_CommunityServer(t *testing.T) {
141141 assert .Equal (t , "ce" , * called )
142142}
143143
144- // TestAuthorizeCommunityMode_CleansDesktopEntries verifies that the real
145- // authorizeCommunityMode function cleans stale Desktop Secrets Engine
146- // entries before proceeding with the Gateway OAuth flow.
147- func TestAuthorizeCommunityMode_CleansDesktopEntries (t * testing.T ) {
144+ // TestAuthorizeCommunityMode_NoCleanupOnFailure verifies that
145+ // authorizeCommunityMode does NOT clean stale Desktop entries when the
146+ // authorize flow fails before token storage. This ensures the user
147+ // retains their existing Desktop authorization as a fallback if the
148+ // community flow fails mid-way (port conflict, user closes browser, etc.).
149+ // Cleanup only runs after the fresh token is safely stored in docker pass.
150+ func TestAuthorizeCommunityMode_NoCleanupOnFailure (t * testing.T ) {
148151 // Save and restore all function pointers touched by this test.
149152 oldDesktopCleanup := cleanStaleDesktopEntriesFunc
150153 oldCheckPass := checkHasDockerPassFunc
@@ -158,21 +161,20 @@ func TestAuthorizeCommunityMode_CleansDesktopEntries(t *testing.T) {
158161 // Mock docker pass check to succeed.
159162 checkHasDockerPassFunc = func (_ context.Context ) error { return nil }
160163
161- // Mock callback server creation to fail — this stops execution right
162- // after cleanup runs, avoiding the need to mock DCR, PKCE, etc.
164+ // Mock callback server creation to fail — simulates a mid-flow failure.
163165 newCallbackServerFunc = func () (* pkgoauth.CallbackServer , error ) {
164- return nil , fmt .Errorf ("test: stop after cleanup " )
166+ return nil , fmt .Errorf ("test: port conflict " )
165167 }
166168
167- var desktopCleanupCalled string
168- cleanStaleDesktopEntriesFunc = func (_ context.Context , app string ) {
169- desktopCleanupCalled = app
169+ var desktopCleanupCalled bool
170+ cleanStaleDesktopEntriesFunc = func (_ context.Context , _ string ) {
171+ desktopCleanupCalled = true
170172 }
171173
172174 // Call the real authorizeCommunityMode directly.
173175 err := authorizeCommunityMode (t .Context (), "my-community-server" , "" )
174176 require .Error (t , err )
175177 assert .Contains (t , err .Error (), "failed to create callback server" )
176- assert .Equal ( t , "my-community-server" , desktopCleanupCalled ,
177- "community authorize should clean stale Desktop entries before proceeding " )
178+ assert .False ( t , desktopCleanupCalled ,
179+ "community authorize should NOT clean Desktop entries when flow fails before token storage " )
178180}
0 commit comments