Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/docs/asciidoc/users.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
[[์œ ์ €-์ •๋ณด-์กฐํšŒ]]
=== `GET` ์œ ์ € ์ •๋ณด ์กฐํšŒ

operation::user-controller-test/find-user-info[snippets='http-request,curl-request,path-parameters,http-response,response-fields']
operation::user-controller-test/find-user-info[snippets='http-request,curl-request,path-parameters,http-response,response-fields']

[[๋ณธ์ธ-์ •๋ณด-์กฐํšŒ]]
=== `GET` ๋ณธ์ธ ์ •๋ณด ์กฐํšŒ

operation::user-controller-test/find-me[snippets='http-request,curl-request,request-headers,http-response,response-fields']
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import com.swyp8team2.auth.domain.Provider;
import com.swyp8team2.auth.domain.SocialAccount;
import com.swyp8team2.auth.domain.SocialAccountRepository;
import com.swyp8team2.auth.presentation.dto.TokenResponse;
import com.swyp8team2.common.annotation.GuestTokenCryptoService;
import com.swyp8team2.crypto.application.CryptoService;
import com.swyp8team2.user.application.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -37,7 +37,7 @@ public AuthService(
}

@Transactional
public TokenPair oauthSignIn(String code, String redirectUri) {
public TokenResponse oauthSignIn(String code, String redirectUri) {
OAuthUserInfo oAuthUserInfo = oAuthService.getUserInfo(code, redirectUri);
SocialAccount socialAccount = socialAccountRepository.findBySocialIdAndProvider(
oAuthUserInfo.socialId(),
Expand All @@ -52,7 +52,7 @@ private SocialAccount createUser(OAuthUserInfo oAuthUserInfo) {
}

@Transactional
public TokenPair reissue(String refreshToken) {
public TokenResponse reissue(String refreshToken) {
return jwtService.reissue(refreshToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.swyp8team2.auth.domain.RefreshToken;
import com.swyp8team2.auth.domain.RefreshTokenRepository;
import com.swyp8team2.auth.presentation.dto.TokenResponse;
import com.swyp8team2.common.exception.BadRequestException;
import com.swyp8team2.common.exception.ErrorCode;
import jakarta.transaction.Transactional;
Expand All @@ -18,7 +19,7 @@ public class JwtService {
private final RefreshTokenRepository refreshTokenRepository;

@Transactional
public TokenPair createToken(long userId) {
public TokenResponse createToken(long userId) {
TokenPair tokenPair = jwtProvider.createToken(new JwtClaim(userId));
RefreshToken refreshToken = refreshTokenRepository.findByUserId(userId)
.orElseGet(() -> new RefreshToken(userId, tokenPair.refreshToken()));
Expand All @@ -27,11 +28,11 @@ public TokenPair createToken(long userId) {

log.debug("createToken userId: {} accessToken: {} refreshToken: {}",
userId, tokenPair.accessToken(), tokenPair.refreshToken());
return tokenPair;
return new TokenResponse(tokenPair, userId);
}

@Transactional
public TokenPair reissue(String refreshToken) {
public TokenResponse reissue(String refreshToken) {
JwtClaim claim = jwtProvider.parseToken(refreshToken);
RefreshToken findRefreshToken = refreshTokenRepository.findByUserId(claim.idAsLong())
.orElseThrow(() -> new BadRequestException(ErrorCode.REFRESH_TOKEN_NOT_FOUND));
Expand All @@ -41,6 +42,6 @@ public TokenPair reissue(String refreshToken) {

log.debug("reissue userId: {} accessToken: {} refreshToken: {}",
claim.id(), tokenPair.accessToken(), tokenPair.refreshToken());
return tokenPair;
return new TokenResponse(tokenPair, claim.idAsLong());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.swyp8team2.auth.presentation.dto.GuestTokenResponse;
import com.swyp8team2.auth.presentation.dto.OAuthSignInRequest;
import com.swyp8team2.auth.presentation.dto.TokenResponse;
import com.swyp8team2.auth.presentation.dto.AuthResponse;
import com.swyp8team2.common.exception.BadRequestException;
import com.swyp8team2.common.exception.ErrorCode;
import com.swyp8team2.common.presentation.CustomHeader;
Expand All @@ -31,28 +32,30 @@ public class AuthController {
private final AuthService authService;

@PostMapping("/oauth2/code/kakao")
public ResponseEntity<TokenResponse> kakaoOAuthSignIn(
public ResponseEntity<AuthResponse> kakaoOAuthSignIn(
@Valid @RequestBody OAuthSignInRequest request,
HttpServletResponse response
) {
TokenPair tokenPair = authService.oauthSignIn(request.code(), request.redirectUri());
TokenResponse tokenResponse = authService.oauthSignIn(request.code(), request.redirectUri());
TokenPair tokenPair = tokenResponse.tokenPair();
Cookie cookie = refreshTokenCookieGenerator.createCookie(tokenPair.refreshToken());
response.addCookie(cookie);
return ResponseEntity.ok(new TokenResponse(tokenPair.accessToken()));
return ResponseEntity.ok(new AuthResponse(tokenPair.accessToken(), tokenResponse.userId()));
}

@PostMapping("/reissue")
public ResponseEntity<TokenResponse> reissue(
public ResponseEntity<AuthResponse> reissue(
@CookieValue(name = CustomHeader.CustomCookie.REFRESH_TOKEN, required = false) String refreshToken,
HttpServletResponse response
) {
if (Objects.isNull(refreshToken)) {
throw new BadRequestException(ErrorCode.INVALID_REFRESH_TOKEN_HEADER);
}
TokenPair tokenPair = authService.reissue(refreshToken);
TokenResponse tokenResponse = authService.reissue(refreshToken);
TokenPair tokenPair = tokenResponse.tokenPair();
Cookie cookie = refreshTokenCookieGenerator.createCookie(tokenPair.refreshToken());
response.addCookie(cookie);
return ResponseEntity.ok(new TokenResponse(tokenPair.accessToken()));
return ResponseEntity.ok(new AuthResponse(tokenPair.accessToken(), tokenResponse.userId()));
}

@PostMapping("/guest/token")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.swyp8team2.auth.presentation.dto;

public record AuthResponse(String accessToken, Long userId) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package com.swyp8team2.auth.presentation.dto;

public record TokenResponse(String accessToken) {
import com.swyp8team2.auth.application.jwt.TokenPair;

public record TokenResponse(
TokenPair tokenPair,
Long userId
) {
}
4 changes: 3 additions & 1 deletion src/main/java/com/swyp8team2/common/dev/DataInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.swyp8team2.auth.application.jwt.JwtService;
import com.swyp8team2.auth.application.jwt.TokenPair;
import com.swyp8team2.auth.presentation.dto.TokenResponse;
import com.swyp8team2.comment.domain.Comment;
import com.swyp8team2.comment.domain.CommentRepository;
import com.swyp8team2.common.annotation.ShareUrlCryptoService;
Expand Down Expand Up @@ -69,7 +70,8 @@ public void init() {
}
List<NicknameAdjective> adjectives = nicknameAdjectiveRepository.findAll();
User testUser = userRepository.save(User.create("nickname", "https://t1.kakaocdn.net/account_images/default_profile.jpeg"));
TokenPair tokenPair = jwtService.createToken(testUser.getId());
TokenResponse tokenResponse = jwtService.createToken(testUser.getId());
TokenPair tokenPair = tokenResponse.tokenPair();
System.out.println("accessToken = " + tokenPair.accessToken());
System.out.println("refreshToken = " + tokenPair.refreshToken());
List<User> users = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.swyp8team2.user.presentation;

import com.swyp8team2.auth.domain.UserInfo;
import com.swyp8team2.user.application.UserService;
import com.swyp8team2.user.presentation.dto.UserInfoResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -20,4 +22,11 @@ public class UserController {
public ResponseEntity<UserInfoResponse> findUserInfo(@PathVariable("userId") Long userId) {
return ResponseEntity.ok(userService.findById(userId));
}

@GetMapping("/me")
public ResponseEntity<UserInfoResponse> findMyInfo(
@AuthenticationPrincipal UserInfo userInfo
) {
return ResponseEntity.ok(userService.findById(userInfo.userId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.swyp8team2.auth.domain.Provider;
import com.swyp8team2.auth.domain.SocialAccount;
import com.swyp8team2.auth.domain.SocialAccountRepository;
import com.swyp8team2.auth.presentation.dto.TokenResponse;
import com.swyp8team2.support.IntegrationTest;
import com.swyp8team2.user.domain.User;
import com.swyp8team2.user.domain.UserRepository;
Expand Down Expand Up @@ -52,9 +53,10 @@ void oAuthSignIn() throws Exception {
.willReturn(expectedTokenPair);

//when
TokenPair tokenPair = authService.oauthSignIn("code", "https://dev.photopic.site");
TokenResponse tokenResponse = authService.oauthSignIn("code", "https://dev.photopic.site");

//then
TokenPair tokenPair = tokenResponse.tokenPair();
SocialAccount socialAccount = socialAccountRepository.findBySocialIdAndProvider(oAuthUserInfo.socialId(), Provider.KAKAO).get();
User user = userRepository.findById(socialAccount.getId()).get();
assertAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.swyp8team2.auth.application.jwt.TokenPair;
import com.swyp8team2.auth.domain.RefreshToken;
import com.swyp8team2.auth.domain.RefreshTokenRepository;
import com.swyp8team2.auth.presentation.dto.TokenResponse;
import com.swyp8team2.common.exception.BadRequestException;
import com.swyp8team2.common.exception.ErrorCode;
import com.swyp8team2.support.IntegrationTest;
Expand Down Expand Up @@ -39,9 +40,10 @@ void createToken() throws Exception {
.willReturn(expectedTokenPair);

//when
TokenPair tokenPair = jwtService.createToken(givenUserId);
TokenResponse tokenResponse = jwtService.createToken(givenUserId);

//then
TokenPair tokenPair = tokenResponse.tokenPair();
RefreshToken findRefreshToken = refreshTokenRepository.findByUserId(givenUserId).get();
assertThat(tokenPair).isEqualTo(expectedTokenPair);
assertThat(findRefreshToken.getToken()).isEqualTo(expectedTokenPair.refreshToken());
Expand All @@ -62,9 +64,10 @@ void reissue() throws Exception {
refreshTokenRepository.save(new RefreshToken(givenUserId, givenRefreshToken));

//when
TokenPair tokenPair = jwtService.reissue(givenRefreshToken);
TokenResponse tokenResponse = jwtService.reissue(givenRefreshToken);

//then
TokenPair tokenPair = tokenResponse.tokenPair();
RefreshToken findRefreshToken = refreshTokenRepository.findByUserId(givenUserId).get();
assertThat(tokenPair).isEqualTo(expectedTokenPair);
assertThat(findRefreshToken.getToken()).isEqualTo(newRefreshToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.swyp8team2.auth.application.jwt.TokenPair;
import com.swyp8team2.auth.presentation.dto.GuestTokenResponse;
import com.swyp8team2.auth.presentation.dto.OAuthSignInRequest;
import com.swyp8team2.auth.presentation.dto.AuthResponse;
import com.swyp8team2.auth.presentation.dto.TokenResponse;
import com.swyp8team2.common.exception.BadRequestException;
import com.swyp8team2.common.exception.ErrorCode;
Expand Down Expand Up @@ -41,9 +42,9 @@ class AuthControllerTest extends RestDocsTest {
void kakaoOAuthSignIn() throws Exception {
//given
TokenPair expectedTokenPair = new TokenPair("accessToken", "refreshToken");
TokenResponse response = new TokenResponse(expectedTokenPair.accessToken());
AuthResponse response = new AuthResponse(expectedTokenPair.accessToken(), 1L);
given(authService.oauthSignIn(anyString(), anyString()))
.willReturn(expectedTokenPair);
.willReturn(new TokenResponse(expectedTokenPair, 1L));
OAuthSignInRequest request = new OAuthSignInRequest("code", "https://dev.photopic.site");

//when then
Expand All @@ -64,7 +65,8 @@ void kakaoOAuthSignIn() throws Exception {
fieldWithPath("redirectUri").description("์นด์นด์˜ค ์ธ์ฆ redirect uri")
),
responseFields(
fieldWithPath("accessToken").description("์•ก์„ธ์Šค ํ† ํฐ")
fieldWithPath("accessToken").description("์•ก์„ธ์Šค ํ† ํฐ"),
fieldWithPath("userId").description("์œ ์ € Id")
),
responseCookies(
cookieWithName(CustomHeader.CustomCookie.REFRESH_TOKEN).description("๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ")
Expand All @@ -78,9 +80,10 @@ void kakaoOAuthSignIn() throws Exception {
void reissue() throws Exception {
//given
String newRefreshToken = "newRefreshToken";
TokenPair tokenPair = new TokenPair("accessToken", newRefreshToken);
given(authService.reissue(anyString()))
.willReturn(new TokenPair("accessToken", newRefreshToken));
TokenResponse response = new TokenResponse("accessToken");
.willReturn(new TokenResponse(tokenPair, 1L));
AuthResponse response = new AuthResponse(tokenPair.accessToken(), 1L);

//when then
mockMvc.perform(post("/auth/reissue")
Expand All @@ -101,7 +104,8 @@ void reissue() throws Exception {
cookieWithName(CustomHeader.CustomCookie.REFRESH_TOKEN).description("์ƒˆ ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ")
),
responseFields(
fieldWithPath("accessToken").description("์ƒˆ ์•ก์„ธ์Šค ํ† ํฐ")
fieldWithPath("accessToken").description("์ƒˆ ์•ก์„ธ์Šค ํ† ํฐ"),
fieldWithPath("userId").description("์œ ์ € Id")
)
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.swyp8team2.user.presentation;

import com.swyp8team2.support.RestDocsTest;
import com.swyp8team2.support.WithMockUserInfo;
import com.swyp8team2.user.presentation.dto.UserInfoResponse;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.security.test.context.support.WithMockUser;

import static org.mockito.BDDMockito.given;
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders;
import static org.springframework.restdocs.payload.JsonFieldType.NUMBER;
import static org.springframework.restdocs.payload.JsonFieldType.STRING;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
Expand All @@ -20,7 +23,7 @@
class UserControllerTest extends RestDocsTest {

@Test
@WithMockUser
@WithMockUserInfo
@DisplayName("์œ ์ € ์ •๋ณด ์กฐํšŒ")
void findUserInfo() throws Exception {
//given
Expand All @@ -43,4 +46,28 @@ void findUserInfo() throws Exception {
)
));
}

@Test
@WithMockUserInfo
@DisplayName("๋ณธ์ธ ์ •๋ณด ์กฐํšŒ")
void findMe() throws Exception {
//given
UserInfoResponse response = new UserInfoResponse(1L, "nickname", "https://image.com/profile-image");
given(userService.findById(1L))
.willReturn(response);

//when then
mockMvc.perform(RestDocumentationRequestBuilders.get("/users/me")
.header(HttpHeaders.AUTHORIZATION, "Bearer access-token"))
.andExpect(status().isOk())
.andExpect(content().json(objectMapper.writeValueAsString(response)))
.andDo(restDocs.document(
requestHeaders(authorizationHeader()),
responseFields(
fieldWithPath("id").description("์œ ์ € ์•„์ด๋””").type(NUMBER),
fieldWithPath("nickname").description("๋‹‰๋„ค์ž„").type(STRING),
fieldWithPath("profileUrl").description("ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ URL").type(STRING)
)
));
}
}