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
Open
ji2081
wants to merge
1
commit into
develop
Choose a base branch
from
feat/74-user-me
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
| 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.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) { | ||
| return ApiResponse.success(UserSuccessCode.USER_FOUND, UserDetailResponse.from(userQueryService.getDetail(userId))); | ||
| } | ||
|
|
||
| } | ||
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
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
| 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(), | ||
| SocialProvider.GOOGLE); | ||
|
Comment on lines
+13
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 나중에 네이버를 추가할 때 여기를 놓치면 네이버 회원도 GOOGLE로 내려가요. 주석 대신 이슈 번호를 단 TODO로 남겨주시면 좋겠습니다! 그리고 나중에 소셜 계정에서 읽으려면 |
||
| } | ||
|
|
||
| } | ||
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 = "me@gmail.com"; | ||
|
|
||
| @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("other@gmail.com")).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.
(사소) 다른 컨트롤러처럼 서비스 결과를 변수로 받은 뒤 응답을 만드는 모양으로 맞춰주시면 좋겠습니다!