(t *testing.T)
| 519 | } |
| 520 | |
| 521 | func TestLoginRefreshable(t *testing.T) { |
| 522 | expiry := time.Date(2030, 1, 2, 3, 4, 5, 0, time.UTC) |
| 523 | tests := []struct { |
| 524 | name string |
| 525 | secure bool |
| 526 | keyringErr bool |
| 527 | wantSecure bool |
| 528 | }{ |
| 529 | {name: "secure storage", secure: true, wantSecure: true}, |
| 530 | {name: "insecure storage", secure: false, wantSecure: false}, |
| 531 | {name: "secure storage falls back when keyring errors", secure: true, keyringErr: true, wantSecure: false}, |
| 532 | } |
| 533 | for _, tt := range tests { |
| 534 | t.Run(tt.name, func(t *testing.T) { |
| 535 | authCfg := newTestAuthConfig(t) |
| 536 | if tt.keyringErr { |
| 537 | keyring.MockInitWithError(errors.New("keyring is unavailable")) |
| 538 | } |
| 539 | credential := gh.Credential{ |
| 540 | Token: "access-token", |
| 541 | RefreshToken: "refresh-token", |
| 542 | ExpiresIn: 3600, |
| 543 | ExpiresAt: &expiry, |
| 544 | RefreshTokenExpiresIn: 86400, |
| 545 | RefreshTokenExpiresAt: &expiry, |
| 546 | } |
| 547 | |
| 548 | insecureStorageUsed, err := authCfg.LoginRefreshable("github.com", "test-user", credential, "ssh", tt.secure) |
| 549 | require.NoError(t, err) |
| 550 | require.Equal(t, tt.wantSecure, !insecureStorageUsed) |
| 551 | |
| 552 | gotCredential, secure, err := authCfg.refreshableForUser("github.com", "test-user") |
| 553 | require.NoError(t, err) |
| 554 | require.Equal(t, tt.wantSecure, secure) |
| 555 | |
| 556 | // refreshableForUser tags the credential with the source it was read from, so compare against the stored |
| 557 | // credential with the expected source filled in. |
| 558 | wantCredential := credential |
| 559 | if tt.wantSecure { |
| 560 | wantCredential.Source = "keyring" |
| 561 | } else { |
| 562 | wantCredential.Source = "refreshable_oauth_token" |
| 563 | } |
| 564 | require.Equal(t, wantCredential, gotCredential) |
| 565 | |
| 566 | // The user is active and the credential resolves as the active token. |
| 567 | activeUser, err := authCfg.ActiveUser("github.com") |
| 568 | require.NoError(t, err) |
| 569 | require.Equal(t, "test-user", activeUser) |
| 570 | |
| 571 | token := authCfg.ActiveToken("github.com").Token |
| 572 | require.Equal(t, "access-token", token) |
| 573 | |
| 574 | requireKeyWithValue(t, authCfg.cfg, []string{hostsKey, "github.com", gitProtocolKey}, "ssh") |
| 575 | }) |
| 576 | } |
| 577 | } |
| 578 |
nothing calls this directly
no test coverage detected