diff --git a/src/main/resources/db/changelog/changes/2-ays-dml.xml b/src/main/resources/db/changelog/changes/2-ays-dml.xml index 20df7134d..98111d50e 100644 --- a/src/main/resources/db/changelog/changes/2-ays-dml.xml +++ b/src/main/resources/db/changelog/changes/2-ays-dml.xml @@ -117,7 +117,6 @@ - @@ -147,7 +146,7 @@ - + @@ -156,6 +155,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/com/ays/AbstractSystemTest.java b/src/test/java/com/ays/AbstractSystemTest.java new file mode 100644 index 000000000..a3367cb1c --- /dev/null +++ b/src/test/java/com/ays/AbstractSystemTest.java @@ -0,0 +1,139 @@ +package com.ays; + +import com.ays.admin_user.model.entity.AdminUserEntityBuilder; +import com.ays.auth.config.AysTokenConfigurationParameter; +import com.ays.auth.model.AysToken; +import com.ays.auth.model.enums.AysTokenClaims; +import com.ays.common.util.AysRandomUtil; +import com.ays.parameter.model.AysParameter; +import com.ays.parameter.model.AysParameterBuilder; +import com.ays.parameter.service.AysParameterService; +import com.ays.user.model.entity.UserEntityBuilder; +import com.ays.util.AysTestData; +import io.jsonwebtoken.JwtBuilder; +import io.jsonwebtoken.Jwts; +import io.jsonwebtoken.SignatureAlgorithm; +import org.apache.commons.lang3.time.DateUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.oauth2.core.OAuth2AccessToken; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.Date; +import java.util.Map; +import java.util.Set; + +@SpringBootTest +@ExtendWith(MockitoExtension.class) +@AutoConfigureMockMvc +public abstract class AbstractSystemTest extends AbstractTestContainerConfiguration { + + @Autowired + protected MockMvc mockMvc; + + protected AysToken adminUserTokenOne; + protected AysToken adminUserTokenTwo; + protected AysToken userTokenOne; + protected AysToken userTokenTwo; + protected AysToken userTokenThree; + + + @Mock + private AysTokenConfigurationParameter tokenConfiguration; + @Mock + private AysParameterService parameterService; + + @BeforeEach + public void initializeAuth() { + Set parameters = AysParameterBuilder.getParameters(); + Mockito.when(parameterService.getParameters(Mockito.anyString())) + .thenReturn(parameters); + this.tokenConfiguration = new AysTokenConfigurationParameter(parameterService); + + final Map claimsOfAdminUserOne = new AdminUserEntityBuilder() + .withId(AysTestData.AdminUser.VALID_ID_ONE) + .withUsername(AysTestData.AdminUser.VALID_USERNAME_ONE) + .withEmail(AysTestData.AdminUser.VALID_EMAIL_ONE) + .withInstitutionId(AysTestData.Institution.VALID_ID_ONE) + .build() + .getClaims(); + this.adminUserTokenOne = this.generate(claimsOfAdminUserOne); + + final Map claimsOfAdminUserTwo = new AdminUserEntityBuilder() + .withId(AysTestData.AdminUser.VALID_ID_TWO) + .withUsername(AysTestData.AdminUser.VALID_USERNAME_TWO) + .withEmail(AysTestData.AdminUser.VALID_EMAIL_TWO) + .withInstitutionId(AysTestData.Institution.VALID_ID_TWO) + .build() + .getClaims(); + this.adminUserTokenTwo = this.generate(claimsOfAdminUserTwo); + + + final Map claimsOfUserOne = new UserEntityBuilder() + .withId(AysTestData.User.VALID_ID_ONE) + .withUsername(AysTestData.User.VALID_USERNAME_ONE) + .withInstitutionId(AysTestData.Institution.VALID_ID_ONE) + .build() + .getClaims(); + this.userTokenOne = this.generate(claimsOfUserOne); + + final Map claimsOfUserTwo = new UserEntityBuilder() + .withId(AysTestData.User.VALID_ID_TWO) + .withUsername(AysTestData.User.VALID_USERNAME_TWO) + .withInstitutionId(AysTestData.Institution.VALID_ID_ONE) + .build() + .getClaims(); + this.userTokenTwo = this.generate(claimsOfUserTwo); + + final Map claimsOfUserThree = new UserEntityBuilder() + .withId(AysTestData.User.VALID_ID_THREE) + .withUsername(AysTestData.User.VALID_USERNAME_THREE) + .withInstitutionId(AysTestData.Institution.VALID_ID_TWO) + .build() + .getClaims(); + this.userTokenThree = this.generate(claimsOfUserThree); + } + + private AysToken generate(Map claims) { + final long currentTimeMillis = System.currentTimeMillis(); + + final Date tokenIssuedAt = new Date(currentTimeMillis); + + final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute()); + final String accessToken = Jwts.builder() + .setId(AysRandomUtil.generateUUID()) + .setIssuer(tokenConfiguration.getIssuer()) + .setIssuedAt(tokenIssuedAt) + .setExpiration(accessTokenExpiresAt) + .signWith(tokenConfiguration.getPrivateKey(), SignatureAlgorithm.RS512) + .setHeaderParam(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) + .addClaims(claims) + .compact(); + + final Date refreshTokenExpiresAt = DateUtils.addDays(new Date(currentTimeMillis), tokenConfiguration.getRefreshTokenExpireDay()); + final JwtBuilder refreshTokenBuilder = Jwts.builder(); + final String refreshToken = refreshTokenBuilder + .setId(AysRandomUtil.generateUUID()) + .setIssuer(tokenConfiguration.getIssuer()) + .setIssuedAt(tokenIssuedAt) + .setExpiration(refreshTokenExpiresAt) + .signWith(tokenConfiguration.getPrivateKey(), SignatureAlgorithm.RS512) + .setHeaderParam(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) + .claim(AysTokenClaims.USER_ID.getValue(), claims.get(AysTokenClaims.USER_ID.getValue())) + .claim(AysTokenClaims.USERNAME.getValue(), claims.get(AysTokenClaims.USERNAME.getValue())) + .compact(); + + return AysToken.builder() + .accessToken(accessToken) + .accessTokenExpiresAt(accessTokenExpiresAt.toInstant().getEpochSecond()) + .refreshToken(refreshToken) + .build(); + } + +} diff --git a/src/test/java/com/ays/AbstractTestContainerConfiguration.java b/src/test/java/com/ays/AbstractTestContainerConfiguration.java index 1b898c36a..703ded14e 100644 --- a/src/test/java/com/ays/AbstractTestContainerConfiguration.java +++ b/src/test/java/com/ays/AbstractTestContainerConfiguration.java @@ -1,17 +1,22 @@ package com.ays; +import org.junit.jupiter.api.BeforeAll; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.testcontainers.containers.MySQLContainer; -import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; @Testcontainers abstract class AbstractTestContainerConfiguration { - @Container static MySQLContainer MYSQL_CONTAINER = new MySQLContainer<>("mysql:8.0.33"); + @BeforeAll + static void beforeAll() { + MYSQL_CONTAINER.withReuse(true); + MYSQL_CONTAINER.start(); + } + @DynamicPropertySource private static void overrideProps(DynamicPropertyRegistry dynamicPropertyRegistry) { dynamicPropertyRegistry.add("ays.db.username", MYSQL_CONTAINER::getUsername); diff --git a/src/test/java/com/ays/admin_user/controller/AdminUserAuthSystemTest.java b/src/test/java/com/ays/admin_user/controller/AdminUserAuthSystemTest.java new file mode 100644 index 000000000..53e053a13 --- /dev/null +++ b/src/test/java/com/ays/admin_user/controller/AdminUserAuthSystemTest.java @@ -0,0 +1,229 @@ +package com.ays.admin_user.controller; + +import com.ays.AbstractSystemTest; +import com.ays.admin_user.model.dto.request.AdminUserRegisterRequest; +import com.ays.admin_user.model.dto.request.AdminUserRegisterRequestBuilder; +import com.ays.auth.model.dto.request.AysLoginRequest; +import com.ays.auth.model.dto.request.AysLoginRequestBuilder; +import com.ays.auth.model.dto.request.AysTokenInvalidateRequest; +import com.ays.auth.model.dto.request.AysTokenRefreshRequest; +import com.ays.auth.model.dto.response.AysTokenResponse; +import com.ays.auth.model.dto.response.AysTokenResponseBuilder; +import com.ays.auth.model.mapper.AysTokenToAysTokenResponseMapper; +import com.ays.common.model.AysPhoneNumber; +import com.ays.common.model.AysPhoneNumberBuilder; +import com.ays.common.model.dto.response.AysResponse; +import com.ays.common.model.dto.response.AysResponseBuilder; +import com.ays.common.util.exception.model.AysError; +import com.ays.common.util.exception.model.AysErrorBuilder; +import com.ays.util.AysMockMvcRequestBuilders; +import com.ays.util.AysMockResultMatchersBuilders; +import com.ays.util.AysTestData; +import org.junit.jupiter.api.Test; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +class AdminUserAuthSystemTest extends AbstractSystemTest { + + private final AysTokenToAysTokenResponseMapper aysTokenToAysTokenResponseMapper = AysTokenToAysTokenResponseMapper.initialize(); + + private static final String BASE_PATH = "/api/v1/authentication/admin"; + + @Test + void givenValidAdminUserRegisterRequest_whenAdminUserRegistered_thenReturnSuccessResponse() throws Exception { + // Given + AdminUserRegisterRequest registerRequest = new AdminUserRegisterRequestBuilder() + .withValidFields() + .withInstitutionId(AysTestData.Institution.VALID_ID_ONE) + .withVerificationId(AysTestData.VALID_VERIFICATION_ID) + .build(); + + // Then + AysResponse response = AysResponseBuilder.SUCCESS; + mockMvc.perform(AysMockMvcRequestBuilders + .post(BASE_PATH.concat("/register"), registerRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + @Test + void givenPhoneNumberWithAlphanumericCharacter_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { + // Given + AysPhoneNumber phoneNumber = new AysPhoneNumberBuilder() + .withCountryCode("ABC") + .withLineNumber("ABC").build(); + AdminUserRegisterRequest registerRequest = new AdminUserRegisterRequestBuilder() + .withValidFields() + .withPhoneNumber(phoneNumber).build(); + + // Then + AysError errorResponse = AysErrorBuilder.VALIDATION_ERROR; + mockMvc.perform(AysMockMvcRequestBuilders + .post(BASE_PATH.concat("/register"), registerRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(errorResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.header() + .value(errorResponse.getHeader())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(errorResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + } + + @Test + void givenPhoneNumberWithInvalidLength_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { + // Given + AysPhoneNumber phoneNumber = new AysPhoneNumberBuilder() + .withCountryCode("456786745645") + .withLineNumber("6546467456435548676845321346656654").build(); + AdminUserRegisterRequest registerRequest = new AdminUserRegisterRequestBuilder() + .withValidFields() + .withPhoneNumber(phoneNumber).build(); + + // Then + AysError errorResponse = AysErrorBuilder.VALIDATION_ERROR; + mockMvc.perform(AysMockMvcRequestBuilders + .post(BASE_PATH.concat("/register"), registerRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(errorResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.header() + .value(errorResponse.getHeader())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(errorResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + } + + @Test + void givenValidLoginRequest_whenTokensGeneratedSuccessfully_thenReturnTokenResponse() throws Exception { + // Given + AysLoginRequest loginRequest = new AysLoginRequestBuilder() + .withUsername(AysTestData.AdminUser.VALID_USERNAME_ONE) + .withPassword(AysTestData.AdminUser.VALID_PASSWORD_ONE).build(); + + // Then + AysResponse response = AysResponseBuilder + .successOf(new AysTokenResponseBuilder().build()); + mockMvc.perform(AysMockMvcRequestBuilders + .post(BASE_PATH.concat("/token"), loginRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") + .isNotEmpty()); + + } + + @Test + void givenValidTokenRefreshRequest_whenAccessTokenGeneratedSuccessfully_thenReturnTokenResponse() throws Exception { + // Given + AysTokenRefreshRequest tokenRefreshRequest = AysTokenRefreshRequest.builder() + .refreshToken(adminUserTokenOne.getRefreshToken()) + .build(); + + // Then + AysResponse response = AysResponseBuilder + .successOf(new AysTokenResponseBuilder().build()); + mockMvc.perform(AysMockMvcRequestBuilders + .post(BASE_PATH.concat("/token/refresh"), tokenRefreshRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") + .isNotEmpty()); + } + + @Test + void givenValidAysTokenInvalidateRequest_whenTokensInvalidated_thenReturnSuccessResponse() throws Exception { + // Given + AysTokenInvalidateRequest mockRequest = AysTokenInvalidateRequest.builder() + .refreshToken(adminUserTokenOne.getRefreshToken()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/token/invalidate"); + AysResponse response = AysResponseBuilder.SUCCESS; + mockMvc.perform(AysMockMvcRequestBuilders + .post(endpoint, adminUserTokenOne.getAccessToken(), mockRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + @Test + void givenValidAysTokenInvalidateRequest_whenUserUnauthorizedForTokensInvalidating_thenReturnAccessDeniedException() throws Exception { + // Given + AysTokenInvalidateRequest tokenInvalidateRequest = AysTokenInvalidateRequest.builder() + .refreshToken(adminUserTokenOne.getRefreshToken()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/token/invalidate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, userTokenOne.getAccessToken(), tokenInvalidateRequest); + + AysResponse response = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + +} diff --git a/src/test/java/com/ays/admin_user/controller/AdminUserSystemTest.java b/src/test/java/com/ays/admin_user/controller/AdminUserSystemTest.java new file mode 100644 index 000000000..7f38a4cfe --- /dev/null +++ b/src/test/java/com/ays/admin_user/controller/AdminUserSystemTest.java @@ -0,0 +1,95 @@ +package com.ays.admin_user.controller; + +import com.ays.AbstractSystemTest; +import com.ays.admin_user.model.AdminUser; +import com.ays.admin_user.model.dto.request.AdminUserListRequest; +import com.ays.admin_user.model.dto.request.AdminUserListRequestBuilder; +import com.ays.admin_user.model.dto.response.AdminUsersResponse; +import com.ays.admin_user.model.entity.AdminUserEntity; +import com.ays.admin_user.model.entity.AdminUserEntityBuilder; +import com.ays.admin_user.model.mapper.AdminUserEntityToAdminUserMapper; +import com.ays.admin_user.model.mapper.AdminUserToAdminUsersResponseMapper; +import com.ays.common.model.AysPage; +import com.ays.common.model.dto.response.AysPageResponse; +import com.ays.common.model.dto.response.AysResponse; +import com.ays.common.model.dto.response.AysResponseBuilder; +import com.ays.common.util.exception.model.AysError; +import com.ays.util.AysMockMvcRequestBuilders; +import com.ays.util.AysMockResultMatchersBuilders; +import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; + +import java.util.List; + +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +class AdminUserSystemTest extends AbstractSystemTest { + + private final AdminUserToAdminUsersResponseMapper adminUserToAdminUserResponseMapper = AdminUserToAdminUsersResponseMapper.initialize(); + + private final AdminUserEntityToAdminUserMapper adminUserEntityToAdminUserMapper = AdminUserEntityToAdminUserMapper.initialize(); + + + private static final String BASE_PATH = "/api/v1"; + + @Test + void givenValidUserListRequest_whenAdminUsersFound_thenReturnAdminUsersResponse() throws Exception { + + // Given + AdminUserListRequest adminUserListRequest = new AdminUserListRequestBuilder() + .withValidValues() + .build(); + + // When + Page adminUserEntities = new PageImpl<>( + AdminUserEntityBuilder.generateValidUserEntities(1) + ); + List adminUsers = adminUserEntityToAdminUserMapper.map(adminUserEntities.getContent()); + AysPage pageOfUsers = AysPage.of(adminUserEntities, adminUsers); + + // Then + List adminUsersResponses = adminUserToAdminUserResponseMapper.map(pageOfUsers.getContent()); + AysPageResponse pageOfAdminUsersResponse = AysPageResponse.builder() + .of(pageOfUsers) + .content(adminUsersResponses) + .build(); + + AysResponse> response = AysResponse.successOf(pageOfAdminUsersResponse); + String endpoint = BASE_PATH.concat("/admins"); + mockMvc.perform(AysMockMvcRequestBuilders + .post(endpoint, adminUserTokenOne.getAccessToken(), adminUserListRequest)) + .andDo(print()) + .andExpect(status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time().isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus().value(response.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess().value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response().isNotEmpty()); + } + + @Test + void givenValidUserListRequest_whenAdminUserUnauthorizedForListing_thenReturnAccessDeniedException() throws Exception { + // Given + AdminUserListRequest adminUserListRequest = new AdminUserListRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admins"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, userTokenOne.getAccessToken(), adminUserListRequest); + + AysResponse mockResponse = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time().isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus().value(mockResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess().value(mockResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response().doesNotExist()); + } + +} diff --git a/src/test/java/com/ays/admin_user/model/entity/AdminUserEntityBuilder.java b/src/test/java/com/ays/admin_user/model/entity/AdminUserEntityBuilder.java index 944672951..34d9fbf9b 100644 --- a/src/test/java/com/ays/admin_user/model/entity/AdminUserEntityBuilder.java +++ b/src/test/java/com/ays/admin_user/model/entity/AdminUserEntityBuilder.java @@ -53,6 +53,11 @@ public AdminUserEntityBuilder withPassword(String password) { return this; } + public AdminUserEntityBuilder withEmail(String email) { + data.setEmail(email); + return this; + } + public AdminUserEntityBuilder withPhoneNumber(AysPhoneNumber phoneNumber) { data.setCountryCode(phoneNumber.getCountryCode()); data.setLineNumber(phoneNumber.getLineNumber()); diff --git a/src/test/java/com/ays/admin_user/service/impl/AdminUserAuthServiceImplTest.java b/src/test/java/com/ays/admin_user/service/impl/AdminUserAuthServiceImplTest.java index 7f7e5e989..955be3cb3 100644 --- a/src/test/java/com/ays/admin_user/service/impl/AdminUserAuthServiceImplTest.java +++ b/src/test/java/com/ays/admin_user/service/impl/AdminUserAuthServiceImplTest.java @@ -13,7 +13,6 @@ import com.ays.auth.service.AysInvalidTokenService; import com.ays.auth.service.AysTokenService; import com.ays.auth.util.exception.*; -import com.ays.user.model.dto.request.AysUserLoginRequestBuilder; import io.jsonwebtoken.Claims; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -82,7 +81,7 @@ void givenValidLoginRequest_whenAdminUserAuthenticated_thenReturnAysToken() { @Test void givenInvalidLoginRequest_whenAdminUserNotFound_thenThrowUsernameNotValidException() { // Given - AysLoginRequest mockRequest = new AysUserLoginRequestBuilder().build(); + AysLoginRequest mockRequest = new AysLoginRequestBuilder().build(); // When Mockito.when(adminUserRepository.findByUsername(mockRequest.getUsername())) diff --git a/src/test/java/com/ays/auth/model/dto/request/AysLoginRequestBuilder.java b/src/test/java/com/ays/auth/model/dto/request/AysLoginRequestBuilder.java index 20beab8ec..18292d757 100644 --- a/src/test/java/com/ays/auth/model/dto/request/AysLoginRequestBuilder.java +++ b/src/test/java/com/ays/auth/model/dto/request/AysLoginRequestBuilder.java @@ -8,4 +8,14 @@ public AysLoginRequestBuilder() { super(AysLoginRequest.class); } + public AysLoginRequestBuilder withUsername(final String username) { + data.setUsername(username); + return this; + } + + public AysLoginRequestBuilder withPassword(final String password) { + data.setPassword(password); + return this; + } + } diff --git a/src/test/java/com/ays/auth/model/dto/response/AysTokenResponseBuilder.java b/src/test/java/com/ays/auth/model/dto/response/AysTokenResponseBuilder.java new file mode 100644 index 000000000..fb4b1eb29 --- /dev/null +++ b/src/test/java/com/ays/auth/model/dto/response/AysTokenResponseBuilder.java @@ -0,0 +1,11 @@ +package com.ays.auth.model.dto.response; + +import com.ays.common.model.TestDataBuilder; + +public class AysTokenResponseBuilder extends TestDataBuilder { + + public AysTokenResponseBuilder() { + super(AysTokenResponse.class); + } + +} diff --git a/src/test/java/com/ays/user/controller/UserAuthControllerTest.java b/src/test/java/com/ays/user/controller/UserAuthControllerTest.java index b94f1217d..42c7a388c 100644 --- a/src/test/java/com/ays/user/controller/UserAuthControllerTest.java +++ b/src/test/java/com/ays/user/controller/UserAuthControllerTest.java @@ -7,7 +7,6 @@ import com.ays.common.model.dto.response.AysResponse; import com.ays.common.model.dto.response.AysResponseBuilder; import com.ays.common.util.exception.model.AysError; -import com.ays.user.model.dto.request.AysUserLoginRequestBuilder; import com.ays.user.service.UserAuthService; import com.ays.util.AysMockMvcRequestBuilders; import com.ays.util.AysMockResultMatchersBuilders; @@ -24,14 +23,14 @@ class UserAuthControllerTest extends AbstractRestControllerTest { private UserAuthService userAuthService; - private final AysTokenToAysTokenResponseMapper AYS_TOKEN_TO_AYS_TOKEN_RESPONSE_MAPPER = AysTokenToAysTokenResponseMapper.initialize(); + private static final AysTokenToAysTokenResponseMapper AYS_TOKEN_TO_AYS_TOKEN_RESPONSE_MAPPER = AysTokenToAysTokenResponseMapper.initialize(); private static final String BASE_PATH = "/api/v1/authentication"; @Test void givenValidUserLoginRequest_whenTokensGeneratedSuccessfully_thenReturnTokenResponse() throws Exception { // Given - AysLoginRequest mockRequest = new AysUserLoginRequestBuilder().build(); + AysLoginRequest mockRequest = new AysLoginRequestBuilder().build(); // when Mockito.when(userAuthService.authenticate(Mockito.any())) diff --git a/src/test/java/com/ays/user/controller/UserAuthSystemTest.java b/src/test/java/com/ays/user/controller/UserAuthSystemTest.java new file mode 100644 index 000000000..7d0187eb8 --- /dev/null +++ b/src/test/java/com/ays/user/controller/UserAuthSystemTest.java @@ -0,0 +1,140 @@ +package com.ays.user.controller; + +import com.ays.AbstractSystemTest; +import com.ays.auth.model.dto.request.AysLoginRequest; +import com.ays.auth.model.dto.request.AysLoginRequestBuilder; +import com.ays.auth.model.dto.request.AysTokenInvalidateRequest; +import com.ays.auth.model.dto.request.AysTokenRefreshRequest; +import com.ays.auth.model.dto.response.AysTokenResponse; +import com.ays.auth.model.dto.response.AysTokenResponseBuilder; +import com.ays.auth.model.mapper.AysTokenToAysTokenResponseMapper; +import com.ays.common.model.dto.response.AysResponse; +import com.ays.common.model.dto.response.AysResponseBuilder; +import com.ays.common.util.exception.model.AysError; +import com.ays.util.AysMockMvcRequestBuilders; +import com.ays.util.AysMockResultMatchersBuilders; +import com.ays.util.AysTestData; +import org.junit.jupiter.api.Test; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +class UserAuthSystemTest extends AbstractSystemTest { + + private final AysTokenToAysTokenResponseMapper aysTokenToAysTokenResponseMapper = AysTokenToAysTokenResponseMapper.initialize(); + + + private static final String BASE_PATH = "/api/v1/authentication"; + + @Test + void givenValidUserLoginRequest_whenTokensGeneratedSuccessfully_thenReturnTokenResponse() throws Exception { + // Given + AysLoginRequest loginRequest = new AysLoginRequestBuilder() + .withUsername(AysTestData.User.VALID_USERNAME_ONE) + .withPassword(AysTestData.User.VALID_PASSWORD_ONE) + .build(); + + // Then + AysResponse response = AysResponseBuilder + .successOf(new AysTokenResponseBuilder().build()); + mockMvc.perform(AysMockMvcRequestBuilders + .post(BASE_PATH.concat("/token"), loginRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") + .isNotEmpty()); + } + + @Test + void givenValidTokenRefreshRequest_whenAccessTokenGeneratedSuccessfully_thenReturnTokenResponse() throws Exception { + // Given + AysTokenRefreshRequest tokenRefreshRequest = AysTokenRefreshRequest.builder() + .refreshToken(userTokenOne.getRefreshToken()) + .build(); + + // Then + AysResponse response = AysResponseBuilder + .successOf(new AysTokenResponseBuilder().build()); + mockMvc.perform(AysMockMvcRequestBuilders + .post(BASE_PATH.concat("/token/refresh"), tokenRefreshRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") + .isNotEmpty()); + } + + @Test + void givenValidAysTokenInvalidateRequest_whenTokensInvalidated_thenReturnSuccessResponse() throws Exception { + // Given + AysTokenInvalidateRequest tokenInvalidateRequest = AysTokenInvalidateRequest.builder() + .refreshToken(userTokenOne.getRefreshToken()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/token/invalidate"); + AysResponse mockResponse = AysResponseBuilder.SUCCESS; + mockMvc.perform(AysMockMvcRequestBuilders + .post(endpoint, userTokenOne.getAccessToken(), tokenInvalidateRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + @Test + void givenValidAysTokenInvalidateRequest_whenUserUnauthorizedForTokensInvalidating_thenReturnAccessDeniedException() throws Exception { + // Given + AysTokenInvalidateRequest tokenInvalidateRequest = AysTokenInvalidateRequest.builder() + .refreshToken(userTokenOne.getRefreshToken()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/token/invalidate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, adminUserTokenOne.getAccessToken(), tokenInvalidateRequest); + + AysResponse response = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + +} diff --git a/src/test/java/com/ays/user/controller/UserSelfSystemTest.java b/src/test/java/com/ays/user/controller/UserSelfSystemTest.java new file mode 100644 index 000000000..535c8c945 --- /dev/null +++ b/src/test/java/com/ays/user/controller/UserSelfSystemTest.java @@ -0,0 +1,71 @@ +package com.ays.user.controller; + +import com.ays.AbstractSystemTest; +import com.ays.common.model.dto.response.AysResponse; +import com.ays.common.model.dto.response.AysResponseBuilder; +import com.ays.common.util.exception.model.AysError; +import com.ays.user.model.dto.request.UserSupportStatusUpdateRequest; +import com.ays.user.model.dto.request.UserSupportStatusUpdateRequestBuilder; +import com.ays.user.model.enums.UserSupportStatus; +import com.ays.util.AysMockMvcRequestBuilders; +import com.ays.util.AysMockResultMatchersBuilders; +import org.junit.jupiter.api.Test; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; + +class UserSelfSystemTest extends AbstractSystemTest { + + private static final String BASE_PATH = "/api/v1/user-self"; + + @Test + void givenValidUserSupportStatusUpdateRequest_whenUserRole_thenReturnSuccess() throws Exception { + // Given + UserSupportStatus userSupportStatus = UserSupportStatus.READY; + + UserSupportStatusUpdateRequest mockUserSupportStatusUpdateRequest = new UserSupportStatusUpdateRequestBuilder() + .withSupportStatus(userSupportStatus).build(); + + // Then + AysResponse mockAysResponse = AysResponse.SUCCESS; + + mockMvc.perform(AysMockMvcRequestBuilders + .put(BASE_PATH.concat("/status/support"), userTokenOne.getAccessToken(), mockUserSupportStatusUpdateRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockAysResponse.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockAysResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + @Test + void givenValidUserSupportStatusUpdateRequest_whenAdminRole_thenReturnAccessDeniedException() throws Exception { + // Given + UserSupportStatus userSupportStatus = UserSupportStatus.READY; + + UserSupportStatusUpdateRequest mockUserSupportStatusUpdateRequest = new UserSupportStatusUpdateRequestBuilder() + .withSupportStatus(userSupportStatus).build(); + + // Then + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(BASE_PATH.concat("/status/support"), adminUserTokenOne.getAccessToken(), mockUserSupportStatusUpdateRequest); + + AysResponse mockResponse = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + +} diff --git a/src/test/java/com/ays/user/controller/UserSystemTest.java b/src/test/java/com/ays/user/controller/UserSystemTest.java new file mode 100644 index 000000000..f37cae2d5 --- /dev/null +++ b/src/test/java/com/ays/user/controller/UserSystemTest.java @@ -0,0 +1,370 @@ +package com.ays.user.controller; + +import com.ays.AbstractSystemTest; +import com.ays.common.model.AysPage; +import com.ays.common.model.AysPhoneNumber; +import com.ays.common.model.AysPhoneNumberBuilder; +import com.ays.common.model.dto.response.AysPageResponse; +import com.ays.common.model.dto.response.AysResponse; +import com.ays.common.model.dto.response.AysResponseBuilder; +import com.ays.common.util.exception.model.AysError; +import com.ays.common.util.exception.model.AysErrorBuilder; +import com.ays.user.model.User; +import com.ays.user.model.UserBuilder; +import com.ays.user.model.dto.request.*; +import com.ays.user.model.dto.response.UserResponse; +import com.ays.user.model.dto.response.UserSavedResponse; +import com.ays.user.model.dto.response.UserSavedResponseBuilder; +import com.ays.user.model.dto.response.UsersResponse; +import com.ays.user.model.entity.UserEntity; +import com.ays.user.model.entity.UserEntityBuilder; +import com.ays.user.model.enums.UserRole; +import com.ays.user.model.enums.UserStatus; +import com.ays.user.model.mapper.UserEntityToUserMapper; +import com.ays.user.model.mapper.UserToUserResponseMapper; +import com.ays.user.model.mapper.UserToUsersResponseMapper; +import com.ays.util.AysMockMvcRequestBuilders; +import com.ays.util.AysMockResultMatchersBuilders; +import com.ays.util.AysTestData; +import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.util.List; + +class UserSystemTest extends AbstractSystemTest { + + private final UserToUsersResponseMapper userToUsersResponseMapper = UserToUsersResponseMapper.initialize(); + private final UserToUserResponseMapper userToUserResponseMapper = UserToUserResponseMapper.initialize(); + private final UserEntityToUserMapper userEntityToUserMapper = UserEntityToUserMapper.initialize(); + + + private static final String BASE_PATH = "/api/v1"; + + @Test + void givenValidUserSaveRequest_whenUserSaved_thenReturnUserSavedResponse() throws Exception { + // Given + UserSaveRequest userSaveRequest = new UserSaveRequestBuilder() + .withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + UserSavedResponse userSavedResponse = new UserSavedResponseBuilder().build(); + AysResponse response = AysResponseBuilder.successOf(userSavedResponse); + mockMvc.perform(AysMockMvcRequestBuilders + .post(endpoint, adminUserTokenOne.getAccessToken(), userSaveRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.username") + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.password") + .isNotEmpty()); + } + + @Test + void givenValidUserSaveRequest_whenUserUnauthorizedForSaving_thenReturnAccessDeniedException() throws Exception { + // Given + UserSaveRequest userSaveRequest = new UserSaveRequestBuilder() + .withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, userTokenOne.getAccessToken(), userSaveRequest); + + AysResponse response = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + @Test + void givenPhoneNumberWithAlphanumericCharacter_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { + // Given + AysPhoneNumber phoneNumber = new AysPhoneNumberBuilder() + .withCountryCode("ABC") + .withLineNumber("ABC").build(); + UserSaveRequest userSaveRequest = new UserSaveRequestBuilder() + .withValidFields() + .withPhoneNumber(phoneNumber).build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + AysError errorResponse = AysErrorBuilder.VALIDATION_ERROR; + mockMvc.perform(AysMockMvcRequestBuilders + .post(endpoint, adminUserTokenOne.getAccessToken(), userSaveRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(errorResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.header() + .value(errorResponse.getHeader())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(errorResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + } + + @Test + void givenPhoneNumberWithInvalidLength_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { + // Given + AysPhoneNumber phoneNumber = new AysPhoneNumberBuilder() + .withCountryCode("456786745645") + .withLineNumber("6546467456435548676845321346656654").build(); + UserSaveRequest userSaveRequest = new UserSaveRequestBuilder() + .withValidFields() + .withPhoneNumber(phoneNumber).build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + AysError errorResponse = AysErrorBuilder.VALIDATION_ERROR; + mockMvc.perform(AysMockMvcRequestBuilders + .post(endpoint, adminUserTokenOne.getAccessToken(), userSaveRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(errorResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.header() + .value(errorResponse.getHeader())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(errorResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + } + + @Test + void givenValidUserListRequest_whenUsersFound_thenReturnUsersResponse() throws Exception { + // Given + UserListRequest userListRequest = new UserListRequestBuilder().withValidValues().build(); + + // When + Page userEntities = new PageImpl<>( + UserEntityBuilder.generateValidUserEntities(1) + ); + List users = userEntityToUserMapper.map(userEntities.getContent()); + AysPage pageOfUsers = AysPage.of(userEntities, users); + + // Then + String endpoint = BASE_PATH.concat("/users"); + List usersResponses = userToUsersResponseMapper.map(pageOfUsers.getContent()); + AysPageResponse pageOfUsersResponse = AysPageResponse.builder() + .of(pageOfUsers) + .content(usersResponses) + .build(); + AysResponse> response = AysResponse.successOf(pageOfUsersResponse); + mockMvc.perform(AysMockMvcRequestBuilders + .post(endpoint, adminUserTokenOne.getAccessToken(), userListRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + } + + @Test + void givenValidUserListRequest_whenUserUnauthorizedForListing_thenReturnAccessDeniedException() throws Exception { + // Given + UserListRequest userListRequest = new UserListRequestBuilder().withValidValues().build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, userTokenOne.getAccessToken(), userListRequest); + + AysResponse response = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + @Test + void givenValidUserId_whenUserFound_thenReturnUserResponse() throws Exception { + // Given + String userId = AysTestData.User.VALID_ID_ONE; + User user = new UserBuilder() + .withId(userId) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(userId); + UserResponse userResponse = userToUserResponseMapper.map(user); + AysResponse response = AysResponse.successOf(userResponse); + mockMvc.perform(AysMockMvcRequestBuilders + .get(endpoint, adminUserTokenOne.getAccessToken())) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(response.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(response.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + } + + @Test + void givenValidValidUserId_whenUserUnauthorizedForGetting_thenReturnAccessDeniedException() throws Exception { + // Given + String userId = AysTestData.User.VALID_ID_ONE; + + // Then + String endpoint = BASE_PATH.concat("/user/".concat(userId)); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, userTokenOne.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + + @Test + void givenValidUserIdAndUserUpdateRequest_whenUserUpdated_thenReturnAysResponseOfSuccess() throws Exception { + // Given + String userId = AysTestData.User.VALID_ID_TWO; + UserUpdateRequest mockUpdateRequest = new UserUpdateRequestBuilder() + .withRole(UserRole.VOLUNTEER) + .withStatus(UserStatus.PASSIVE).build(); + + // Then + String endpoint = BASE_PATH.concat("/user/".concat(userId)); + AysResponse mockAysResponse = AysResponse.SUCCESS; + mockMvc.perform(AysMockMvcRequestBuilders + .put(endpoint, adminUserTokenOne.getAccessToken(), mockUpdateRequest)) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockAysResponse.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockAysResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + @Test + void givenValidUserIdAndUserUpdateRequest_whenUserUnauthorizedForUpdating_thenReturnAccessDeniedException() throws Exception { + // Given + String userId = AysTestData.User.VALID_ID_TWO; + UserUpdateRequest mockUpdateRequest = new UserUpdateRequestBuilder() + .withRole(UserRole.VOLUNTEER) + .withStatus(UserStatus.PASSIVE).build(); + + // Then + String endpoint = BASE_PATH.concat("/user/".concat(userId)); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, userTokenTwo.getAccessToken(), mockUpdateRequest); + + AysResponse mockResponse = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + @Test + void givenValidUserId_whenUserDeleted_thenReturnAysResponseOfSuccess() throws Exception { + // Given + String userId = AysTestData.User.VALID_ID_THREE; + + // Then + String endpoint = BASE_PATH.concat("/user/".concat(userId)); + AysResponse mockAysResponse = AysResponse.SUCCESS; + mockMvc.perform(AysMockMvcRequestBuilders + .delete(endpoint, adminUserTokenTwo.getAccessToken())) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isOk()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockAysResponse.getHttpStatus().getReasonPhrase())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockAysResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } + + @Test + void givenValidValidUserId_whenUserUnauthorizedForDeleting_thenReturnAccessDeniedException() throws Exception { + // Given + String userId = AysTestData.User.VALID_ID_THREE; + + // Then + String endpoint = BASE_PATH.concat("/user/".concat(userId)); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, userTokenThree.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.FORBIDDEN; + mockMvc.perform(mockHttpServletRequestBuilder) + .andDo(MockMvcResultHandlers.print()) + .andExpect(AysMockResultMatchersBuilders.status().isForbidden()) + .andExpect(AysMockResultMatchersBuilders.time() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.httpStatus() + .value(mockResponse.getHttpStatus().name())) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(mockResponse.getIsSuccess())) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + } +} diff --git a/src/test/java/com/ays/user/model/UserBuilder.java b/src/test/java/com/ays/user/model/UserBuilder.java index 90f0f1e48..55fcbe488 100644 --- a/src/test/java/com/ays/user/model/UserBuilder.java +++ b/src/test/java/com/ays/user/model/UserBuilder.java @@ -9,6 +9,11 @@ public UserBuilder() { super(User.class); } + public UserBuilder withId(String id) { + data.setId(id); + return this; + } + public UserBuilder withUsername(String username) { data.setUsername(username); return this; diff --git a/src/test/java/com/ays/user/model/dto/request/AysUserLoginRequestBuilder.java b/src/test/java/com/ays/user/model/dto/request/AysUserLoginRequestBuilder.java deleted file mode 100644 index a2f7a69f0..000000000 --- a/src/test/java/com/ays/user/model/dto/request/AysUserLoginRequestBuilder.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.ays.user.model.dto.request; - - -import com.ays.auth.model.dto.request.AysLoginRequest; -import com.ays.common.model.TestDataBuilder; - -public class AysUserLoginRequestBuilder extends TestDataBuilder { - - public AysUserLoginRequestBuilder() { - super(AysLoginRequest.class); - } - -} diff --git a/src/test/java/com/ays/user/service/impl/UserAuthServiceImplTest.java b/src/test/java/com/ays/user/service/impl/UserAuthServiceImplTest.java index 154c32b9d..1446f3be5 100644 --- a/src/test/java/com/ays/user/service/impl/UserAuthServiceImplTest.java +++ b/src/test/java/com/ays/user/service/impl/UserAuthServiceImplTest.java @@ -8,10 +8,10 @@ import com.ays.auth.model.AysToken; import com.ays.auth.model.AysTokenBuilder; import com.ays.auth.model.dto.request.AysLoginRequest; +import com.ays.auth.model.dto.request.AysLoginRequestBuilder; import com.ays.auth.service.AysInvalidTokenService; import com.ays.auth.service.AysTokenService; import com.ays.auth.util.exception.*; -import com.ays.user.model.dto.request.AysUserLoginRequestBuilder; import com.ays.user.model.entity.UserEntity; import com.ays.user.model.entity.UserEntityBuilder; import com.ays.user.model.enums.UserStatus; @@ -50,7 +50,7 @@ class UserAuthServiceImplTest extends AbstractUnitTest { @Test void givenValidLoginRequest_whenUserAuthenticated_thenReturnAysToken() { // Given - AysLoginRequest mockLoginRequest = new AysUserLoginRequestBuilder().build(); + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder().build(); UserEntity mockUserEntity = new UserEntityBuilder() .withStatus(UserStatus.ACTIVE) @@ -84,7 +84,7 @@ void givenValidLoginRequest_whenUserAuthenticated_thenReturnAysToken() { @Test void givenInvalidLoginRequest_whenUserNotFound_thenThrowUsernameNotValidException() { // Given - AysLoginRequest mockLoginRequest = new AysUserLoginRequestBuilder().build(); + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder().build(); // When Mockito.when(userRepository.findByUsername(mockLoginRequest.getUsername())) @@ -103,7 +103,7 @@ void givenInvalidLoginRequest_whenUserNotFound_thenThrowUsernameNotValidExceptio @Test void givenInvalidLoginRequest_whenUserNotAuthenticated_thenThrowPasswordNotValidException() { // Given - AysLoginRequest mockLoginRequest = new AysUserLoginRequestBuilder().build(); + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder().build(); UserEntity mockUserEntity = new UserEntityBuilder() .withStatus(UserStatus.ACTIVE) @@ -132,7 +132,7 @@ void givenInvalidLoginRequest_whenUserNotAuthenticated_thenThrowPasswordNotValid @Test void givenValidLoginRequest_whenUserNotActive_thenThrowUserNotActiveException() { // Given - AysLoginRequest mockLoginRequest = new AysUserLoginRequestBuilder().build(); + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder().build(); UserEntity mockUserEntity = new UserEntityBuilder() .withStatus(UserStatus.PASSIVE) diff --git a/src/test/java/com/ays/util/AysTestData.java b/src/test/java/com/ays/util/AysTestData.java index 44eed4d2e..1dd56c2f4 100644 --- a/src/test/java/com/ays/util/AysTestData.java +++ b/src/test/java/com/ays/util/AysTestData.java @@ -6,5 +6,38 @@ public class AysTestData { public static final String VALID_EMAIL = "test@ays.com"; + public static final String VALID_VERIFICATION_ID = "3cfeb994-88f4-48d0-b105-309f42f91412"; + + public static class AdminUser { + public static final String VALID_ID_ONE = "9262f0fc-93db-4f7e-81c6-aaad85c2b206"; + public static final String VALID_USERNAME_ONE = "ays-admin-1"; + public static final String VALID_PASSWORD_ONE = "A123y456S."; + public static final String VALID_EMAIL_ONE = "admin1@ays.com"; + + public static final String VALID_ID_TWO = "87ab173b-1b7d-4264-abd7-438e5ed2c1fa"; + public static final String VALID_USERNAME_TWO = "ays-admin-2"; + public static final String VALID_PASSWORD_TWO = "A123y456S."; + public static final String VALID_EMAIL_TWO = "admin2@ays.com"; + } + + public static class User { + public static final String VALID_ID_ONE = "c4b4e4db-5641-41f7-8222-a76deb1c065c"; + public static final String VALID_USERNAME_ONE = "232180"; + public static final String VALID_PASSWORD_ONE = "367894"; + + public static final String VALID_ID_TWO = "0c8034d2-f91b-4ef4-bf6c-bdbeda945764"; + public static final String VALID_USERNAME_TWO = "930047"; + public static final String VALID_PASSWORD_TWO = "367894"; + + public static final String VALID_ID_THREE = "edb36891-b898-4c12-bcec-d9aaa5d45190"; + public static final String VALID_USERNAME_THREE = "648193"; + public static final String VALID_PASSWORD_THREE = "367894"; + } + + public static class Institution { + public static final String VALID_ID_ONE = "77ece256-bf0e-4bbe-801d-173083f8bdcf"; + + public static final String VALID_ID_TWO = "91df7ae9-d5b9-44ae-b54f-d5d55359c4a4"; + } }