11package auth_test
22
33import (
4- "bytes"
54 "context"
65 "errors"
7- "io"
8- "strings"
96 "testing"
107
118 "github.com/fastly/go-fastly/v12/fastly"
129
13- "github.com/fastly/cli/pkg/app "
14- "github.com/fastly/cli/pkg/global "
10+ root "github.com/fastly/cli/pkg/commands/service "
11+ sub "github.com/fastly/cli/pkg/commands/service/auth "
1512 "github.com/fastly/cli/pkg/mock"
1613 "github.com/fastly/cli/pkg/testutil"
1714)
1815
1916func TestServiceAuthCreate (t * testing.T ) {
20- args := testutil .SplitArgs
21- scenarios := []struct {
22- args []string
23- api mock.API
24- wantError string
25- wantOutput string
26- }{
17+ scenarios := []testutil.CLIScenario {
2718 {
28- args : args ( "service auth create" ) ,
29- wantError : "error parsing arguments: required flag --user-id not provided" ,
19+ Args : "" ,
20+ WantError : "error parsing arguments: required flag --user-id not provided" ,
3021 },
3122 {
32- args : args ( "service auth create --user-id 123 --service-id 123") ,
33- api : mock.API {CreateServiceAuthorizationFn : createServiceAuthError },
34- wantError : errTest .Error (),
23+ Args : " --user-id 123 --service-id 123" ,
24+ API : mock.API {CreateServiceAuthorizationFn : createServiceAuthError },
25+ WantError : errTest .Error (),
3526 },
3627 {
37- args : args ( "service auth create --user-id 123 --service-id 123") ,
38- api : mock.API {CreateServiceAuthorizationFn : createServiceAuthOK },
39- wantOutput : "Created service authorization 12345" ,
28+ Args : " --user-id 123 --service-id 123" ,
29+ API : mock.API {CreateServiceAuthorizationFn : createServiceAuthOK },
30+ WantOutput : "Created service authorization 12345" ,
4031 },
4132 }
42- for testcaseIdx := range scenarios {
43- testcase := & scenarios [testcaseIdx ]
44- t .Run (strings .Join (testcase .args , " " ), func (t * testing.T ) {
45- var stdout bytes.Buffer
46- app .Init = func (_ []string , _ io.Reader ) (* global.Data , error ) {
47- opts := testutil .MockGlobalData (testcase .args , & stdout )
48- opts .APIClientFactory = mock .APIClient (testcase .api )
49- return opts , nil
50- }
51- err := app .Run (testcase .args , nil )
52- testutil .AssertErrorContains (t , err , testcase .wantError )
53- testutil .AssertStringContains (t , stdout .String (), testcase .wantOutput )
54- })
55- }
33+
34+ testutil .RunCLIScenarios (t , []string {root .CommandName , sub .CommandName , "create" }, scenarios )
5635}
5736
5837func TestServiceAuthList (t * testing.T ) {
59- args := testutil .SplitArgs
60- scenarios := []struct {
61- args []string
62- api mock.API
63- wantError string
64- wantOutput string
65- }{
38+ scenarios := []testutil.CLIScenario {
6639 {
67- args : args ( "service auth list --verbose --json") ,
68- wantError : "invalid flag combination, --verbose and --json" ,
40+ Args : " --verbose --json" ,
41+ WantError : "invalid flag combination, --verbose and --json" ,
6942 },
7043 {
71- args : args ( "service auth list" ) ,
72- api : mock.API {ListServiceAuthorizationsFn : listServiceAuthError },
73- wantError : errTest .Error (),
44+ Args : "" ,
45+ API : mock.API {ListServiceAuthorizationsFn : listServiceAuthError },
46+ WantError : errTest .Error (),
7447 },
7548 {
76- args : args ( "service auth list" ) ,
77- api : mock.API {ListServiceAuthorizationsFn : listServiceAuthOK },
78- wantOutput : "AUTH ID USER ID SERVICE ID PERMISSION\n 123 456 789 read_only\n " ,
49+ Args : "" ,
50+ API : mock.API {ListServiceAuthorizationsFn : listServiceAuthOK },
51+ WantOutput : "AUTH ID USER ID SERVICE ID PERMISSION\n 123 456 789 read_only\n " ,
7952 },
8053 {
81- args : args ( "service auth list --json") ,
82- api : mock.API {ListServiceAuthorizationsFn : listServiceAuthOK },
83- wantOutput : `{
54+ Args : " --json" ,
55+ API : mock.API {ListServiceAuthorizationsFn : listServiceAuthOK },
56+ WantOutput : `{
8457 "Info": {
8558 "links": {},
8659 "meta": {}
@@ -103,58 +76,39 @@ func TestServiceAuthList(t *testing.T) {
10376}` ,
10477 },
10578 {
106- args : args ( "service auth list --verbose") ,
107- api : mock.API {ListServiceAuthorizationsFn : listServiceAuthOK },
108- wantOutput : "Fastly API endpoint: https://api.fastly.com\n Fastly API token provided via config file (profile: user)\n \n Auth ID: 123\n User ID: 456\n Service ID: 789\n Permission: read_only\n " ,
79+ Args : " --verbose" ,
80+ API : mock.API {ListServiceAuthorizationsFn : listServiceAuthOK },
81+ WantOutput : "Fastly API endpoint: https://api.fastly.com\n Fastly API token provided via config file (profile: user)\n \n Auth ID: 123\n User ID: 456\n Service ID: 789\n Permission: read_only\n " ,
10982 },
11083 }
111- for testcaseIdx := range scenarios {
112- testcase := & scenarios [testcaseIdx ]
113- t .Run (strings .Join (testcase .args , " " ), func (t * testing.T ) {
114- var stdout bytes.Buffer
115- app .Init = func (_ []string , _ io.Reader ) (* global.Data , error ) {
116- opts := testutil .MockGlobalData (testcase .args , & stdout )
117- opts .APIClientFactory = mock .APIClient (testcase .api )
118- return opts , nil
119- }
120- err := app .Run (testcase .args , nil )
121- t .Log (stdout .String ())
122- testutil .AssertErrorContains (t , err , testcase .wantError )
123- testutil .AssertStringContains (t , stdout .String (), testcase .wantOutput )
124- })
125- }
84+
85+ testutil .RunCLIScenarios (t , []string {root .CommandName , sub .CommandName , "list" }, scenarios )
12686}
12787
12888func TestServiceAuthDescribe (t * testing.T ) {
129- args := testutil .SplitArgs
130- scenarios := []struct {
131- args []string
132- api mock.API
133- wantError string
134- wantOutput string
135- }{
89+ scenarios := []testutil.CLIScenario {
13690 {
137- args : args ( "service auth describe" ) ,
138- wantError : "error parsing arguments: required flag --id not provided" ,
91+ Args : "" ,
92+ WantError : "error parsing arguments: required flag --id not provided" ,
13993 },
14094 {
141- args : args ( "service auth describe --id 123 --verbose --json") ,
142- wantError : "invalid flag combination, --verbose and --json" ,
95+ Args : " --id 123 --verbose --json" ,
96+ WantError : "invalid flag combination, --verbose and --json" ,
14397 },
14498 {
145- args : args ( "service auth describe --id 123") ,
146- api : mock.API {GetServiceAuthorizationFn : describeServiceAuthError },
147- wantError : errTest .Error (),
99+ Args : " --id 123" ,
100+ API : mock.API {GetServiceAuthorizationFn : describeServiceAuthError },
101+ WantError : errTest .Error (),
148102 },
149103 {
150- args : args ( "service auth describe --id 123") ,
151- api : mock.API {GetServiceAuthorizationFn : describeServiceAuthOK },
152- wantOutput : "Auth ID: 12345\n User ID: 456\n Service ID: 789\n Permission: read_only\n " ,
104+ Args : " --id 123" ,
105+ API : mock.API {GetServiceAuthorizationFn : describeServiceAuthOK },
106+ WantOutput : "Auth ID: 12345\n User ID: 456\n Service ID: 789\n Permission: read_only\n " ,
153107 },
154108 {
155- args : args ( "service auth describe --id 123 --json") ,
156- api : mock.API {GetServiceAuthorizationFn : describeServiceAuthOK },
157- wantOutput : `{
109+ Args : " --id 123 --json" ,
110+ API : mock.API {GetServiceAuthorizationFn : describeServiceAuthOK },
111+ WantOutput : `{
158112 "CreatedAt": null,
159113 "DeletedAt": null,
160114 "ID": "12345",
@@ -169,103 +123,54 @@ func TestServiceAuthDescribe(t *testing.T) {
169123}` ,
170124 },
171125 }
172- for testcaseIdx := range scenarios {
173- testcase := & scenarios [testcaseIdx ]
174- t .Run (strings .Join (testcase .args , " " ), func (t * testing.T ) {
175- var stdout bytes.Buffer
176- app .Init = func (_ []string , _ io.Reader ) (* global.Data , error ) {
177- opts := testutil .MockGlobalData (testcase .args , & stdout )
178- opts .APIClientFactory = mock .APIClient (testcase .api )
179- return opts , nil
180- }
181- err := app .Run (testcase .args , nil )
182- t .Log (stdout .String ())
183- testutil .AssertErrorContains (t , err , testcase .wantError )
184- testutil .AssertStringContains (t , stdout .String (), testcase .wantOutput )
185- })
186- }
126+
127+ testutil .RunCLIScenarios (t , []string {root .CommandName , sub .CommandName , "describe" }, scenarios )
187128}
188129
189130func TestServiceAuthUpdate (t * testing.T ) {
190- args := testutil .SplitArgs
191- scenarios := []struct {
192- args []string
193- api mock.API
194- wantError string
195- wantOutput string
196- }{
131+ scenarios := []testutil.CLIScenario {
197132 {
198- args : args ( "service auth update --permission full") ,
199- wantError : "error parsing arguments: required flag --id not provided" ,
133+ Args : " --permission full" ,
134+ WantError : "error parsing arguments: required flag --id not provided" ,
200135 },
201136 {
202- args : args ( "service auth update --id 123") ,
203- wantError : "error parsing arguments: required flag --permission not provided" ,
137+ Args : " --id 123" ,
138+ WantError : "error parsing arguments: required flag --permission not provided" ,
204139 },
205140 {
206- args : args ( "service auth update --id 123 --permission full") ,
207- api : mock.API {UpdateServiceAuthorizationFn : updateServiceAuthError },
208- wantError : errTest .Error (),
141+ Args : " --id 123 --permission full" ,
142+ API : mock.API {UpdateServiceAuthorizationFn : updateServiceAuthError },
143+ WantError : errTest .Error (),
209144 },
210145 {
211- args : args ( "service auth update --id 123 --permission full") ,
212- api : mock.API {UpdateServiceAuthorizationFn : updateServiceAuthOK },
213- wantOutput : "Updated service authorization 123" ,
146+ Args : " --id 123 --permission full" ,
147+ API : mock.API {UpdateServiceAuthorizationFn : updateServiceAuthOK },
148+ WantOutput : "Updated service authorization 123" ,
214149 },
215150 }
216- for testcaseIdx := range scenarios {
217- testcase := & scenarios [testcaseIdx ]
218- t .Run (strings .Join (testcase .args , " " ), func (t * testing.T ) {
219- var stdout bytes.Buffer
220- app .Init = func (_ []string , _ io.Reader ) (* global.Data , error ) {
221- opts := testutil .MockGlobalData (testcase .args , & stdout )
222- opts .APIClientFactory = mock .APIClient (testcase .api )
223- return opts , nil
224- }
225- err := app .Run (testcase .args , nil )
226- testutil .AssertErrorContains (t , err , testcase .wantError )
227- testutil .AssertStringContains (t , stdout .String (), testcase .wantOutput )
228- })
229- }
151+
152+ testutil .RunCLIScenarios (t , []string {root .CommandName , sub .CommandName , "update" }, scenarios )
230153}
231154
232155func TestServiceAuthDelete (t * testing.T ) {
233- args := testutil .SplitArgs
234- scenarios := []struct {
235- args []string
236- api mock.API
237- wantError string
238- wantOutput string
239- }{
156+ scenarios := []testutil.CLIScenario {
240157 {
241- args : args ( "service auth delete" ) ,
242- wantError : "error parsing arguments: required flag --id not provided" ,
158+ Args : "" ,
159+ WantError : "error parsing arguments: required flag --id not provided" ,
243160 },
244161 {
245- args : args ( "service auth delete --id 123") ,
246- api : mock.API {DeleteServiceAuthorizationFn : deleteServiceAuthError },
247- wantError : errTest .Error (),
162+ Args : " --id 123" ,
163+ API : mock.API {DeleteServiceAuthorizationFn : deleteServiceAuthError },
164+ WantError : errTest .Error (),
248165 },
249166 {
250- args : args ( "service auth delete --id 123") ,
251- api : mock.API {DeleteServiceAuthorizationFn : deleteServiceAuthOK },
252- wantOutput : "Deleted service authorization 123" ,
167+ Args : " --id 123" ,
168+ API : mock.API {DeleteServiceAuthorizationFn : deleteServiceAuthOK },
169+ WantOutput : "Deleted service authorization 123" ,
253170 },
254171 }
255- for testcaseIdx := range scenarios {
256- testcase := & scenarios [testcaseIdx ]
257- t .Run (strings .Join (testcase .args , " " ), func (t * testing.T ) {
258- var stdout bytes.Buffer
259- app .Init = func (_ []string , _ io.Reader ) (* global.Data , error ) {
260- opts := testutil .MockGlobalData (testcase .args , & stdout )
261- opts .APIClientFactory = mock .APIClient (testcase .api )
262- return opts , nil
263- }
264- err := app .Run (testcase .args , nil )
265- testutil .AssertErrorContains (t , err , testcase .wantError )
266- testutil .AssertStringContains (t , stdout .String (), testcase .wantOutput )
267- })
268- }
172+
173+ testutil .RunCLIScenarios (t , []string {root .CommandName , sub .CommandName , "delete" }, scenarios )
269174}
270175
271176var errTest = errors .New ("fixture error" )
0 commit comments