Skip to content
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

edit : OAuth 로그인 프로세스 변경 #169

Merged
merged 15 commits into from
Apr 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

public record OAuthUserInfo(
String email,
String name,
String nickname,
String imageUrl,
String birthYear,
String ageRange,
String gender,
String provider,
String providerId
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
@RequiredArgsConstructor
public class OAuth2LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

private final String DOMAIN = "https://team-civilwar-boardsignal-fe.pages.dev";
private final String DOMAIN = "http://localhost:5173";
// "https://team-civilwar-boardsignal-fe.pages.dev";
private final String REFRESHTOKEN_NAME = "RefreshToken_Id";

private final AuthService authService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ public final class AuthApiMapper {
public static UserLoginRequest toUserLoginRequest(OAuthUserInfo oAuthUserInfo) {
return new UserLoginRequest(
oAuthUserInfo.email(),
oAuthUserInfo.name(),
oAuthUserInfo.nickname(),
oAuthUserInfo.imageUrl(),
oAuthUserInfo.birthYear(),
oAuthUserInfo.ageRange(),
oAuthUserInfo.gender(),
oAuthUserInfo.provider(),
oAuthUserInfo.providerId()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,14 @@ private static OAuthUserInfo kakaoUserInfo(Map<String, Object> userAttributes,
Map<String, Object> profile = (Map<String, Object>) kakaoAccount.get("profile");

String email = (String) kakaoAccount.get("email");
String name = (String) kakaoAccount.get("name");
String nickName = (String) profile.get("nickname");
String imageUrl = (String) profile.get("profile_image_url");
String birthYear = (String) kakaoAccount.get("birthyear");
String ageRange = (String) kakaoAccount.get("age_range");
String gender = (String) kakaoAccount.get("gender");
String providerId = userAttributes.get("id").toString();

return new OAuthUserInfo(
email,
name,
nickName,
imageUrl,
birthYear,
ageRange,
gender,
provider,
providerId
);
Expand All @@ -54,26 +46,18 @@ private static OAuthUserInfo kakaoUserInfo(Map<String, Object> userAttributes,
private static OAuthUserInfo naverUserInfo(Map<String, Object> userAttributes,
String provider) {

Map<String, String> kakaoAccount = (Map<String, String>) userAttributes
Map<String, String> naverAccount = (Map<String, String>) userAttributes
.get("response");

String email = kakaoAccount.get("email");
String name = kakaoAccount.get("name");
String nickName = kakaoAccount.get("nickname");
String imageUrl = kakaoAccount.get("profile_image");
String birthYear = kakaoAccount.get("birthyear");
String ageRange = kakaoAccount.get("age");
String gender = kakaoAccount.get("gender");
String providerId = kakaoAccount.get("id");
String email = naverAccount.get("email");
String nickName = naverAccount.get("nickname");
String imageUrl = naverAccount.get("profile_image");
String providerId = naverAccount.get("id");

return new OAuthUserInfo(
email,
name,
nickName,
imageUrl,
birthYear,
ageRange,
gender,
provider,
providerId
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public record ApiUserModifyRequest(
String nickName,
String gender,
int birth,
String line,
String station,
List<String> categories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static UserModifyRequest toUserModifyRequest(Long userId,
userId,
apiUserModifyRequest.nickName(),
userCategories,
apiUserModifyRequest.gender(),
apiUserModifyRequest.birth(),
apiUserModifyRequest.line(),
apiUserModifyRequest.station(),
image
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.civilwar.boardsignal.auth.presentation;

import static org.mockito.BDDMockito.given;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand All @@ -10,21 +9,18 @@
import com.civilwar.boardsignal.auth.domain.model.Token;
import com.civilwar.boardsignal.auth.exception.AuthErrorCode;
import com.civilwar.boardsignal.auth.infrastructure.JwtTokenProvider;
import com.civilwar.boardsignal.boardgame.domain.constant.Category;
import com.civilwar.boardsignal.common.support.ApiTestSupport;
import com.civilwar.boardsignal.user.UserFixture;
import com.civilwar.boardsignal.user.domain.constants.Role;
import com.civilwar.boardsignal.user.domain.entity.User;
import com.civilwar.boardsignal.user.domain.repository.UserRepository;
import jakarta.servlet.http.Cookie;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Supplier;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.transaction.annotation.Transactional;

@DisplayName("[AuthApiController 테스트]")
class AuthApiControllerTest extends ApiTestSupport {
Expand Down Expand Up @@ -95,41 +91,6 @@ void logoutTest2() throws Exception {
.andExpect(jsonPath("$.logoutResult").value(false));
}

@Test
@Transactional
@DisplayName("[AccessToken의 사용자 정보를 반환한다.]")
void getLoginUserInfoTest() throws Exception {
//given
User loginUserEntity = userRepository.findById(loginUser.getId())
.orElseThrow();

String updateNickname = "업데이트된 닉네임";
List<Category> categories = List.of(Category.CUSTOMIZABLE, Category.PARTY);
String subwayLine = "2호선";
String subwayStation = "사당역";
String image = "update image";
loginUserEntity.updateUser(updateNickname, categories, subwayLine, subwayStation, image);

LocalDateTime now = LocalDateTime.of(2024, 3, 4, 9, 27, 0);
given(nowTime.get()).willReturn(now);

int expectedAge = nowTime.get().getYear() - loginUser.getBirth() + 1;

//then
mockMvc.perform(get("/api/v1/auth")
.header(AUTHORIZATION, accessToken))
.andExpect(jsonPath("$.id").value(loginUser.getId()))
.andExpect(jsonPath("$.email").value(loginUser.getEmail()))
.andExpect(jsonPath("$.nickname").value(updateNickname))
.andExpect(jsonPath("$.ageGroup").value(loginUser.getAgeGroup().getDescription()))
.andExpect(jsonPath("$.gender").value(loginUser.getGender().getDescription()))
.andExpect(jsonPath("$.isJoined").value(true))
.andExpect(jsonPath("$.age").value(expectedAge))
.andExpect(jsonPath("$.subwayLine").value(subwayLine))
.andExpect(jsonPath("$.subwayStation").value(subwayStation))
.andExpect(jsonPath("$.categories.size()").value(2));
}

@Test
@DisplayName("[인증이 안 된 사용자가 접근 시 401 에러와 전용 에러 메시지 & 코드를 반환한다.]")
void exceptionHandlingTest() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.civilwar.boardsignal.room.dto.response.ParticipantJpaDto;
import com.civilwar.boardsignal.room.infrastructure.repository.MeetingInfoJpaRepository;
import com.civilwar.boardsignal.user.UserFixture;
import com.civilwar.boardsignal.user.domain.constants.AgeGroup;
import com.civilwar.boardsignal.user.domain.constants.Gender;
import com.civilwar.boardsignal.user.domain.constants.Role;
import com.civilwar.boardsignal.user.domain.entity.User;
Expand Down Expand Up @@ -781,14 +780,10 @@ void participantRoomTest3() throws Exception {
roomRepository.save(room);

User user = User.of("email",
"name",
"nickName",
"provider",
"providerId",
"testURL",
1998,
AgeGroup.TWENTY,
Gender.MALE);
"testURL");
User savedUser = userRepository.save(user);

Token token = tokenProvider.createToken(savedUser.getId(), Role.USER);
Expand Down Expand Up @@ -829,14 +824,10 @@ void participantRoomTest4() throws Exception {
roomRepository.save(room);

User user = User.of("email",
"name",
"nickName",
"provider",
"providerId",
"testURL",
2020,
AgeGroup.TWENTY,
Gender.MALE);
"testURL");
User savedUser = userRepository.save(user);

Token token = tokenProvider.createToken(savedUser.getId(), Role.USER);
Expand Down Expand Up @@ -877,14 +868,10 @@ void participantRoomTest5() throws Exception {
roomRepository.save(room);

User user = User.of("email",
"name",
"nickName",
"provider",
"providerId",
"testURL",
1998,
AgeGroup.TWENTY,
Gender.MALE);
"testURL");
User savedUser = userRepository.save(user);

blackListRepository.save(RoomBlackList.of(room.getId(), savedUser.getId()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.civilwar.boardsignal.user.presentation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
Expand All @@ -20,6 +21,8 @@
import com.civilwar.boardsignal.review.domain.entity.ReviewEvaluation;
import com.civilwar.boardsignal.review.domain.repository.ReviewRepository;
import com.civilwar.boardsignal.user.UserFixture;
import com.civilwar.boardsignal.user.domain.constants.AgeGroup;
import com.civilwar.boardsignal.user.domain.constants.Gender;
import com.civilwar.boardsignal.user.domain.constants.OAuthProvider;
import com.civilwar.boardsignal.user.domain.constants.Role;
import com.civilwar.boardsignal.user.domain.entity.User;
Expand All @@ -28,10 +31,13 @@
import com.civilwar.boardsignal.user.dto.request.ValidNicknameRequest;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Supplier;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
Expand All @@ -47,12 +53,16 @@ class UserApiControllerTest extends ApiTestSupport {
private TokenProvider tokenProvider;
@Autowired
private WishRepository wishRepository;
@MockBean
private Supplier<LocalDateTime> now;

@Test
@DisplayName("[회원은 정보를 수정 할 수 있다]")
void joinUserTest() throws Exception {

//given
given(now.get()).willReturn(LocalDateTime.of(2024, 4, 16, 0, 0, 0));

User userFixture = UserFixture.getUserFixture(OAuthProvider.KAKAO.getType(), "testURL");
userRepository.save(userFixture);

Expand All @@ -61,6 +71,8 @@ void joinUserTest() throws Exception {

ApiUserModifyRequest apiUserModifyRequest = new ApiUserModifyRequest(
"injuning",
Gender.MALE.getDescription(),
1970,
"2호선",
"사당역",
List.of("가족게임", "파티게임")
Expand Down Expand Up @@ -99,13 +111,16 @@ void joinUserTest() throws Exception {
assertThat(findUser.getNickname()).isEqualTo(apiUserModifyRequest.nickName());
assertThat(findUser.getLine()).isEqualTo(apiUserModifyRequest.line());
assertThat(findUser.getStation()).isEqualTo(apiUserModifyRequest.station());
assertThat(findUser.getAgeGroup()).isEqualTo(AgeGroup.FIFTY);
}


@Test
@DisplayName("유저의 자신의 프로필을 조회할 수 있다.")
void getUserProfileTest() throws Exception {
//given
given(now.get()).willReturn(LocalDateTime.of(2024, 4, 16, 0, 0, 0));

List<ReviewEvaluation> evaluationFixture = ReviewFixture.getEvaluationFixture();
Review review = ReviewFixture.getReviewFixture(100L, loginUser.getId(), 1L,
evaluationFixture);
Expand Down Expand Up @@ -145,6 +160,8 @@ void getUserProfileTest() throws Exception {
@DisplayName("타인의 프로필을 조회할 수 있다.")
void getUserProfileTest2() throws Exception {
//given
given(now.get()).willReturn(LocalDateTime.of(2024, 4, 16, 0, 0, 0));

User anotherUser = UserFixture.getUserFixture2("providerId", "testUrl");
userRepository.save(anotherUser);
List<ReviewEvaluation> evaluationFixture = ReviewFixture.getEvaluationFixture();
Expand Down Expand Up @@ -202,7 +219,8 @@ void validNicknameTest1() throws Exception {
void validNicknameTest2() throws Exception {
//given
String existName = loginUser.getName();
loginUser.updateUser(existName, List.of(Category.CUSTOMIZABLE), "2호선", "사당역", "testURL");
loginUser.updateUser(existName, List.of(Category.CUSTOMIZABLE), Gender.MALE, 2000,
AgeGroup.TWENTY, "2호선", "사당역", "testURL");
userRepository.save(loginUser);
ValidNicknameRequest validNicknameRequest = new ValidNicknameRequest(existName);

Expand Down Expand Up @@ -240,7 +258,8 @@ void validNicknameTest4() throws Exception {
User anotherUser = UserFixture.getUserFixture("providerId", "testURL");
String existName = anotherUser.getName();
userRepository.save(anotherUser);
anotherUser.updateUser(existName, List.of(Category.CUSTOMIZABLE), "2호선", "사당역", "testURL");
anotherUser.updateUser(existName, List.of(Category.CUSTOMIZABLE), Gender.MALE, 2000,
AgeGroup.TWENTY, "2호선", "사당역", "testURL");
userRepository.save(loginUser);

ValidNicknameRequest validNicknameRequest = new ValidNicknameRequest(existName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import com.civilwar.boardsignal.auth.dto.response.UserLoginResponse;
import com.civilwar.boardsignal.auth.dto.response.UserLogoutResponse;
import com.civilwar.boardsignal.auth.mapper.AuthMapper;
import com.civilwar.boardsignal.user.domain.constants.AgeGroup;
import com.civilwar.boardsignal.user.domain.constants.Gender;
import com.civilwar.boardsignal.user.domain.entity.User;
import com.civilwar.boardsignal.user.domain.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -33,14 +31,10 @@ public UserLoginResponse login(UserLoginRequest userLoginRequest) {
user = userRepository.save(
User.of(
userLoginRequest.email(),
userLoginRequest.name(),
userLoginRequest.nickname(),
userLoginRequest.provider(),
userLoginRequest.providerId(),
userLoginRequest.imageUrl(),
Integer.parseInt(userLoginRequest.birthYear()),
AgeGroup.of(userLoginRequest.ageRange(), userLoginRequest.provider()),
Gender.of(userLoginRequest.gender(), userLoginRequest.provider())
userLoginRequest.imageUrl()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

public record UserLoginRequest(
String email,
String name,
String nickname,
String imageUrl,
String birthYear,
String ageRange,
String gender,
String provider,
String providerId
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.civilwar.boardsignal.common.exception.NotFoundException;
import com.civilwar.boardsignal.image.domain.ImageRepository;
import com.civilwar.boardsignal.room.domain.repository.RoomRepository;
import com.civilwar.boardsignal.user.domain.constants.AgeGroup;
import com.civilwar.boardsignal.user.domain.constants.Gender;
import com.civilwar.boardsignal.user.domain.entity.User;
import com.civilwar.boardsignal.user.domain.entity.UserCategory;
import com.civilwar.boardsignal.user.domain.repository.UserRepository;
Expand Down Expand Up @@ -50,6 +52,9 @@ public UserModifyResponse modifyUser(UserModifyRequest userModifyRequest) {
findUser.updateUser(
userModifyRequest.nickName(),
userModifyRequest.categories(),
Gender.toGender(userModifyRequest.gender()),
userModifyRequest.birth(),
AgeGroup.convert(userModifyRequest.birth(), now.get()),
userModifyRequest.line(),
userModifyRequest.station(),
profileImageUrl
Expand Down
Loading
Loading