@@ -44,13 +44,15 @@ func Test_NewCommand(t *testing.T) {
4444
4545func Test_runAPICommand_BodyFormats (t * testing.T ) {
4646 tests := map [string ]struct {
47- flags cmdFlags
48- args []string
49- expectedMethod string
50- expectedCT string
51- expectedAuth string
52- bodyContains []string
53- bodyEquals string
47+ flags cmdFlags
48+ args []string
49+ expectedMethod string
50+ expectedCT string
51+ expectedAuth string
52+ assertNoAuth bool
53+ bodyContains []string
54+ bodyNotContains []string
55+ bodyEquals string
5456 }{
5557 "form-encoded key=value params" : {
5658 flags : cmdFlags {method : "POST" },
@@ -84,6 +86,36 @@ func Test_runAPICommand_BodyFormats(t *testing.T) {
8486 args : []string {"auth.test" },
8587 expectedMethod : "GET" ,
8688 },
89+ "no token with key=value params" : {
90+ flags : cmdFlags {method : "POST" },
91+ args : []string {"blocks.validate" , "blocks=[...]" },
92+ expectedCT : "application/x-www-form-urlencoded" ,
93+ assertNoAuth : true ,
94+ bodyContains : []string {"blocks=" },
95+ bodyNotContains : []string {"token=" },
96+ },
97+ "no token with --data flag" : {
98+ flags : cmdFlags {method : "POST" , data : "blocks=[...]" },
99+ args : []string {"blocks.validate" },
100+ expectedCT : "application/x-www-form-urlencoded" ,
101+ assertNoAuth : true ,
102+ bodyEquals : "blocks=[...]" ,
103+ bodyNotContains : []string {"token=" },
104+ },
105+ "no token with --json flag" : {
106+ flags : cmdFlags {method : "POST" , json : `{"blocks":[]}` },
107+ args : []string {"blocks.validate" },
108+ expectedCT : "application/json; charset=utf-8" ,
109+ assertNoAuth : true ,
110+ bodyEquals : `{"blocks":[]}` ,
111+ },
112+ "no token with no params" : {
113+ flags : cmdFlags {method : "POST" },
114+ args : []string {"api.test" },
115+ expectedCT : "application/x-www-form-urlencoded" ,
116+ assertNoAuth : true ,
117+ bodyEquals : "" ,
118+ },
87119 }
88120 for name , tc := range tests {
89121 t .Run (name , func (t * testing.T ) {
@@ -105,7 +137,9 @@ func Test_runAPICommand_BodyFormats(t *testing.T) {
105137 ctx := slackcontext .MockContext (t .Context ())
106138 clientsMock := shared .NewClientsMock ()
107139 clientsMock .AddDefaultMocks ()
108- clientsMock .Config .TokenFlag = "xoxb-test-token"
140+ if ! tc .assertNoAuth {
141+ clientsMock .Config .TokenFlag = "xoxb-test-token"
142+ }
109143 clientsMock .Config .APIHostResolved = server .URL
110144 clients := shared .NewClientFactory (clientsMock .MockClientFactory ())
111145
@@ -126,12 +160,22 @@ func Test_runAPICommand_BodyFormats(t *testing.T) {
126160 if tc .expectedAuth != "" {
127161 assert .Equal (t , tc .expectedAuth , receivedAuth )
128162 }
163+ if tc .assertNoAuth {
164+ assert .Empty (t , receivedAuth )
165+ assert .NotContains (t , receivedBody , "token=" )
166+ } else {
167+ assert .True (t , receivedAuth != "" || strings .Contains (receivedBody , "token=" ),
168+ "expected auth via Authorization header or token in body" )
169+ }
129170 if tc .bodyEquals != "" {
130171 assert .Equal (t , tc .bodyEquals , receivedBody )
131172 }
132173 for _ , s := range tc .bodyContains {
133174 assert .Contains (t , receivedBody , s )
134175 }
176+ for _ , s := range tc .bodyNotContains {
177+ assert .NotContains (t , receivedBody , s )
178+ }
135179 })
136180 }
137181}
@@ -548,7 +592,63 @@ func Test_resolveToken_NoTokenFound(t *testing.T) {
548592 clientsMock := shared .NewClientsMock ()
549593 clients := shared .NewClientFactory (clientsMock .MockClientFactory ())
550594
551- _ , err := resolveToken (ctx , clients )
552- assert .Error (t , err )
553- assert .Contains (t , err .Error (), "no token found" )
595+ token , err := resolveToken (ctx , clients )
596+ assert .NoError (t , err )
597+ assert .Empty (t , token )
598+ }
599+
600+ func Test_runAPICommand_NoAuth_MutualExclusivity (t * testing.T ) {
601+ tests := map [string ]struct {
602+ tokenFlag string
603+ appFlag string
604+ }{
605+ "no-auth with --token" : {
606+ tokenFlag : "xoxb-test" ,
607+ },
608+ "no-auth with --app" : {
609+ appFlag : "A123" ,
610+ },
611+ }
612+ for name , tc := range tests {
613+ t .Run (name , func (t * testing.T ) {
614+ ctx := slackcontext .MockContext (t .Context ())
615+ clientsMock := shared .NewClientsMock ()
616+ clientsMock .Config .TokenFlag = tc .tokenFlag
617+ clientsMock .Config .AppFlag = tc .appFlag
618+ clientsMock .Config .APIHostResolved = "https://slack.com"
619+ clients := shared .NewClientFactory (clientsMock .MockClientFactory ())
620+
621+ cmd := NewCommand (clients )
622+ testutil .MockCmdIO (clients .IO , cmd )
623+
624+ flags = cmdFlags {method : "POST" , noAuth : true }
625+ cmd .SetArgs ([]string {"blocks.validate" })
626+ err := cmd .ExecuteContext (ctx )
627+
628+ assert .Error (t , err )
629+ assert .Contains (t , err .Error (), "--no-auth cannot be used with --token or --app" )
630+ })
631+ }
632+ }
633+
634+ func Test_runAPICommand_NoAuth_SkipsTokenResolution (t * testing.T ) {
635+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
636+ fmt .Fprint (w , `{"ok":true}` )
637+ }))
638+ defer server .Close ()
639+
640+ ctx := slackcontext .MockContext (t .Context ())
641+ clientsMock := shared .NewClientsMock ()
642+ clientsMock .AddDefaultMocks ()
643+ clientsMock .Config .APIHostResolved = server .URL
644+ clients := shared .NewClientFactory (clientsMock .MockClientFactory ())
645+
646+ cmd := NewCommand (clients )
647+ testutil .MockCmdIO (clients .IO , cmd )
648+
649+ flags = cmdFlags {method : "POST" , noAuth : true }
650+ cmd .SetArgs ([]string {"api.test" })
651+ err := cmd .ExecuteContext (ctx )
652+
653+ assert .NoError (t , err )
554654}
0 commit comments