|
| 1 | +package tn.cita.app.service.impl; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.junit.Assert.assertThrows; |
| 5 | +import static org.mockito.Mockito.doNothing; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | + |
| 8 | +import java.time.LocalDateTime; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.UUID; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 16 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 17 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 18 | + |
| 19 | +import tn.cita.app.domain.UserRoleBasedAuthority; |
| 20 | +import tn.cita.app.domain.entity.Credential; |
| 21 | +import tn.cita.app.domain.entity.Employee; |
| 22 | +import tn.cita.app.domain.entity.VerificationToken; |
| 23 | +import tn.cita.app.exception.wrapper.ExpiredVerificationTokenException; |
| 24 | +import tn.cita.app.exception.wrapper.VerificationTokenNotFoundException; |
| 25 | +import tn.cita.app.repository.CredentialRepository; |
| 26 | +import tn.cita.app.repository.CustomerRepository; |
| 27 | +import tn.cita.app.repository.EmployeeRepository; |
| 28 | +import tn.cita.app.repository.VerificationTokenRepository; |
| 29 | +import tn.cita.app.service.RegistrationService; |
| 30 | +import tn.cita.app.util.NotificationUtil; |
| 31 | + |
| 32 | +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
| 33 | +class RegistrationServiceImplTest { |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private RegistrationService registrationService; |
| 37 | + |
| 38 | + @MockBean |
| 39 | + private CustomerRepository customerRepository; |
| 40 | + |
| 41 | + @MockBean |
| 42 | + private EmployeeRepository employeeRepository; |
| 43 | + |
| 44 | + @MockBean |
| 45 | + private CredentialRepository credentialRepository; |
| 46 | + |
| 47 | + @MockBean |
| 48 | + private VerificationTokenRepository verificationTokenRepository; |
| 49 | + |
| 50 | + @MockBean |
| 51 | + private PasswordEncoder passwordEncoder; |
| 52 | + |
| 53 | + @MockBean |
| 54 | + private NotificationUtil notificationUtil; |
| 55 | + |
| 56 | + @Test |
| 57 | + void givenValidCustomerRegisterRequest_whenRegister_thenRegisterResponseShouldBeFound() { |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void givenValidWorkerRegisterRequest_whenRegister_thenRegisterResponseShouldBeFound() { |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void givenValidManagerRegisterRequest_whenRegister_thenRegisterResponseShouldBeFound() { |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void givenValidOwnerRegisterRequest_whenRegister_thenRegisterResponseShouldBeFound() { |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void givenInvalidUserRoleInRegisterRequest_whenRegister_thenIllegalRegistrationRoleTypeExceptionShouldBeThrown() { |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void givenExistingUsernameInRegisterRequest_whenRegister_thenUsernameAlreadyExistsExceptionShouldBeThrown() { |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void givenUnmatchedPasswordsInRegisterRequest_whenRegister_thenPasswordNotMatchExceptionShouldBeThrown() { |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void givenValidToken_whenValidateToken_thenConfirmationMsgShouldBeReturned() { |
| 93 | + |
| 94 | + final var token = "c856b457-ed66-4dd4-bc1a-f0be552a28e5"; |
| 95 | + |
| 96 | + final var verificationToken = VerificationToken.builder() |
| 97 | + .id(null) |
| 98 | + .token(token) |
| 99 | + .expireDate(LocalDateTime.of(2023, 11, 26, 10, 50, 9)) |
| 100 | + .credential( |
| 101 | + Credential.builder() |
| 102 | + .id(null) |
| 103 | + .username("jamesbond") |
| 104 | + .userRoleBasedAuthority(UserRoleBasedAuthority.CUSTOMER) |
| 105 | + .isEnabled(true) |
| 106 | + .isAccountNonExpired(true) |
| 107 | + .isAccountNonLocked(true) |
| 108 | + .isCredentialsNonExpired(true) |
| 109 | + .employee( |
| 110 | + Employee.builder() |
| 111 | + .build()) |
| 112 | + .build()) |
| 113 | + .build(); |
| 114 | + |
| 115 | + when(this.verificationTokenRepository.findByToken(verificationToken.getToken())) |
| 116 | + .thenReturn(Optional.ofNullable(verificationToken)); |
| 117 | + doNothing().when(this.verificationTokenRepository) |
| 118 | + .deleteByToken(verificationToken.getToken()); |
| 119 | + when(this.verificationTokenRepository.save(verificationToken)) |
| 120 | + .thenReturn(verificationToken); |
| 121 | + |
| 122 | + final var validateToken = this.registrationService.validateToken(verificationToken.getToken()); |
| 123 | + |
| 124 | + assertThat(validateToken) |
| 125 | + .isNotBlank() |
| 126 | + .isEqualTo("User has been activated successfully, go and login!"); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void givenInvalidToken_whenValidateToken_thenVerificationTokenNotFoundExceptionShouldBeThrown() { |
| 131 | + |
| 132 | + final var wrongToken = "c856b457-ed66-4dd4-bc1a-f0be552a28e5" + UUID.randomUUID().toString(); |
| 133 | + when(this.verificationTokenRepository.findByToken(wrongToken)) |
| 134 | + .thenReturn(Optional.empty()); |
| 135 | + |
| 136 | + final var verificationTokenNotFoundException = assertThrows(VerificationTokenNotFoundException.class, |
| 137 | + () -> this.registrationService.validateToken(wrongToken)); |
| 138 | + |
| 139 | + assertThat(verificationTokenNotFoundException).isNotNull(); |
| 140 | + assertThat(verificationTokenNotFoundException.getMessage()) |
| 141 | + .startsWith("Link ") |
| 142 | + .endsWith(" disactivated") |
| 143 | + .isEqualTo("Link has been disactivated"); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + void givenExpiredToken_whenValidateToken_thenExpiredVerificationTokenExceptionShouldBeThrown() { |
| 148 | + |
| 149 | + final var token = "c856b457-ed66-4dd4-bc1a-f0be552a28e5"; |
| 150 | + |
| 151 | + final var verificationToken = VerificationToken.builder() |
| 152 | + .id(null) |
| 153 | + .token(token) |
| 154 | + .expireDate(LocalDateTime.of(2020, 1, 26, 10, 50, 9)) |
| 155 | + .credential( |
| 156 | + Credential.builder() |
| 157 | + .id(null) |
| 158 | + .username("jamesbond") |
| 159 | + .userRoleBasedAuthority(UserRoleBasedAuthority.CUSTOMER) |
| 160 | + .isEnabled(true) |
| 161 | + .isAccountNonExpired(true) |
| 162 | + .isAccountNonLocked(true) |
| 163 | + .isCredentialsNonExpired(true) |
| 164 | + .employee( |
| 165 | + Employee.builder() |
| 166 | + .build()) |
| 167 | + .build()) |
| 168 | + .build(); |
| 169 | + |
| 170 | + when(this.verificationTokenRepository.findByToken(verificationToken.getToken())) |
| 171 | + .thenReturn(Optional.ofNullable(verificationToken)); |
| 172 | + doNothing().when(this.verificationTokenRepository) |
| 173 | + .deleteByToken(verificationToken.getToken()); |
| 174 | + |
| 175 | + final var expiredVerificationTokenException = assertThrows(ExpiredVerificationTokenException.class, |
| 176 | + () -> this.registrationService.validateToken(token)); |
| 177 | + |
| 178 | + assertThat(expiredVerificationTokenException).isNotNull(); |
| 179 | + assertThat(expiredVerificationTokenException.getMessage()) |
| 180 | + .startsWith("Verification ") |
| 181 | + .endsWith(" expired") |
| 182 | + .isEqualTo("Verification token has been expired"); |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | +} |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + |
0 commit comments