diff --git a/README.md b/README.md index 61dbf6b4f..b92d0f794 100644 --- a/README.md +++ b/README.md @@ -276,7 +276,7 @@ For other information, you can contact the project's [Discord channel](https://d ## Postman -### AYS Local Collections +### AYS Local Environments https://fromsmash.com/sX28.CH0NF-dt diff --git a/src/main/java/com/ays/admin_user/model/entity/AdminUserRegisterVerificationEntity.java b/src/main/java/com/ays/admin_user/model/entity/AdminUserRegisterVerificationEntity.java index 1e0e1aac9..bfa98f0ab 100644 --- a/src/main/java/com/ays/admin_user/model/entity/AdminUserRegisterVerificationEntity.java +++ b/src/main/java/com/ays/admin_user/model/entity/AdminUserRegisterVerificationEntity.java @@ -39,6 +39,10 @@ public class AdminUserRegisterVerificationEntity extends BaseEntity { @JoinColumn(name = "ADMIN_USER_ID", referencedColumnName = "ID", insertable = false, updatable = false) private AdminUserEntity adminUser; + public boolean isWaiting(){ + return AdminUserRegisterVerificationStatus.WAITING.equals(this.status); + } + public void complete(final String adminUserId) { this.adminUserId = adminUserId; this.status = AdminUserRegisterVerificationStatus.COMPLETED; diff --git a/src/main/java/com/ays/admin_user/service/impl/AdminUserRegisterServiceImpl.java b/src/main/java/com/ays/admin_user/service/impl/AdminUserRegisterServiceImpl.java index dbec18a89..ce20b4714 100644 --- a/src/main/java/com/ays/admin_user/service/impl/AdminUserRegisterServiceImpl.java +++ b/src/main/java/com/ays/admin_user/service/impl/AdminUserRegisterServiceImpl.java @@ -63,6 +63,7 @@ public void register(final AdminUserRegisterRequest registerRequest) { final AdminUserRegisterVerificationEntity verificationEntity = adminUserRegisterVerificationRepository .findById(registerRequest.getVerificationId()) + .filter(AdminUserRegisterVerificationEntity::isWaiting) .orElseThrow(() -> new AysAdminUserRegisterVerificationCodeNotValidException(registerRequest.getVerificationId())); if (!institutionRepository.existsById(registerRequest.getInstitutionId())) { diff --git a/src/test/java/com/ays/admin_user/model/entity/AdminUserRegisterVerificationEntityBuilder.java b/src/test/java/com/ays/admin_user/model/entity/AdminUserRegisterVerificationEntityBuilder.java index eb4c88dff..649441778 100644 --- a/src/test/java/com/ays/admin_user/model/entity/AdminUserRegisterVerificationEntityBuilder.java +++ b/src/test/java/com/ays/admin_user/model/entity/AdminUserRegisterVerificationEntityBuilder.java @@ -1,5 +1,6 @@ package com.ays.admin_user.model.entity; +import com.ays.admin_user.model.enums.AdminUserRegisterVerificationStatus; import com.ays.common.model.TestDataBuilder; public class AdminUserRegisterVerificationEntityBuilder extends TestDataBuilder { @@ -8,4 +9,13 @@ public AdminUserRegisterVerificationEntityBuilder() { super(AdminUserRegisterVerificationEntity.class); } + public AdminUserRegisterVerificationEntityBuilder withValidFields() { + return this + .withStatus(AdminUserRegisterVerificationStatus.WAITING); + } + + public AdminUserRegisterVerificationEntityBuilder withStatus(AdminUserRegisterVerificationStatus status){ + data.setStatus(status); + return this; + } } diff --git a/src/test/java/com/ays/admin_user/service/impl/AdminUserRegisterServiceImplTest.java b/src/test/java/com/ays/admin_user/service/impl/AdminUserRegisterServiceImplTest.java index ff71d4768..0725794c9 100644 --- a/src/test/java/com/ays/admin_user/service/impl/AdminUserRegisterServiceImplTest.java +++ b/src/test/java/com/ays/admin_user/service/impl/AdminUserRegisterServiceImplTest.java @@ -7,6 +7,7 @@ import com.ays.admin_user.model.entity.AdminUserEntityBuilder; import com.ays.admin_user.model.entity.AdminUserRegisterVerificationEntity; import com.ays.admin_user.model.entity.AdminUserRegisterVerificationEntityBuilder; +import com.ays.admin_user.model.enums.AdminUserRegisterVerificationStatus; import com.ays.admin_user.repository.AdminUserRegisterVerificationRepository; import com.ays.admin_user.repository.AdminUserRepository; import com.ays.admin_user.util.exception.AysAdminUserAlreadyExistsByEmailException; @@ -56,10 +57,14 @@ void givenValidAdminUserRegisterRequest_thenAdminUserRegistered() { .withEmail(AysTestData.VALID_EMAIL) .withPhoneNumber(mockPhoneNumber).build(); + + + AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder() + .withValidFields() + .build(); // When - AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder().build(); Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString())) - .thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity)); + .thenReturn(Optional.of(mockAdminUserRegisterVerificationEntity)); Mockito.when(institutionRepository.existsById(Mockito.anyString())) .thenReturn(true); @@ -83,12 +88,13 @@ void givenValidAdminUserRegisterRequest_thenAdminUserRegistered() { Mockito.when(adminUserRepository.save(Mockito.any(AdminUserEntity.class))) .thenReturn(mockAdminUserEntityToBeSaved); - assert mockAdminUserRegisterVerificationEntity != null; - mockAdminUserRegisterVerificationEntity.complete(mockAdminUserEntityToBeSaved.getId()); - Mockito.when(adminUserRegisterVerificationRepository.save( - Mockito.any(AdminUserRegisterVerificationEntity.class)) - ) - .thenReturn(mockAdminUserRegisterVerificationEntity); + AdminUserRegisterVerificationEntity mockCompletedVerificationEntity = new AdminUserRegisterVerificationEntityBuilder() + .withValidFields() + .build(); + mockCompletedVerificationEntity.complete(mockAdminUserEntityToBeSaved.getId()); + Mockito.when(adminUserRegisterVerificationRepository + .save(Mockito.any(AdminUserRegisterVerificationEntity.class))) + .thenReturn(mockCompletedVerificationEntity); // Then adminUserRegisterService.register(mockAdminUserRegisterRequest); @@ -130,6 +136,35 @@ void givenInvalidVerificationIdFromAdminUserRegisterRequest_whenVerificationEnti .findById(Mockito.anyString()); } + @Test + void givenUsedVerificationIdFromAdminUserRegisterRequest_whenVerificationEntityStatusIsWaiting_thenThrowAysAdminUserRegisterVerificationCodeNotValidException() { + + // Given + AysPhoneNumber mockPhoneNumber = new AysPhoneNumberBuilder().withValidFields().build(); + AdminUserRegisterRequest mockAdminUserRegisterRequest = new AdminUserRegisterRequestBuilder() + .withVerificationId(AysRandomUtil.generateUUID()) + .withInstitutionId(AysRandomUtil.generateUUID()) + .withEmail(AysTestData.VALID_EMAIL) + .withPhoneNumber(mockPhoneNumber).build(); + + AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = + new AdminUserRegisterVerificationEntityBuilder() + .withStatus(AdminUserRegisterVerificationStatus.COMPLETED) + .build(); + // When + Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString())) + .thenReturn(Optional.of(mockAdminUserRegisterVerificationEntity)); + + // Then + Assertions.assertThrows( + AysAdminUserRegisterVerificationCodeNotValidException.class, + () -> adminUserRegisterService.register(mockAdminUserRegisterRequest) + ); + + Mockito.verify(adminUserRegisterVerificationRepository, Mockito.times(1)) + .findById(Mockito.anyString()); + } + @Test void givenInvalidInstitutionIdFromAdminUserRegisterRequest_whenInstitutionNotExist_thenThrowAysInstitutionNotExistException() { @@ -138,8 +173,10 @@ void givenInvalidInstitutionIdFromAdminUserRegisterRequest_whenInstitutionNotExi .withVerificationId(AysRandomUtil.generateUUID()) .withInstitutionId("Invalid").build(); + AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder() + .withValidFields() + .build(); // When - AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder().build(); Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString())) .thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity)); @@ -167,8 +204,10 @@ void givenExistingEmailFromAdminUserRegisterRequest_whenAdminUserExist_thenThrow .withInstitutionId(AysRandomUtil.generateUUID()) .withEmail(AysTestData.VALID_EMAIL).build(); + AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder() + .withValidFields() + .build(); // When - AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder().build(); Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString())) .thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity)); @@ -201,8 +240,10 @@ void givenExistingUsernameFromAdminUserRegisterRequest_whenAdminUserExist_thenTh .withInstitutionId(AysRandomUtil.generateUUID()) .withEmail(AysTestData.VALID_EMAIL).build(); + AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder() + .withValidFields() + .build(); // When - AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder().build(); Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString())) .thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity)); @@ -242,8 +283,10 @@ void givenExistingPhoneNumberFromAdminUserRegisterRequest_whenAdminUserExist_the .withEmail(AysTestData.VALID_EMAIL) .withPhoneNumber(mockPhoneNumber).build(); + AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder() + .withValidFields() + .build(); // When - AdminUserRegisterVerificationEntity mockAdminUserRegisterVerificationEntity = new AdminUserRegisterVerificationEntityBuilder().build(); Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString())) .thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity));