Skip to content

Commit 6dbc51c

Browse files
chore: undo storage changes from previous commit
Relates to STACKITTPR-761
1 parent 8e5ba12 commit 6dbc51c

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

internal/pkg/auth/auth.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ func UserSessionExpired() (bool, error) {
134134
}
135135

136136
func GetAccessToken() (string, error) {
137+
if accessToken := os.Getenv(envAccessTokenName); accessToken != "" {
138+
return accessToken, nil
139+
}
137140
accessToken, err := GetAuthField(ACCESS_TOKEN)
138141
if err != nil {
139142
return "", fmt.Errorf("get %s: %w", ACCESS_TOKEN, err)

internal/pkg/auth/auth_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,68 @@ func TestInitKeyFlow(t *testing.T) {
335335
})
336336
}
337337
}
338+
339+
func TestGetAccessToken_EnvVar(t *testing.T) {
340+
const envValue = "token-from-env"
341+
const storedValue = "stored-token"
342+
343+
tests := []struct {
344+
description string
345+
envToken string
346+
storedToken string
347+
expectedToken string
348+
isValid bool
349+
}{
350+
{
351+
description: "env var set and no stored token",
352+
envToken: envValue,
353+
expectedToken: envValue,
354+
isValid: true,
355+
},
356+
{
357+
description: "env var set and stored token present",
358+
envToken: envValue,
359+
storedToken: storedValue,
360+
expectedToken: envValue,
361+
isValid: true,
362+
},
363+
{
364+
description: "env var not set and stored token present",
365+
storedToken: storedValue,
366+
expectedToken: storedValue,
367+
isValid: true,
368+
},
369+
{
370+
description: "env var not set and no stored token",
371+
isValid: false,
372+
},
373+
}
374+
375+
for _, tt := range tests {
376+
t.Run(tt.description, func(t *testing.T) {
377+
keyring.MockInit()
378+
if tt.envToken != "" {
379+
t.Setenv(envAccessTokenName, tt.envToken)
380+
}
381+
if tt.storedToken != "" {
382+
if err := SetAuthField(ACCESS_TOKEN, tt.storedToken); err != nil {
383+
t.Fatalf("Failed to set stored token: %v", err)
384+
}
385+
if err := SetAuthFlow(AUTH_FLOW_SERVICE_ACCOUNT_TOKEN); err != nil {
386+
t.Fatalf("Failed to set auth flow: %v", err)
387+
}
388+
}
389+
390+
got, err := GetAccessToken()
391+
if err != nil {
392+
if !tt.isValid {
393+
return
394+
}
395+
t.Fatalf("unexpected error: %v", err)
396+
}
397+
if tt.expectedToken != got {
398+
t.Errorf("expected token %q, got %q", tt.expectedToken, got)
399+
}
400+
})
401+
}
402+
}

0 commit comments

Comments
 (0)