@@ -2,6 +2,9 @@ package cmdtest
22
33import (
44 "context"
5+ "encoding/json"
6+ "errors"
7+ "flag"
58 "io"
69 "net/http"
710 "path/filepath"
@@ -56,6 +59,166 @@ func TestProfilesListRejectsInvalidNextURL(t *testing.T) {
5659 }
5760}
5861
62+ func TestProfilesListDefaultsToActiveAndInvalidStates (t * testing.T ) {
63+ setupAuth (t )
64+ t .Setenv ("ASC_CONFIG_PATH" , filepath .Join (t .TempDir (), "nonexistent.json" ))
65+
66+ originalTransport := http .DefaultTransport
67+ t .Cleanup (func () {
68+ http .DefaultTransport = originalTransport
69+ })
70+
71+ http .DefaultTransport = roundTripFunc (func (req * http.Request ) (* http.Response , error ) {
72+ if req .Method != http .MethodGet || req .URL .Path != "/v1/profiles" {
73+ t .Fatalf ("unexpected request: %s %s" , req .Method , req .URL .String ())
74+ }
75+ if got := req .URL .Query ().Get ("filter[profileState]" ); got != "ACTIVE,INVALID" {
76+ t .Fatalf ("expected default profileState filter ACTIVE,INVALID, got %q" , got )
77+ }
78+ body := `{"data":[` +
79+ `{"type":"profiles","id":"profile-active","attributes":{"name":"Active","profileType":"IOS_APP_STORE","profileState":"ACTIVE"}},` +
80+ `{"type":"profiles","id":"profile-invalid","attributes":{"name":"Expired","profileType":"IOS_APP_ADHOC","profileState":"INVALID"}}` +
81+ `]}`
82+ return & http.Response {
83+ StatusCode : http .StatusOK ,
84+ Body : io .NopCloser (strings .NewReader (body )),
85+ Header : http.Header {"Content-Type" : []string {"application/json" }},
86+ }, nil
87+ })
88+
89+ root := RootCommand ("1.2.3" )
90+ root .FlagSet .SetOutput (io .Discard )
91+
92+ var runErr error
93+ stdout , stderr := captureOutput (t , func () {
94+ if err := root .Parse ([]string {"profiles" , "list" , "--output" , "json" }); err != nil {
95+ t .Fatalf ("parse error: %v" , err )
96+ }
97+ runErr = root .Run (context .Background ())
98+ })
99+
100+ if runErr != nil {
101+ t .Fatalf ("run error: %v" , runErr )
102+ }
103+ if stderr != "" {
104+ t .Fatalf ("expected empty stderr, got %q" , stderr )
105+ }
106+
107+ var payload struct {
108+ Data []struct {
109+ ID string `json:"id"`
110+ Attributes struct {
111+ ProfileState string `json:"profileState"`
112+ } `json:"attributes"`
113+ } `json:"data"`
114+ }
115+ if err := json .Unmarshal ([]byte (stdout ), & payload ); err != nil {
116+ t .Fatalf ("unmarshal profiles output: %v\n %s" , err , stdout )
117+ }
118+ if len (payload .Data ) != 2 {
119+ t .Fatalf ("expected active and invalid profiles, got %d" , len (payload .Data ))
120+ }
121+ if payload .Data [0 ].Attributes .ProfileState != "ACTIVE" || payload .Data [1 ].Attributes .ProfileState != "INVALID" {
122+ t .Fatalf ("unexpected profile states: %+v" , payload .Data )
123+ }
124+ }
125+
126+ func TestProfilesListProfileStateFilter (t * testing.T ) {
127+ setupAuth (t )
128+ t .Setenv ("ASC_CONFIG_PATH" , filepath .Join (t .TempDir (), "nonexistent.json" ))
129+
130+ originalTransport := http .DefaultTransport
131+ t .Cleanup (func () {
132+ http .DefaultTransport = originalTransport
133+ })
134+
135+ http .DefaultTransport = roundTripFunc (func (req * http.Request ) (* http.Response , error ) {
136+ if req .Method != http .MethodGet || req .URL .Path != "/v1/profiles" {
137+ t .Fatalf ("unexpected request: %s %s" , req .Method , req .URL .String ())
138+ }
139+ if got := req .URL .Query ().Get ("filter[profileState]" ); got != "INVALID" {
140+ t .Fatalf ("expected profileState filter INVALID, got %q" , got )
141+ }
142+ body := `{"data":[{"type":"profiles","id":"profile-invalid","attributes":{"name":"Expired","profileType":"IOS_APP_ADHOC","profileState":"INVALID"}}]}`
143+ return & http.Response {
144+ StatusCode : http .StatusOK ,
145+ Body : io .NopCloser (strings .NewReader (body )),
146+ Header : http.Header {"Content-Type" : []string {"application/json" }},
147+ }, nil
148+ })
149+
150+ root := RootCommand ("1.2.3" )
151+ root .FlagSet .SetOutput (io .Discard )
152+
153+ var runErr error
154+ stdout , stderr := captureOutput (t , func () {
155+ if err := root .Parse ([]string {"profiles" , "list" , "--profile-state" , "invalid" , "--output" , "json" }); err != nil {
156+ t .Fatalf ("parse error: %v" , err )
157+ }
158+ runErr = root .Run (context .Background ())
159+ })
160+
161+ if runErr != nil {
162+ t .Fatalf ("run error: %v" , runErr )
163+ }
164+ if stderr != "" {
165+ t .Fatalf ("expected empty stderr, got %q" , stderr )
166+ }
167+
168+ var payload struct {
169+ Data []struct {
170+ ID string `json:"id"`
171+ Attributes struct {
172+ ProfileState string `json:"profileState"`
173+ } `json:"attributes"`
174+ } `json:"data"`
175+ }
176+ if err := json .Unmarshal ([]byte (stdout ), & payload ); err != nil {
177+ t .Fatalf ("unmarshal profiles output: %v\n %s" , err , stdout )
178+ }
179+ if len (payload .Data ) != 1 || payload .Data [0 ].ID != "profile-invalid" || payload .Data [0 ].Attributes .ProfileState != "INVALID" {
180+ t .Fatalf ("unexpected profiles output: %+v" , payload .Data )
181+ }
182+ }
183+
184+ func TestProfilesListProfileStateInvalidValueReturnsUsageError (t * testing.T ) {
185+ originalTransport := http .DefaultTransport
186+ t .Cleanup (func () {
187+ http .DefaultTransport = originalTransport
188+ })
189+
190+ requestCount := 0
191+ http .DefaultTransport = roundTripFunc (func (req * http.Request ) (* http.Response , error ) {
192+ requestCount ++
193+ t .Fatalf ("unexpected HTTP request for invalid profile-state: %s %s" , req .Method , req .URL .String ())
194+ return nil , nil
195+ })
196+
197+ root := RootCommand ("1.2.3" )
198+ root .FlagSet .SetOutput (io .Discard )
199+
200+ var runErr error
201+ stdout , stderr := captureOutput (t , func () {
202+ if err := root .Parse ([]string {"profiles" , "list" , "--profile-state" , "EXPIRED" }); err != nil {
203+ t .Fatalf ("parse error: %v" , err )
204+ }
205+ runErr = root .Run (context .Background ())
206+ })
207+
208+ if ! errors .Is (runErr , flag .ErrHelp ) {
209+ t .Fatalf ("expected flag.ErrHelp usage error, got %v" , runErr )
210+ }
211+ if stdout != "" {
212+ t .Fatalf ("expected empty stdout, got %q" , stdout )
213+ }
214+ if ! strings .Contains (stderr , "--profile-state must be one of: ACTIVE, INVALID" ) {
215+ t .Fatalf ("expected profile-state usage error, got %q" , stderr )
216+ }
217+ if requestCount != 0 {
218+ t .Fatalf ("expected 0 requests, got %d" , requestCount )
219+ }
220+ }
221+
59222func TestProfilesListPaginateFromNext (t * testing.T ) {
60223 setupAuth (t )
61224 t .Setenv ("ASC_CONFIG_PATH" , filepath .Join (t .TempDir (), "nonexistent.json" ))
0 commit comments