Skip to content

Commit

Permalink
TSPS-404 Throw correct exception for Sam user not found (#223)
Browse files Browse the repository at this point in the history
* TSPS-404 Throw correct exception for Sam user not found

* spotless
  • Loading branch information
mmorgantaylor authored Feb 18, 2025
1 parent 08af612 commit b3c133c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/bio/terra/common/iam/SamUserFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public SamUser from(BearerToken bearerToken, String samBasePath) {
} catch (final NullPointerException e) {
throw new UnauthorizedException(e.getMessage(), e);
} catch (final ApiException e) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()) {
if (e.getCode() == HttpStatus.NOT_FOUND.value()
|| e.getCode() == HttpStatus.FORBIDDEN.value()) {
throw new UnauthorizedException("User not found", e);
} else {
throw new InternalServerErrorException(e);
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/bio/terra/common/iam/SamUserFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ public void notFoundUser() throws ApiException {
assertThrows(
UnauthorizedException.class, () -> factory.from(SAM_USER.getBearerToken(), SAM_BASE_PATH));
}

@Test
public void forbiddenUser() throws ApiException {
SamUserFactory factory = spy(new SamUserFactory(new BearerTokenFactory(), Optional.empty()));
UsersApi usersApi = mock(UsersApi.class);
when(factory.createUsersApi(SAM_USER.getBearerToken(), SAM_BASE_PATH)).thenReturn(usersApi);
when(usersApi.getSamUserSelf())
.thenThrow(new ApiException(HttpStatus.FORBIDDEN.value(), "not found"));

assertThrows(
UnauthorizedException.class, () -> factory.from(SAM_USER.getBearerToken(), SAM_BASE_PATH));
}
}

0 comments on commit b3c133c

Please sign in to comment.