(t *testing.T)
| 680 | } |
| 681 | |
| 682 | func TestLoginRefreshable(t *testing.T) { |
| 683 | expiry := time.Date(2030, 1, 2, 3, 4, 5, 0, time.UTC) |
| 684 | tests := []struct { |
| 685 | name string |
| 686 | secure bool |
| 687 | keyringErr bool |
| 688 | wantSecure bool |
| 689 | }{ |
| 690 | {name: "secure storage", secure: true, wantSecure: true}, |
| 691 | {name: "insecure storage", secure: false, wantSecure: false}, |
| 692 | {name: "secure storage falls back when keyring errors", secure: true, keyringErr: true, wantSecure: false}, |
| 693 | } |
| 694 | for _, tt := range tests { |
| 695 | t.Run(tt.name, func(t *testing.T) { |
| 696 | authCfg := newTestAuthConfig(t) |
| 697 | if tt.keyringErr { |
| 698 | keyring.MockInitWithError(errors.New("keyring is unavailable")) |
| 699 | } |
| 700 | credential := gh.Credential{ |
| 701 | Token: "access-token", |
| 702 | RefreshToken: "refresh-token", |
| 703 | ExpiresIn: 3600, |
| 704 | ExpiresAt: &expiry, |
| 705 | RefreshTokenExpiresIn: 86400, |
| 706 | RefreshTokenExpiresAt: &expiry, |
| 707 | } |
| 708 | |
| 709 | insecureStorageUsed, err := authCfg.LoginRefreshable("github.com", "test-user", credential, "ssh", tt.secure) |
| 710 | require.NoError(t, err) |
| 711 | require.Equal(t, tt.wantSecure, !insecureStorageUsed) |
| 712 | |
| 713 | gotCredential, secure, err := authCfg.refreshableForUser("github.com", "test-user") |
| 714 | require.NoError(t, err) |
| 715 | require.Equal(t, tt.wantSecure, secure) |
| 716 | |
| 717 | // refreshableForUser tags the credential with the source it was read from, so compare against the stored |
| 718 | // credential with the expected source filled in. |
| 719 | wantCredential := credential |
| 720 | if tt.wantSecure { |
| 721 | wantCredential.Source = "keyring" |
| 722 | } else { |
| 723 | wantCredential.Source = "refreshable_oauth_token" |
| 724 | } |
| 725 | require.Equal(t, wantCredential, gotCredential) |
| 726 | |
| 727 | // The user is active and the credential resolves as the active token. |
| 728 | activeUser, err := authCfg.ActiveUser("github.com") |
| 729 | require.NoError(t, err) |
| 730 | require.Equal(t, "test-user", activeUser) |
| 731 | |
| 732 | token := authCfg.ActiveToken("github.com").Token |
| 733 | require.Equal(t, "access-token", token) |
| 734 | |
| 735 | requireKeyWithValue(t, authCfg.cfg, []string{hostsKey, "github.com", gitProtocolKey}, "ssh") |
| 736 | }) |
| 737 | } |
| 738 | } |
| 739 |
nothing calls this directly
no test coverage detected