Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#157 Admin Registration Flow Bugfix #158

Merged
merged 7 commits into from
Sep 3, 2023
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AdminUserRegisterVerificationEntity> {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()))
moaydogdu marked this conversation as resolved.
Show resolved Hide resolved
.thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity));
.thenReturn(Optional.of(mockAdminUserRegisterVerificationEntity));

Mockito.when(institutionRepository.existsById(Mockito.anyString()))
.thenReturn(true);
Expand All @@ -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);
Expand Down Expand Up @@ -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() {

Expand All @@ -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();
moaydogdu marked this conversation as resolved.
Show resolved Hide resolved
Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString()))
.thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity));

Expand Down Expand Up @@ -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();
moaydogdu marked this conversation as resolved.
Show resolved Hide resolved
Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString()))
.thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity));

Expand Down Expand Up @@ -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();
moaydogdu marked this conversation as resolved.
Show resolved Hide resolved
Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString()))
.thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity));

Expand Down Expand Up @@ -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();
moaydogdu marked this conversation as resolved.
Show resolved Hide resolved
Mockito.when(adminUserRegisterVerificationRepository.findById(Mockito.anyString()))
.thenReturn(Optional.ofNullable(mockAdminUserRegisterVerificationEntity));

Expand Down