Skip to content

Commit

Permalink
feat: SaveMemberUseCase, TokenUseCase API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
belljun3395 committed Aug 1, 2024
1 parent 972d6d1 commit c13bd7b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.few.api.web.controller.member

import com.few.api.domain.member.usecase.SaveMemberUseCase
import com.few.api.domain.member.usecase.TokenUseCase
import com.few.api.domain.member.usecase.dto.SaveMemberUseCaseIn
import com.few.api.domain.member.usecase.dto.TokenUseCaseIn
import com.few.api.web.controller.member.request.SaveMemberRequest
import com.few.api.web.controller.member.request.TokenRequest
import com.few.api.web.controller.member.response.SaveMemberResponse
import com.few.api.web.controller.member.response.TokenResponse
import com.few.api.web.support.ApiResponse
Expand All @@ -17,28 +22,47 @@ import org.springframework.web.bind.annotation.RestController
@Validated
@RestController
@RequestMapping(value = ["/api/v1/members"], produces = [MediaType.APPLICATION_JSON_VALUE])
class MemberController {
class MemberController(
private val saveMemberUseCase: SaveMemberUseCase,
private val tokenUseCase: TokenUseCase,
) {
@PostMapping
fun saveMember(
@RequestBody request: SaveMemberRequest,
): ApiResponse<ApiResponse.SuccessBody<SaveMemberResponse>> {
val useCaseOut = SaveMemberUseCaseIn(
email = request.email
).let {
saveMemberUseCase.execute(it)
}

SaveMemberResponse(
isSendAuth = true
isSendAuth = useCaseOut.isSendAuthEmail
).let {
return ApiResponseGenerator.success(it, HttpStatus.OK)
}
}

@PostMapping("/token")
fun token(
@RequestParam(value = "id", required = true) id: String,
@RequestParam(value = "at", required = false) at: Long,
@RequestParam(value = "rt", required = false) rt: Long,
@RequestParam(value = "auth_token", required = false) token: String?,
@RequestParam(value = "at", required = false) at: Long?,
@RequestParam(value = "rt", required = false) rt: Long?,
@RequestBody request: TokenRequest?,
): ApiResponse<ApiResponse.SuccessBody<TokenResponse>> {
val useCaseOut = TokenUseCaseIn(
token = token,
at = at,
rt = rt,
refreshToken = request?.refreshToken
).let {
tokenUseCase.execute(it)
}

TokenResponse(
accessToken = "accessToken",
refreshToken = "refreshToken",
isLogin = true
accessToken = useCaseOut.accessToken,
refreshToken = useCaseOut.refreshToken,
isLogin = useCaseOut.isLogin
).let {
return ApiResponseGenerator.success(it, HttpStatus.OK)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@ import com.epages.restdocs.apispec.ResourceDocumentation
import com.epages.restdocs.apispec.ResourceSnippetParameters
import com.epages.restdocs.apispec.Schema
import com.fasterxml.jackson.databind.ObjectMapper
import com.few.api.domain.member.usecase.SaveMemberUseCase
import com.few.api.domain.member.usecase.TokenUseCase
import com.few.api.domain.member.usecase.dto.SaveMemberUseCaseIn
import com.few.api.domain.member.usecase.dto.SaveMemberUseCaseOut
import com.few.api.domain.member.usecase.dto.TokenUseCaseIn
import com.few.api.domain.member.usecase.dto.TokenUseCaseOut
import com.few.api.web.controller.ControllerTestSpec
import com.few.api.web.controller.description.Description
import com.few.api.web.controller.helper.*
import com.few.api.web.controller.member.request.SaveMemberRequest
import com.few.api.web.controller.member.request.TokenRequest
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.mockito.Mockito.`when`
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.http.MediaType
import org.springframework.restdocs.RestDocumentationContextProvider
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post
import org.springframework.restdocs.payload.PayloadDocumentation
import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation
import org.springframework.test.web.reactive.server.WebTestClient
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import org.springframework.web.util.UriComponentsBuilder

class MemberControllerTest : ControllerTestSpec() {
Expand All @@ -26,9 +39,18 @@ class MemberControllerTest : ControllerTestSpec() {

private lateinit var webTestClient: WebTestClient

@Autowired
private lateinit var mockMvc: MockMvc

@Autowired
private lateinit var memberController: MemberController

@MockBean
private lateinit var saveMemberUseCase: SaveMemberUseCase

@MockBean
private lateinit var tokenUseCase: TokenUseCase

companion object {
private val BASE_URL = "/api/v1/members"
private val TAG = "MemberController"
Expand Down Expand Up @@ -57,6 +79,8 @@ class MemberControllerTest : ControllerTestSpec() {
val body = objectMapper.writeValueAsString(SaveMemberRequest(email = email))

// set mock
val useCaseIn = SaveMemberUseCaseIn(email = email)
`when`(saveMemberUseCase.execute(useCaseIn)).thenReturn(SaveMemberUseCaseOut(isSendAuthEmail = true))

// when
this.webTestClient.post()
Expand Down Expand Up @@ -98,43 +122,57 @@ class MemberControllerTest : ControllerTestSpec() {
fun token() {
// given
val api = "Token"
val id = "adfabdfk3"
val auth_token = "edb966d6ba882cc0df51579c9a94aca0"
val at = 1000L
val rt = 1000L
val tokenRequest = TokenRequest(refreshToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtZW1iZXJJZCI6NTcsIm1lbWJlclJvbGUiOiJbUk9MRV9VU0VSXSIsImlhdCI6MTcyMjI1NTE4MywiZXhwIjoxNzUzODEyNzgzfQ.1KXRim0MVvz1vxOQB_700XPCD9zPQtHNItF_A9upvA8")
val body = objectMapper.writeValueAsString(tokenRequest)

val uri = UriComponentsBuilder.newInstance()
.path("$BASE_URL/token")
.queryParam("id", id)
.queryParam("at", at)
.queryParam("rt", rt)
.build()
.toUriString()

// set mock
val useCaseIn = TokenUseCaseIn(
token = "edb966d6ba882cc0df51579c9a94aca0",
at = at,
rt = rt,
refreshToken = tokenRequest.refreshToken
)
`when`(tokenUseCase.execute(useCaseIn)).thenReturn(
TokenUseCaseOut(
accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtZW1iZXJJZCI6NTcsIm1lbWJlclJvbGUiOiJbUk9MRV9VU0VSXSIsImlhdCI6MTcyMjI1NTE4MywiZXhwIjoxNzUzODEyNzgzfQ.1KXRim0MVvz1vxOQB_700XPCD9zPQtHNItF_A9upvA8",
refreshToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtZW1iZXJJZCI6NTcsIm1lbWJlclJvbGUiOiJbUk9MRV9VU0VSXSIsImlhdCI6MTcyMjI1NTE4MywiZXhwIjoxNzUzODEyNzgzfQ.1KXRim0MVvz1vxOQB_700XPCD9zPQtHNItF_A9upvA8",
isLogin = false
)
)

// when
this.webTestClient.post()
.uri(uri)
.accept(MediaType.APPLICATION_JSON)
.exchange().expectStatus().is2xxSuccessful()
.expectBody().consumeWith(
WebTestClientRestDocumentation.document(
mockMvc.perform(
post(uri)
.contentType(MediaType.APPLICATION_JSON)
.queryParam("auth_token", auth_token)
.queryParam("at", at.toString())
.queryParam("rt", rt.toString())
.content(body)
).andExpect(status().is2xxSuccessful)
.andDo(
document(
api.toIdentifier(),
ResourceDocumentation.resource(
ResourceSnippetParameters.builder()
.description("토큰 발급")
.summary(api.toIdentifier())
.privateResource(false)
.deprecated(false)
.description("토큰 발급")
.tag(TAG)
.requestSchema(Schema.schema(api.toRequestSchema()))
.queryParameters(
ResourceDocumentation.parameterWithName("id")
.description("아이디"),
ResourceDocumentation.parameterWithName("auth_token")
.description("아이디").optional(),
ResourceDocumentation.parameterWithName("at")
.description("액세스 토큰 만료 시간"),
.description("액세스 토큰 만료 시간").optional(),
ResourceDocumentation.parameterWithName("rt")
.description("리프레시 토큰 만료 시간")
.description("리프레시 토큰 만료 시간").optional()
)
.responseSchema(Schema.schema(api.toResponseSchema()))
.responseFields(
Expand Down

0 comments on commit c13bd7b

Please sign in to comment.