@@ -8,10 +8,13 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
88 let CLOUD_API ;
99 let CLOUD_NAME ;
1010 let CLOUD_ID ;
11- let USER_NAME = `SDK TEST ${ Date . now ( ) } ` ;
12- let USER_EMAIL = `sdk-test+${ Date . now ( ) } @cloudinary.com` ;
11+ let USER_NAME_1 = `SDK TEST ${ Date . now ( ) } ` ;
12+ let USER_NAME_2 = `SDK TEST 2 ${ Date . now ( ) } ` ;
13+ let USER_EMAIL_1 = `sdk-test+${ Date . now ( ) } @cloudinary.com` ;
14+ let USER_EMAIL_2 = `sdk-test2+${ Date . now ( ) } @cloudinary.com` ;
1315 let USER_ROLE = 'billing' ;
14- let USER_ID ;
16+ let USER_ID_1 ;
17+ let USER_ID_2 ;
1518 let GROUP_ID ;
1619 let CLOUD_NAME_PREFIX = `justaname${ process . hrtime ( ) [ 1 ] % 10000 } ` ;
1720 this . timeout ( TIMEOUT . LONG ) ;
@@ -34,11 +37,17 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
3437 CLOUD_NAME = res . cloud_name ;
3538 CLOUD_ID = res . id ;
3639
37- let createUser = await cloudinary . provisioning . account . create_user ( USER_NAME , USER_EMAIL , USER_ROLE , [ ] ) . catch ( ( err ) => {
38- throw err ;
39- } ) ;
40+ let createdUsers = await Promise . all ( [
41+ cloudinary . provisioning . account . create_user ( USER_NAME_1 , USER_EMAIL_1 , USER_ROLE , [ ] ) . catch ( ( err ) => {
42+ throw err ;
43+ } ) ,
44+ cloudinary . provisioning . account . create_user ( USER_NAME_2 , USER_EMAIL_2 , USER_ROLE , [ ] ) . catch ( ( err ) => {
45+ throw err ;
46+ } )
47+ ] ) ;
4048
41- USER_ID = createUser . id ;
49+ USER_ID_1 = createdUsers [ 0 ] . id ;
50+ USER_ID_2 = createdUsers [ 1 ] . id ;
4251
4352 // create a user group
4453
@@ -50,19 +59,21 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
5059 return true ;
5160 } ) ;
5261
53- after ( 'Destroy the sub_account and user that was created' , async ( ) => {
62+ after ( 'Destroy the sub_account and users that were created' , async ( ) => {
5463 // Skip 'after' in case we don't have account configuration available
5564 // This means that the beforeHook also didn't run
5665 let config = cloudinary . config ( true ) ;
5766 if ( ! ( config . provisioning_api_key && config . provisioning_api_secret && config . account_id ) ) {
5867 return ;
5968 }
6069 let delRes = await cloudinary . provisioning . account . delete_sub_account ( CLOUD_ID ) ;
61- let delUserRes = await cloudinary . provisioning . account . delete_user ( USER_ID ) ;
70+ let delUser1Res = await cloudinary . provisioning . account . delete_user ( USER_ID_1 ) ;
71+ let delUser2Res = await cloudinary . provisioning . account . delete_user ( USER_ID_2 ) ;
6272 let delGroupRes = await cloudinary . provisioning . account . delete_user_group ( GROUP_ID ) ;
6373
6474 expect ( delRes . message ) . to . eql ( 'ok' ) ;
65- expect ( delUserRes . message ) . to . eql ( 'ok' ) ;
75+ expect ( delUser1Res . message ) . to . eql ( 'ok' ) ;
76+ expect ( delUser2Res . message ) . to . eql ( 'ok' ) ;
6677 expect ( delGroupRes . ok ) . to . eql ( true ) ; // notice the different response structure
6778 } ) ;
6879
@@ -126,39 +137,78 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
126137 it ( 'Updates a user' , async function ( ) {
127138 let NEW_EMAIL_ADDRESS = `updated+${ Date . now ( ) } @cloudinary.com` ;
128139
129- await cloudinary . provisioning . account . update_user ( USER_ID , 'updated' , NEW_EMAIL_ADDRESS ) . then ( ( res ) => {
140+ await cloudinary . provisioning . account . update_user ( USER_ID_1 , 'updated' , NEW_EMAIL_ADDRESS ) . then ( ( res ) => {
130141 expect ( res . name ) . to . eql ( 'updated' ) ;
131142 expect ( res . email ) . to . eql ( NEW_EMAIL_ADDRESS ) ;
132143 } ) . catch ( ( err ) => {
133144 throw err ;
134145 } ) ;
135146
136- await cloudinary . provisioning . account . user ( USER_ID ) . then ( ( res ) => {
137- expect ( res . id ) . to . eql ( USER_ID ) ;
147+ await cloudinary . provisioning . account . user ( USER_ID_1 ) . then ( ( res ) => {
148+ expect ( res . id ) . to . eql ( USER_ID_1 ) ;
138149 expect ( res . email ) . to . eql ( NEW_EMAIL_ADDRESS ) ;
139150 } ) . catch ( ( err ) => {
140151 throw err ;
141152 } ) ;
142153
143154 await cloudinary . provisioning . account . users ( ) . then ( ( res ) => {
144155 let user = res . users . find ( ( userEntry ) => {
145- return userEntry . id === USER_ID ;
156+ return userEntry . id === USER_ID_1 ;
146157 } ) ;
147- expect ( user . id ) . to . eql ( USER_ID ) ;
158+ expect ( user . id ) . to . eql ( USER_ID_1 ) ;
148159 expect ( user . email ) . to . eql ( NEW_EMAIL_ADDRESS ) ;
149160 } ) . catch ( ( err ) => {
150161 throw err ;
151162 } ) ;
152163 } ) ;
153164
154165 it ( 'Gets users in a list of userIDs' , async ( ) => {
155- await cloudinary . provisioning . account . users ( null , [ USER_ID ] ) . then ( ( res ) => {
166+ await cloudinary . provisioning . account . users ( null , [ USER_ID_1 ] ) . then ( ( res ) => {
156167 expect ( res . users . length ) . to . eql ( 1 ) ;
157168 } ) . catch ( ( err ) => {
158169 throw err ;
159170 } ) ;
160171 } ) ;
161172
173+ it ( 'Gets pending users' , async ( ) => {
174+ const result = await cloudinary . provisioning . account . users ( true , [ USER_ID_1 ] ) ;
175+ expect ( result . users . length ) . to . eql ( 1 ) ;
176+ } ) ;
177+
178+ it ( 'Gets non-pending users' , async ( ) => {
179+ const result = await cloudinary . provisioning . account . users ( false , [ USER_ID_1 ] ) ;
180+ expect ( result . users . length ) . to . eql ( 0 ) ;
181+ } ) ;
182+
183+ it ( 'Gets pending and non-pending users' , async ( ) => {
184+ const result = await cloudinary . provisioning . account . users ( null , [ USER_ID_1 ] ) ;
185+ expect ( result . users . length ) . to . eql ( 1 ) ;
186+ } ) ;
187+
188+ it ( 'Gets users by prefix' , async ( ) => {
189+ const [ result_1 , result_2 ] = await Promise . all ( [
190+ cloudinary . provisioning . account . users ( true , null , USER_NAME_2 . slice ( 0 , - 1 ) ) ,
191+ cloudinary . provisioning . account . users ( true , null , USER_NAME_2 + 'zzz' )
192+ ] ) ;
193+ expect ( result_1 . users . length ) . to . eql ( 1 ) ;
194+ expect ( result_2 . users . length ) . to . eql ( 0 ) ;
195+ } ) ;
196+
197+ it ( 'Gets users by sub_account_id' , async ( ) => {
198+ const result = await cloudinary . provisioning . account . users ( true , null , USER_NAME_2 , CLOUD_ID ) ;
199+ expect ( result . users . length ) . to . eql ( 1 ) ;
200+ } ) ;
201+
202+ it ( 'Should throw an error when attempting to get users by a nonexistent sub_account_id' , async ( ) => {
203+ const random_id = Math . floor ( Math . random ( ) * 100000 ) ;
204+ try {
205+ await cloudinary . provisioning . account . users ( true , null , null , random_id ) ;
206+ expect ( ) . fail ( )
207+ } catch ( { error} ) {
208+ expect ( error . message ) . to . eql ( `Cannot find sub account with id ${ random_id } ` ) ;
209+ }
210+ } ) ;
211+
162212 it ( 'Updates the user group' , async ( ) => {
163213 let NEW_NAME = `new-test-name_${ Date . now ( ) } ` ;
164214 let res = await cloudinary . provisioning . account . update_user_group ( GROUP_ID , NEW_NAME ) ;
@@ -168,13 +218,13 @@ runOnlyForInternalPRs('account API - Provisioning', function () {
168218 } ) ;
169219
170220 it ( 'Adds and remove a user from a group' , async ( ) => {
171- let res = await cloudinary . provisioning . account . add_user_to_group ( GROUP_ID , USER_ID ) ;
221+ let res = await cloudinary . provisioning . account . add_user_to_group ( GROUP_ID , USER_ID_1 ) ;
172222 expect ( res . users . length ) . to . eql ( 1 ) ;
173223
174224 let groupUserData = await cloudinary . provisioning . account . user_group_users ( ( GROUP_ID ) ) ;
175225 expect ( groupUserData . users . length ) . to . eql ( 1 ) ;
176226 //
177- let remUserFromGroupResp = await cloudinary . provisioning . account . remove_user_from_group ( GROUP_ID , USER_ID ) ;
227+ let remUserFromGroupResp = await cloudinary . provisioning . account . remove_user_from_group ( GROUP_ID , USER_ID_1 ) ;
178228 expect ( remUserFromGroupResp . users . length ) . to . eql ( 0 ) ;
179229 } ) ;
180230
0 commit comments