-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathwhoami_test.go
More file actions
107 lines (86 loc) · 2.92 KB
/
whoami_test.go
File metadata and controls
107 lines (86 loc) · 2.92 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
package whoami_test
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/launchdarkly/ldcli/cmd"
"github.com/launchdarkly/ldcli/internal/analytics"
"github.com/launchdarkly/ldcli/internal/resources"
)
// sequentialMockClient returns responses in order, one per call.
type sequentialMockClient struct {
responses [][]byte
callIndex int
}
var _ resources.Client = &sequentialMockClient{}
func (c *sequentialMockClient) MakeRequest(_, _, _ string, _ string, _ url.Values, _ []byte, _ bool) ([]byte, error) {
if c.callIndex >= len(c.responses) {
return nil, nil
}
res := c.responses[c.callIndex]
c.callIndex++
return res, nil
}
func (c *sequentialMockClient) MakeUnauthenticatedRequest(_ string, _ string, _ []byte) ([]byte, error) {
return nil, nil
}
func TestWhoAmI(t *testing.T) {
t.Run("shows member name, email, role, and token", func(t *testing.T) {
mockClient := &sequentialMockClient{
responses: [][]byte{
[]byte(`{"memberId": "abc123", "tokenName": "my-token", "tokenKind": "personal", "accountId": "acct1"}`),
[]byte(`{"_id": "abc123", "email": "ariel@acme.com", "firstName": "Ariel", "lastName": "Flores", "role": "admin"}`),
},
}
t.Setenv("LD_ACCESS_TOKEN", "abcd1234")
output, err := cmd.CallCmd(
t,
cmd.APIClients{ResourcesClient: mockClient},
analytics.NoopClientFn{}.Tracker(),
[]string{"whoami"},
)
require.NoError(t, err)
assert.Contains(t, string(output), "Ariel Flores <ariel@acme.com>")
assert.Contains(t, string(output), "Role: admin")
assert.Contains(t, string(output), "Token: my-token (personal)")
})
t.Run("without member ID shows token info only", func(t *testing.T) {
mockClient := &resources.MockClient{
Response: []byte(`{"tokenName": "sdk-key", "tokenKind": "server", "accountId": "acct1"}`),
}
t.Setenv("LD_ACCESS_TOKEN", "abcd1234")
output, err := cmd.CallCmd(
t,
cmd.APIClients{ResourcesClient: mockClient},
analytics.NoopClientFn{}.Tracker(),
[]string{"whoami"},
)
require.NoError(t, err)
assert.Contains(t, string(output), "Token: sdk-key (server)")
assert.NotContains(t, string(output), "Role:")
})
t.Run("without configured token returns helpful error", func(t *testing.T) {
_, err := cmd.CallCmd(
t,
cmd.APIClients{},
analytics.NoopClientFn{}.Tracker(),
[]string{"whoami"},
)
require.ErrorContains(t, err, "no access token configured")
})
t.Run("with --output json returns raw caller-identity JSON", func(t *testing.T) {
mockClient := &resources.MockClient{
Response: []byte(`{"tokenName": "my-token", "memberId": "abc123"}`),
}
t.Setenv("LD_ACCESS_TOKEN", "abcd1234")
output, err := cmd.CallCmd(
t,
cmd.APIClients{ResourcesClient: mockClient},
analytics.NoopClientFn{}.Tracker(),
[]string{"whoami", "--output", "json"},
)
require.NoError(t, err)
assert.Contains(t, string(output), `"tokenName": "my-token"`)
})
}