-
Notifications
You must be signed in to change notification settings - Fork 593
/
sso_cached_token_test.go
189 lines (173 loc) · 4.78 KB
/
sso_cached_token_test.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package ssocreds
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/google/go-cmp/cmp"
)
var tokenCmpOptions = cmp.Options{
cmp.AllowUnexported(token{}, tokenKnownFields{}, rfc3339{}),
}
func TestStandardSSOCacheTokenFilepath(t *testing.T) {
origHomeDur := osUserHomeDur
defer func() {
osUserHomeDur = origHomeDur
}()
cases := map[string]struct {
key string
osUserHomeDir func() string
expectFilename string
expectErr string
}{
"success": {
key: "https://example.awsapps.com/start",
osUserHomeDir: func() string {
return os.TempDir()
},
expectFilename: filepath.Join(os.TempDir(), ".aws", "sso", "cache",
"e8be5486177c5b5392bd9aa76563515b29358e6e.json"),
},
"failure": {
key: "https://example.awsapps.com/start",
osUserHomeDir: func() string {
return ""
},
expectErr: "some error",
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
osUserHomeDur = c.osUserHomeDir
actual, err := StandardCachedTokenFilepath(c.key)
if c.expectErr != "" {
if err == nil {
t.Fatalf("expect error, got none")
}
return
}
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := c.expectFilename, actual; e != a {
t.Errorf("expect %v filename, got %v", e, a)
}
})
}
}
func TestLoadCachedToken(t *testing.T) {
cases := map[string]struct {
filename string
expectToken token
expectErr string
}{
"file not found": {
filename: filepath.Join("testdata", "does_not_exist.json"),
expectErr: "failed to read cached SSO token file",
},
"invalid json": {
filename: filepath.Join("testdata", "invalid_json.json"),
expectErr: "failed to parse cached SSO token file",
},
"missing accessToken": {
filename: filepath.Join("testdata", "missing_accessToken.json"),
expectErr: "must contain accessToken and expiresAt fields",
},
"missing expiresAt": {
filename: filepath.Join("testdata", "missing_expiresAt.json"),
expectErr: "must contain accessToken and expiresAt fields",
},
"standard token": {
filename: filepath.Join("testdata", "valid_token.json"),
expectToken: token{
tokenKnownFields: tokenKnownFields{
AccessToken: "dGhpcyBpcyBub3QgYSByZWFsIHZhbHVl",
ExpiresAt: (*rfc3339)(aws.Time(time.Date(2044, 4, 4, 7, 0, 1, 0, time.UTC))),
ClientID: "client id",
ClientSecret: "client secret",
RefreshToken: "refresh token",
},
UnknownFields: map[string]interface{}{
"unknownField": "some value",
"registrationExpiresAt": "2044-04-04T07:00:01Z",
"region": "region",
"startURL": "start URL",
},
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
actualToken, err := loadCachedToken(c.filename)
if c.expectErr != "" {
if err == nil {
t.Fatalf("expect %v error, got none", c.expectErr)
}
if e, a := c.expectErr, err.Error(); !strings.Contains(a, e) {
t.Fatalf("expect %v error, got %v", e, a)
}
return
}
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if diff := cmp.Diff(c.expectToken, actualToken, tokenCmpOptions...); diff != "" {
t.Errorf("expect tokens match\n%s", diff)
}
})
}
}
func TestStoreCachedToken(t *testing.T) {
tempDir, err := ioutil.TempDir(os.TempDir(), "aws-sdk-go-v2-"+t.Name())
if err != nil {
t.Fatalf("failed to create temporary test directory, %v", err)
}
defer func() {
if err := os.RemoveAll(tempDir); err != nil {
t.Errorf("failed to cleanup temporary test directory, %v", err)
}
}()
cases := map[string]struct {
token token
filename string
fileMode os.FileMode
}{
"standard token": {
filename: filepath.Join(tempDir, "token_file.json"),
fileMode: 0600,
token: token{
tokenKnownFields: tokenKnownFields{
AccessToken: "dGhpcyBpcyBub3QgYSByZWFsIHZhbHVl",
ExpiresAt: (*rfc3339)(aws.Time(time.Date(2044, 4, 4, 7, 0, 1, 0, time.UTC))),
ClientID: "client id",
ClientSecret: "client secret",
RefreshToken: "refresh token",
},
UnknownFields: map[string]interface{}{
"unknownField": "some value",
"registrationExpiresAt": "2044-04-04T07:00:01Z",
"region": "region",
"startURL": "start URL",
},
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
err := storeCachedToken(c.filename, c.token, c.fileMode)
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
actual, err := loadCachedToken(c.filename)
if err != nil {
t.Fatalf("failed to load stored token, %v", err)
}
if diff := cmp.Diff(c.token, actual, tokenCmpOptions...); diff != "" {
t.Errorf("expect tokens match\n%s", diff)
}
})
}
}