Skip to content

Commit

Permalink
[#83] test : 카카오 아이디로 회원가입이 되는지 테스트 한다
Browse files Browse the repository at this point in the history
  • Loading branch information
packdev937 committed Jan 18, 2024
1 parent e9045fa commit 89ad8bd
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Schema(title = "카카오 로그인 및 회원가입")
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class KakaoLogin {
public record KakaoLoginRequest(
@NotBlank(message = "이메일을 입력해주세요.")
@Email(message = "올바른 이메일 주소를 입력해주세요.")
@Schema(description = "이메일", example = "[email protected]")
private String email;
String email,

@Schema(description = "providerId", example = "10378247832195")
private String providerId;
String providerId
) {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ssu.eatssu.domain.auth.entity;
package ssu.eatssu.domain.auth.infrastructure;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
Expand All @@ -13,6 +13,7 @@
import org.springframework.web.filter.GenericFilterBean;

import java.io.IOException;
import ssu.eatssu.domain.auth.entity.JwtTokenProvider;

@Slf4j
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import ssu.eatssu.domain.auth.entity.JwtAuthenticationFilter;
import ssu.eatssu.domain.auth.entity.JwtTokenProvider;
import ssu.eatssu.global.handler.JwtAccessDeniedHandler;
import ssu.eatssu.global.handler.JwtAuthenticationEntryPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ssu.eatssu.domain.user.repository.UserRepository;
import ssu.eatssu.domain.auth.service.OauthService;
import ssu.eatssu.global.handler.response.BaseResponse;
import ssu.eatssu.domain.auth.dto.AppleLogin;
import ssu.eatssu.domain.auth.dto.KakaoLogin;
import ssu.eatssu.domain.auth.dto.KakaoLoginRequest;
import ssu.eatssu.domain.user.dto.Tokens;

import java.security.NoSuchAlgorithmException;
Expand All @@ -43,8 +42,8 @@ public class OauthController {
@ApiResponse(responseCode = "200", description = "카카오 회원가입/로그인 성공", content = @Content(schema = @Schema(implementation = BaseResponse.class)))
})
@PostMapping("/kakao")
public BaseResponse<Tokens> kakaoLogin(@Valid @RequestBody KakaoLogin login) {
Tokens tokens = oauthService.kakaoLogin(login.getEmail(), login.getProviderId());
public BaseResponse<Tokens> kakaoLogin(@Valid @RequestBody KakaoLoginRequest request) {
Tokens tokens = oauthService.kakaoLogin(request);
return BaseResponse.success(tokens);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package ssu.eatssu.domain.auth.entity;
package ssu.eatssu.domain.auth.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import ssu.eatssu.domain.auth.entity.CustomUserDetails;
import ssu.eatssu.domain.user.entity.User;
import ssu.eatssu.domain.user.repository.UserRepository;
import ssu.eatssu.global.handler.response.BaseException;
Expand Down
39 changes: 13 additions & 26 deletions src/main/java/ssu/eatssu/domain/auth/service/OauthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import ssu.eatssu.domain.auth.dto.KakaoLoginRequest;
import ssu.eatssu.domain.user.entity.User;
import ssu.eatssu.domain.auth.entity.OauthProvider;
import ssu.eatssu.domain.user.repository.UserRepository;
Expand All @@ -43,54 +44,40 @@
@Transactional
public class OauthService {

private final UserService userService;
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final UserService userService;
private final RestTemplate restTemplate;

/**
* 카카오 로그인
*/
public Tokens kakaoLogin(String email, String providerId) {

//가입 안된 유저일 경우 회원가입 진행
User user = userRepository.findByProviderId(providerId)
.orElseGet(() -> join(email, OauthProvider.KAKAO, providerId));
public Tokens kakaoLogin(KakaoLoginRequest request) {
User user = userRepository.findByProviderId(request.providerId())
.orElseGet(() -> join(request.email(), OauthProvider.KAKAO, request.providerId()));

// return Tokens
return userService.generateOauthJwtTokens(user.getEmail(), OauthProvider.KAKAO, providerId);
return userService.generateOauthJwtTokens(user.getEmail(), OauthProvider.KAKAO,
request.providerId());
}

/**
* 애플 로그인
*/

public Tokens appleLogin(String identityToken) {

//애플 유저 정보 조회
OauthInfo oauthInfo = getUserInfoFromApple(identityToken);


//가입 안된 유저일 경우 회원가입 진행
User user = userRepository.findByProviderId(oauthInfo.providerId())
.orElseGet(() -> join(oauthInfo.email(), OauthProvider.APPLE, oauthInfo.providerId()));

.orElseGet(() -> join(oauthInfo.email(), OauthProvider.APPLE, oauthInfo.providerId()));

//이메일 갱신
updateAppleUserEmail(user, oauthInfo.email());

// return Tokens
return userService.generateOauthJwtTokens(user.getEmail(), OauthProvider.APPLE, oauthInfo.providerId());
return userService.generateOauthJwtTokens(user.getEmail(), OauthProvider.APPLE,
oauthInfo.providerId());
}

/**
* 회원가입
*/

private User join(String email, OauthProvider provider, String providerId) {
String credentials = createCredentials(provider, providerId);

//회원가입
User user = User.oAuthJoin(email, provider, providerId, credentials);
return userRepository.save(user);
}
Expand All @@ -107,7 +94,6 @@ private String createCredentials(OauthProvider provider, String providerId) {
* 애플 유저 private email -> 찐 email 로 갱신
*/
private void updateAppleUserEmail(User user, String email) {

if (isHideEmail(user.getEmail()) && !isHideEmail(email)) {
user.updateEmail(email);
userRepository.save(user);
Expand Down Expand Up @@ -198,8 +184,9 @@ private AppleKeys.Key selectMatchedKey(String identityToken, AppleKeys candidate
//decode 된 header 정보를 통해 정답키의 key id, algorithm 정보를 가져온다.
Map<String, String> headerMap;
try {
headerMap = new ObjectMapper().readValue(decodedHeader, new TypeReference<Map<String, String>>() {
});
headerMap = new ObjectMapper().readValue(decodedHeader,
new TypeReference<Map<String, String>>() {
});
} catch (JsonProcessingException e) {
throw new BaseException(INVALID_IDENTITY_TOKEN);
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/ssu/eatssu/domain/auth/service/OauthServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ssu.eatssu.domain.auth.service;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ssu.eatssu.domain.auth.dto.KakaoLoginRequest;
import ssu.eatssu.domain.user.entity.User;
import ssu.eatssu.domain.user.repository.UserRepository;

@SpringBootTest
class OauthServiceTest {

@Autowired
private OauthService oauthService;

@Autowired
private UserRepository userRepository;

@Test
void 카카오로_회원가입을_한다() {
// given
KakaoLoginRequest request = new KakaoLoginRequest("[email protected]", "10378247832195");

// when
oauthService.kakaoLogin(request);
User user = userRepository.findByProviderId(request.providerId()).get();

// then
assertThat(userRepository.findAll()).hasSize(1);
assertThat(user.getEmail()).isEqualTo(request.email());
assertThat(user.getProviderId()).isEqualTo(request.providerId());
}

}
29 changes: 29 additions & 0 deletions src/test/java/ssu/eatssu/domain/user/service/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ssu.eatssu.domain.user.service;

import static org.junit.jupiter.api.Assertions.*;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ssu.eatssu.domain.user.repository.UserRepository;

@SpringBootTest
class UserServiceTest {

@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;

@Test
void 회원_탈퇴_성공시_참을_반환한다() {
// given

// when
// then
Assertions.assertThat(userRepository.findAll()).hasSize(0);

}

}

0 comments on commit 89ad8bd

Please sign in to comment.