@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)); }
@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 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 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); }
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); }
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);  }