Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ keys `display` and `email`.

`GET /api/V2/users/?list=<comma separated user names>`
Validate a set of user names and get the users' display names. Returns a map of username ->
display name. Any usernames that do not correspond to accounts will not be included in the map.
display name. Any usernames that do not correspond to accounts, as well as any empty or illegal
usernames, will not be included in the map.

`GET /api/V2/users/search/<prefix>/?fields=<comma separated fields>`
Find users based on a prefix of the username or any parts of the display name, where parts are
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/us/kbase/auth2/service/api/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import us.kbase.auth2.lib.DisplayName;
import us.kbase.auth2.lib.UserName;
import us.kbase.auth2.lib.UserSearchSpec;
import us.kbase.auth2.lib.exceptions.ErrorType;
import us.kbase.auth2.lib.exceptions.IllegalParameterException;
import us.kbase.auth2.lib.exceptions.InvalidTokenException;
import us.kbase.auth2.lib.exceptions.MissingParameterException;
Expand Down Expand Up @@ -54,8 +53,11 @@ public Map<String, String> getUsers(
Collectors.toMap(e -> e.getKey().getName(), e -> e.getValue().getName()));
}

static Set<UserName> processUserListString(final String users)
throws IllegalParameterException {
/* Entries that are empty or not legal user names are skipped, in the same way names that
* don't correspond to an account are omitted from the results, rather than failing the
* entire call. An illegal user name can never correspond to an account.
*/
static Set<UserName> processUserListString(final String users) {
final Set<UserName> uns = new HashSet<>();
if (nullOrEmpty(users)) {
return uns;
Expand All @@ -65,9 +67,7 @@ static Set<UserName> processUserListString(final String users)
try {
uns.add(new UserName(u.trim()));
} catch (MissingParameterException | IllegalParameterException e) {
//TODO CODE this exception could use some clean up
throw new IllegalParameterException(ErrorType.ILLEGAL_USER_NAME, String.format(
"Illegal user name [%s]: %s", u, e.getMessage()));
// skip, see above
}
}
return uns;
Expand Down
21 changes: 14 additions & 7 deletions src/test/java/us/kbase/test/auth2/service/api/TestModeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,21 @@ public void getUserDisplayNameFailInput() {
new NoTokenProvidedException("No user token provided"));
failGetUserDisplayName(tm, " \t \n ", "",
new NoTokenProvidedException("No user token provided"));
}

@Test
public void getUserDisplayNameSkipMissingAndIllegalNames() throws Exception {
final Authentication auth = mock(Authentication.class);
final TestMode tm = new TestMode(auth);

when(auth.testModeGetUserDisplayNames(new IncomingToken("token"),
set(new UserName("foo"), new UserName("bar"))))
.thenReturn(ImmutableMap.of(
new UserName("foo"), new DisplayName("whee"),
new UserName("bar"), new DisplayName("whoo")));

failGetUserDisplayName(tm, "token", "foo, , bar",
new IllegalParameterException(ErrorType.ILLEGAL_USER_NAME,
"Illegal user name [ ]: 30000 Missing input parameter: user name"));
failGetUserDisplayName(tm, "token", "foo, MyName, bar",
new IllegalParameterException(ErrorType.ILLEGAL_USER_NAME,
"Illegal user name [ MyName]: 30010 Illegal user name: " +
"Illegal character in user name MyName: M"));
assertThat("incorrect users", tm.getUsers("token", "foo, , MyName, 9baz, bar"),
is(ImmutableMap.of("foo", "whee", "bar", "whoo")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,18 +638,19 @@ private IncomingToken setUpUsersForTesting() throws Exception {
}

@Test
public void getUserListFailMissingUser() throws Exception {
failGetUserList(" u1 , , u3", "foobar", 400, "Bad Request",
new IllegalParameterException(ErrorType.ILLEGAL_USER_NAME,
"Illegal user name [ ]: 30000 Missing input parameter: user name"));
public void getUserListSkipMissingUser() throws Exception {
getUserList(" baz , , mua", ImmutableMap.of("baz", "fuz", "mua", "paz"));
}

@Test
public void getUserListFailIllegalUser() throws Exception {
failGetUserList(" u1 , aA , u3", "foobar", 400, "Bad Request",
new IllegalParameterException(ErrorType.ILLEGAL_USER_NAME,
"Illegal user name [ aA ]: 30010 Illegal user name: " +
"Illegal character in user name aA: A"));
public void getUserListSkipIllegalUser() throws Exception {
getUserList(" baz , aA , mua, 9foo, ba*z, u3",
ImmutableMap.of("baz", "fuz", "mua", "paz"));
}

@Test
public void getUserListOnlyIllegalUsers() throws Exception {
getUserList(" aA , , 9foo", Collections.emptyMap());
}

@Test
Expand All @@ -659,6 +660,15 @@ public void getUserListFailBadToken() throws Exception {
failGetUserList("u1", "boobar", 401, "Unauthorized", new InvalidTokenException());
}

@Test
public void getUserListFailBadTokenWithIllegalUsers() throws Exception {
// illegal names are skipped, so the token must still be checked
failGetUserList(" u1 , aA , ", null, 400, "Bad Request",
new NoTokenProvidedException("No user token provided"));
failGetUserList(" u1 , aA , ", "boobar", 401, "Unauthorized",
new InvalidTokenException());
}

private void failGetUserList(
final String list,
final String token,
Expand Down