Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
clhuang authored and lucix-aws committed Jul 10, 2024
1 parent 60f7cd9 commit ea816c4
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions credentials/ssocreds/sso_cached_token_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ssocreds

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -182,3 +183,90 @@ func TestStoreCachedToken(t *testing.T) {
})
}
}

func TestStoreTokenTimeZone(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
expectedExpiresAt string
}{
"utc 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",
},
},
expectedExpiresAt: "2044-04-04T07:00:01Z",
},
"non-utc 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.FixedZone("UTC-8", -8*60*60)))),
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",
},
},
expectedExpiresAt: "2044-04-04T15:00:01Z",
},
}

type ExpiresAt struct {
ExpiresAt string `json:"expiresAt"`
}

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)
}

fileBytes, err := ioutil.ReadFile(c.filename)
if err != nil {
t.Fatalf("failed to load file, %v", err)
}
var expiresAtData ExpiresAt
err = json.Unmarshal(fileBytes, &expiresAtData)
if err != nil {
t.Fatalf("failed to unmarshal expiresAt, %v", err)
}

if expiresAtData.ExpiresAt != c.expectedExpiresAt {
t.Errorf("expect %v, got %v", c.expectedExpiresAt, expiresAtData.ExpiresAt)
}
})
}
}

0 comments on commit ea816c4

Please sign in to comment.