Skip to content

Commit

Permalink
refactor: 회원 카드 등록시 JWT 정보도 함께 반환하도록 수정 (#114)
Browse files Browse the repository at this point in the history
* chore : application.properties 에 jwt 관련 설정 값 추가 (#113)

* refactor : userService 회원 카드 등록 로직 수정 (#113)

* feat : UserRegisterResponse 필드 추가(#113)

* test: API 변경에 따른 테스트 관련 코드 수정 (#113)
  • Loading branch information
choidongkuen authored Jan 18, 2024
1 parent 7f87be9 commit 43fd45c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package net.teumteum.user.domain.response;

import net.teumteum.auth.domain.response.TokenResponse;

public record UserRegisterResponse(
Long id
Long id,
String accessToken,
String refreshToken
) {

public static UserRegisterResponse of(Long id, TokenResponse tokenResponse) {
return new UserRegisterResponse(id, tokenResponse.getAccessToken(), tokenResponse.getRefreshToken());
}
}
5 changes: 4 additions & 1 deletion src/main/java/net/teumteum/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import net.teumteum.core.security.Authenticated;
import net.teumteum.core.security.service.JwtService;
import net.teumteum.core.security.service.RedisService;
import net.teumteum.core.security.service.SecurityService;
import net.teumteum.user.domain.BalanceGameType;
Expand All @@ -29,6 +30,7 @@ public class UserService {
private final UserRepository userRepository;
private final InterestQuestion interestQuestion;
private final RedisService redisService;
private final JwtService jwtService;

public UserGetResponse getUserById(Long userId) {
var existUser = getUser(userId);
Expand Down Expand Up @@ -79,8 +81,9 @@ public void withdraw(Long userId) {
@Transactional
public UserRegisterResponse register(UserRegisterRequest request) {
checkUserExistence(request.authenticated(), request.id());
User savedUser = userRepository.save(request.toUser());

return new UserRegisterResponse(userRepository.save(request.toUser()).getId());
return UserRegisterResponse.of(savedUser.getId(), jwtService.createServiceToken(savedUser));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import net.teumteum.core.error.ErrorResponse;
import net.teumteum.user.domain.User;
import net.teumteum.user.domain.UserFixture;
import net.teumteum.user.domain.response.FriendsResponse;
import net.teumteum.user.domain.response.UserGetResponse;
import net.teumteum.user.domain.response.UserMeGetResponse;
Expand Down Expand Up @@ -271,9 +272,7 @@ class Register_user_card_api {
@DisplayName("등록할 회원의 정보가 주어지면, 회원 정보를 저장한다.")
void Register_user_info() {
// given
var additionalUser = repository.saveAndGetUser();

var UserRegister = RequestFixture.userRegisterRequest(additionalUser);
var UserRegister = RequestFixture.userRegisterRequest(UserFixture.getIdUser());
// when
var result = api.registerUserCard(VALID_TOKEN, UserRegister);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.teumteum.unit.user.controller;

import static net.teumteum.unit.auth.common.SecurityValue.VALID_ACCESS_TOKEN;
import static net.teumteum.unit.auth.common.SecurityValue.VALID_REFRESH_TOKEN;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
Expand Down Expand Up @@ -72,7 +73,7 @@ void Register_user_card_with_201_created() throws Exception {
// given
UserRegisterRequest request = RequestFixture.userRegisterRequest(user);

UserRegisterResponse response = new UserRegisterResponse(1L);
UserRegisterResponse response = new UserRegisterResponse(1L, VALID_ACCESS_TOKEN, VALID_REFRESH_TOKEN);

given(userService.register(any(UserRegisterRequest.class))).willReturn(response);

Expand All @@ -84,7 +85,9 @@ void Register_user_card_with_201_created() throws Exception {
.header(AUTHORIZATION, VALID_ACCESS_TOKEN))
.andDo(print())
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value(1));
.andExpect(jsonPath("$.id").value(1))
.andExpect(jsonPath("$.accessToken").value(VALID_ACCESS_TOKEN))
.andExpect(jsonPath("$.refreshToken").value(VALID_REFRESH_TOKEN));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package net.teumteum.unit.user.service;

import static net.teumteum.unit.auth.common.SecurityValue.VALID_ACCESS_TOKEN;
import static net.teumteum.unit.auth.common.SecurityValue.VALID_REFRESH_TOKEN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;

import net.teumteum.auth.domain.response.TokenResponse;
import net.teumteum.core.security.service.JwtService;
import net.teumteum.core.security.service.RedisService;
import net.teumteum.integration.RequestFixture;
import net.teumteum.user.domain.User;
Expand Down Expand Up @@ -35,6 +39,9 @@ public class UserServiceTest {
@Mock
RedisService redisService;

@Mock
JwtService jwtService;

private User user;

@BeforeEach
Expand All @@ -51,14 +58,21 @@ class Register_user_card_api_unit {
void If_valid_user_request_register_user_card() {
// given
UserRegisterRequest request = RequestFixture.userRegisterRequest(user);
TokenResponse tokenResponse = new TokenResponse(VALID_ACCESS_TOKEN, VALID_REFRESH_TOKEN);

UserRegisterResponse response = UserRegisterResponse.of(1L, tokenResponse);

given(userRepository.save(any(User.class))).willReturn(user);

given(jwtService.createServiceToken(any(User.class))).willReturn(tokenResponse);

// when
UserRegisterResponse response = userService.register(request);
UserRegisterResponse result = userService.register(request);

// then
assertThat(response.id()).isEqualTo(1);
assertThat(response.accessToken()).isNotNull();
assertThat(response.refreshToken()).isNotNull();
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/teumteum/user/domain/UserFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static class UserBuilder {
@Builder.Default
private String name = "Jennifer";
@Builder.Default
private String birth = "2000.02.05";
private String birth = "20000205";
@Builder.Default
private Long characterId = 1L;
@Builder.Default
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ spring.data.redis.port=6378
### JWT ###
jwt.bearer=Bearer
jwt.secret=a2FyaW10b2thcmltdG9rYXJpbXRva2FyaW10b2thcmltdG9rYXJpbXRva2FyaW10b2thcmltdG9rYXJpbXRva2FyaW10b2thcmltdG9rYXJpbXRvsdsadwsadasdwSDSAweasDSadwXJsecretsecretsecretsecretsecreetsecret
jwt.access.header=Authorization
jwt.access.expiration=3600000
jwt.refresh.expiration=3600000
jwt.refresh.header=Authorization-refresh

0 comments on commit 43fd45c

Please sign in to comment.