@@ -10,7 +10,12 @@ import {http, HttpResponse} from 'msw';
1010import { setupServer } from 'msw/node' ;
1111import { isolateConfig , restoreConfig } from '@salesforce/b2c-tooling-sdk/test-utils' ;
1212import ClientUpdate from '../../../../src/commands/am/clients/update.js' ;
13- import { stubCommandConfigAndLogger , stubJsonEnabled , stubImplicitOAuthStrategy } from '../../../helpers/test-setup.js' ;
13+ import {
14+ stubCommandConfigAndLogger ,
15+ stubJsonEnabled ,
16+ stubImplicitOAuthStrategy ,
17+ makeCommandThrowOnError ,
18+ } from '../../../helpers/test-setup.js' ;
1419
1520const TEST_HOST = 'account.test.demandware.com' ;
1621const BASE_URL = `https://${ TEST_HOST } /dw/rest/v1` ;
@@ -52,6 +57,68 @@ describe('am clients update', () => {
5257 } ) ;
5358 } ) ;
5459
60+ describe ( 'validation' , ( ) => {
61+ it ( 'should error when no flags provided' , async ( ) => {
62+ const command = new ClientUpdate ( [ ] , { } as any ) ;
63+ ( command as any ) . args = { 'api-client-id' : 'client-123' } ;
64+ ( command as any ) . flags = { } ;
65+ stubCommandConfigAndLogger ( command ) ;
66+ makeCommandThrowOnError ( command ) ;
67+
68+ try {
69+ await command . run ( ) ;
70+ expect . fail ( 'Should have thrown' ) ;
71+ } catch ( error : unknown ) {
72+ expect ( ( error as Error ) . message ) . to . match ( / N o c h a n g e s s p e c i f i e d / ) ;
73+ }
74+ } ) ;
75+
76+ it ( 'should error when name exceeds 200 chars' , async ( ) => {
77+ const command = new ClientUpdate ( [ ] , { } as any ) ;
78+ ( command as any ) . args = { 'api-client-id' : 'client-123' } ;
79+ ( command as any ) . flags = { name : 'a' . repeat ( 201 ) } ;
80+ stubCommandConfigAndLogger ( command ) ;
81+ makeCommandThrowOnError ( command ) ;
82+
83+ try {
84+ await command . run ( ) ;
85+ expect . fail ( 'Should have thrown' ) ;
86+ } catch ( error : unknown ) {
87+ expect ( ( error as Error ) . message ) . to . match ( / a t m o s t 2 0 0 c h a r a c t e r s / ) ;
88+ }
89+ } ) ;
90+
91+ it ( 'should error when description exceeds 256 chars' , async ( ) => {
92+ const command = new ClientUpdate ( [ ] , { } as any ) ;
93+ ( command as any ) . args = { 'api-client-id' : 'client-123' } ;
94+ ( command as any ) . flags = { description : 'a' . repeat ( 257 ) } ;
95+ stubCommandConfigAndLogger ( command ) ;
96+ makeCommandThrowOnError ( command ) ;
97+
98+ try {
99+ await command . run ( ) ;
100+ expect . fail ( 'Should have thrown' ) ;
101+ } catch ( error : unknown ) {
102+ expect ( ( error as Error ) . message ) . to . match ( / a t m o s t 2 5 6 c h a r a c t e r s / ) ;
103+ }
104+ } ) ;
105+
106+ it ( 'should error on invalid role-tenant-filter' , async ( ) => {
107+ const command = new ClientUpdate ( [ ] , { } as any ) ;
108+ ( command as any ) . args = { 'api-client-id' : 'client-123' } ;
109+ ( command as any ) . flags = { 'role-tenant-filter' : 'INVALID FORMAT!!' } ;
110+ stubCommandConfigAndLogger ( command ) ;
111+ makeCommandThrowOnError ( command ) ;
112+
113+ try {
114+ await command . run ( ) ;
115+ expect . fail ( 'Should have thrown' ) ;
116+ } catch ( error : unknown ) {
117+ expect ( ( error as Error ) . message ) . to . match ( / R o l e t e n a n t f i l t e r m u s t m a t c h p a t t e r n / ) ;
118+ }
119+ } ) ;
120+ } ) ;
121+
55122 describe ( 'output formatting' , ( ) => {
56123 it ( 'should return updated client in JSON mode' , async ( ) => {
57124 const command = new ClientUpdate ( [ ] , { } as any ) ;
@@ -83,5 +150,66 @@ describe('am clients update', () => {
83150 expect ( result . id ) . to . equal ( 'client-123' ) ;
84151 expect ( result . name ) . to . equal ( 'Updated Name' ) ;
85152 } ) ;
153+
154+ it ( 'should handle all update flags' , async ( ) => {
155+ const command = new ClientUpdate ( [ ] , { } as any ) ;
156+ ( command as any ) . args = { 'api-client-id' : 'client-123' } ;
157+ ( command as any ) . flags = {
158+ name : 'New Name' ,
159+ description : 'New desc' ,
160+ organizations : 'org-1,org-2' ,
161+ roles : 'role-1,role-2' ,
162+ 'role-tenant-filter' : 'SALESFORCE_COMMERCE_API:abcd_prd' ,
163+ active : true ,
164+ 'redirect-urls' : 'https://a.com,https://b.com' ,
165+ scopes : 'openid,mail' ,
166+ 'default-scopes' : 'openid' ,
167+ 'version-control' : 'v1' ,
168+ 'token-endpoint-auth-method' : 'client_secret_post' ,
169+ 'jwt-public-key' : ' some-key ' ,
170+ } ;
171+ stubCommandConfigAndLogger ( command ) ;
172+ stubJsonEnabled ( command , true ) ;
173+ stubImplicitOAuthStrategy ( command ) ;
174+
175+ server . use (
176+ http . put ( `${ BASE_URL } /apiclients/client-123` , async ( { request} ) => {
177+ const body = ( await request . json ( ) ) as Record < string , unknown > ;
178+ expect ( body . name ) . to . equal ( 'New Name' ) ;
179+ expect ( body . active ) . to . equal ( true ) ;
180+ expect ( body . organizations ) . to . deep . equal ( [ 'org-1' , 'org-2' ] ) ;
181+ expect ( body . roles ) . to . deep . equal ( [ 'role-1' , 'role-2' ] ) ;
182+ expect ( body . scopes ) . to . deep . equal ( [ 'openid' , 'mail' ] ) ;
183+ expect ( body . defaultScopes ) . to . deep . equal ( [ 'openid' ] ) ;
184+ expect ( body . redirectUrls ) . to . deep . equal ( [ 'https://a.com' , 'https://b.com' ] ) ;
185+ expect ( body . tokenEndpointAuthMethod ) . to . equal ( 'client_secret_post' ) ;
186+ expect ( body . jwtPublicKey ) . to . equal ( 'some-key' ) ;
187+ return HttpResponse . json ( { id : 'client-123' , ...body } ) ;
188+ } ) ,
189+ ) ;
190+
191+ const result = await command . run ( ) ;
192+ expect ( result . id ) . to . equal ( 'client-123' ) ;
193+ } ) ;
194+
195+ it ( 'should handle empty jwt-public-key as null' , async ( ) => {
196+ const command = new ClientUpdate ( [ ] , { } as any ) ;
197+ ( command as any ) . args = { 'api-client-id' : 'client-123' } ;
198+ ( command as any ) . flags = { 'jwt-public-key' : '' } ;
199+ stubCommandConfigAndLogger ( command ) ;
200+ stubJsonEnabled ( command , true ) ;
201+ stubImplicitOAuthStrategy ( command ) ;
202+
203+ server . use (
204+ http . put ( `${ BASE_URL } /apiclients/client-123` , async ( { request} ) => {
205+ const body = ( await request . json ( ) ) as Record < string , unknown > ;
206+ expect ( body . jwtPublicKey ) . to . equal ( null ) ;
207+ return HttpResponse . json ( { id : 'client-123' } ) ;
208+ } ) ,
209+ ) ;
210+
211+ const result = await command . run ( ) ;
212+ expect ( result . id ) . to . equal ( 'client-123' ) ;
213+ } ) ;
86214 } ) ;
87215} ) ;
0 commit comments