forked from auth0/auth0-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps_test.go
More file actions
174 lines (152 loc) · 4.53 KB
/
Copy pathapps_test.go
File metadata and controls
174 lines (152 loc) · 4.53 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
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
package cli
import (
"bytes"
"io"
"testing"
"github.com/auth0/go-auth0/management"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/auth0/auth0-cli/internal/auth0"
"github.com/auth0/auth0-cli/internal/auth0/mock"
"github.com/auth0/auth0-cli/internal/display"
)
func TestAppsListCmd(t *testing.T) {
tests := []struct {
name string
assertOutput func(t testing.TB, out string)
args []string
}{
{
name: "happy path",
assertOutput: func(t testing.TB, out string) {
expectTable(t, out,
[]string{"CLIENT ID", "NAME", "TYPE", "RESOURCE SERVER"},
[][]string{
{"some-id", "some-name", "Generic", ""},
},
)
},
},
{
name: "reveal secrets",
args: []string{"--reveal-secrets"},
assertOutput: func(t testing.TB, out string) {
expectTable(t, out,
[]string{"CLIENT ID", "NAME", "TYPE", "CLIENT SECRET", "RESOURCE SERVER"},
[][]string{
{"some-id", "some-name", "Generic", "secret-here", ""},
},
)
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// Step 1: Setup our client mock for this test. We only care about
// Clients so no need to bootstrap other bits.
ctrl := gomock.NewController(t)
defer ctrl.Finish()
clientAPI := mock.NewMockClientAPI(ctrl)
clientAPI.EXPECT().
List(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&management.ClientList{
Clients: []*management.Client{
{
Name: auth0.String("some-name"),
ClientID: auth0.String("some-id"),
Callbacks: &[]string{"http://localhost"},
ClientSecret: auth0.String("secret-here"),
},
},
}, nil)
stdout := &bytes.Buffer{}
// Step 2: Setup our cli context. The important bits are
// renderer and api.
cli := &cli{
renderer: &display.Renderer{
MessageWriter: io.Discard,
ResultWriter: stdout,
},
api: &auth0.API{Client: clientAPI},
}
cmd := listAppsCmd(cli)
cmd.SetArgs(test.args)
if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
test.assertOutput(t, stdout.String())
})
}
}
func TestAppsCreateCmd(t *testing.T) {
tests := []struct {
name string
args []string
expectedError string
}{
{
name: "Resource Server - resource-server-identifier is empty string",
args: []string{
"--name", "My Resource Server App",
"--type", "resource_server",
"--resource-server-identifier", "",
},
expectedError: "resource-server-identifier cannot be empty for resource_server app type",
},
{
name: "Resource Server - resource-server-identifier is whitespace only",
args: []string{
"--name", "My Resource Server App",
"--type", "resource_server",
"--resource-server-identifier", " ",
},
expectedError: "resource-server-identifier cannot be empty for resource_server app type",
},
{
name: "Resource Server - resource-server-identifier is tab/newline",
args: []string{
"--name", "My Resource Server App",
"--type", "resource_server",
"--resource-server-identifier", "\t\n",
},
expectedError: "resource-server-identifier cannot be empty for resource_server app type",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cli := &cli{}
cli.noInput = true // Non-interactive mode.
cmd := createAppCmd(cli)
cmd.SetArgs(test.args)
err := cmd.Execute()
assert.EqualError(t, err, test.expectedError)
})
}
}
func TestFormatAppSettingsPath(t *testing.T) {
assert.Empty(t, formatAppSettingsPath(""))
assert.Equal(t, "applications/app-id-1/settings", formatAppSettingsPath("app-id-1"))
}
func TestTypeFor(t *testing.T) {
testAppType := appTypeNative
expected := "Native"
assert.Equal(t, &expected, typeFor(&testAppType))
testAppType = appTypeSPA
expected = "Single Page Web Application"
assert.Equal(t, &expected, typeFor(&testAppType))
testAppType = appTypeRegularWeb
expected = "Regular Web Application"
assert.Equal(t, &expected, typeFor(&testAppType))
testAppType = appTypeNonInteractive
expected = "Machine to Machine"
assert.Equal(t, &expected, typeFor(&testAppType))
testAppType = appTypeResourceServer
expected = "Resource Server"
assert.Equal(t, &expected, typeFor(&testAppType))
testAppType = "some-unknown-api-type"
assert.Nil(t, typeFor(&testAppType))
}
func TestCommaSeparatedStringToSlice(t *testing.T) {
assert.Equal(t, []string{}, commaSeparatedStringToSlice(""))
assert.Equal(t, []string{"foo", "bar", "baz"}, commaSeparatedStringToSlice(" foo , bar , baz "))
}