Repository navigation
feat: 내 정보 조회 API (#74) #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
391224c
feat: 내 정보 조회 API 구현 (#74)
ji2081 3f20e0a
refactor: 내 정보 컨트롤러를 다른 컨트롤러 모양으로 맞춤 (#74)
ji2081 32becce
refactor: 로그인 방식 고정 부분에 TODO 이슈 번호 추가 (#74)
ji2081 3e577d9
Merge remote-tracking branch 'origin/develop' into feat/74-user-me
ji2081 350ec9e
Merge remote-tracking branch 'origin/develop' into feat/74-user-me
ji2081 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/com/sniptext/domain/user/controller/UserController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.sniptext.domain.user.controller; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.sniptext.domain.user.controller.dto.UserDetailResponse; | ||
| import com.sniptext.domain.user.service.UserQueryService; | ||
| import com.sniptext.domain.user.service.dto.result.UserDetailResult; | ||
| import com.sniptext.global.response.ApiResponse; | ||
| import com.sniptext.global.security.LoginUser; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| /** | ||
| * 회원 (로그인 필요, 캐시 금지). | ||
| */ | ||
| @RestController | ||
| @RequestMapping("/api/v1/users") | ||
| @RequiredArgsConstructor | ||
| public class UserController { | ||
|
|
||
| private final UserQueryService userQueryService; | ||
|
|
||
| @GetMapping("/me") | ||
| public ResponseEntity<ApiResponse<UserDetailResponse>> getMe(@LoginUser Long userId) { | ||
| UserDetailResult result = userQueryService.getDetail(userId); | ||
|
|
||
| return ApiResponse.success(UserSuccessCode.USER_FOUND, UserDetailResponse.from(result)); | ||
| } | ||
|
|
||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/sniptext/domain/user/controller/UserSuccessCode.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.sniptext.domain.user.controller; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| import com.sniptext.global.response.ResultCode; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum UserSuccessCode implements ResultCode { | ||
|
|
||
| USER_FOUND(HttpStatus.OK, "USER200_1", "내 정보 조회에 성공했습니다."); | ||
|
|
||
| private final HttpStatus httpStatus; | ||
| private final String code; | ||
| private final String message; | ||
|
|
||
| } |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/sniptext/domain/user/controller/dto/UserDetailResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.sniptext.domain.user.controller.dto; | ||
|
|
||
| import com.sniptext.domain.auth.entity.SocialProvider; | ||
| import com.sniptext.domain.user.service.dto.result.UserDetailResult; | ||
|
|
||
| /** 내 정보 (명세 5번). */ | ||
| public record UserDetailResponse( | ||
| Long userId, | ||
| String nickname, | ||
| String email, | ||
| String profileImageUrl, | ||
| SocialProvider loginType | ||
| ) { | ||
|
|
||
| public static UserDetailResponse from(UserDetailResult result) { | ||
| return new UserDetailResponse( | ||
| result.userId(), | ||
| result.nickname(), | ||
| result.email(), | ||
| result.profileImageUrl(), | ||
| result.loginType() | ||
| ); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/sniptext/domain/user/service/dto/result/UserDetailResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package com.sniptext.domain.user.service.dto.result; | ||
|
|
||
| import com.sniptext.domain.auth.entity.SocialProvider; | ||
| import com.sniptext.domain.user.entity.User; | ||
|
|
||
| /** | ||
| * 내 정보. 프로필 이미지가 없으면 profileImageUrl은 null이다. | ||
| * S1 로그인은 구글뿐이라 loginType은 GOOGLE로 고정한다. | ||
| */ | ||
| public record UserDetailResult(Long userId, String nickname, String email, String profileImageUrl, | ||
| SocialProvider loginType) { | ||
|
|
||
| public static UserDetailResult from(User user) { | ||
| return new UserDetailResult(user.getId(), user.getNickname(), user.getEmail(), user.getProfileImageUrl(), | ||
| // TODO(#105): 제공자가 늘면 소셜 계정에서 읽는다. 그대로 두면 다른 제공자 회원도 GOOGLE로 내려간다 | ||
| SocialProvider.GOOGLE); | ||
| } | ||
|
|
||
| } | ||
101 changes: 101 additions & 0 deletions
101
src/test/java/com/sniptext/domain/user/controller/UserControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package com.sniptext.domain.user.controller; | ||
|
|
||
| import static org.hamcrest.Matchers.nullValue; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import com.sniptext.domain.user.repository.UserRepository; | ||
| import com.sniptext.fixture.UserFixture; | ||
| import com.sniptext.support.IntegrationTestSupport; | ||
|
|
||
| /** 명세 5번. 실제 토큰 필터와 MySQL까지 거쳐 확인한다. */ | ||
| @Transactional | ||
| class UserControllerTest extends IntegrationTestSupport { | ||
|
|
||
| private static final String PATH = "/api/v1/users/me"; | ||
| private static final String EMAIL = "[email protected]"; | ||
|
|
||
| @Autowired | ||
| private UserRepository userRepository; | ||
|
|
||
| @Test | ||
| @DisplayName("내 정보를 명세의 필드로 내려주고 캐시하지 않는다") | ||
| void getMe() throws Exception { | ||
| Long userId = userRepository.save(UserFixture.user(EMAIL)).getId(); | ||
|
|
||
| mockMvc.perform(get(PATH).header(HttpHeaders.AUTHORIZATION, bearer(userId))) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(header().string(HttpHeaders.CACHE_CONTROL, "no-store")) | ||
| .andExpect(jsonPath("$.isSuccess").value(true)) | ||
| .andExpect(jsonPath("$.code").value("USER200_1")) | ||
| .andExpect(jsonPath("$.message").value("내 정보 조회에 성공했습니다.")) | ||
| .andExpect(jsonPath("$.result.userId").value(userId)) | ||
| .andExpect(jsonPath("$.result.nickname").value(UserFixture.NICKNAME)) | ||
| .andExpect(jsonPath("$.result.email").value(EMAIL)) | ||
| .andExpect(jsonPath("$.result.profileImageUrl").value(UserFixture.PROFILE_IMAGE_URL)) | ||
| .andExpect(jsonPath("$.result.loginType").value("GOOGLE")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("프로필 이미지가 없으면 profileImageUrl은 null이다") | ||
| void withoutProfileImage() throws Exception { | ||
| Long userId = userRepository.save(UserFixture.userWithoutProfileImage(EMAIL)).getId(); | ||
|
|
||
| mockMvc.perform(get(PATH).header(HttpHeaders.AUTHORIZATION, bearer(userId))) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.result.profileImageUrl").value(nullValue())); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("?userId로 다른 회원을 지정해도 토큰의 회원 정보만 내려준다") | ||
| void ignoresUserIdParameter() throws Exception { | ||
| Long me = userRepository.save(UserFixture.user(EMAIL)).getId(); | ||
| Long other = userRepository.save(UserFixture.user("[email protected]")).getId(); | ||
|
|
||
| mockMvc.perform(get(PATH).param("userId", String.valueOf(other)) | ||
| .header(HttpHeaders.AUTHORIZATION, bearer(me))) | ||
| .andExpect(status().isOk()) | ||
| .andExpect(jsonPath("$.result.userId").value(me)) | ||
| .andExpect(jsonPath("$.result.email").value(EMAIL)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("탈퇴한 회원의 토큰은 USER404_1이다") | ||
| void deletedUser() throws Exception { | ||
| Long userId = userRepository.save(UserFixture.deletedUser(EMAIL, LocalDateTime.of(2026, 11, 1, 10, 0))).getId(); | ||
|
|
||
| mockMvc.perform(get(PATH).header(HttpHeaders.AUTHORIZATION, bearer(userId))) | ||
| .andExpect(status().isNotFound()) | ||
| .andExpect(header().string(HttpHeaders.CACHE_CONTROL, "no-store")) | ||
| .andExpect(jsonPath("$.code").value("USER404_1")) | ||
| .andExpect(jsonPath("$.message").value("회원 정보를 찾을 수 없어요.")) | ||
| .andExpect(jsonPath("$.result").value(nullValue())); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("없는 회원의 토큰은 USER404_1이다") | ||
| void unknownUser() throws Exception { | ||
| mockMvc.perform(get(PATH).header(HttpHeaders.AUTHORIZATION, bearer(Long.MAX_VALUE))) | ||
| .andExpect(status().isNotFound()) | ||
| .andExpect(jsonPath("$.code").value("USER404_1")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("토큰이 없으면 COMMON401_1이다") | ||
| void withoutToken() throws Exception { | ||
| mockMvc.perform(get(PATH)) | ||
| .andExpect(status().isUnauthorized()) | ||
| .andExpect(jsonPath("$.code").value("COMMON401_1")); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
나중에 네이버를 추가할 때 여기를 놓치면 네이버 회원도 GOOGLE로 내려가요. 주석 대신 이슈 번호를 단 TODO로 남겨주시면 좋겠습니다! 그리고 나중에 소셜 계정에서 읽으려면
user가auth를 부르게 되는데, 지금은auth가user를 부르고 있어서(signUp,recordLogin) 그대로 하면 순환이 생겨요. 그때는 조합 서비스로 빼거나auth쪽에서 값을 넘기는 방향으로 가야 할 것 같습니다There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이슈 #105로 남기고 TODO에 번호를 달았습니다. 순환 참조는 말씀해 주신 대로 그 이슈에서 조합 서비스로 빼거나 auth에서 값을 넘기는 방향으로 진행하겠습니다!