From dbb7adc8f702e9b9148e94f616568113f9677954 Mon Sep 17 00:00:00 2001 From: choidongkuen <danaver12@daum.net> Date: Sat, 6 Jan 2024 13:48:20 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20ci=20=EC=97=90=EB=9F=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../teumteum/core/context/LoginContext.java | 9 ++++ .../core/context/LoginContextImpl.java | 25 +++++++++++ .../teumteum/core/property/RedisProperty.java | 1 - .../user/controller/UserController.java | 20 +++------ .../java/net/teumteum/user/domain/OAuth.java | 5 ++- .../teumteum/integration/IntegrationTest.java | 8 ++-- .../teumteum/integration/SecurityService.java | 34 -------------- .../integration/TestLoginContext.java | 20 +++++++++ .../integration/UserIntegrationTest.java | 25 ++++++----- .../net/teumteum/user/domain/UserFixture.java | 44 +++++++++---------- 10 files changed, 102 insertions(+), 89 deletions(-) create mode 100644 src/main/java/net/teumteum/core/context/LoginContext.java create mode 100644 src/main/java/net/teumteum/core/context/LoginContextImpl.java delete mode 100644 src/test/java/net/teumteum/integration/SecurityService.java create mode 100644 src/test/java/net/teumteum/integration/TestLoginContext.java diff --git a/src/main/java/net/teumteum/core/context/LoginContext.java b/src/main/java/net/teumteum/core/context/LoginContext.java new file mode 100644 index 00000000..5a5a9ee7 --- /dev/null +++ b/src/main/java/net/teumteum/core/context/LoginContext.java @@ -0,0 +1,9 @@ +package net.teumteum.core.context; + +public interface LoginContext { + + Long getUserId(); + + void setUserId(Long userId); + +} diff --git a/src/main/java/net/teumteum/core/context/LoginContextImpl.java b/src/main/java/net/teumteum/core/context/LoginContextImpl.java new file mode 100644 index 00000000..91c91068 --- /dev/null +++ b/src/main/java/net/teumteum/core/context/LoginContextImpl.java @@ -0,0 +1,25 @@ +package net.teumteum.core.context; + +import org.springframework.context.annotation.Profile; +import org.springframework.context.annotation.ScopedProxyMode; +import org.springframework.stereotype.Component; +import org.springframework.web.context.annotation.RequestScope; + +@Component +@Profile("prod") +@RequestScope(proxyMode = ScopedProxyMode.INTERFACES) +public class LoginContextImpl implements LoginContext { + + private Long userId; + + @Override + public Long getUserId() { + return userId; + } + + @Override + public void setUserId(Long userId) { + this.userId = userId; + } + +} \ No newline at end of file diff --git a/src/main/java/net/teumteum/core/property/RedisProperty.java b/src/main/java/net/teumteum/core/property/RedisProperty.java index 81f8888d..fbc645fb 100644 --- a/src/main/java/net/teumteum/core/property/RedisProperty.java +++ b/src/main/java/net/teumteum/core/property/RedisProperty.java @@ -3,7 +3,6 @@ import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; @Getter @Setter diff --git a/src/main/java/net/teumteum/user/controller/UserController.java b/src/main/java/net/teumteum/user/controller/UserController.java index 4f7b753f..5d3e8fa6 100644 --- a/src/main/java/net/teumteum/user/controller/UserController.java +++ b/src/main/java/net/teumteum/user/controller/UserController.java @@ -1,6 +1,5 @@ package net.teumteum.user.controller; -import java.util.Arrays; import lombok.RequiredArgsConstructor; import net.teumteum.core.error.ErrorResponse; import net.teumteum.core.security.service.SecurityService; @@ -9,16 +8,9 @@ import net.teumteum.user.domain.response.UsersGetByIdResponse; import net.teumteum.user.service.UserService; import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.Arrays; @RestController @RequiredArgsConstructor @@ -28,20 +20,18 @@ public class UserController { private final UserService userService; private final SecurityService securityService; - /* userId 로 회원 조회 */ @GetMapping("/{userId}") @ResponseStatus(HttpStatus.OK) public UserGetResponse getUserById(@PathVariable("userId") Long userId) { return userService.getUserById(userId); } - /* 여러 userId 로 회원 들 조회 */ @GetMapping @ResponseStatus(HttpStatus.OK) public UsersGetByIdResponse getUsersById(@RequestParam("id") String userIds) { var parsedUserIds = Arrays.stream(userIds.split(",")) - .map(Long::valueOf) - .toList(); + .map(Long::valueOf) + .toList(); return userService.getUsersById(parsedUserIds); } diff --git a/src/main/java/net/teumteum/user/domain/OAuth.java b/src/main/java/net/teumteum/user/domain/OAuth.java index d00777da..32011235 100644 --- a/src/main/java/net/teumteum/user/domain/OAuth.java +++ b/src/main/java/net/teumteum/user/domain/OAuth.java @@ -2,6 +2,8 @@ import jakarta.persistence.Column; import jakarta.persistence.Embeddable; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @@ -16,6 +18,7 @@ public class OAuth { @Column(name = "oauth_id", unique = true, nullable = false) private String oauthId; - @Column(name = "provider_type", nullable = false) + @Enumerated(EnumType.STRING) + @Column(name = "provider_type", nullable = false) private Authenticated authenticated; } diff --git a/src/test/java/net/teumteum/integration/IntegrationTest.java b/src/test/java/net/teumteum/integration/IntegrationTest.java index 138472e5..b4d56f5c 100644 --- a/src/test/java/net/teumteum/integration/IntegrationTest.java +++ b/src/test/java/net/teumteum/integration/IntegrationTest.java @@ -1,7 +1,7 @@ package net.teumteum.integration; import net.teumteum.Application; -import net.teumteum.core.security.service.SecurityService; +import net.teumteum.core.context.LoginContext; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; @@ -10,7 +10,7 @@ import org.springframework.test.context.ContextConfiguration; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@ContextConfiguration(classes = {Application.class, Api.class, Repository.class, SecurityService.class}) +@ContextConfiguration(classes = {Application.class, Api.class, Repository.class, TestLoginContext.class}) abstract public class IntegrationTest { @Autowired @@ -20,8 +20,8 @@ abstract public class IntegrationTest { protected Repository repository; @Autowired - protected SecurityService securityService; - + protected LoginContext loginContext; + @AfterEach @BeforeEach void clearAll() { diff --git a/src/test/java/net/teumteum/integration/SecurityService.java b/src/test/java/net/teumteum/integration/SecurityService.java deleted file mode 100644 index 79f4e04f..00000000 --- a/src/test/java/net/teumteum/integration/SecurityService.java +++ /dev/null @@ -1,34 +0,0 @@ -package net.teumteum.integration; - -import net.teumteum.core.security.UserAuthentication; -import org.springframework.boot.test.context.TestComponent; -import org.springframework.security.core.context.SecurityContextHolder; - -@TestComponent -public class SecurityService { - - private static UserAuthentication getUserAuthentication() { - return (UserAuthentication) SecurityContextHolder.getContext().getAuthentication(); - } - - public void clearSecurityContext() { - SecurityContextHolder.clearContext(); - } - - /* 해당 요청에서 로그인한 회원 id 반환 */ - public Long getCurrentUserId() { - UserAuthentication userAuthentication = getUserAuthentication(); - return userAuthentication.getId(); - } - - /* 해당 요청에서 로그인한 회원 OAuth id 반환 */ - public String getCurrentUserOAuthId() { - UserAuthentication userAuthentication = getUserAuthentication(); - return userAuthentication.getOauthId(); - } - - public void setUserId(Long userId) { - UserAuthentication userAuthentication = getUserAuthentication(); - userAuthentication.setUserId(userId); - } -} diff --git a/src/test/java/net/teumteum/integration/TestLoginContext.java b/src/test/java/net/teumteum/integration/TestLoginContext.java new file mode 100644 index 00000000..d184c563 --- /dev/null +++ b/src/test/java/net/teumteum/integration/TestLoginContext.java @@ -0,0 +1,20 @@ +package net.teumteum.integration; + +import net.teumteum.core.context.LoginContext; +import org.springframework.boot.test.context.TestComponent; + +@TestComponent +public class TestLoginContext implements LoginContext { + + private Long userId; + + @Override + public Long getUserId() { + return this.userId; + } + + @Override + public void setUserId(Long userId) { + this.userId = userId; + } +} diff --git a/src/test/java/net/teumteum/integration/UserIntegrationTest.java b/src/test/java/net/teumteum/integration/UserIntegrationTest.java index 0561eb9a..66202ae4 100644 --- a/src/test/java/net/teumteum/integration/UserIntegrationTest.java +++ b/src/test/java/net/teumteum/integration/UserIntegrationTest.java @@ -1,6 +1,5 @@ package net.teumteum.integration; -import java.util.List; import net.teumteum.core.error.ErrorResponse; import net.teumteum.user.domain.response.UserGetResponse; import net.teumteum.user.domain.response.UsersGetByIdResponse; @@ -9,6 +8,8 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.util.List; + @DisplayName("유저 통합테스트의") class UserIntegrationTest extends IntegrationTest { @@ -31,11 +32,11 @@ void Return_user_info_if_exist_user_id_received() { // then Assertions.assertThat( - result.expectStatus().isOk() - .expectBody(UserGetResponse.class) - .returnResult().getResponseBody()) - .usingRecursiveComparison() - .isEqualTo(expected); + result.expectStatus().isOk() + .expectBody(UserGetResponse.class) + .returnResult().getResponseBody()) + .usingRecursiveComparison() + .isEqualTo(expected); } @Test @@ -49,7 +50,7 @@ void Return_400_bad_request_if_not_exists_user_id_received() { // then result.expectStatus().isBadRequest() - .expectBody(ErrorResponse.class); + .expectBody(ErrorResponse.class); } } @@ -71,9 +72,9 @@ void Return_user_info_if_exist_user_ids_received() { // then Assertions.assertThat(result.expectStatus().isOk() - .expectBody(UsersGetByIdResponse.class) - .returnResult() - .getResponseBody() + .expectBody(UsersGetByIdResponse.class) + .returnResult() + .getResponseBody() ).usingRecursiveComparison().isEqualTo(expected); } @@ -113,7 +114,7 @@ void Update_user_info() { var existUser = repository.saveAndGetUser(); var updateUser = RequestFixture.userUpdateRequest(existUser); - securityService.setUserId(existUser.getId()); + loginContext.setUserId(existUser.getId()); // when var result = api.updateUser(VALID_TOKEN, updateUser); @@ -135,7 +136,7 @@ void Return_200_ok_with_success_make_friends() { var myToken = "JWT MY_TOKEN"; var friend = repository.saveAndGetUser(); - securityService.setUserId(me.getId()); + loginContext.setUserId(me.getId()); // when var result = api.addFriends(myToken, friend.getId()); diff --git a/src/test/java/net/teumteum/user/domain/UserFixture.java b/src/test/java/net/teumteum/user/domain/UserFixture.java index d5761a78..12f091d2 100644 --- a/src/test/java/net/teumteum/user/domain/UserFixture.java +++ b/src/test/java/net/teumteum/user/domain/UserFixture.java @@ -1,10 +1,10 @@ package net.teumteum.user.domain; +import lombok.Builder; + import java.util.List; import java.util.Set; import java.util.UUID; -import lombok.Builder; -import net.teumteum.core.security.Authenticated; import static net.teumteum.core.security.Authenticated.네이버; @@ -12,14 +12,14 @@ public class UserFixture { public static User getNullIdUser() { return newUserByBuilder(UserBuilder.builder() - .id(null) - .build()); + .id(null) + .build()); } public static User getUserWithId(Long id) { return newUserByBuilder(UserBuilder.builder() - .id(id) - .build()); + .id(id) + .build()); } public static User getDefaultUser() { @@ -28,21 +28,21 @@ public static User getDefaultUser() { public static User newUserByBuilder(UserBuilder userBuilder) { return new User( - userBuilder.id, - userBuilder.name, - userBuilder.birth, - userBuilder.characterId, - userBuilder.mannerTemperature, - userBuilder.oauth, - userBuilder.roleType, - userBuilder.activityArea, - userBuilder.mbti, - userBuilder.status, - userBuilder.goal, - userBuilder.job, - userBuilder.interests, - userBuilder.terms, - Set.of() + userBuilder.id, + userBuilder.name, + userBuilder.birth, + userBuilder.characterId, + userBuilder.mannerTemperature, + userBuilder.oauth, + userBuilder.roleType, + userBuilder.activityArea, + userBuilder.mbti, + userBuilder.status, + userBuilder.goal, + userBuilder.job, + userBuilder.interests, + userBuilder.terms, + Set.of() ); } @@ -75,7 +75,7 @@ public static class UserBuilder { private Job job = new Job("netflix", true, "developer", "backend"); @Builder.Default private List<String> interests = List.of( - "game", "sleep", "Eating delicious food" + "game", "sleep", "Eating delicious food" ); @Builder.Default private Terms terms = new Terms(true, true);