use of org.openlmis.stockmanagement.exception.AuthenticationException in project openlmis-stockmanagement by OpenLMIS.
the class AuthenticationHelper method getCurrentUser.
/**
* Method returns current user based on Spring context
* and fetches his data from reference-data service.
*
* @return UserDto entity of current user.
* @throws AuthenticationException if user cannot be found.
*/
public UserDto getCurrentUser() {
UUID userId = (UUID) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserDto user = userReferenceDataService.findOne(userId);
if (user == null) {
throw new AuthenticationException(new Message(ERROR_USER_NOT_FOUND, userId));
}
return user;
}
use of org.openlmis.stockmanagement.exception.AuthenticationException in project openlmis-stockmanagement by OpenLMIS.
the class StockCardTemplatesControllerIntegrationTest method shouldReturn401WhenUserUnauthorized.
@Test
public void shouldReturn401WhenUserUnauthorized() throws Exception {
// given
Mockito.doThrow(new AuthenticationException("MANAGE_STOCK_CARD_TEMPLATES")).when(permissionService).canCreateStockCardTemplate();
// when
ResultActions resultActions = mvc.perform(post(STOCK_CARD_TEMPLATE_API).param(ACCESS_TOKEN, ACCESS_TOKEN_VALUE).contentType(MediaType.APPLICATION_JSON).content(objectToJsonString(new StockCardTemplate())));
// then
resultActions.andExpect(status().isUnauthorized());
}
Aggregations