AdminServiceImpl implements AdminService { @Override @Transactional public void updateAdministrator(AdminProfileDto adminProfileDto) throws AuthenticationFailedException { AuthenticatedUser currentUser = userContext.getCurrentUser(); String username = adminProfileDto.getUsername(); if (!currentUser.getUsername().equals(username)) throw new AuthenticationFailedException( "Username does not match current active user."); Users user = usersRepository.loadUserByUsername(username); if (user != null) if (!passwordEncoder.matches(adminProfileDto.getPassword(), user.getPassword())) throw new AuthenticationFailedException( "Password is incorrect."); logger.info("{} being run...", "updatePatient"); Staff admin = adminProfileDtoToAdministratorMapper.map(adminProfileDto); administratorRepository.save(admin); try { emailSender.sendMessage(adminProfileDto.getFirstName() + " " + adminProfileDto.getLastName(), adminProfileDto.getEmail(), EmailType.USER_PROFILE_CHANGE, null, null); } catch (MessagingException e) { logger.warn("Error when sending the email message."); } } AdminServiceImpl(
			Integer accountVerificationTokenExpireInHours,
			StaffRepository administratorRepository,
			PatientRepository patientRepository,
			DtoToDomainEntityMapper<PatientProfileDto, Patient> patientProfileDtoToPatientMapper,
			AdminProfileDtoToAdministratorMapper adminProfileDtoToAdministratorMapper,
			ModelMapper modelMapper,
			UserContext userContext,
			PasswordEncoder passwordEncoder,
			EmailSender emailSender,
			TokenGenerator tokenGenerator,
			EmailTokenRepository emailTokenRepository,
			UsersRepository usersRepository,
			AdministrativeGenderCodeRepository administrativeGenderCodeRepository,
			MrnService mrnService, PixService pixService); @Override AdminProfileDto findAdminProfileByUsername(String username); @Override @Transactional void updateAdministrator(AdminProfileDto adminProfileDto); @Override @Transactional void updatePatient(PatientProfileDto patientProfileDto); @Override AdminCreatePatientResponseDto createPatientAccount(
			BasicPatientAccountDto basicPatientAccountDto); @Override Boolean sendLoginInformationEmail(long patientId, String linkUrl);  }
@Test public void testUpdateAdministrator_when_authentication_success() throws AuthenticationFailedException { final String username = "TheUsername"; AuthenticatedUser authenticatedUser = mock(AuthenticatedUser.class); when(authenticatedUser.getUsername()).thenReturn(username); when(userContext.getCurrentUser()).thenReturn(authenticatedUser); Users user = mock(Users.class); when(user.getUsername()).thenReturn(username); when(user.getPassword()).thenReturn("hashedpassword"); when(usersRepository.loadUserByUsername(anyString())).thenReturn(user); when(passwordEncoder.matches("rawpassword", "hashedpassword")) .thenReturn(true); AdminProfileDto adminProfileDtoInput = mock(AdminProfileDto.class); Staff admin = mock(Staff.class); when(adminProfileDtoInput.getUsername()).thenReturn(username); when(adminProfileDtoInput.getPassword()).thenReturn("rawpassword"); when(adminProfileDtoInput.getFirstName()).thenReturn("albert"); when(adminProfileDtoInput.getLastName()).thenReturn("Smith"); when(adminProfileDtoInput.getEmail()).thenReturn( "albert.smith@consent2share.com"); when(adminProfileDtoToAdministratorMapper.map(adminProfileDtoInput)) .thenReturn(admin); sut.updateAdministrator(adminProfileDtoInput); verify(administratorRepository, times(1)).save(admin); }
@Test public void testUpdateAdministrator_when_username_does_not_match_and_so_authentication_fails() { AuthenticatedUser authenticatedUser = mock(AuthenticatedUser.class); when(authenticatedUser.getUsername()).thenReturn("TheUsername"); when(userContext.getCurrentUser()).thenReturn(authenticatedUser); Users user = mock(Users.class); when(user.getUsername()).thenReturn("AnotherUsername"); AdminProfileDto adminProfileDtoInput = mock(AdminProfileDto.class); when(adminProfileDtoInput.getUsername()).thenReturn("AnotherUsername"); try { sut.updateAdministrator(adminProfileDtoInput); } catch (AuthenticationFailedException e) { assertEquals(e.getMessage(), "Username does not match current active user."); } verify(administratorRepository, times(0)).save(any(Staff.class)); }
@Test public void testUpdateAdministrator_when_username_matches_but_password_is_wrong() { final String username = "TheUsername"; AuthenticatedUser authenticatedUser = mock(AuthenticatedUser.class); when(authenticatedUser.getUsername()).thenReturn(username); when(userContext.getCurrentUser()).thenReturn(authenticatedUser); Users user = mock(Users.class); when(user.getUsername()).thenReturn(username); when(user.getPassword()).thenReturn("hashedpassword"); when(usersRepository.loadUserByUsername(anyString())).thenReturn(user); when(passwordEncoder.matches("rawpassword", "hashedpassword")) .thenReturn(false); AdminProfileDto adminProfileDtoInput = mock(AdminProfileDto.class); Staff admin = mock(Staff.class); when(adminProfileDtoInput.getUsername()).thenReturn(username); when(adminProfileDtoInput.getPassword()).thenReturn("rawpassword"); when(adminProfileDtoInput.getFirstName()).thenReturn("albert"); when(adminProfileDtoInput.getLastName()).thenReturn("Smith"); when(adminProfileDtoInput.getEmail()).thenReturn( "albert.smith@consent2share.com"); when(adminProfileDtoToAdministratorMapper.map(adminProfileDtoInput)) .thenReturn(admin); try { sut.updateAdministrator(adminProfileDtoInput); } catch (AuthenticationFailedException e) { assertEquals(e.getMessage(), "Password is incorrect."); } verify(administratorRepository, times(0)).save(admin); }