diff --git a/src/main/java/org/ays/admin_user/model/dto/request/AdminUserRegisterApplicationListRequest.java b/src/main/java/org/ays/admin_user/model/dto/request/AdminUserRegisterApplicationListRequest.java index d175612af..df60e8167 100644 --- a/src/main/java/org/ays/admin_user/model/dto/request/AdminUserRegisterApplicationListRequest.java +++ b/src/main/java/org/ays/admin_user/model/dto/request/AdminUserRegisterApplicationListRequest.java @@ -3,6 +3,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.validation.Valid; import jakarta.validation.constraints.AssertTrue; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import org.apache.commons.collections4.CollectionUtils; @@ -21,6 +22,7 @@ */ @Getter @Setter +@EqualsAndHashCode(callSuper = true) public class AdminUserRegisterApplicationListRequest extends AysPagingRequest implements AysFilteringRequest { @Valid diff --git a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotActiveException.java b/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotActiveException.java deleted file mode 100644 index 2baccd77d..000000000 --- a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotActiveException.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.ays.admin_user.util.exception; - -import java.io.Serial; - -/** - * Exception indicating that an admin user is not active and cannot perform the requested action. - * This exception is a subclass of RuntimeException, which means it is an unchecked exception and does not need to - * be declared in a throws clause or caught explicitly. - * Typically, this exception is thrown when an admin user tries to perform an action that is only allowed for active - * users, but the user is currently inactive. - */ -public class AysAdminUserNotActiveException extends RuntimeException { - - /** - * Unique identifier for serialization. - */ - @Serial - private static final long serialVersionUID = 1008678165854009528L; - - /** - * Constructs a new AysAdminUserNotActiveException with the specified username. - * - * @param username The username of the inactive admin user. - */ - public AysAdminUserNotActiveException(String username) { - super("USER IS NOT ACTIVE! username:" + username); - } - -} diff --git a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotExistByIdException.java b/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotExistByIdException.java deleted file mode 100644 index b7aa3b4c1..000000000 --- a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotExistByIdException.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.ays.admin_user.util.exception; - -import org.ays.common.util.exception.AysNotExistException; - -import java.io.Serial; - -/** - * Exception indicating that an admin user does not exist with the specified ID. - * This exception is a subclass of AysNotExistException, which is typically used to indicate that an entity or - * resource does not exist. - * Typically, this exception is thrown when an operation or query is performed on an admin user entity using an ID - * that does not correspond to an existing admin user. - */ -public class AysAdminUserNotExistByIdException extends AysNotExistException { - - /** - * Unique identifier for serialization. - */ - @Serial - private static final long serialVersionUID = -4832610600125442954L; - - /** - * Constructs a new AysAdminUserNotExistByIdException with the specified ID. - * - * @param id The ID of the admin user that does not exist. - */ - public AysAdminUserNotExistByIdException(String id) { - super("ADMIN USER NOT EXIST! id:" + id); - } - -} diff --git a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotExistByUsernameException.java b/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotExistByUsernameException.java deleted file mode 100644 index 30cea9973..000000000 --- a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotExistByUsernameException.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.ays.admin_user.util.exception; - -import org.ays.common.util.exception.AysNotExistException; - -import java.io.Serial; - -/** - * Exception indicating that an admin user does not exist with the specified username. - * This exception is a subclass of AysNotExistException, which is typically used to indicate that an entity or - * resource does not exist. - * Typically, this exception is thrown when an operation or query is performed on an admin user entity using a - * username that does not correspond to an existing admin user. - */ -public class AysAdminUserNotExistByUsernameException extends AysNotExistException { - - /** - * Unique identifier for serialization. - */ - @Serial - private static final long serialVersionUID = -3596142609379071521L; - - /** - * Constructs a new AysAdminUserNotExistByUsernameException with the specified username. - * - * @param username The username of the admin user that does not exist. - */ - public AysAdminUserNotExistByUsernameException(String username) { - super("ADMIN USER NOT EXIST! username:" + username); - } - -} diff --git a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotVerifiedException.java b/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotVerifiedException.java deleted file mode 100644 index f84284122..000000000 --- a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserNotVerifiedException.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.ays.admin_user.util.exception; - -import java.io.Serial; - -/** - * Exception indicating that an admin user is not verified. - * This exception is a subclass of RuntimeException, which is typically used to indicate that an unexpected error - * has occurred. - * Typically, this exception is thrown when an operation or query is performed on an admin user entity using a - * username that corresponds to an existing admin user, but the user has not been verified or authenticated. - */ -public class AysAdminUserNotVerifiedException extends RuntimeException { - - /** - * Unique identifier for serialization. - */ - @Serial - private static final long serialVersionUID = 7664416170459803790L; - - /** - * Constructs a new AysAdminUserNotVerifiedException with the specified username. - * - * @param username The username of the admin user that is not verified. - */ - public AysAdminUserNotVerifiedException(String username) { - super("ADMIN USER IS NOT VERIFIED! username:" + username); - } - -} diff --git a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserRegisterApplicationCodeNotValidException.java b/src/main/java/org/ays/admin_user/util/exception/AysAdminUserRegisterApplicationCodeNotValidException.java deleted file mode 100644 index f7bdb77a3..000000000 --- a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserRegisterApplicationCodeNotValidException.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.ays.admin_user.util.exception; - -import org.ays.common.util.exception.AysNotExistException; - -import java.io.Serial; - -/** - * Exception indicating that the verification code provided during registration for an admin user is not valid. - * This exception is a subclass of AysNotExistException, which is typically used to indicate that an entity or - * resource does not exist. - * Typically, this exception is thrown when a verification code provided during the registration process of an admin - * user is not valid, meaning that it may have already been used or expired. - */ -public class AysAdminUserRegisterApplicationCodeNotValidException extends AysNotExistException { - - /** - * Unique identifier for serialization. - */ - @Serial - private static final long serialVersionUID = -2140764454266823885L; - - /** - * Constructs a new AysAdminUserRegisterVerificationCodeNotValidException with the specified verification ID. - * - * @param verificationId The verification ID that is not valid. - */ - public AysAdminUserRegisterApplicationCodeNotValidException(String verificationId) { - super("VERIFICATION ID IS NOT VALID! verificationId:" + verificationId); - } - -} diff --git a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserRegisterApplicationSummaryNotExistByIdException.java b/src/main/java/org/ays/admin_user/util/exception/AysAdminUserRegisterApplicationSummaryNotExistByIdException.java index a4a4f3b2e..d9f41731a 100644 --- a/src/main/java/org/ays/admin_user/util/exception/AysAdminUserRegisterApplicationSummaryNotExistByIdException.java +++ b/src/main/java/org/ays/admin_user/util/exception/AysAdminUserRegisterApplicationSummaryNotExistByIdException.java @@ -17,7 +17,7 @@ public class AysAdminUserRegisterApplicationSummaryNotExistByIdException extends * Unique identifier for serialization. */ @Serial - private static final long serialVersionUID = 8416712253227498925L; + private static final long serialVersionUID = 1262328240991745084L; /** * Constructs a new AysAdminUserRegisterApplicationSummaryNotExistByIdException with the specified ID. diff --git a/src/main/java/org/ays/auth/config/SecurityConfiguration.java b/src/main/java/org/ays/auth/config/SecurityConfiguration.java index 2e220fb30..5140d5228 100644 --- a/src/main/java/org/ays/auth/config/SecurityConfiguration.java +++ b/src/main/java/org/ays/auth/config/SecurityConfiguration.java @@ -50,7 +50,7 @@ class SecurityConfiguration { * @return the new instance of {@link SessionAuthenticationStrategy} */ @Bean - protected SessionAuthenticationStrategy sessionAuthenticationStrategy() { + SessionAuthenticationStrategy sessionAuthenticationStrategy() { return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl()); } @@ -64,9 +64,9 @@ protected SessionAuthenticationStrategy sessionAuthenticationStrategy() { * @throws Exception if there is an error setting up the filter chain */ @Bean - public SecurityFilterChain filterChain(HttpSecurity httpSecurity, - AysBearerTokenAuthenticationFilter bearerTokenAuthenticationFilter, - CustomAuthenticationEntryPoint customAuthenticationEntryPoint) + SecurityFilterChain filterChain(HttpSecurity httpSecurity, + AysBearerTokenAuthenticationFilter bearerTokenAuthenticationFilter, + CustomAuthenticationEntryPoint customAuthenticationEntryPoint) throws Exception { httpSecurity @@ -109,7 +109,8 @@ private CorsConfigurationSource corsConfigurationSource() { * @return the new instance of {@link PasswordEncoder} */ @Bean - public PasswordEncoder passwordEncoder() { + PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } + } diff --git a/src/main/java/org/ays/auth/service/impl/AysTokenServiceImpl.java b/src/main/java/org/ays/auth/service/impl/AysTokenServiceImpl.java index c38004b57..6cfeece26 100644 --- a/src/main/java/org/ays/auth/service/impl/AysTokenServiceImpl.java +++ b/src/main/java/org/ays/auth/service/impl/AysTokenServiceImpl.java @@ -48,34 +48,26 @@ class AysTokenServiceImpl implements AysTokenService { */ @Override public AysToken generate(final Map claims) { + final long currentTimeMillis = System.currentTimeMillis(); - final Date tokenIssuedAt = new Date(currentTimeMillis); + final JwtBuilder tokenBuilder = this.initializeTokenBuilder(currentTimeMillis); - final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute()); - final String accessToken = Jwts.builder() - .header() - .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) - .and() + final Date accessTokenExpiresAt = DateUtils.addMinutes( + new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute() + ); + final String accessToken = tokenBuilder .id(AysRandomUtil.generateUUID()) - .issuer(tokenConfiguration.getIssuer()) - .issuedAt(tokenIssuedAt) .expiration(accessTokenExpiresAt) - .signWith(tokenConfiguration.getPrivateKey()) .claims(claims) .compact(); - final Date refreshTokenExpiresAt = DateUtils.addDays(new Date(currentTimeMillis), tokenConfiguration.getRefreshTokenExpireDay()); - final JwtBuilder refreshTokenBuilder = Jwts.builder(); - final String refreshToken = refreshTokenBuilder - .header() - .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) - .and() + final Date refreshTokenExpiresAt = DateUtils.addDays( + new Date(currentTimeMillis), tokenConfiguration.getRefreshTokenExpireDay() + ); + final String refreshToken = tokenBuilder .id(AysRandomUtil.generateUUID()) - .issuer(tokenConfiguration.getIssuer()) - .issuedAt(tokenIssuedAt) .expiration(refreshTokenExpiresAt) - .signWith(tokenConfiguration.getPrivateKey()) .claim(AysTokenClaims.USER_ID.getValue(), claims.get(AysTokenClaims.USER_ID.getValue())) .compact(); @@ -86,6 +78,7 @@ public AysToken generate(final Map claims) { .build(); } + /** * Generates an access token based on the provided claims and refresh token. * @@ -97,18 +90,13 @@ public AysToken generate(final Map claims) { public AysToken generate(final Map claims, final String refreshToken) { final long currentTimeMillis = System.currentTimeMillis(); - final Date accessTokenIssuedAt = new Date(currentTimeMillis); - final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute()); - final String accessToken = Jwts.builder() - .header() - .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) - .and() + final Date accessTokenExpiresAt = DateUtils.addMinutes( + new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute() + ); + final String accessToken = this.initializeTokenBuilder(currentTimeMillis) .id(AysRandomUtil.generateUUID()) - .issuer(tokenConfiguration.getIssuer()) - .issuedAt(accessTokenIssuedAt) .expiration(accessTokenExpiresAt) - .signWith(tokenConfiguration.getPrivateKey()) .claims(claims) .compact(); @@ -119,6 +107,29 @@ public AysToken generate(final Map claims, final String refreshT .build(); } + /** + * Initializes a JwtBuilder for creating a JSON Web Token (JWT) with the specified current time. + * + * @param currentTimeMillis The current time in milliseconds to be used as the "issued at" claim. + * @return JwtBuilder instance configured with default and provided settings. + *

+ * The JWT will have the following claims set: + * - Header with the token type set to Bearer. + * - Issuer claim set to the configured issuer from the token configuration. + * - Issued At (iat) claim set to the specified current time. + * - Signature configured with the private key from the token configuration. + */ + private JwtBuilder initializeTokenBuilder(long currentTimeMillis) { + return Jwts.builder() + .header() + .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) + .and() + .issuer(tokenConfiguration.getIssuer()) + .issuedAt(new Date(currentTimeMillis)) + .signWith(tokenConfiguration.getPrivateKey()); + } + + /** * Verifies and validates the given JWT (JSON Web Token). * This method parses the token using the public key from the {@link AysTokenConfigurationParameter}, diff --git a/src/main/java/org/ays/common/util/exception/handler/GlobalExceptionHandler.java b/src/main/java/org/ays/common/util/exception/handler/GlobalExceptionHandler.java index 996d0f232..bc25e51da 100644 --- a/src/main/java/org/ays/common/util/exception/handler/GlobalExceptionHandler.java +++ b/src/main/java/org/ays/common/util/exception/handler/GlobalExceptionHandler.java @@ -29,7 +29,7 @@ class GlobalExceptionHandler { @ExceptionHandler(HttpMessageNotReadableException.class) - protected ResponseEntity handleJsonParseErrors(final HttpMessageNotReadableException exception) { + ResponseEntity handleJsonParseErrors(final HttpMessageNotReadableException exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.builder() @@ -40,7 +40,7 @@ protected ResponseEntity handleJsonParseErrors(final HttpMessageNotReada } @ExceptionHandler(MethodArgumentTypeMismatchException.class) - protected ResponseEntity handleValidationErrors(final MethodArgumentTypeMismatchException exception) { + ResponseEntity handleValidationErrors(final MethodArgumentTypeMismatchException exception) { log.error(exception.getMessage(), exception); @@ -52,7 +52,7 @@ protected ResponseEntity handleValidationErrors(final MethodArgumentType } @ExceptionHandler(MethodArgumentNotValidException.class) - protected ResponseEntity handleValidationErrors(final MethodArgumentNotValidException exception) { + ResponseEntity handleValidationErrors(final MethodArgumentNotValidException exception) { log.error(exception.getMessage(), exception); @@ -64,7 +64,7 @@ protected ResponseEntity handleValidationErrors(final MethodArgumentNotV } @ExceptionHandler(ConstraintViolationException.class) - protected ResponseEntity handlePathVariableErrors(final ConstraintViolationException exception) { + ResponseEntity handlePathVariableErrors(final ConstraintViolationException exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.subErrors(exception.getConstraintViolations()) @@ -75,7 +75,7 @@ protected ResponseEntity handlePathVariableErrors(final ConstraintViolat } @ExceptionHandler(AysNotExistException.class) - protected ResponseEntity handleNotExistError(final AysNotExistException exception) { + ResponseEntity handleNotExistError(final AysNotExistException exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.builder() @@ -87,7 +87,7 @@ protected ResponseEntity handleNotExistError(final AysNotExistException } @ExceptionHandler(AysAlreadyException.class) - protected ResponseEntity handleAlreadyExistError(final AysAlreadyException exception) { + ResponseEntity handleAlreadyExistError(final AysAlreadyException exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.builder() @@ -99,7 +99,7 @@ protected ResponseEntity handleAlreadyExistError(final AysAlreadyExcepti } @ExceptionHandler(AysProcessException.class) - protected ResponseEntity handleProcessError(final AysProcessException exception) { + ResponseEntity handleProcessError(final AysProcessException exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.builder() @@ -111,7 +111,7 @@ protected ResponseEntity handleProcessError(final AysProcessException ex } @ExceptionHandler(Exception.class) - protected ResponseEntity handleProcessError(final Exception exception) { + ResponseEntity handleProcessError(final Exception exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.builder() @@ -122,7 +122,7 @@ protected ResponseEntity handleProcessError(final Exception exception) { } @ExceptionHandler(AysAuthException.class) - protected ResponseEntity handleAuthError(final AysAuthException exception) { + ResponseEntity handleAuthError(final AysAuthException exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.builder() @@ -133,7 +133,7 @@ protected ResponseEntity handleAuthError(final AysAuthException exceptio } @ExceptionHandler(AccessDeniedException.class) - protected ResponseEntity handleAccessDeniedError(final AccessDeniedException exception) { + ResponseEntity handleAccessDeniedError(final AccessDeniedException exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.builder() @@ -144,7 +144,7 @@ protected ResponseEntity handleAccessDeniedError(final AccessDeniedExcep } @ExceptionHandler(SQLException.class) - protected ResponseEntity handleSQLError(final SQLException exception) { + ResponseEntity handleSQLError(final SQLException exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.builder() @@ -155,7 +155,7 @@ protected ResponseEntity handleSQLError(final SQLException exception) { } @ExceptionHandler(HttpRequestMethodNotSupportedException.class) - protected ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) { + ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) { log.error(exception.getMessage(), exception); AysError aysError = AysError.builder() @@ -167,7 +167,7 @@ protected ResponseEntity handleHttpRequestMethodNotSupportedException(Ht } @ExceptionHandler(HttpMediaTypeNotSupportedException.class) - protected ResponseEntity handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException exception) { + ResponseEntity handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException exception) { log.error(exception.getMessage(), exception); @@ -180,7 +180,7 @@ protected ResponseEntity handleHttpMediaTypeNotSupportedException(HttpMe } @ExceptionHandler(DataAccessException.class) - protected ResponseEntity handleDataAccessException(DataAccessException exception) { + ResponseEntity handleDataAccessException(DataAccessException exception) { log.error(exception.getMessage(), exception); diff --git a/src/main/java/org/ays/user/util/exception/AysUserCannotUpdateSupportStatusException.java b/src/main/java/org/ays/user/util/exception/AysUserCannotUpdateSupportStatusException.java index 29e7bf80d..3a4b82da6 100644 --- a/src/main/java/org/ays/user/util/exception/AysUserCannotUpdateSupportStatusException.java +++ b/src/main/java/org/ays/user/util/exception/AysUserCannotUpdateSupportStatusException.java @@ -13,7 +13,7 @@ public class AysUserCannotUpdateSupportStatusException extends AysAlreadyExcepti * Unique serial version ID. */ @Serial - private static final long serialVersionUID = -3686691276790127586L; + private static final long serialVersionUID = -4727384445754973709L; /** * Constructs a new {@code AysUserCannotUpdateSupportStatusException} with the specified id and assignmentId. diff --git a/src/main/java/org/ays/user/util/exception/AysUserNotActiveException.java b/src/main/java/org/ays/user/util/exception/AysUserNotActiveException.java deleted file mode 100644 index 16f0cdcec..000000000 --- a/src/main/java/org/ays/user/util/exception/AysUserNotActiveException.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.ays.user.util.exception; - -import org.ays.common.util.exception.AysNotActiveException; - -import java.io.Serial; - -/** - * Exception thrown when a user is not active and attempting to perform an action that requires an active user. - */ -public class AysUserNotActiveException extends AysNotActiveException { - - /** - * Unique serial version ID. - */ - @Serial - private static final long serialVersionUID = -6280453284388968774L; - - /** - * Constructs a new {@code AysUserNotActiveException} with the specified username. - * - * @param username the username of the inactive user - */ - public AysUserNotActiveException(String username) { - super("USER IS NOT ACTIVE! username:" + username); - } - -} diff --git a/src/main/java/org/ays/user/util/exception/AysUserNotExistByUsernameException.java b/src/main/java/org/ays/user/util/exception/AysUserNotExistByUsernameException.java deleted file mode 100644 index 38a3a1fef..000000000 --- a/src/main/java/org/ays/user/util/exception/AysUserNotExistByUsernameException.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.ays.user.util.exception; - -import org.ays.common.util.exception.AysNotExistException; - -import java.io.Serial; - -/** - * An exception thrown when attempting to perform an action on a user that is not active. - */ -public class AysUserNotExistByUsernameException extends AysNotExistException { - - /** - * Unique serial version ID. - */ - @Serial - private static final long serialVersionUID = 5558109482647165290L; - - /** - * Constructs a new {@code AysUserNotActiveException} with the specified username. - * - * @param username the username of the inactive user. - */ - public AysUserNotExistByUsernameException(String username) { - super("USER NOT EXIST! username:{}" + username); - } - -} diff --git a/src/test/java/org/ays/AbstractJpaIntegrationTest.java b/src/test/java/org/ays/AbstractJpaIntegrationTest.java deleted file mode 100644 index 73988e5a1..000000000 --- a/src/test/java/org/ays/AbstractJpaIntegrationTest.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.ays; - -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -public abstract class AbstractJpaIntegrationTest extends AbstractTestContainerConfiguration { -} diff --git a/src/test/java/org/ays/AbstractRestControllerTest.java b/src/test/java/org/ays/AbstractRestControllerTest.java index bb30abd22..85490e832 100644 --- a/src/test/java/org/ays/AbstractRestControllerTest.java +++ b/src/test/java/org/ays/AbstractRestControllerTest.java @@ -4,6 +4,7 @@ import io.jsonwebtoken.Jwts; import org.apache.commons.lang3.time.DateUtils; import org.ays.admin_user.model.entity.AdminUserEntityBuilder; +import org.ays.admin_user.model.enums.AdminRole; import org.ays.auth.config.AysTokenConfigurationParameter; import org.ays.auth.model.AysToken; import org.ays.auth.model.enums.AysTokenClaims; @@ -11,7 +12,6 @@ import org.ays.parameter.model.AysParameter; import org.ays.parameter.model.AysParameterBuilder; import org.ays.parameter.service.AysParameterService; -import org.ays.super_admin.entity.SuperAdminEntityBuilder; import org.ays.user.model.entity.UserEntityBuilder; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; @@ -52,8 +52,8 @@ public void initializeAuth() { .thenReturn(parameters); this.tokenConfiguration = new AysTokenConfigurationParameter(parameterService); - this.mockSuperAdminToken = this.generate(new SuperAdminEntityBuilder().withValidFields().build().getClaims()); - this.mockAdminUserToken = this.generate(new AdminUserEntityBuilder().build().getClaims()); + this.mockSuperAdminToken = this.generate(new AdminUserEntityBuilder().withValidFields().withRole(AdminRole.SUPER_ADMIN).withInstitutionId(null).build().getClaims()); + this.mockAdminUserToken = this.generate(new AdminUserEntityBuilder().withRole(AdminRole.ADMIN).build().getClaims()); this.mockUserToken = this.generate(new UserEntityBuilder().build().getClaims()); } diff --git a/src/test/java/org/ays/AbstractSystemTest.java b/src/test/java/org/ays/AbstractSystemTest.java index 4e3f22512..5ed9fa7ae 100644 --- a/src/test/java/org/ays/AbstractSystemTest.java +++ b/src/test/java/org/ays/AbstractSystemTest.java @@ -4,6 +4,7 @@ import io.jsonwebtoken.Jwts; import org.apache.commons.lang3.time.DateUtils; import org.ays.admin_user.model.entity.AdminUserEntityBuilder; +import org.ays.admin_user.model.enums.AdminRole; import org.ays.admin_user.repository.AdminUserRegisterApplicationRepository; import org.ays.admin_user.repository.AdminUserRepository; import org.ays.assignment.repository.AssignmentRepository; @@ -13,7 +14,6 @@ import org.ays.auth.repository.AysInvalidTokenRepository; import org.ays.common.util.AysRandomUtil; import org.ays.institution.repository.InstitutionRepository; -import org.ays.super_admin.entity.SuperAdminEntityBuilder; import org.ays.user.model.entity.UserEntityBuilder; import org.ays.user.repository.UserRepository; import org.ays.util.AysValidTestData; @@ -65,9 +65,10 @@ public abstract class AbstractSystemTest extends AbstractTestContainerConfigurat @BeforeEach protected void setUp() { - final Map claimsOfSuperAdmin = new SuperAdminEntityBuilder() + final Map claimsOfSuperAdmin = new AdminUserEntityBuilder() .withId(AysValidTestData.SuperAdminUser.ID) .withUsername(AysValidTestData.SuperAdminUser.USERNAME) + .withRole(AdminRole.SUPER_ADMIN) .withInstitutionId(null) .build() .getClaims(); diff --git a/src/test/java/org/ays/admin_user/model/entity/AdminUserEntityBuilder.java b/src/test/java/org/ays/admin_user/model/entity/AdminUserEntityBuilder.java index f2544945c..6ac9aaa35 100644 --- a/src/test/java/org/ays/admin_user/model/entity/AdminUserEntityBuilder.java +++ b/src/test/java/org/ays/admin_user/model/entity/AdminUserEntityBuilder.java @@ -1,5 +1,6 @@ package org.ays.admin_user.model.entity; +import org.ays.admin_user.model.enums.AdminRole; import org.ays.admin_user.model.enums.AdminUserStatus; import org.ays.common.model.AysPhoneNumber; import org.ays.common.model.AysPhoneNumberBuilder; @@ -72,6 +73,11 @@ public AdminUserEntityBuilder withStatus(AdminUserStatus status) { return this; } + public AdminUserEntityBuilder withRole(AdminRole adminRole) { + data.setRole(adminRole); + return this; + } + public AdminUserEntityBuilder withInstitutionId(String institutionId) { data.setInstitutionId(institutionId); return this; diff --git a/src/test/java/org/ays/auth/service/impl/AysTokenServiceImplTest.java b/src/test/java/org/ays/auth/service/impl/AysTokenServiceImplTest.java index 7bb9630f6..ed17ada00 100644 --- a/src/test/java/org/ays/auth/service/impl/AysTokenServiceImplTest.java +++ b/src/test/java/org/ays/auth/service/impl/AysTokenServiceImplTest.java @@ -64,10 +64,10 @@ void givenValidAdminUserClaims_whenTokensGenerated_thenReturnAysToken() { Assertions.assertNotNull(aysToken.getRefreshToken()); // Verify - Mockito.verify(tokenConfiguration, Mockito.times(2)).getIssuer(); + Mockito.verify(tokenConfiguration, Mockito.times(1)).getIssuer(); Mockito.verify(tokenConfiguration, Mockito.times(1)).getAccessTokenExpireMinute(); Mockito.verify(tokenConfiguration, Mockito.times(1)).getRefreshTokenExpireDay(); - Mockito.verify(tokenConfiguration, Mockito.times(2)).getPrivateKey(); + Mockito.verify(tokenConfiguration, Mockito.times(1)).getPrivateKey(); Mockito.verify(tokenConfiguration, Mockito.times(0)).getPublicKey(); Mockito.verifyNoMoreInteractions(tokenConfiguration); } @@ -93,10 +93,10 @@ void givenValidUserClaims_whenTokensGenerated_thenReturnAysToken() { Assertions.assertNotNull(aysToken.getRefreshToken()); // Verify - Mockito.verify(tokenConfiguration, Mockito.times(2)).getIssuer(); + Mockito.verify(tokenConfiguration, Mockito.times(1)).getIssuer(); Mockito.verify(tokenConfiguration, Mockito.times(1)).getAccessTokenExpireMinute(); Mockito.verify(tokenConfiguration, Mockito.times(1)).getRefreshTokenExpireDay(); - Mockito.verify(tokenConfiguration, Mockito.times(2)).getPrivateKey(); + Mockito.verify(tokenConfiguration, Mockito.times(1)).getPrivateKey(); Mockito.verify(tokenConfiguration, Mockito.times(0)).getPublicKey(); Mockito.verifyNoMoreInteractions(tokenConfiguration); } diff --git a/src/test/java/org/ays/common/util/exception/model/AysErrorBuilder.java b/src/test/java/org/ays/common/util/exception/model/AysErrorBuilder.java index bf7944232..18ee78f38 100644 --- a/src/test/java/org/ays/common/util/exception/model/AysErrorBuilder.java +++ b/src/test/java/org/ays/common/util/exception/model/AysErrorBuilder.java @@ -9,11 +9,6 @@ public class AysErrorBuilder { .header(AysError.Header.VALIDATION_ERROR.getName()) .isSuccess(false).build(); - public static final AysError UNAUTHORIZED = AysError.builder() - .httpStatus(HttpStatus.UNAUTHORIZED) - .header(AysError.Header.AUTH_ERROR.getName()) - .isSuccess(false).build(); - public static final AysError FORBIDDEN = AysError.builder() .httpStatus(HttpStatus.FORBIDDEN) .header(AysError.Header.AUTH_ERROR.getName()) diff --git a/src/test/java/org/ays/super_admin/entity/SuperAdminEntityBuilder.java b/src/test/java/org/ays/super_admin/entity/SuperAdminEntityBuilder.java deleted file mode 100644 index d89a4d7f0..000000000 --- a/src/test/java/org/ays/super_admin/entity/SuperAdminEntityBuilder.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.ays.super_admin.entity; - -import org.ays.admin_user.model.entity.AdminUserEntity; -import org.ays.admin_user.model.enums.AdminRole; -import org.ays.admin_user.model.enums.AdminUserStatus; -import org.ays.common.model.AysPhoneNumber; -import org.ays.common.model.AysPhoneNumberBuilder; -import org.ays.common.model.TestDataBuilder; -import org.ays.common.util.AysRandomUtil; - -public class SuperAdminEntityBuilder extends TestDataBuilder { - - public SuperAdminEntityBuilder() { - super(AdminUserEntity.class); - } - - public SuperAdminEntityBuilder withValidFields() { - return this - .withId(AysRandomUtil.generateUUID()) - .withUsername(String.valueOf(AysRandomUtil.generateNumber(6))) - .withPassword("$2a$10$16pFBczPxydfiRS4whgKfOCxq58L.bB6.i2abkZKR4fpNleQ4SmDy") - .withPhoneNumber(new AysPhoneNumberBuilder().withValidFields().build()) - .withStatus(AdminUserStatus.ACTIVE) - .withInstitutionId(null) - .withRole(); - } - - public SuperAdminEntityBuilder withId(String id) { - data.setId(id); - return this; - } - - public SuperAdminEntityBuilder withUsername(String username) { - data.setUsername(username); - return this; - } - - public SuperAdminEntityBuilder withPassword(String password) { - data.setPassword(password); - return this; - } - - public SuperAdminEntityBuilder withPhoneNumber(AysPhoneNumber phoneNumber) { - data.setCountryCode(phoneNumber.getCountryCode()); - data.setLineNumber(phoneNumber.getLineNumber()); - return this; - } - - public SuperAdminEntityBuilder withStatus(AdminUserStatus status) { - data.setStatus(status); - return this; - } - - public SuperAdminEntityBuilder withRole() { - data.setRole(AdminRole.SUPER_ADMIN); - return this; - } - - public SuperAdminEntityBuilder withInstitutionId(String institutionId) { - data.setInstitutionId(institutionId); - return this; - } - -} - diff --git a/src/test/java/org/ays/util/AysValidTestData.java b/src/test/java/org/ays/util/AysValidTestData.java index 73f47f67f..e8e4eb0e2 100644 --- a/src/test/java/org/ays/util/AysValidTestData.java +++ b/src/test/java/org/ays/util/AysValidTestData.java @@ -6,8 +6,6 @@ public class AysValidTestData { public static final String EMAIL = "test@ays.com"; - public static final String APPLICATION_ID = "3cfeb994-88f4-48d0-b105-309f42f91412"; - public static class SuperAdminUser { public static final String ID = "ea29a0bd-ad5d-4068-88c3-a8835b8d555e";