Skip to content

Commit c721dff

Browse files
authored
Implement missing tests Users API
1 parent 8a3c757 commit c721dff

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public ApiResponse user(String userId, Map<String, Object> options) throws Excep
283283
/**
284284
* Get a list of the users according to filters.
285285
*
286-
* @param pending Optional. Whether to fetch pending users. Default all.
286+
* @param pending Optional. Limit results to pending users (true), users that are not pending (false), or all users (null)
287287
* @param userIds Optionals. List of user IDs. Up to 100
288288
* @param prefix Optional. Search by prefix of the user's name or email. Case-insensitive
289289
* @param subAccountId Optional. Return only users who have access to the given sub-account
@@ -297,7 +297,7 @@ public ApiResponse users(Boolean pending, List<String> userIds, String prefix, S
297297
/**
298298
* Get a list of the users according to filters.
299299
*
300-
* @param pending Optional. Whether to fetch pending users. Default all.
300+
* @param pending Optional. Limit results to pending users (true), users that are not pending (false), or all users (null)
301301
* @param userIds Optionals. List of user IDs. Up to 100
302302
* @param prefix Optional. Search by prefix of the user's name or email. Case-insensitive
303303
* @param subAccountId Optional. Return only users who have access to the given sub-account

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.cloudinary.api.ApiResponse;
66
import com.cloudinary.provisioning.Account;
77
import org.junit.*;
8+
import org.junit.rules.ExpectedException;
89
import org.junit.rules.TestName;
910

1011
import java.util.*;
@@ -29,6 +30,8 @@ public static void setUpClass() {
2930

3031
@Rule
3132
public TestName currentTest = new TestName();
33+
@Rule
34+
public ExpectedException expectedException = ExpectedException.none();
3235

3336
@Before
3437
public void setUp() throws Exception {
@@ -180,6 +183,63 @@ public void testGetUsers() throws Exception {
180183
deleteUser(user2Id);
181184
}
182185

186+
@Test
187+
public void testGetPendingUsers() throws Exception {
188+
String id = createUser(Account.Role.BILLING).get("id").toString();
189+
190+
ApiResponse pending = account.users(true, Collections.singletonList(id), null, null, null);
191+
assertEquals(1, ((ArrayList) pending.get("users")).size());
192+
193+
ApiResponse notPending = account.users(false, Collections.singletonList(id), null, null, null);
194+
assertEquals(0, ((ArrayList) notPending.get("users")).size());
195+
196+
ApiResponse all = account.users(null, Collections.singletonList(id), null, null, null);
197+
assertEquals(1, ((ArrayList) all.get("users")).size());
198+
}
199+
200+
@Test
201+
public void testGetUsersByPrefix() throws Exception {
202+
final long timeMillis = System.currentTimeMillis();
203+
final String userName = String.format("SDK TEST Get Users By Prefix %d", timeMillis);
204+
final String userEmail = String.format("sdk-test-get-users-by-prefix+%d@cloudinary.com", timeMillis);
205+
206+
createUser(userName,
207+
userEmail,
208+
Account.Role.BILLING,
209+
Collections.<String>emptyList());
210+
211+
ApiResponse userByPrefix = account.users(true, null, userName.substring(0, userName.length() - 1), null, null);
212+
assertEquals(1, ((ArrayList) userByPrefix.get("users")).size());
213+
214+
ApiResponse userByNonExistingPrefix = account.users(true, null, userName + "zzz", null, null);
215+
assertEquals(0, ((ArrayList) userByNonExistingPrefix.get("users")).size());
216+
}
217+
218+
@Test
219+
public void testGetUsersBySubAccountIds() throws Exception {
220+
ApiResponse subAccount = createSubAccount();
221+
final String subAccountId = subAccount.get("id").toString();
222+
223+
final long timeMillis = System.currentTimeMillis();
224+
final String userName = String.format("SDK TEST Get Users By Sub Account Ids %d", timeMillis);
225+
final String userEmail = String.format("sdk-test-get-users-by-sub-account-ids+%d@cloudinary.com", timeMillis);
226+
227+
createUser(userName,
228+
userEmail,
229+
Account.Role.BILLING,
230+
Collections.singletonList(subAccountId));
231+
232+
ApiResponse usersBySubAccount = account.users(true, null, userName, subAccountId, null);
233+
assertEquals(1, ((ArrayList) usersBySubAccount.get("users")).size());
234+
}
235+
236+
@Test
237+
public void testGetUsersThrowsWhenSubAccountIdDoesntExist() throws Exception {
238+
final String subAccountId = randomLetters();
239+
expectedException.expectMessage("Cannot find sub account with id " + subAccountId);
240+
account.users(true, null, null, subAccountId, null);
241+
}
242+
183243
@Test
184244
public void testCreateUser() throws Exception {
185245
ApiResponse createResult = createSubAccount();
@@ -294,6 +354,7 @@ private ApiResponse createGroup() throws Exception {
294354
ApiResponse userGroup = account.createUserGroup(name);
295355
createdGroupIds.add(userGroup.get("id").toString());
296356
return userGroup;
357+
297358
}
298359

299360
private ApiResponse createUser(Account.Role role) throws Exception {
@@ -310,7 +371,11 @@ private ApiResponse createUser(List<String> subAccountsIds) throws Exception {
310371

311372
private ApiResponse createUser(List<String> subAccountsIds, Account.Role role) throws Exception {
312373
String email = String.format("%s@%s.com", randomLetters(), randomLetters());
313-
ApiResponse user = account.createUser("TestUserJava"+new Date().toString(), email, role, subAccountsIds, null);
374+
return createUser("TestName", email, role, subAccountsIds);
375+
}
376+
377+
private ApiResponse createUser(final String name, String email, Account.Role role, List<String> subAccountsIds) throws Exception {
378+
ApiResponse user = account.createUser(name, email, role, subAccountsIds, null);
314379
createdUserIds.add(user.get("id").toString());
315380
return user;
316381
}
@@ -336,6 +401,7 @@ private static String randomLetters() {
336401
for (int i = 0; i < 10; i++) {
337402
sb.append((char) ('a' + rand.nextInt('z' - 'a' + 1)));
338403
}
404+
339405
return sb.toString();
340406
}
341407
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy