Skip to content

Commit

Permalink
Merge pull request #45 from Project-Catcher/feat-hs-user-info
Browse files Browse the repository at this point in the history
내 정보 가져오기 구현
  • Loading branch information
toad0403 authored Dec 10, 2023
2 parents 0222f6e + 119db95 commit fe4658c
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/catcher/core/dto/user/UserInfoResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.catcher.core.dto.user;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.ZonedDateTime;

@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class UserInfoResponse {
private String username;
private String phone;
private String email;
private String profileImageUrl;
private String nickname;
private ZonedDateTime emailMarketingTerm;
private ZonedDateTime phoneMarketingTerm;
}
11 changes: 11 additions & 0 deletions src/main/java/com/catcher/core/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.catcher.core.domain.entity.User;
import com.catcher.core.dto.TokenDto;
import com.catcher.core.dto.user.UserCreateRequest;
import com.catcher.core.dto.user.UserInfoResponse;
import com.catcher.core.dto.user.UserLoginRequest;
import com.catcher.security.CatcherUser;
import com.catcher.utils.KeyGenerator;
Expand Down Expand Up @@ -118,4 +119,14 @@ private void checkOAuthUser(User user) {
throw new BaseException(INVALID_USER_INFO);
}
}

public UserInfoResponse getMyInfo(User user){
return new UserInfoResponse(user.getUsername(),
user.getPhone(),
user.getEmail(),
user.getProfileImageUrl(),
user.getNickname(),
user.getEmailMarketingTerm(),
user.getPhoneMarketingTerm());
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/catcher/resource/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import cn.apiclub.captcha.Captcha;
import com.catcher.common.response.CommonResponse;
import com.catcher.core.domain.entity.User;
import com.catcher.core.dto.TokenDto;
import com.catcher.core.dto.user.UserCreateRequest;
import com.catcher.core.dto.user.UserInfoResponse;
import com.catcher.core.dto.user.UserLoginRequest;
import com.catcher.core.service.AuthCodeService;
import com.catcher.core.service.CaptchaService;
Expand All @@ -15,6 +17,7 @@
import com.catcher.resource.request.CaptchaValidateRequest;
import com.catcher.resource.response.AuthCodeVerifyResponse;
import com.catcher.resource.response.CaptchaValidateResponse;
import com.catcher.security.annotation.CurrentUser;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -109,4 +112,10 @@ public CommonResponse<CaptchaValidateResponse> validateCaptcha(final CaptchaVali
return success(new CaptchaValidateResponse(isValidated));

}

@Operation(summary = "내 정보 가져오기")
@GetMapping("/info")
public CommonResponse<UserInfoResponse> getMyInfo(@CurrentUser User user){
return success(userService.getMyInfo(user));
}
}
26 changes: 24 additions & 2 deletions src/test/java/com/catcher/resource/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import com.catcher.core.domain.entity.User;
import com.catcher.core.domain.entity.enums.UserRole;
import com.catcher.core.dto.user.UserCreateRequest;
import com.catcher.core.dto.user.UserInfoResponse;
import com.catcher.core.dto.user.UserLoginRequest;
import com.catcher.testconfiguriation.EmbeddedRedisConfiguration;
import com.catcher.testconfiguriation.WithCustomMockUser;
import com.catcher.utils.KeyGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
Expand All @@ -24,6 +27,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
Expand All @@ -39,8 +43,7 @@
import static com.catcher.utils.JwtUtils.REFRESH_TOKEN_NAME;
import static com.catcher.utils.KeyGenerator.AuthType.BLACK_LIST_ACCESS_TOKEN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ActiveProfiles("test")
Expand Down Expand Up @@ -72,6 +75,7 @@ void beforeEach() {
.standaloneSetup(userController)
.setControllerAdvice(new CatcherControllerAdvice())
.addFilter(new CharacterEncodingFilter("UTF-8", true))
.setCustomArgumentResolvers(new AuthenticationPrincipalArgumentResolver())
.build();
user = userRepository.save(createUser());
flushAndClearPersistence();
Expand Down Expand Up @@ -371,6 +375,24 @@ void invalid_logout() throws Exception {
assertThat(dbManager.getValue(KeyGenerator.generateKey(invalidAccessToken, BLACK_LIST_ACCESS_TOKEN))).isEmpty();
}

@DisplayName("로그인 한 유저의 정보를 정상적으로 불러옴")
@Test
@WithCustomMockUser(username = "test", role = UserRole.USER)
void valid_getMyInfo() throws Exception {
//given

//when
MvcResult mvcResult = mockMvc.perform(get("/users/info")
.contentType(MediaType.APPLICATION_JSON)
).andReturn();

CommonResponse<UserInfoResponse> commonResponse = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), new TypeReference<>(){});

//then
assertThat(commonResponse.isSuccess()).isTrue();
assertThat(commonResponse.getResult().getUsername()).isEqualTo("test");
}

private UserCreateRequest userCreateRequest(String username, String nickname, String phone, String email) {
return UserCreateRequest.builder()
.nickname(nickname)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.catcher.testconfiguriation;

import com.catcher.core.domain.entity.enums.UserRole;
import org.springframework.security.test.context.support.WithSecurityContext;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.time.ZonedDateTime;

@Retention(RetentionPolicy.RUNTIME)
@WithSecurityContext(factory = WithCustomMockUserSecurityContextFactory.class)
public @interface WithCustomMockUser{
Long id() default 1L;
String username() default "username";
String password() default "password";
String phone() default "phone";
String email() default "email";
String profileImageUrl() default "profileImageUrl";
String introduceContent() default "introduceContent";
String nickname() default "nickname";
UserRole role();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.catcher.testconfiguriation;

import com.catcher.core.domain.entity.User;
import com.catcher.core.domain.entity.enums.UserRole;
import com.catcher.security.CatcherUser;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithSecurityContextFactory;

import java.time.ZonedDateTime;

import static com.catcher.core.domain.entity.enums.UserProvider.CATCHER;


public class WithCustomMockUserSecurityContextFactory implements WithSecurityContextFactory<WithCustomMockUser> {

@Override
public SecurityContext createSecurityContext(WithCustomMockUser annotation) {
Long id = annotation.id();
String username = annotation.username();
String password = annotation.password();
String phone = annotation.phone();
String email = annotation.email();
String profileImageUrl = annotation.profileImageUrl();
String introduceContent = annotation.introduceContent();
String nickname = annotation.nickname();
UserRole role = annotation.role();

CatcherUser user =
new CatcherUser(createUser(id, username, password, phone, email, profileImageUrl, introduceContent, nickname, role));

UsernamePasswordAuthenticationToken token =
new UsernamePasswordAuthenticationToken(user, password);
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(token);
return context;
}

private User createUser(long id, String username, String password, String phone, String email, String profileImageUrl, String introduceContent, String nickname, UserRole role) {
return User.builder()
.id(id)
.username(username)
.password(password)
.phone(phone)
.email(email)
.profileImageUrl(profileImageUrl)
.introduceContent(introduceContent)
.nickname(nickname)
.userProvider(CATCHER)
.userRole(role)
.userAgeTerm(ZonedDateTime.now())
.userServiceTerm(ZonedDateTime.now())
.userPrivacyTerm(ZonedDateTime.now())
.emailMarketingTerm(ZonedDateTime.now())
.phoneMarketingTerm(ZonedDateTime.now())
.build();
}
}

0 comments on commit fe4658c

Please sign in to comment.