-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(profile/token): 'profile token' command must check the validity o…
…f the stored token. (#1339) * fix(profile/token): 'profile token' command must check the validity of the stored token. The 'fastly profile token' command did not check the validity (expiration) of the stored token, which meant that it would emit an invalid token if the stored session token (and refresh token) had expired. This PR changes the behavior so that the validity of the token is checked as it is for all commands which actually make use of the token. * Apply review feedback.
- Loading branch information
Showing
4 changed files
with
139 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,15 @@ import ( | |
"fmt" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/fastly/go-fastly/v9/fastly" | ||
|
||
root "github.com/fastly/cli/pkg/commands/profile" | ||
"github.com/fastly/cli/pkg/config" | ||
"github.com/fastly/cli/pkg/mock" | ||
"github.com/fastly/cli/pkg/testutil" | ||
fsttime "github.com/fastly/cli/pkg/time" | ||
) | ||
|
||
func TestProfileCreate(t *testing.T) { | ||
|
@@ -398,6 +400,8 @@ func TestProfileSwitch(t *testing.T) { | |
} | ||
|
||
func TestProfileToken(t *testing.T) { | ||
now := time.Now() | ||
|
||
scenarios := []testutil.CLIScenario{ | ||
{ | ||
Name: "validate the active profile token is displayed by default", | ||
|
@@ -417,14 +421,18 @@ func TestProfileToken(t *testing.T) { | |
ConfigFile: &config.File{ | ||
Profiles: config.Profiles{ | ||
"foo": &config.Profile{ | ||
Default: true, | ||
Email: "[email protected]", | ||
Token: "123", | ||
Default: true, | ||
Email: "[email protected]", | ||
Token: "123", | ||
RefreshTokenCreated: now.Unix(), | ||
RefreshTokenTTL: 600, | ||
}, | ||
"bar": &config.Profile{ | ||
Default: false, | ||
Email: "[email protected]", | ||
Token: "456", | ||
Default: false, | ||
Email: "[email protected]", | ||
Token: "456", | ||
RefreshTokenCreated: now.Unix(), | ||
RefreshTokenTTL: 600, | ||
}, | ||
}, | ||
}, | ||
|
@@ -449,14 +457,18 @@ func TestProfileToken(t *testing.T) { | |
ConfigFile: &config.File{ | ||
Profiles: config.Profiles{ | ||
"foo": &config.Profile{ | ||
Default: true, | ||
Email: "[email protected]", | ||
Token: "123", | ||
Default: true, | ||
Email: "[email protected]", | ||
Token: "123", | ||
RefreshTokenCreated: now.Unix(), | ||
RefreshTokenTTL: 600, | ||
}, | ||
"bar": &config.Profile{ | ||
Default: false, | ||
Email: "[email protected]", | ||
Token: "456", | ||
Default: false, | ||
Email: "[email protected]", | ||
Token: "456", | ||
RefreshTokenCreated: now.Unix(), | ||
RefreshTokenTTL: 600, | ||
}, | ||
}, | ||
}, | ||
|
@@ -481,14 +493,18 @@ func TestProfileToken(t *testing.T) { | |
ConfigFile: &config.File{ | ||
Profiles: config.Profiles{ | ||
"foo": &config.Profile{ | ||
Default: true, | ||
Email: "[email protected]", | ||
Token: "123", | ||
Default: true, | ||
Email: "[email protected]", | ||
Token: "123", | ||
RefreshTokenCreated: now.Unix(), | ||
RefreshTokenTTL: 600, | ||
}, | ||
"bar": &config.Profile{ | ||
Default: false, | ||
Email: "[email protected]", | ||
Token: "456", | ||
Default: false, | ||
Email: "[email protected]", | ||
Token: "456", | ||
RefreshTokenCreated: now.Unix(), | ||
RefreshTokenTTL: 600, | ||
}, | ||
}, | ||
}, | ||
|
@@ -512,6 +528,62 @@ func TestProfileToken(t *testing.T) { | |
}, | ||
WantError: "profile 'unknown' does not exist", | ||
}, | ||
{ | ||
Name: "validate that an expired token generates an error", | ||
Env: &testutil.EnvConfig{ | ||
Opts: &testutil.EnvOpts{ | ||
Copy: []testutil.FileIO{ | ||
{ | ||
Src: filepath.Join("testdata", "config.toml"), | ||
Dst: "config.toml", | ||
}, | ||
}, | ||
}, | ||
EditScenario: func(scenario *testutil.CLIScenario, rootdir string) { | ||
scenario.ConfigPath = filepath.Join(rootdir, "config.toml") | ||
}, | ||
}, | ||
ConfigFile: &config.File{ | ||
Profiles: config.Profiles{ | ||
"foo": &config.Profile{ | ||
Default: true, | ||
Email: "[email protected]", | ||
Token: "123", | ||
RefreshTokenCreated: now.Add(time.Duration(-1200) * time.Second).Unix(), | ||
RefreshTokenTTL: 600, | ||
}, | ||
}, | ||
}, | ||
WantError: fmt.Sprintf("the token in profile 'foo' expired at '%s'", now.Add(time.Duration(-600)*time.Second).UTC().Format(fsttime.Format)), | ||
}, | ||
{ | ||
Name: "validate that a soon-to-expire token generates an error", | ||
Env: &testutil.EnvConfig{ | ||
Opts: &testutil.EnvOpts{ | ||
Copy: []testutil.FileIO{ | ||
{ | ||
Src: filepath.Join("testdata", "config.toml"), | ||
Dst: "config.toml", | ||
}, | ||
}, | ||
}, | ||
EditScenario: func(scenario *testutil.CLIScenario, rootdir string) { | ||
scenario.ConfigPath = filepath.Join(rootdir, "config.toml") | ||
}, | ||
}, | ||
ConfigFile: &config.File{ | ||
Profiles: config.Profiles{ | ||
"foo": &config.Profile{ | ||
Default: true, | ||
Email: "[email protected]", | ||
Token: "123", | ||
RefreshTokenCreated: now.Unix(), | ||
RefreshTokenTTL: 30, | ||
}, | ||
}, | ||
}, | ||
WantError: fmt.Sprintf("the token in profile 'foo' will expire at '%s'", now.Add(time.Duration(30)*time.Second).UTC().Format(fsttime.Format)), | ||
}, | ||
} | ||
|
||
testutil.RunCLIScenarios(t, []string{root.CommandName, "token"}, scenarios) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters