diff --git a/src/main/java/org/runimo/runimo/auth/test/TestAuthController.java b/src/main/java/org/runimo/runimo/auth/test/TestAuthController.java new file mode 100644 index 00000000..cad8a5d9 --- /dev/null +++ b/src/main/java/org/runimo/runimo/auth/test/TestAuthController.java @@ -0,0 +1,46 @@ +package org.runimo.runimo.auth.test; + +import lombok.RequiredArgsConstructor; +import org.runimo.runimo.common.response.SuccessResponse; +import org.runimo.runimo.user.enums.UserHttpResponseCode; +import org.springframework.context.annotation.Profile; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Profile({"test", "dev"}) +@RestController +@RequestMapping("/api/v1/auth/test") +@RequiredArgsConstructor +public class TestAuthController { + + private final TestAuthService testAuthService; + + @PostMapping("/login") + public ResponseEntity> login( + @RequestBody TestAuthRequest request + ) { + TestAuthResponse response = testAuthService.login(request.userId()); + + return ResponseEntity.ok( + SuccessResponse.of( + UserHttpResponseCode.LOGIN_SUCCESS, + response + ) + ); + } + + @PostMapping("/signup") + public ResponseEntity> signUp() { + TestAuthResponse response = testAuthService.signUp(); + return ResponseEntity.ok( + SuccessResponse.of( + UserHttpResponseCode.SIGNUP_SUCCESS, + response + ) + ); + } + +} diff --git a/src/main/java/org/runimo/runimo/auth/test/TestAuthRequest.java b/src/main/java/org/runimo/runimo/auth/test/TestAuthRequest.java new file mode 100644 index 00000000..9403cec9 --- /dev/null +++ b/src/main/java/org/runimo/runimo/auth/test/TestAuthRequest.java @@ -0,0 +1,7 @@ +package org.runimo.runimo.auth.test; + +public record TestAuthRequest( + Long userId +) { + +} diff --git a/src/main/java/org/runimo/runimo/auth/test/TestAuthResponse.java b/src/main/java/org/runimo/runimo/auth/test/TestAuthResponse.java new file mode 100644 index 00000000..bc8ba55c --- /dev/null +++ b/src/main/java/org/runimo/runimo/auth/test/TestAuthResponse.java @@ -0,0 +1,11 @@ +package org.runimo.runimo.auth.test; + +import io.swagger.v3.oas.annotations.media.Schema; +import org.runimo.runimo.auth.service.dto.TokenPair; + +@Schema(description = "테스트용 인증 응답 DTO") +public record TestAuthResponse( + TokenPair tokens +) { + +} diff --git a/src/main/java/org/runimo/runimo/auth/test/TestAuthService.java b/src/main/java/org/runimo/runimo/auth/test/TestAuthService.java new file mode 100644 index 00000000..fbc5bde4 --- /dev/null +++ b/src/main/java/org/runimo/runimo/auth/test/TestAuthService.java @@ -0,0 +1,53 @@ +package org.runimo.runimo.auth.test; + +import java.util.UUID; +import lombok.RequiredArgsConstructor; +import org.runimo.runimo.auth.exceptions.SignUpException; +import org.runimo.runimo.auth.jwt.JwtTokenFactory; +import org.runimo.runimo.rewards.service.eggs.EggGrantService; +import org.runimo.runimo.user.domain.Gender; +import org.runimo.runimo.user.domain.User; +import org.runimo.runimo.user.enums.UserHttpResponseCode; +import org.runimo.runimo.user.repository.UserRepository; +import org.runimo.runimo.user.service.UserCreator; +import org.runimo.runimo.user.service.UserItemCreator; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Profile({"test", "dev"}) +@Service +@RequiredArgsConstructor +public class TestAuthService { + + private final UserRepository userRepository; + private final JwtTokenFactory jwtTokenFactory; + private final UserCreator userCreator; + private final UserItemCreator userItemCreator; + private final EggGrantService eggGrantService; + + + @Transactional + public TestAuthResponse login(Long userId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> SignUpException.of(UserHttpResponseCode.LOGIN_FAIL_NOT_SIGN_IN)); + return new TestAuthResponse(jwtTokenFactory.generateTokenPair(user)); + } + + @Transactional + public TestAuthResponse signUp() { + User testUser = User.builder() + .nickname("test-user-" + UUID.randomUUID()) + .gender(Gender.UNKNOWN) + .totalTimeInSeconds(0L) + .totalDistanceInMeters(0L) + .build(); + userRepository.save(testUser); + + userCreator.createLovePoint(testUser.getId()); + userItemCreator.createAll(testUser.getId()); + eggGrantService.grantGreetingEggToUser(testUser); + + return new TestAuthResponse(jwtTokenFactory.generateTokenPair(testUser)); + } +} diff --git a/src/main/java/org/runimo/runimo/config/SwaggerConfig.java b/src/main/java/org/runimo/runimo/config/SwaggerConfig.java index d2ac79bb..68f5ba45 100644 --- a/src/main/java/org/runimo/runimo/config/SwaggerConfig.java +++ b/src/main/java/org/runimo/runimo/config/SwaggerConfig.java @@ -9,6 +9,7 @@ import io.swagger.v3.oas.models.security.SecurityScheme; import io.swagger.v3.oas.models.servers.Server; import java.util.Collections; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -16,11 +17,14 @@ @Configuration public class SwaggerConfig { + @Value("${runimo.server.ui.path}") + private String serverUiPath; + @Bean public OpenAPI customOpenAPI() { Server server = new Server(); - server.setUrl("https://toy.hyeonjae.dev"); + server.setUrl(serverUiPath); return new OpenAPI() .info(new Info() diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index de53c9b2..574e349e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -10,6 +10,9 @@ runimo: security: secret-key: ${AES_KEY} iv: ${AES_IV} + server: + ui: + path: ${SWAGGER_UI_SERVER:http://localhost:8080} spring: servlet: diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index a45ad1bf..294dae77 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -10,6 +10,9 @@ runimo: security: secret-key: ${AES_KEY} iv: ${AES_IV} + server: + ui: + path: ${SWAGGER_UI_SERVER:http://localhost:8080} spring: config: @@ -17,15 +20,12 @@ spring: - optional:file:${ENV_PATH:.}/.env.${SPRING_PROFILES_ACTIVE:test}[.properties] datasource: driver-class-name: org.h2.Driver - url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL + url: jdbc:h2:mem:testdb;MODE=MYSQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE username: ${DB_USERNAME} password: ${DB_PASSWORD} jpa: hibernate: ddl-auto: none - properties: - hibernate: - dialect: org.hibernate.dialect.H2Dialect open-in-view: false show-sql: true defer-datasource-initialization: true @@ -36,7 +36,7 @@ spring: init: mode: always schema-locations: classpath*:sql/schema.sql - data-locations: classpath*:sql/data.sql + data-locations: security: oauth2: