|
1 | 1 | package auth |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "io" |
| 5 | + "os" |
4 | 6 | "testing" |
5 | 7 |
|
6 | 8 | "github.com/brevdev/brev-cli/pkg/entity" |
@@ -38,10 +40,12 @@ func TestIsAccessTokenValid(t *testing.T) { |
38 | 40 |
|
39 | 41 | type MockAuthStore struct { |
40 | 42 | authTokens *entity.AuthTokens |
| 43 | + saved entity.AuthTokens |
41 | 44 | didSave bool |
42 | 45 | } |
43 | 46 |
|
44 | | -func (m *MockAuthStore) SaveAuthTokens(_ entity.AuthTokens) error { |
| 47 | +func (m *MockAuthStore) SaveAuthTokens(tokens entity.AuthTokens) error { |
| 48 | + m.saved = tokens |
45 | 49 | m.didSave = true |
46 | 50 | return nil |
47 | 51 | } |
@@ -79,6 +83,116 @@ func (m MockOauth) GetNewAuthTokensWithRefresh(_ string) (*entity.AuthTokens, er |
79 | 83 |
|
80 | 84 | const validToken = "abc" |
81 | 85 |
|
| 86 | +func TestGetFreshAccessTokenOrNil_APIKeySkipsJWTValidationAndRefresh(t *testing.T) { |
| 87 | + s := MockAuthStore{authTokens: &entity.AuthTokens{ |
| 88 | + AccessToken: "expired-jwt", |
| 89 | + APIKey: "brev_api_test-key", |
| 90 | + RefreshToken: "should-not-refresh", |
| 91 | + }} |
| 92 | + a := Auth{ |
| 93 | + &s, |
| 94 | + &MockOauth{}, func(_ string) (bool, error) { |
| 95 | + t.Fatal("api keys must not be parsed as JWTs") |
| 96 | + return false, nil |
| 97 | + }, |
| 98 | + func() (bool, error) { |
| 99 | + t.Fatal("api keys must not trigger login") |
| 100 | + return false, nil |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + res, err := a.GetFreshAccessTokenOrNil() |
| 105 | + assert.NoError(t, err) |
| 106 | + assert.Equal(t, "brev_api_test-key", res) |
| 107 | + assert.False(t, s.didSave) |
| 108 | +} |
| 109 | + |
| 110 | +func TestGetFreshAccessTokenOrNil_APIKeyOnlyCredentialReturnsAPIKey(t *testing.T) { |
| 111 | + s := MockAuthStore{authTokens: &entity.AuthTokens{ |
| 112 | + APIKey: "brev_api_test-key", |
| 113 | + }} |
| 114 | + a := Auth{ |
| 115 | + &s, |
| 116 | + &MockOauth{}, func(_ string) (bool, error) { |
| 117 | + t.Fatal("api keys must not be parsed as JWTs") |
| 118 | + return false, nil |
| 119 | + }, |
| 120 | + func() (bool, error) { |
| 121 | + t.Fatal("api keys must not trigger login") |
| 122 | + return false, nil |
| 123 | + }, |
| 124 | + } |
| 125 | + |
| 126 | + res, err := a.GetFreshAccessTokenOrNil() |
| 127 | + assert.NoError(t, err) |
| 128 | + assert.Equal(t, "brev_api_test-key", res) |
| 129 | + assert.False(t, s.didSave) |
| 130 | +} |
| 131 | + |
| 132 | +func TestLoginWithAPIKey_SavesTypedCredential(t *testing.T) { |
| 133 | + s := MockAuthStore{} |
| 134 | + a := Auth{ |
| 135 | + authStore: &s, |
| 136 | + oauth: &MockOauth{}, |
| 137 | + } |
| 138 | + |
| 139 | + err := a.LoginWithAPIKey("brev_api_test-key") |
| 140 | + assert.NoError(t, err) |
| 141 | + assert.True(t, s.didSave) |
| 142 | + assert.Equal(t, entity.AuthTokens{ |
| 143 | + APIKey: "brev_api_test-key", |
| 144 | + }, s.saved) |
| 145 | +} |
| 146 | + |
| 147 | +func TestLoginWithAPIKey_PreservesExistingJWT(t *testing.T) { |
| 148 | + s := MockAuthStore{authTokens: &entity.AuthTokens{ |
| 149 | + AccessToken: "existing-jwt", |
| 150 | + RefreshToken: "existing-refresh", |
| 151 | + }} |
| 152 | + a := Auth{ |
| 153 | + authStore: &s, |
| 154 | + oauth: &MockOauth{}, |
| 155 | + } |
| 156 | + |
| 157 | + err := a.LoginWithAPIKey("brev_api_test-key") |
| 158 | + assert.NoError(t, err) |
| 159 | + assert.Equal(t, entity.AuthTokens{ |
| 160 | + AccessToken: "existing-jwt", |
| 161 | + RefreshToken: "existing-refresh", |
| 162 | + APIKey: "brev_api_test-key", |
| 163 | + }, s.saved) |
| 164 | +} |
| 165 | + |
| 166 | +func TestLoginWithAPIKey_EmptyKeyReturnsError(t *testing.T) { |
| 167 | + s := MockAuthStore{} |
| 168 | + a := Auth{ |
| 169 | + authStore: &s, |
| 170 | + oauth: &MockOauth{}, |
| 171 | + } |
| 172 | + |
| 173 | + err := a.LoginWithAPIKey("") |
| 174 | + assert.Error(t, err) |
| 175 | + assert.False(t, s.didSave) |
| 176 | +} |
| 177 | + |
| 178 | +func TestStandardLogin_APIKeyCredentialDoesNotProbeOAuthProviders(t *testing.T) { |
| 179 | + oldStdout := os.Stdout |
| 180 | + readPipe, writePipe, err := os.Pipe() |
| 181 | + assert.NoError(t, err) |
| 182 | + os.Stdout = writePipe |
| 183 | + |
| 184 | + _ = StandardLogin("", "", &entity.AuthTokens{ |
| 185 | + AccessToken: "existing-jwt", |
| 186 | + APIKey: "brev_api_test-key", |
| 187 | + }) |
| 188 | + |
| 189 | + assert.NoError(t, writePipe.Close()) |
| 190 | + os.Stdout = oldStdout |
| 191 | + out, err := io.ReadAll(readPipe) |
| 192 | + assert.NoError(t, err) |
| 193 | + assert.Empty(t, string(out)) |
| 194 | +} |
| 195 | + |
82 | 196 | func TestSuccessNoRefreshGetFreshAccessTokenOrLogin(t *testing.T) { |
83 | 197 | s := MockAuthStore{authTokens: &entity.AuthTokens{ |
84 | 198 | AccessToken: validToken, |
|
0 commit comments