-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtoken_test.go
More file actions
71 lines (63 loc) · 1.67 KB
/
token_test.go
File metadata and controls
71 lines (63 loc) · 1.67 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
package auth_test
import (
"bytes"
"errors"
"testing"
"github.com/fastly/kingpin"
authcmd "github.com/fastly/cli/pkg/commands/auth"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/global"
)
func newTokenCommand(g *global.Data) *authcmd.TokenCommand {
app := kingpin.New("fastly", "test")
parent := app.Command("auth", "test auth")
return authcmd.NewTokenCommand(parent, g)
}
func globalDataWithToken(token string) *global.Data {
return &global.Data{
Config: config.File{
Auth: config.Auth{
Default: "user",
Tokens: config.AuthTokens{
"user": &config.AuthToken{
Type: config.AuthTokenTypeStatic,
Token: token,
},
},
},
},
}
}
func TestToken_NonTTY_Success(t *testing.T) {
var buf bytes.Buffer
cmd := newTokenCommand(globalDataWithToken("test-api-token-value"))
err := cmd.Exec(nil, &buf)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if got := buf.String(); got != "test-api-token-value" {
t.Errorf("expected token %q, got %q", "test-api-token-value", got)
}
if got := buf.Bytes(); got[len(got)-1] == '\n' {
t.Error("output should not have a trailing newline")
}
}
func TestToken_NonTTY_NoToken(t *testing.T) {
var buf bytes.Buffer
g := &global.Data{
Config: config.File{},
}
cmd := newTokenCommand(g)
err := cmd.Exec(nil, &buf)
if err == nil {
t.Fatal("expected error for missing token")
}
var re fsterr.RemediationError
if !errors.As(err, &re) {
t.Fatalf("expected RemediationError, got %T: %v", err, err)
}
if re.Inner == nil || re.Inner.Error() != "no API token configured" {
t.Errorf("unexpected inner error: %v", re.Inner)
}
}