-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsso_test.go
More file actions
174 lines (152 loc) · 4.57 KB
/
Copy pathsso_test.go
File metadata and controls
174 lines (152 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package aws
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
"time"
"unic/internal/config"
)
const testSSOStartURL = "https://example.awsapps.com/start"
func testSSOConfig() *config.Config {
return &config.Config{
ContextName: "dev-sso",
AuthType: config.AuthTypeSSO,
Region: "us-east-1",
SSOStartURL: testSSOStartURL,
SSOAccountID: "123456789012",
SSORoleName: "AdministratorAccess",
}
}
func writeTestSSOToken(t *testing.T, home string, startURL string, expiresAt time.Time) {
t.Helper()
cacheDir := filepath.Join(home, ".aws", "sso", "cache")
if err := os.MkdirAll(cacheDir, 0700); err != nil {
t.Fatal(err)
}
token := ssoTokenCache{
AccessToken: "cached-token",
ExpiresAt: expiresAt.UTC().Format(time.RFC3339),
Region: "us-east-1",
StartURL: startURL,
}
data, err := json.Marshal(token)
if err != nil {
t.Fatal(err)
}
hash := sha1.Sum([]byte(startURL))
filename := hex.EncodeToString(hash[:]) + ".json"
if err := os.WriteFile(filepath.Join(cacheDir, filename), data, 0600); err != nil {
t.Fatal(err)
}
}
func TestCheckSSOSessionUsesValidCachedToken(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
writeTestSSOToken(t, home, testSSOStartURL, time.Now().Add(time.Hour))
check, err := CheckSSOSession(testSSOConfig())
if err != nil {
t.Fatalf("expected session check to succeed, got %v", err)
}
if check.LoginRequired {
t.Fatal("expected valid cached SSO token to skip login")
}
if check.StartURL != testSSOStartURL {
t.Fatalf("expected start URL %q, got %q", testSSOStartURL, check.StartURL)
}
}
func TestEnsureSSOLoginReusesValidCachedToken(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
writeTestSSOToken(t, home, testSSOStartURL, time.Now().Add(time.Hour))
origRunLogin := runSSOLoginFn
defer func() { runSSOLoginFn = origRunLogin }()
runSSOLoginFn = func(*config.Config) error {
t.Fatal("RunSSOLogin should not be called with a valid cached token")
return nil
}
result, err := EnsureSSOLogin(testSSOConfig())
if err != nil {
t.Fatalf("expected cached SSO login to succeed, got %v", err)
}
if result.Refreshed {
t.Fatal("expected cached SSO token to be reused")
}
if result.StartURL != testSSOStartURL {
t.Fatalf("expected start URL %q, got %q", testSSOStartURL, result.StartURL)
}
}
func TestEnsureSSOLoginRunsLoginWhenTokenMissing(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
origRunLogin := runSSOLoginFn
defer func() { runSSOLoginFn = origRunLogin }()
loginCalls := 0
runSSOLoginFn = func(*config.Config) error {
loginCalls++
writeTestSSOToken(t, home, testSSOStartURL, time.Now().Add(time.Hour))
return nil
}
result, err := EnsureSSOLogin(testSSOConfig())
if err != nil {
t.Fatalf("expected missing SSO token to trigger login, got %v", err)
}
if loginCalls != 1 {
t.Fatalf("expected one login call, got %d", loginCalls)
}
if !result.Refreshed {
t.Fatal("expected login result to report a refreshed session")
}
}
func TestEnsureSSOLoginRunsLoginWhenTokenExpired(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
writeTestSSOToken(t, home, testSSOStartURL, time.Now().Add(-time.Hour))
origRunLogin := runSSOLoginFn
defer func() { runSSOLoginFn = origRunLogin }()
loginCalls := 0
runSSOLoginFn = func(*config.Config) error {
loginCalls++
writeTestSSOToken(t, home, testSSOStartURL, time.Now().Add(time.Hour))
return nil
}
result, err := EnsureSSOLogin(testSSOConfig())
if err != nil {
t.Fatalf("expected expired SSO token to trigger login, got %v", err)
}
if loginCalls != 1 {
t.Fatalf("expected one login call, got %d", loginCalls)
}
if !result.Refreshed {
t.Fatal("expected login result to report a refreshed session")
}
}
func TestBuildSSOLoginCmdCleansTempDirOnConfigWriteError(t *testing.T) {
tempRoot := t.TempDir()
t.Setenv("TMPDIR", tempRoot)
origWriteSSOConfigFile := writeSSOConfigFile
defer func() { writeSSOConfigFile = origWriteSSOConfigFile }()
writeSSOConfigFile = func(string, []byte, os.FileMode) error {
return errors.New("write failed")
}
cmd, cleanup, err := BuildSSOLoginCmd(testSSOConfig())
if err == nil {
t.Fatal("expected config write error")
}
if cmd != nil {
t.Fatalf("expected nil command on error, got %v", cmd)
}
if cleanup != nil {
t.Fatal("expected nil cleanup when setup fails")
}
matches, globErr := filepath.Glob(filepath.Join(tempRoot, "unic-sso-*"))
if globErr != nil {
t.Fatalf("failed to inspect temp dirs: %v", globErr)
}
if len(matches) != 0 {
t.Fatalf("expected temp config directory to be removed, found %v", matches)
}
}