use of org.openlmis.stockmanagement.dto.referencedata.UserDto in project openlmis-stockmanagement by OpenLMIS.
the class SupervisingUsersReferenceDataServiceTest method testFindAll.
@Test
public void testFindAll() {
mockArrayResponse(new UserDto[] { userDto });
Collection<UserDto> userDtos = service.findAll(supervisoryNode, right, program);
assertThat(userDtos, hasSize(1));
assertThat(userDtos, hasItem(userDto));
verify(restTemplate).exchange(uriCaptor.capture(), eq(HttpMethod.GET), entityCaptor.capture(), eq(UserDto[].class));
URI uri = uriCaptor.getValue();
String url = serviceUrl + "/api/supervisoryNodes/" + supervisoryNode + "/supervisingUsers";
assertThat(uri.toString(), startsWith(url));
assertThat(uri.toString(), containsString("rightId=" + right));
assertThat(uri.toString(), containsString("programId=" + program));
assertAuthHeader(entityCaptor.getValue());
assertNull(entityCaptor.getValue().getBody());
}
use of org.openlmis.stockmanagement.dto.referencedata.UserDto in project openlmis-stockmanagement by OpenLMIS.
the class NotificationServiceTest method shouldNotifyUser.
@Test
public void shouldNotifyUser() throws Exception {
UserDto user = mock(UserDto.class);
when(user.getEmail()).thenReturn(USER_EMAIL);
notificationService.notify(user, MAIL_SUBJECT, MAIL_CONTENT);
ArgumentCaptor<HttpEntity> captor = ArgumentCaptor.forClass(HttpEntity.class);
verify(restTemplate).postForObject(eq(new URI(NOTIFICATION_URL)), captor.capture(), eq(Object.class));
assertEquals(singletonList("Bearer " + ACCESS_TOKEN), captor.getValue().getHeaders().get(HttpHeaders.AUTHORIZATION));
assertTrue(EqualsBuilder.reflectionEquals(getNotificationRequest(user), captor.getValue().getBody()));
}
use of org.openlmis.stockmanagement.dto.referencedata.UserDto 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.dto.referencedata.UserDto in project openlmis-stockmanagement by OpenLMIS.
the class StockCardService method search.
/**
* Find stock card page by parameters. Allowed multiple id parameters.
*
* @param ids collection of ids for batch fetch
* @param pageable pagination and sorting parameters
* @return page of filtered stock cards.
*/
public Page<StockCardDto> search(@NotNull Collection<UUID> ids, Pageable pageable) {
OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
Page<StockCard> page;
if (!authentication.isClientOnly()) {
UserDto user = authenticationHelper.getCurrentUser();
LOGGER.info("list of ids:" + ids);
PermissionStrings.Handler handler = permissionService.getPermissionStrings(user.getId());
Set<PermissionStringDto> permissionStrings = handler.get();
LOGGER.info("list of permission strings:" + permissionStrings);
Set<UUID> facilityIds = new HashSet<>();
Set<UUID> programIds = new HashSet<>();
permissionStrings.stream().filter(permissionString -> STOCK_CARDS_VIEW.equalsIgnoreCase(permissionString.getRightName())).forEach(permission -> {
facilityIds.add(permission.getFacilityId());
programIds.add(permission.getProgramId());
});
LOGGER.info("list of facility ids:" + facilityIds);
LOGGER.info("list of program ids:" + programIds);
if (isEmpty(ids)) {
page = cardRepository.findByFacilityIdInAndProgramIdIn(facilityIds, programIds, pageable);
} else {
page = cardRepository.findByFacilityIdInAndProgramIdInAndIdIn(facilityIds, programIds, ids, pageable);
}
} else {
if (isEmpty(ids)) {
page = cardRepository.findAll(pageable);
} else {
page = cardRepository.findByIdIn(ids, pageable);
}
}
return Pagination.getPage(createDtos(page.getContent()), pageable, page.getTotalElements());
}
use of org.openlmis.stockmanagement.dto.referencedata.UserDto in project openlmis-stockmanagement by OpenLMIS.
the class AuthenticationHelperTest method shouldReturnUser.
@Test
public void shouldReturnUser() {
// given
UserDto userMock = mock(UserDto.class);
when(userReferenceDataService.findOne(userId)).thenReturn(userMock);
// when
UserDto user = authenticationHelper.getCurrentUser();
// then
assertNotNull(user);
}
Aggregations