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

Feature 194/join logic change #195

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
38 changes: 33 additions & 5 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ include::{snippets}/api/v1/oauth/jwt/google/http-response.adoc[]
.response fields
include::{snippets}/api/v1/oauth/jwt/google/response-fields.adoc[]

== POST /api/v1/oauth/jwt/kakao
== POST /api/v1/oauth/jwt/kakao - 로그인

.request
include::{snippets}/api/v1/oauth/jwt/kakao/http-request.adoc[]
Expand All @@ -37,6 +37,20 @@ include::{snippets}/api/v1/oauth/jwt/kakao/http-response.adoc[]
.response fields
include::{snippets}/api/v1/oauth/jwt/kakao/response-fields.adoc[]

== POST /api/v1/oauth/jwt/kakao - 가입필요

.request
include::{snippets}/api/v1/oauth/jwt/kakao/notjoin/http-request.adoc[]

.request fields
include::{snippets}/api/v1/oauth/jwt/kakao/notjoin/request-fields.adoc[]

.response
include::{snippets}/api/v1/oauth/jwt/kakao/notjoin/http-response.adoc[]

.response fields
include::{snippets}/api/v1/oauth/jwt/kakao/notjoin/response-fields.adoc[]

== POST /api/v1/oauth/jwt/naver

.request
Expand Down Expand Up @@ -65,6 +79,20 @@ include::{snippets}/api/v1/oauth/jwt/get/naver/http-response.adoc[]
.response fields
include::{snippets}/api/v1/oauth/jwt/get/naver/response-fields.adoc[]

== POST /api/v1/oauth/join/kakao

.request
include::{snippets}/api/v1/oauth/join/kakao/http-request.adoc[]

.request fields
include::{snippets}/api/v1/oauth/join/kakao/request-fields.adoc[]

.response
include::{snippets}/api/v1/oauth/join/kakao/http-response.adoc[]

.response fields
include::{snippets}/api/v1/oauth/join/kakao/response-fields.adoc[]

= 사용자

== GET /api/v1/member/me
Expand All @@ -81,16 +109,16 @@ include::{snippets}/api/v1/member/me/get/response-fields.adoc[]
== GET /api/v1/member

.request
include::{snippets}/api/v1/member/http-request.adoc[]
include::{snippets}/api/v1/member/get/http-request.adoc[]

.request parameters
include::{snippets}/api/v1/member/request-parameters.adoc[]
include::{snippets}/api/v1/member/get/request-parameters.adoc[]

.response
include::{snippets}/api/v1/member/http-response.adoc[]
include::{snippets}/api/v1/member/get/http-response.adoc[]

.response fields
include::{snippets}/api/v1/member/response-fields.adoc[]
include::{snippets}/api/v1/member/get/response-fields.adoc[]

== PATCH /api/v1/member/me

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ResponseMessage {
public static final String UPDATE = "update";
public static final String DELETED = "deleted";
public static final String SUBMITTED = "submitted";
public static final String USER_NOT_JOINED = "회원 가입이 필요합니다";

// friend
public static final String READ_MEMBER_FRIENDS = "사용자 친구 목록을 조회합니다.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.meme.ala.core.auth.jwt.JwtProvider;
import com.meme.ala.core.auth.oauth.model.OAuthUserInfo;
import com.meme.ala.core.auth.oauth.service.OAuthService;
import com.meme.ala.domain.member.model.dto.JwtVO;
import com.meme.ala.domain.member.service.MemberAuthService;
import com.meme.ala.domain.member.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -21,7 +19,6 @@
@RestController
public class MemberAuthController {
private final MemberService memberService;
private final MemberAuthService memberAuthService;
private final OAuthService oAuthService;
private final JwtProvider jwtProvider;

Expand All @@ -34,23 +31,28 @@ public ResponseEntity<ResponseDto<String>> jwtCreate(@RequestBody Map<String, Ob
OAuthUserInfo authUserInfo = oAuthService.getMemberByProvider(data, provider);

if (!memberService.existsProviderId(authUserInfo.getProviderId())) {
oAuthMap.put("message", ResponseMessage.JOIN);
memberService.join(authUserInfo, provider);
} else
oAuthMap.put("message", ResponseMessage.LOGIN);

return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseDto.of(HttpStatus.OK, ResponseMessage.USER_NOT_JOINED, ResponseMessage.USER_NOT_JOINED));
}
oAuthMap.put("message", ResponseMessage.LOGIN);
oAuthMap.put("jwt", jwtProvider.createToken(authUserInfo.getProviderId()));

return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseDto.of(HttpStatus.OK, oAuthMap.get("message"), oAuthMap.get("jwt")));
}

@GetMapping("/jwt/naver")
public ResponseEntity<ResponseDto<String>> jwtNaverCreate(@RequestParam(required = false) String access_token) {
JwtVO result = memberAuthService.tokenTojwt(access_token);
@PostMapping("/join/{provider}")
public ResponseEntity<ResponseDto<String>> joinJwt(@RequestBody Map<String, Object> data,
@PathVariable("provider") String provider) {
Map<String, String> oAuthMap = new HashMap<>();
OAuthUserInfo authUserInfo = oAuthService.getMemberByProvider(data, provider);
oAuthMap.put("message", ResponseMessage.JOIN);
memberService.join(authUserInfo, provider, data.get("nickname").toString());
oAuthMap.put("jwt", jwtProvider.createToken(authUserInfo.getProviderId()));
return ResponseEntity
.status(HttpStatus.OK)
.body(ResponseDto.of(HttpStatus.OK, result.getMessage(), result.getJwt()));
.body(ResponseDto.of(HttpStatus.OK, oAuthMap.get("message"), oAuthMap.get("jwt")));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public interface MemberService {
boolean existsProviderId(String providerId);

String join(OAuthUserInfo authUserInfo, String provider);
String join(OAuthUserInfo authUserInfo, String provider, String nickname);

void updateMember(Member newMember, MemberPrincipalDto memberPrincipalDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public boolean existsProviderId(String providerId) {
@Override
@PublishEvent
@Transactional
public String join(OAuthUserInfo authUserInfo, String provider) {
public String join(OAuthUserInfo authUserInfo, String provider, String nickname) {
int newNumber = 0;
if (memberRepository.count() != 0) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.meme.ala.common.AbstractControllerTest;
import com.meme.ala.common.utils.NaverOauthUtil;
import com.meme.ala.core.auth.jwt.JwtProvider;
import com.meme.ala.core.auth.oauth.model.GoogleUser;
import com.meme.ala.core.auth.oauth.model.KakaoUser;
import com.meme.ala.core.auth.oauth.model.NaverUser;
import com.meme.ala.core.auth.oauth.model.OAuthProvider;
import com.meme.ala.core.auth.oauth.service.OAuthService;
import com.meme.ala.domain.member.model.dto.JwtVO;
import com.meme.ala.domain.member.service.MemberAuthService;
import com.meme.ala.domain.member.service.MemberService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -27,9 +24,6 @@
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.*;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.requestParameters;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand All @@ -46,13 +40,10 @@ public class MemberAuthControllerTest extends AbstractControllerTest {
@MockBean
private JwtProvider jwtProvider;

@MockBean
private MemberAuthService memberAuthService;

@Autowired
private ObjectMapper objectMapper;

@DisplayName("구글 OAuth 로그인/가입 테스트")
@DisplayName("구글 OAuth 로그인 테스트")
@Test
public void 구글_OAuth_로그인_유닛테스트() throws Exception {
String sampleRequestBody =
Expand Down Expand Up @@ -101,7 +92,7 @@ public class MemberAuthControllerTest extends AbstractControllerTest {
));
}

@DisplayName("카카오 OAuth 로그인/가입 테스트")
@DisplayName("카카오 OAuth 로그인 테스트")
@Test
public void 카카오_OAuth_로그인_유닛테스트() throws Exception {
String sampleRequestBody =
Expand Down Expand Up @@ -146,6 +137,49 @@ public class MemberAuthControllerTest extends AbstractControllerTest {
));
}

@DisplayName("카카오 OAuth 이미 가입 테스트")
@Test
public void 카카오_OAuth_이미가입_유닛테스트() throws Exception {
String sampleRequestBody =
"{\n" +
" \"profileObj\": {\n" +
" \"kakaoId\": \"1155\",\n" +
" \"imageUrl\": \"https://user-images.githubusercontent.com/46064193/125324764-2bc8e200-e37b-11eb-8d07-9ac29d0d1b1a.png\",\n" +
" \"email\": \"[email protected]\",\n" +
" \"name\": \"Jongmin Jung\"\n" +
" }\n" +
"}";

Map<String, Object> data = objectMapper.readValue(sampleRequestBody, Map.class);

when(oAuthService.getMemberByProvider(new ObjectMapper().readValue(sampleRequestBody, Map.class), OAuthProvider.KAKAO)).thenReturn(new KakaoUser(data));

when(memberService.existsProviderId(any())).thenReturn(Boolean.FALSE);

mockMvc.perform(post("/api/v1/oauth/jwt/kakao")
.contentType(MediaType.APPLICATION_JSON)
.content(sampleRequestBody))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.data").value("회원 가입이 필요합니다"))
.andDo(print())
.andDo(document("api/v1/oauth/jwt/kakao/notjoin",
getDocumentRequest(),
getDocumentResponse(),
requestFields(
fieldWithPath("profileObj.kakaoId").description("Identifier"),
fieldWithPath("profileObj.imageUrl").description("사용자 프로필 링크"),
fieldWithPath("profileObj.email").description("사용자 이메일"),
fieldWithPath("profileObj.name").description("사용자 이름")
),
responseFields(
fieldWithPath("status").description("응답 상태"),
fieldWithPath("message").description("가입되지 않음"),
fieldWithPath("data").description("가입되지 않음"),
fieldWithPath("timestamp").description("타임스탬프")
)
));
}

@Test
public void 네이버_OAuth_로그인_유닛테스트() throws Exception {
String sampleRequestBody =
Expand Down Expand Up @@ -187,30 +221,38 @@ public class MemberAuthControllerTest extends AbstractControllerTest {
));
}

@DisplayName("카카오 가입 테스트")
@Test
public void 네이버_OAuth_GET_유닛테스트() throws Exception {
String sampleAccessToken = "AAAAO9t3lY18fHzDi0xlDmoRPNNndkqUfYN7zSMAP17vAYOwxyKIHpKzeN6VxRSG7cfvA8hytclT2nydGBd8qiLCJKM";
String sampleRequestBody =
" {\n" +
" \"id\": \"afdasfdadsf\",\n" +
" \"profile_image\": \"https://user-images.githubusercontent.com/46064193/125324764-2bc8e200-e37b-11eb-8d07-9ac29d0d1b1a.png\",\n" +
" \"email\": \"[email protected]\",\n" +
" \"name\": \"Jongmin Jung\"}";

public void 회원가입() throws Exception {
String sampleRequestBody = "{\n" +
" \"nickname\": \"babojjm\",\n" +
" \"profileObj\": {\n" +
" \"kakaoId\": \"1155\",\n" +
" \"imageUrl\": \"https://user-images.githubusercontent.com/46064193/125324764-2bc8e200-e37b-11eb-8d07-9ac29d0d1b1a.png\",\n" +
" \"email\": \"[email protected]\",\n" +
" \"name\": \"Jongmin Jung\"\n" +
" }\n" +
"}";
Map<String, Object> data = objectMapper.readValue(sampleRequestBody, Map.class);

when(memberAuthService.tokenTojwt(any())).thenReturn(new JwtVO(OAuthProvider.NAVER, "dummy token"));
when(oAuthService.getMemberByProvider(new ObjectMapper().readValue(sampleRequestBody, Map.class), OAuthProvider.KAKAO)).thenReturn(new KakaoUser(data));
when(jwtProvider.createToken(any())).thenReturn("dummy token");

mockMvc.perform(get("/api/v1/oauth/jwt/naver")
.param("access_token", sampleAccessToken))
mockMvc.perform(post("/api/v1/oauth/join/kakao")
.contentType(MediaType.APPLICATION_JSON)
.content(sampleRequestBody))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.data").value("dummy token"))
.andDo(print())
.andDo(document("api/v1/oauth/jwt/get/naver",
.andDo(document("api/v1/oauth/join/kakao",
getDocumentRequest(),
getDocumentResponse(),
requestParameters(
parameterWithName("access_token").description("네이버 액세스 토큰")
requestFields(
fieldWithPath("nickname").description("설정할 닉네임"),
fieldWithPath("profileObj.kakaoId").description("Identifier"),
fieldWithPath("profileObj.imageUrl").description("사용자 프로필 링크"),
fieldWithPath("profileObj.email").description("사용자 이메일"),
fieldWithPath("profileObj.name").description("사용자 이름")
),
responseFields(
fieldWithPath("status").description("응답 상태"),
Expand Down
Loading