(t *testing.T)
| 248 | } |
| 249 | |
| 250 | func TestSetActiveCredential(t *testing.T) { |
| 251 | tests := []struct { |
| 252 | name string |
| 253 | setup func(t *testing.T, authCfg *AuthConfig) |
| 254 | wantFound bool |
| 255 | assertActive func(t *testing.T, authCfg *AuthConfig) |
| 256 | }{ |
| 257 | { |
| 258 | // This mixed state should not occur in practice: the write paths keep the two credential forms mutually |
| 259 | // exclusive, and loginRefreshable verifies removal of any non-expiring token before storing a refreshable |
| 260 | // one. The case documents the resolution precedence regardless: the non-expiring keyring token is probed |
| 261 | // first, so it is activated ahead of the refreshable credential, matching TokenForUser so activation and |
| 262 | // resolution agree. |
| 263 | name: "prefers non-expiring keyring over refreshable", |
| 264 | setup: func(t *testing.T, authCfg *AuthConfig) { |
| 265 | require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user", "plain-token")) |
| 266 | setRefreshableConfigField(t, authCfg, "github.com", "test-user", "access_token", "access") |
| 267 | setRefreshableConfigField(t, authCfg, "github.com", "test-user", "refresh_token", "refresh") |
| 268 | }, |
| 269 | wantFound: true, |
| 270 | assertActive: func(t *testing.T, authCfg *AuthConfig) { |
| 271 | // The non-expiring keyring token is moved into the host active slot ahead of the refreshable credential. |
| 272 | got, err := keyring.Get(keyringServiceName("github.com"), "") |
| 273 | require.NoError(t, err) |
| 274 | require.Equal(t, "plain-token", got) |
| 275 | }, |
| 276 | }, |
| 277 | { |
| 278 | name: "falls back to non-expiring keyring token", |
| 279 | setup: func(t *testing.T, authCfg *AuthConfig) { |
| 280 | require.NoError(t, keyring.Set(keyringServiceName("github.com"), "test-user", "plain-token")) |
| 281 | }, |
| 282 | wantFound: true, |
| 283 | assertActive: func(t *testing.T, authCfg *AuthConfig) { |
| 284 | got, err := keyring.Get(keyringServiceName("github.com"), "") |
| 285 | require.NoError(t, err) |
| 286 | require.Equal(t, "plain-token", got) |
| 287 | }, |
| 288 | }, |
| 289 | { |
| 290 | name: "not found", |
| 291 | setup: func(t *testing.T, authCfg *AuthConfig) {}, |
| 292 | wantFound: false, |
| 293 | }, |
| 294 | } |
| 295 | for _, tt := range tests { |
| 296 | t.Run(tt.name, func(t *testing.T) { |
| 297 | authCfg := newTestAuthConfig(t) |
| 298 | tt.setup(t, authCfg) |
| 299 | |
| 300 | found, err := authCfg.setActiveCredential("github.com", "test-user") |
| 301 | |
| 302 | require.NoError(t, err) |
| 303 | require.Equal(t, tt.wantFound, found) |
| 304 | if tt.assertActive != nil { |
| 305 | tt.assertActive(t, authCfg) |
| 306 | } |
| 307 | }) |
nothing calls this directly
no test coverage detected