Skip to content

Commit d0bc4bd

Browse files
authored
Add enabled parameter to updateUser, replaceUser and createUser
1 parent ac5ab2d commit d0bc4bd

File tree

2 files changed

+113
-13
lines changed

2 files changed

+113
-13
lines changed

cloudinary-core/src/main/java/com/cloudinary/provisioning/Account.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.cloudinary.Util;
66
import com.cloudinary.api.ApiResponse;
77
import com.cloudinary.utils.ObjectUtils;
8-
98
import java.util.*;
109

1110
/**
@@ -327,7 +326,7 @@ public ApiResponse users(Boolean pending, List<String> userIds, String prefix, S
327326
* @throws Exception If the request fails.
328327
*/
329328
public ApiResponse createUser(String name, String email, Role role, List<String> subAccountsIds) throws Exception {
330-
return createUser(name, email, role, subAccountsIds);
329+
return createUser(name, email, role, subAccountsIds, null);
331330
}
332331

333332
/**
@@ -343,8 +342,25 @@ public ApiResponse createUser(String name, String email, Role role, List<String>
343342
* @throws Exception If the request fails.
344343
*/
345344
public ApiResponse createUser(String name, String email, Role role, List<String> subAccountsIds, Map<String, Object> options) throws Exception {
345+
return createUser(name, email, role, null, subAccountsIds, options);
346+
}
347+
348+
/**
349+
* Create a new user.
350+
*
351+
* @param name Required. Username.
352+
* @param email Required. User's email.
353+
* @param role Required. User's role.
354+
* @param enabled Optional. User's status (enabled or disabled).
355+
* @param subAccountsIds Optional. Sub-accounts for which the user should have access.
356+
* If not provided or empty, user should have access to all accounts.
357+
* @param options Generic advanced options map, see online documentation.
358+
* @return The newly created user details.
359+
* @throws Exception If the request fails.
360+
*/
361+
public ApiResponse createUser(String name, String email, Role role, Boolean enabled, List<String> subAccountsIds, Map<String, Object> options) throws Exception {
346362
List<String> uri = Arrays.asList(PROVISIONING, ACCOUNTS, accountId, USERS);
347-
return performUserAction(Api.HttpMethod.POST, uri, email, name, role, subAccountsIds, options);
363+
return performUserAction(Api.HttpMethod.POST, uri, email, name, role, enabled, subAccountsIds, options);
348364
}
349365

350366
/**
@@ -377,8 +393,26 @@ public ApiResponse updateUser(String userId, String name, String email, Role rol
377393
* @throws Exception If the request fails.
378394
*/
379395
public ApiResponse updateUser(String userId, String name, String email, Role role, List<String> subAccountsIds, Map<String, Object> options) throws Exception {
396+
return updateUser(userId, name ,email ,role ,null , subAccountsIds , options);
397+
}
398+
399+
/**
400+
* Update an existing user.
401+
*
402+
* @param userId The id of the user to update.
403+
* @param name Username.
404+
* @param email User's email.
405+
* @param role User's role.
406+
* @param enabled User's status (enabled or disabled)
407+
* @param subAccountsIds Sub-accounts for which the user should have access.
408+
* If not provided or empty, user should have access to all accounts.
409+
* @param options Generic advanced options map, see online documentation.
410+
* @return The updated user details
411+
* @throws Exception If the request fails.
412+
*/
413+
public ApiResponse updateUser(String userId, String name, String email, Role role, Boolean enabled, List<String> subAccountsIds, Map<String, Object> options) throws Exception {
380414
List<String> uri = Arrays.asList(PROVISIONING, ACCOUNTS, accountId, USERS, userId);
381-
return performUserAction(Api.HttpMethod.PUT, uri, email, name, role, subAccountsIds, options);
415+
return performUserAction(Api.HttpMethod.PUT, uri, email, name, role, enabled, subAccountsIds, options);
382416
}
383417

384418
/**
@@ -597,14 +631,15 @@ public ApiResponse userGroupUsers(String groupId, Map<String, Object> options) t
597631
* @return The response of the api call.
598632
* @throws Exception If the request fails.
599633
*/
600-
private ApiResponse performUserAction(Api.HttpMethod method, List<String> uri, String email, String name, Role role, List<String> subAccountsIds, Map<String, Object> options) throws Exception {
634+
private ApiResponse performUserAction(Api.HttpMethod method, List<String> uri, String email, String name, Role role, Boolean enabled, List<String> subAccountsIds, Map<String, Object> options) throws Exception {
601635
options = verifyOptions(options);
602636
options.put("content_type", "json");
603637

604638
return callAccountApi(method, uri, ObjectUtils.asMap(
605639
"email", email,
606640
"name", name,
607641
"role", role == null ? null : role.serializedValue,
642+
"enabled", enabled,
608643
"sub_account_ids", subAccountsIds),
609644
options);
610645
}
@@ -616,4 +651,4 @@ private Map<String, Object> verifyOptions(Map<String, Object> options) {
616651

617652
return options;
618653
}
619-
}
654+
}

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractAccountApiTest.java

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.cloudinary.Cloudinary;
55
import com.cloudinary.api.ApiResponse;
66
import com.cloudinary.provisioning.Account;
7+
import com.cloudinary.utils.ObjectUtils;
78
import org.junit.*;
89
import org.junit.rules.ExpectedException;
910
import org.junit.rules.TestName;
@@ -13,8 +14,7 @@
1314
import static java.util.Collections.emptyMap;
1415
import static java.util.Collections.singletonMap;
1516
import static junit.framework.TestCase.assertTrue;
16-
import static org.junit.Assert.assertEquals;
17-
import static org.junit.Assert.assertNotNull;
17+
import static org.junit.Assert.*;
1818

1919
public abstract class AbstractAccountApiTest extends MockableTest {
2020
private static Random rand = new Random();
@@ -247,6 +247,27 @@ public void testCreateUser() throws Exception {
247247
assertNotNull(result);
248248
}
249249

250+
@Test
251+
public void testCreateUserWithOptions() throws Exception {
252+
ApiResponse createResult = createSubAccount();
253+
ApiResponse result = createUser(Collections.singletonList(createResult.get("id").toString()), ObjectUtils.emptyMap());
254+
assertNotNull(result);
255+
}
256+
257+
@Test
258+
public void testCreateUserEnabled() throws Exception {
259+
ApiResponse createResult = createSubAccount();
260+
ApiResponse result = createUser(Collections.singletonList(createResult.get("id").toString()), true);
261+
assertTrue((Boolean) result.get("enabled"));
262+
}
263+
264+
@Test
265+
public void testCreateUserDisabled() throws Exception {
266+
ApiResponse createResult = createSubAccount();
267+
ApiResponse result = createUser(Collections.singletonList(createResult.get("id").toString()), false);
268+
assertFalse((Boolean) result.get("enabled"));
269+
}
270+
250271
@Test
251272
public void testUpdateUser() throws Exception {
252273
ApiResponse user = createUser(Account.Role.ADMIN);
@@ -259,6 +280,30 @@ public void testUpdateUser() throws Exception {
259280
deleteUser(userId);
260281
}
261282

283+
@Test
284+
public void testUpdateUserEnabled() throws Exception {
285+
ApiResponse user = createUser(Account.Role.ADMIN);
286+
String userId = user.get("id").toString();
287+
String newName = randomLetters();
288+
ApiResponse result = account.updateUser(userId, newName, null, null, true, null, null);
289+
290+
assertNotNull(result);
291+
assertTrue((Boolean) result.get("enabled"));
292+
deleteUser(userId);
293+
}
294+
295+
@Test
296+
public void testUpdateUserDisabled() throws Exception {
297+
ApiResponse user = createUser(Account.Role.ADMIN);
298+
String userId = user.get("id").toString();
299+
String newName = randomLetters();
300+
ApiResponse result = account.updateUser(userId, newName, null, null, false, null, null);
301+
302+
assertNotNull(result);
303+
assertFalse((Boolean) result.get("enabled"));
304+
deleteUser(userId);
305+
}
306+
262307
@Test
263308
public void testDeleteUser() throws Exception {
264309
ApiResponse user = createUser(Collections.<String>emptyList());
@@ -354,26 +399,47 @@ private ApiResponse createGroup() throws Exception {
354399
ApiResponse userGroup = account.createUserGroup(name);
355400
createdGroupIds.add(userGroup.get("id").toString());
356401
return userGroup;
402+
}
357403

404+
private ApiResponse createUser() throws Exception {
405+
return createUser(Collections.<String>emptyList());
358406
}
359407

360408
private ApiResponse createUser(Account.Role role) throws Exception {
361409
return createUser(Collections.<String>emptyList(), role);
362410
}
363411

364-
private ApiResponse createUser() throws Exception {
365-
return createUser(Collections.<String>emptyList());
366-
}
367-
368412
private ApiResponse createUser(List<String> subAccountsIds) throws Exception {
369413
return createUser(subAccountsIds, Account.Role.BILLING);
370414
}
371415

416+
private ApiResponse createUser(List<String> subAccountsIds, Map<String, Object> options) throws Exception {
417+
return createUser(subAccountsIds, Account.Role.BILLING, options);
418+
}
419+
420+
private ApiResponse createUser(List<String> subAccountsIds, Boolean enabled) throws Exception {
421+
return createUser(subAccountsIds, Account.Role.BILLING, enabled);
422+
}
423+
372424
private ApiResponse createUser(List<String> subAccountsIds, Account.Role role) throws Exception {
373425
String email = String.format("%s@%s.com", randomLetters(), randomLetters());
374426
return createUser("TestName", email, role, subAccountsIds);
375427
}
376428

429+
private ApiResponse createUser(List<String> subAccountsIds, Account.Role role, Map<String, Object> options) throws Exception {
430+
String email = String.format("%s@%s.com", randomLetters(), randomLetters());
431+
ApiResponse user = account.createUser("TestUserJava"+new Date().toString(), email, role, null, subAccountsIds, options);
432+
createdUserIds.add(user.get("id").toString());
433+
return user;
434+
}
435+
436+
private ApiResponse createUser(List<String> subAccountsIds, Account.Role role, Boolean enabled) throws Exception {
437+
String email = String.format("%s@%s.com", randomLetters(), randomLetters());
438+
ApiResponse user = account.createUser("TestUserJava"+new Date().toString(), email, role, enabled, subAccountsIds, null);
439+
createdUserIds.add(user.get("id").toString());
440+
return user;
441+
}
442+
377443
private ApiResponse createUser(final String name, String email, Account.Role role, List<String> subAccountsIds) throws Exception {
378444
ApiResponse user = account.createUser(name, email, role, subAccountsIds, null);
379445
createdUserIds.add(user.get("id").toString());
@@ -401,7 +467,6 @@ private static String randomLetters() {
401467
for (int i = 0; i < 10; i++) {
402468
sb.append((char) ('a' + rand.nextInt('z' - 'a' + 1)));
403469
}
404-
405470
return sb.toString();
406471
}
407472
}

0 commit comments

Comments
 (0)