Skip to content

Commit

Permalink
refactor: login된 유저 조회 api가 email을 함께 반환하도록 수정한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Mar 8, 2024
1 parent 2c5eecd commit 0c46c82
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class UserAcceptanceTestSupporter {

protected ResultActions getLoginedUser(String token) throws Exception {
return mockMvc.perform(MockMvcRequestBuilders
.get(API_VERSION + "/users/logined")
.get(API_VERSION + "/users/logins")
.accept(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, token)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

public class UserAcceptanceValidator {

public static void assertIsLogined(ResultActions resultActions, Long targetId, String nickname) throws Exception {
public static void assertIsLogined(ResultActions resultActions, Long targetId, String nickname, String email) throws Exception {
resultActions.andExpectAll(
status().isOk(),
jsonPath("$.target_id").value(targetId),
jsonPath("$.nickname").value(nickname)
jsonPath("$.nickname").value(nickname),
jsonPath("$.email").value(email)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,20 @@ class LoginedTargetFindAcceptanceTest extends UserAcceptanceTestSupporter {
void GET_LOGINED_USER_SUCCESS() throws Exception {
// given
String nickname = "devxb";
String email = "email";
Long targetId = targetInitializer.saveTargetAndGetId(nickname, Instant.now());
String token = jwtUtils.createAccessToken(Set.of(new Payload(Payload.Key.NICKNAME, nickname),
new Payload(Payload.Key.USER_ID, 12345 + ""), new Payload(Payload.Key.TARGET_ID, targetId + "")));
new Payload(Payload.Key.USER_ID, 12345 + ""),
new Payload(Payload.Key.TARGET_ID, targetId + ""),
new Payload(Payload.Key.EMAIL, email)));
applicationEventPublisher.publishEvent(
MockUserRegisterEvent.builder().expectedToken("bearer " + token).expectedId(targetId).build());

// when
ResultActions resultActions = getLoginedUser("bearer " + token);

// then
assertIsLogined(resultActions, targetId, nickname);
assertIsLogined(resultActions, targetId, nickname, email);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class CreateAuthTokenRequest {
private final String userId;
private final String nickname;
private final String targetId;
private final String email;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public enum Key {
USER_ID,
TARGET_ID,
NICKNAME,
EMAIL,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ public AuthToken create(CreateAuthTokenRequest request) {
var userId = request.getUserId();
var nickname = request.getNickname();
var targetId = request.getTargetId();
var email = request.getEmail();
Assert.isTrue(userId != null && !userId.isBlank(), "Authentication token 생성 시 유저 식별자는 필수입니다.");
Assert.isTrue(nickname != null && !nickname.isBlank(), "Authentication token 생성 시 유저의 별명은 필수입니다.");
Assert.isTrue(targetId != null && !targetId.isBlank(), "Authentication token 생성 시 유저의 별명은 필수입니다.");
Assert.isTrue(email != null && !email.isBlank(), "Authentication token 생성 시 유저의 이메일은 필수입니다.");

Set<Payload> payload = new HashSet<>();
payload.add(new Payload(Payload.Key.USER_ID, userId));
payload.add(new Payload(Payload.Key.NICKNAME, nickname));
payload.add(new Payload(Payload.Key.TARGET_ID, targetId));
payload.add(new Payload(Payload.Key.EMAIL, email));

String token = createToken(payload);
return new AuthToken(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public LoginedInfo decryptToken(String encryptedToken) {
String nickName = decodedJWT.getClaim(Payload.Key.NICKNAME.name()).asString();
Long userId = Long.valueOf(decodedJWT.getClaim(Payload.Key.USER_ID.name()).asString());
Long targetId = Long.valueOf(decodedJWT.getClaim(Payload.Key.TARGET_ID.name()).asString());
return new LoginedInfo(nickName, targetId, userId);
String email = decodedJWT.getClaim(Payload.Key.EMAIL.name()).asString();
return new LoginedInfo(nickName, targetId, userId, email);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public AuthToken signInWithOAuth(SignInWithOAuthRequest request) {
var userId = foundUser.orElseThrow(IllegalAccessError::new);
var targetId = targetFindByUsernameUseCase.findTargetByUsername(request.getUsername()).orElseThrow().getId();

var authTokenCreateRequest = new CreateAuthTokenRequest(userId.toString(), request.getUsername(), String.valueOf(targetId));
var authTokenCreateRequest = new CreateAuthTokenRequest(userId.toString(), request.getUsername(), String.valueOf(targetId), email);

return authTokenCreateUseCase.create(authTokenCreateRequest);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ void THROW_EXCEPTION_WHEN_USER_ID_IS_BLANK(String userId) {
// given
var nickname = "nickname";
var targetId = "targetId";
var request = new CreateAuthTokenRequest(userId, nickname, targetId);
var email = "email";
var request = new CreateAuthTokenRequest(userId, nickname, targetId, email);

// when
var throwable = Assertions.catchThrowable(() -> authTokenCreateService.create(request));
Expand All @@ -49,7 +50,8 @@ void THROW_EXCEPTION_WHEN_NICKNAME_IS_BLANK(String nickname) {
// given
var userId = "userId";
var targetId = "targetId";
var request = new CreateAuthTokenRequest(userId, nickname, targetId);
var email = "email";
var request = new CreateAuthTokenRequest(userId, nickname, targetId, email);

// when
var throwable = Assertions.catchThrowable(() -> authTokenCreateService.create(request));
Expand All @@ -65,7 +67,25 @@ void THROW_EXCEPTION_WHEN_TARGET_ID_IS_BLANK(String targetId) {
// given
var userId = "userId";
var nickname = "nickname";
var request = new CreateAuthTokenRequest(userId, nickname, targetId);
var email = "email";
var request = new CreateAuthTokenRequest(userId, nickname, targetId, email);

// when
var throwable = Assertions.catchThrowable(() -> authTokenCreateService.create(request));

// then
Assertions.assertThat(throwable).isInstanceOf(IllegalArgumentException.class);
}

@ParameterizedTest
@NullAndEmptySource
@DisplayName("이메일이 비어있거나 null이면 예외를 발생시킨다.")
void THROW_EXCEPTION_WHEN_EMAIL_IS_BLANK(String email) {
// given
var userId = "userId";
var targetId = "targetId";
var nickname = "nickname";
var request = new CreateAuthTokenRequest(userId, nickname, targetId, email);

// when
var throwable = Assertions.catchThrowable(() -> authTokenCreateService.create(request));
Expand All @@ -81,7 +101,8 @@ void RETURN_TOKEN_WHEN_VALID_REQUEST() {
var userId = "userId";
var nickname = "nickname";
var targetId = "targetId";
var request = new CreateAuthTokenRequest(userId, nickname, targetId);
var email = "email";
var request = new CreateAuthTokenRequest(userId, nickname, targetId, email);
var expectedToken = "token";

when(jwtUtils.createAccessToken(any())).thenReturn(expectedToken);
Expand Down
29 changes: 29 additions & 0 deletions support/e2e/v1_13_get_logined_target.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
POST http://nalab-server:8080/v1/oauth/default # Default provider를 통해서 로그인 진행
{
"nickname": "devxb",
"email": "[email protected]"
}

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.access_token" exists
jsonpath "$.token_type" exists

[Captures]
token_type: jsonpath "$.token_type"
auth_token: jsonpath "$.access_token"

#######

GET http://nalab-server:8080/v1/users/logins # Token에 해당하는 유저 조회
Authorization: {{ token_type }} {{ auth_token }}

HTTP 200
[Asserts]
header "Content-type" == "application/json"

jsonpath "$.target_id" exists
jsonpath "$.nickname" == "devxb"
jsonpath "$.email" == "[email protected]"
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public class LoginedInfo {
private final String nickName;
private final Long targetId;
private final Long userId;

private final String email;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LoginedUserGetByTokenServiceTest {
@DisplayName("토큰을 이용해 로그인된 유저의 정보를 조회 성공 테스트")
void GET_LOGINED_INFO_BY_TOKEN_SUCCESS() {
// given
LoginedInfo expected = new LoginedInfo("hello", 12345L, 54321L);
LoginedInfo expected = new LoginedInfo("hello", 12345L, 54321L, "email");
String token = "hello token";

Mockito.when(loginedUserGetByTokenPort.decryptToken(token.split(" ")[1])).thenReturn(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class LoginedUserGetController {

private final LoginedUserGetByTokenUseCase loginedUserGetByTokenUseCase;

@GetMapping("/users/logined")
@GetMapping("/users/logins")
public LoginedInfoResponse getLoginedUserByToken(@RequestHeader(HttpHeaders.AUTHORIZATION) String token) {
LoginedInfo loginedInfo = loginedUserGetByTokenUseCase.decryptToken(token);

return new LoginedInfoResponse(String.valueOf(loginedInfo.getTargetId()), loginedInfo.getNickName());
return LoginedInfoResponse.of(loginedInfo);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Data;
import me.nalab.user.application.common.dto.LoginedInfo;

@Data
public class LoginedInfoResponse {
Expand All @@ -11,5 +12,13 @@ public class LoginedInfoResponse {
private final String targetId;
@JsonProperty("nickname")
private final String nickName;
@JsonProperty("email")
private final String email;

public static LoginedInfoResponse of(LoginedInfo loginedInfo) {
return new LoginedInfoResponse(String.valueOf(loginedInfo.getTargetId()),
loginedInfo.getNickName(),
loginedInfo.getEmail());
}

}

0 comments on commit 0c46c82

Please sign in to comment.