PasswordResetServiceImpl implements PasswordResetService { @Override public void createPasswordResetToken(String username, String emailAddress, String linkUrl) throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { Staff staff = null; if (!StringUtils.hasText(username)) { throw new IllegalArgumentException("Username is required."); } if (!StringUtils.hasText(emailAddress)) { throw new IllegalArgumentException("Email Address is required."); } if (!StringUtils.hasText(linkUrl)) { throw new IllegalArgumentException("Email link is required."); } Patient patient = patientRepository.findByUsername(username); if (patient == null) { staff = staffRepository.findByUsername(username); if (staff == null) throw new UsernameNotExistException( "The username is not found."); } String patientEmailAddress = (patient != null) ? patient.getEmail() : staff.getEmail(); String firstName = (patient != null) ? patient.getFirstName() : staff .getFirstName(); String lastName = (patient != null) ? patient.getLastName() : staff .getLastName(); if (!patientEmailAddress.equalsIgnoreCase(emailAddress)) { String message = String.format( "Email address %s doesn't exist for username %s.", emailAddress, username); logger.warn(message); throw new EmailAddressNotExistException(message); } EmailToken passwordResetToken = new EmailToken(); passwordResetToken.setExpireInHours(passwordResetTokenExpireInHours); passwordResetToken.setRequestDateTime(new Date()); String token = tokenGenerator.generateToken(); passwordResetToken.setUsername(username); passwordResetToken.setToken(token); passwordResetToken.setIsTokenUsed(false); passwordResetToken.setTokenType(TokenType.PASSWORD_RESET); passwordResetTokenRepository.save(passwordResetToken); emailSender.sendMessage(firstName + " " + lastName, emailAddress, EmailType.PASSWORD_RESET_REQUEST, linkUrl, token); } PasswordResetServiceImpl(Integer passwordResetTokenExpireInHours,
			UsersRepository usersRepository,
			PatientRepository patientRepository,
			StaffRepository staffRepository, TokenGenerator tokenGenerator,
			EmailTokenRepository passwordResetTokenRepository,
			EmailSender emailSender, PasswordEncoder passwordEncoder); @Override void createPasswordResetToken(String username, String emailAddress,
			String linkUrl); @Override Boolean isPasswordResetTokenExpired(String token); @Override void resetPassword(PasswordResetDto passwordResetDto, String linkUrl); @Override boolean changePassword(PasswordChangeDto passwordChangeDto);  }
@Test(expected = IllegalArgumentException.class) public void createPasswordResetToken_Throws_Exception_When_Username_Has_Whitespaces_Only() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createPasswordResetToken(" ", "emailAddress", "resetPasswordLinkPlaceHolder"); }
@Test(expected = IllegalArgumentException.class) public void createPasswordResetToken_Throws_Exception_When_Username_Is_Null() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createPasswordResetToken(null, "emailAddress", "resetPasswordLinkPlaceHolder"); }
@Test(expected = IllegalArgumentException.class) public void testCreatePasswordResetToken_Throws_Exception_When_Email_Has_Whitespaces_Only() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createPasswordResetToken("username", " ", "resetPasswordLinkPlaceHolder"); }
@Test(expected = IllegalArgumentException.class) public void testCreatePasswordResetToken_Throws_Exception_When_Email_Is_Null() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createPasswordResetToken("username", null, "resetPasswordLinkPlaceHolder"); }
@Test(expected = IllegalArgumentException.class) public void testCreatePasswordResetToken_Throws_Exception_When_ResetPasswordLinkPlaceHolder_Has_Whitespaces_Only() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createPasswordResetToken("username", "emailAddress", " "); }
@Test(expected = IllegalArgumentException.class) public void testCreatePasswordResetToken_Throws_Exception_When_ResetPasswordLinkPlaceHolder_Is_Null() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { sut.createPasswordResetToken("username", "emailAddress", null); }
@Test public void testCreatePasswordResetToken_Succeeds_When_ResetPasswordLinkPlaceHolder_End_Correctly() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { final String emailAddress = "emailAddress"; Patient patient = mock(Patient.class); when(patientRepository.findByUsername(anyString())).thenReturn(patient); when(patient.getEmail()).thenReturn(emailAddress); final String resetPasswordLinkPlaceHolder = "http: sut.createPasswordResetToken("username", emailAddress, resetPasswordLinkPlaceHolder); }
@Test(expected = UsernameNotExistException.class) public void testCreatePasswordResetToken_Throws_Exception_When_Username_Not_Exist() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { when(usersRepository.loadUserByUsername(anyString())).thenThrow( new UsernameNotFoundException("The Message")); final String resetPasswordLinkPlaceHolder = "http: final String emailAddress = "emailAddress"; sut.createPasswordResetToken("username", emailAddress, resetPasswordLinkPlaceHolder); }
@Test(expected = EmailAddressNotExistException.class) public void testCreatePasswordResetToken_Throws_Exception_When_EmailAddress_Not_Exist() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { final String resetPasswordLinkPlaceHolder = "http: final String emailAddress = "emailAddress"; final String anotherEmailAddress = "anotherEmailAddress"; Patient patient = mock(Patient.class); when(patientRepository.findByUsername(anyString())).thenReturn(patient); when(patient.getEmail()).thenReturn(anotherEmailAddress); sut.createPasswordResetToken("username", emailAddress, resetPasswordLinkPlaceHolder); }
@Test public void testCreatePasswordResetToken_PasswordResetToken_With_Correct_ExpireInHours_Is_Saved_By_PasswordResetTokenRepository() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { final String resetPasswordLinkPlaceHolder = "http: final String emailAddress = "emailAddress"; Patient patient = mock(Patient.class); when(patientRepository.findByUsername(anyString())).thenReturn(patient); when(patient.getEmail()).thenReturn(emailAddress); passwordResetTokenExpireInHours = 5; sut = new PasswordResetServiceImpl(passwordResetTokenExpireInHours, usersRepository, patientRepository, staffRepository, tokenGenerator, passwordResetTokenRepository, emailSender, passwordEncoder); sut.createPasswordResetToken("username", emailAddress, resetPasswordLinkPlaceHolder); verify(passwordResetTokenRepository, times(1)).save( argThat(new IsPasswordResetTokenWithCorrectExpireInHours( passwordResetTokenExpireInHours))); }
@Test public void testCreatePasswordResetToken_PasswordResetToken_With_Correct_Token_Is_Saved_By_PasswordResetTokenRepository() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { final String resetPasswordLinkPlaceHolder = "http: final String emailAddress = "emailAddress"; Patient patient = mock(Patient.class); when(patientRepository.findByUsername(anyString())).thenReturn(patient); when(patient.getEmail()).thenReturn(emailAddress); final String token = "TheToken"; when(tokenGenerator.generateToken()).thenReturn(token); sut = new PasswordResetServiceImpl(passwordResetTokenExpireInHours, usersRepository, patientRepository, staffRepository, tokenGenerator, passwordResetTokenRepository, emailSender, passwordEncoder); sut.createPasswordResetToken("username", emailAddress, resetPasswordLinkPlaceHolder); verify(passwordResetTokenRepository, times(1)).save( argThat(new IsPasswordResetTokenWithCorrectToken(token))); }
@Test public void testCreatePasswordResetToken_PasswordResetToken_With_Correct_RequestDateTime_Is_Saved_By_PasswordResetTokenRepository() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { final String resetPasswordLinkPlaceHolder = "http: final String emailAddress = "emailAddress"; Patient patient = mock(Patient.class); when(patientRepository.findByUsername(anyString())).thenReturn(patient); when(patient.getEmail()).thenReturn(emailAddress); sut = new PasswordResetServiceImpl(passwordResetTokenExpireInHours, usersRepository, patientRepository, staffRepository, tokenGenerator, passwordResetTokenRepository, emailSender, passwordEncoder); sut.createPasswordResetToken("username", emailAddress, resetPasswordLinkPlaceHolder); verify(passwordResetTokenRepository, times(1)).save( argThat(new IsPasswordResetTokenWithCorrectRequestDateTime())); }
@Test public void testCreatePasswordResetToken_Email_Is_Sent_Successfully() throws UsernameNotExistException, EmailAddressNotExistException, MessagingException { final String resetPasswordLinkPlaceHolder = "http: final String emailAddress = "emailAddress"; Patient patient = mock(Patient.class); when(patientRepository.findByUsername(anyString())).thenReturn(patient); when(patient.getEmail()).thenReturn(emailAddress); sut = new PasswordResetServiceImpl(passwordResetTokenExpireInHours, usersRepository, patientRepository, staffRepository, tokenGenerator, passwordResetTokenRepository, emailSender, passwordEncoder); sut.createPasswordResetToken("username", emailAddress, resetPasswordLinkPlaceHolder); verify(emailSender, times(1)).sendMessage(anyString(), anyString(), eq(EmailType.PASSWORD_RESET_REQUEST), anyString(), anyString()); }