-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
211 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[[login-trial]] | ||
=== 체험용 계정 로그인 | ||
|
||
해당 API를 통해 체험 계정에 로그인 할 수 있습니다. | ||
|
||
해당 문서에 있는 Response는 예시이며 실제 받는 토큰값은 상이할 수 있습니다. | ||
|
||
==== Http Request | ||
include::{snippets}/login-trial/http-request.adoc[] | ||
==== Http Response | ||
|
||
include::{snippets}/login-trial/http-response.adoc[] | ||
include::{snippets}/login-trial/response-fields.adoc[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
api/src/main/java/com/lovely4k/backend/authentication/trial_login/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.lovely4k.backend.authentication.trial_login; | ||
|
||
import com.lovely4k.backend.common.ApiResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.hateoas.MediaTypes; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping(value = "/v1/members", produces = MediaTypes.HAL_JSON_VALUE) | ||
public class LoginController { | ||
|
||
private final LoginService loginService; | ||
|
||
@SneakyThrows | ||
@GetMapping(path = "/trial") | ||
public ResponseEntity<ApiResponse<LoginResponse>> trialLogin( | ||
) { | ||
long trialMemberId = 200395L; | ||
return ApiResponse.ok(loginService.trialLogin(trialMemberId)); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
api/src/main/java/com/lovely4k/backend/authentication/trial_login/LoginResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.lovely4k.backend.authentication.trial_login; | ||
|
||
import com.lovely4k.backend.authentication.token.TokenDto; | ||
|
||
public record LoginResponse( | ||
String accessToken, | ||
String refreshToken | ||
) { | ||
public static LoginResponse of(TokenDto tokenDto) { | ||
return new LoginResponse(tokenDto.accessToken(), tokenDto.refreshToken()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
api/src/main/java/com/lovely4k/backend/authentication/trial_login/LoginService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.lovely4k.backend.authentication.trial_login; | ||
|
||
import com.lovely4k.backend.authentication.token.TokenDto; | ||
import com.lovely4k.backend.authentication.token.TokenProvider; | ||
import com.lovely4k.backend.member.Member; | ||
import com.lovely4k.backend.member.repository.MemberRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class LoginService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final TokenProvider tokenProvider; | ||
|
||
@Transactional | ||
public LoginResponse trialLogin(long memberId) { | ||
Member trialMember = memberRepository.findById(memberId).orElseThrow(); | ||
TokenDto tokenDto = tokenProvider.generateTokenDto(trialMember); | ||
return LoginResponse.of(tokenDto); | ||
} | ||
} |
4 changes: 0 additions & 4 deletions
4
api/src/main/java/com/lovely4k/backend/member/controller/MemberController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
api/src/test/java/com/lovely4k/backend/authentication/LoginServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.lovely4k.backend.authentication; | ||
|
||
import com.lovely4k.backend.IntegrationTestSupport; | ||
import com.lovely4k.backend.authentication.trial_login.LoginResponse; | ||
import com.lovely4k.backend.authentication.trial_login.LoginService; | ||
import com.lovely4k.backend.member.Member; | ||
import com.lovely4k.backend.member.Role; | ||
import com.lovely4k.backend.member.repository.MemberRepository; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static com.lovely4k.backend.member.Sex.MALE; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@Transactional | ||
class LoginServiceTest extends IntegrationTestSupport { | ||
|
||
@Autowired | ||
MemberRepository memberRepository; | ||
|
||
@Autowired | ||
RefreshTokenRepository refreshTokenRepository; | ||
|
||
@Autowired | ||
LoginService loginService; | ||
|
||
@DisplayName("trialLogin을 통해 accessToken 값과 refreshToken 값을 받아올 수 있다.") | ||
@Test | ||
void trialLogin() { | ||
|
||
// given | ||
Member member = createMember(); | ||
Member savedMember = memberRepository.save(member); | ||
|
||
// when | ||
LoginResponse loginResponse = loginService.trialLogin(savedMember.getId()); | ||
|
||
// then | ||
RefreshToken refreshToken = refreshTokenRepository.findByMember(savedMember).orElseThrow(); | ||
|
||
assertAll( | ||
() -> assertThat(loginResponse.accessToken()).isNotNull(), | ||
() -> assertThat(loginResponse.refreshToken()).isNotNull(), | ||
() -> assertThat(refreshToken).isNotNull(), | ||
() -> assertThat(refreshToken.getMember()).isEqualTo(savedMember) | ||
); | ||
|
||
} | ||
|
||
private Member createMember() { | ||
return Member.builder() | ||
.coupleId(1L) | ||
.sex(MALE) | ||
.nickname("듬직이") | ||
.birthday(LocalDate.of(1996, 7, 30)) | ||
.mbti("ESFJ") | ||
.calendarColor("white") | ||
.imageUrl("http://www.imageUrlSample.com") | ||
.role(Role.USER) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
api/src/test/java/com/lovely4k/docs/authentication/LoginControllerDocsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.lovely4k.docs.authentication; | ||
|
||
import com.lovely4k.backend.authentication.trial_login.LoginController; | ||
import com.lovely4k.backend.authentication.trial_login.LoginResponse; | ||
import com.lovely4k.backend.authentication.trial_login.LoginService; | ||
import com.lovely4k.docs.RestDocsSupport; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.restdocs.payload.JsonFieldType; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
class LoginControllerDocsTest extends RestDocsSupport { | ||
|
||
private final LoginService loginService = mock(LoginService.class); | ||
|
||
@Override | ||
protected Object initController() { | ||
return new LoginController(loginService); | ||
} | ||
|
||
@DisplayName("체험용 아이디로 로그인 하는 API") | ||
@Test | ||
void trialLogin() throws Exception { | ||
// stubbing | ||
when(loginService.trialLogin(200395L)).thenReturn(new LoginResponse("ekq;jelkr", "qe;jkrqew;lkrjq;ewlrj")); | ||
|
||
mockMvc.perform( | ||
get("/v1/members/trial") | ||
.characterEncoding("UTF-8") | ||
) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andDo(document("login-trial", | ||
responseFields( | ||
fieldWithPath("code").type(JsonFieldType.NUMBER) | ||
.description("응답 코드"), | ||
fieldWithPath("body.accessToken").type(JsonFieldType.STRING) | ||
.description("access token value"), | ||
fieldWithPath("body.refreshToken").type(JsonFieldType.STRING) | ||
.description("refresh token value"), | ||
fieldWithPath("links").type(JsonFieldType.ARRAY) | ||
.description("relation links") | ||
) | ||
)); | ||
} | ||
} |