test #426
@@ -0,0 +1,41 @@
|
||||
package kr.co.vividnext.sodalive.v2.api.creator.channel.series.adapter.`in`.web
|
||||
|
||||
import kr.co.vividnext.sodalive.common.ApiResponse
|
||||
import kr.co.vividnext.sodalive.common.SodaException
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.channel.series.application.CreatorChannelSeriesFacade
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RequestParam
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v2/creator-channels")
|
||||
class CreatorChannelSeriesController(
|
||||
private val creatorChannelSeriesFacade: CreatorChannelSeriesFacade
|
||||
) {
|
||||
@GetMapping("/{creatorId}/series")
|
||||
fun getSeriesTab(
|
||||
@PathVariable creatorId: Long,
|
||||
@RequestParam(required = false) sort: String?,
|
||||
@RequestParam(required = false) page: Int?,
|
||||
@RequestParam(required = false) size: Int?,
|
||||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member?
|
||||
) = run {
|
||||
ApiResponse.ok(
|
||||
creatorChannelSeriesFacade.getSeriesTab(
|
||||
creatorId = creatorId,
|
||||
viewer = requireMember(member),
|
||||
sort = sort,
|
||||
page = page,
|
||||
size = size
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun requireMember(member: Member?): Member {
|
||||
return member ?: throw SodaException(messageKey = "common.error.bad_credentials")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package kr.co.vividnext.sodalive.v2.api.creator.channel.series.adapter.`in`.web
|
||||
|
||||
import kr.co.vividnext.sodalive.common.CountryContext
|
||||
import kr.co.vividnext.sodalive.i18n.LangContext
|
||||
import kr.co.vividnext.sodalive.i18n.SodaMessageSource
|
||||
import kr.co.vividnext.sodalive.member.Member
|
||||
import kr.co.vividnext.sodalive.member.MemberAdapter
|
||||
import kr.co.vividnext.sodalive.member.MemberRole
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.channel.series.application.CreatorChannelSeriesFacade
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.channel.series.dto.CreatorChannelSeriesResponse
|
||||
import kr.co.vividnext.sodalive.v2.api.creator.channel.series.dto.CreatorChannelSeriesTabResponse
|
||||
import kr.co.vividnext.sodalive.v2.common.domain.ContentSort
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.mockito.Mockito
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||
import org.springframework.boot.test.context.TestConfiguration
|
||||
import org.springframework.boot.test.mock.mockito.MockBean
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Import
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity
|
||||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.anonymous
|
||||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user
|
||||
import org.springframework.security.web.SecurityFilterChain
|
||||
import org.springframework.security.web.authentication.HttpStatusEntryPoint
|
||||
import org.springframework.test.web.servlet.MockMvc
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
|
||||
import java.time.LocalDateTime
|
||||
import javax.servlet.http.HttpServletResponse
|
||||
|
||||
@WebMvcTest(CreatorChannelSeriesController::class)
|
||||
@Import(CreatorChannelSeriesControllerTest.TestSecurityConfig::class)
|
||||
class CreatorChannelSeriesControllerTest @Autowired constructor(
|
||||
private val mockMvc: MockMvc
|
||||
) {
|
||||
@MockBean
|
||||
private lateinit var facade: CreatorChannelSeriesFacade
|
||||
|
||||
@MockBean
|
||||
private lateinit var countryContext: CountryContext
|
||||
|
||||
@MockBean
|
||||
private lateinit var langContext: LangContext
|
||||
|
||||
@MockBean
|
||||
private lateinit var sodaMessageSource: SodaMessageSource
|
||||
|
||||
@TestConfiguration
|
||||
class TestSecurityConfig {
|
||||
@Bean
|
||||
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
|
||||
return http
|
||||
.csrf().disable()
|
||||
.authorizeRequests()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.exceptionHandling()
|
||||
.authenticationEntryPoint(HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))
|
||||
.accessDeniedHandler { _, response, _ -> response.sendError(HttpServletResponse.SC_FORBIDDEN) }
|
||||
.and()
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("크리에이터 채널 시리즈 탭 조회는 비회원 요청을 거부한다")
|
||||
fun shouldRejectAnonymousCreatorChannelSeriesRequest() {
|
||||
mockMvc.perform(
|
||||
get("/api/v2/creator-channels/1/series")
|
||||
.with(anonymous())
|
||||
)
|
||||
.andExpect(status().isUnauthorized)
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("크리에이터 채널 시리즈 탭 조회는 query parameter를 facade에 전달하고 성공 응답을 반환한다")
|
||||
fun shouldReturnCreatorChannelSeriesTabForAuthenticatedMember() {
|
||||
val viewer = createMember(id = 10L)
|
||||
Mockito.doReturn(createResponse(sort = ContentSort.POPULAR, page = 1, size = 20)).`when`(facade).getSeriesTab(
|
||||
eqValue(1L),
|
||||
eqValue(viewer),
|
||||
eqValue("POPULAR"),
|
||||
eqValue(1),
|
||||
eqValue(20),
|
||||
anyValue(LocalDateTime.now())
|
||||
)
|
||||
|
||||
mockMvc.perform(
|
||||
get("/api/v2/creator-channels/1/series")
|
||||
.param("sort", "POPULAR")
|
||||
.param("page", "1")
|
||||
.param("size", "20")
|
||||
.with(user(MemberAdapter(viewer)))
|
||||
)
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(jsonPath("$.success").value(true))
|
||||
.andExpect(jsonPath("$.data.seriesCount").value(2))
|
||||
.andExpect(jsonPath("$.data.series").isArray)
|
||||
.andExpect(jsonPath("$.data.series[0].seriesId").value(101))
|
||||
.andExpect(jsonPath("$.data.series[0].coverImageUrl").value("https://cdn.test/cover.png"))
|
||||
.andExpect(jsonPath("$.data.series[0].publishedDaysOfWeek").value("Every Mon, Thu"))
|
||||
.andExpect(jsonPath("$.data.series[0].purchasedPaidContentRate").value(75))
|
||||
.andExpect(jsonPath("$.data.series[0].isOriginal").value(true))
|
||||
.andExpect(jsonPath("$.data.series[0].isAdult").value(false))
|
||||
.andExpect(jsonPath("$.data.series[0].isProceeding").value(true))
|
||||
.andExpect(jsonPath("$.data.sort").value("POPULAR"))
|
||||
.andExpect(jsonPath("$.data.page").value(1))
|
||||
.andExpect(jsonPath("$.data.size").value(20))
|
||||
.andExpect(jsonPath("$.data.hasNext").value(false))
|
||||
|
||||
Mockito.verify(facade).getSeriesTab(
|
||||
eqValue(1L),
|
||||
eqValue(viewer),
|
||||
eqValue("POPULAR"),
|
||||
eqValue(1),
|
||||
eqValue(20),
|
||||
anyValue(LocalDateTime.now())
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("크리에이터 채널 시리즈 탭 조회는 잘못된 sort도 controller에서 거부하지 않고 facade에 전달한다")
|
||||
fun shouldPassInvalidSortToFacade() {
|
||||
val viewer = createMember(id = 10L)
|
||||
Mockito.doReturn(createResponse(sort = ContentSort.LATEST, page = 0, size = 50)).`when`(facade).getSeriesTab(
|
||||
eqValue(1L),
|
||||
eqValue(viewer),
|
||||
eqValue("INVALID"),
|
||||
eqValue(-1),
|
||||
eqValue(100),
|
||||
anyValue(LocalDateTime.now())
|
||||
)
|
||||
|
||||
mockMvc.perform(
|
||||
get("/api/v2/creator-channels/1/series")
|
||||
.param("sort", "INVALID")
|
||||
.param("page", "-1")
|
||||
.param("size", "100")
|
||||
.with(user(MemberAdapter(viewer)))
|
||||
)
|
||||
.andExpect(status().isOk)
|
||||
.andExpect(jsonPath("$.data.sort").value("LATEST"))
|
||||
.andExpect(jsonPath("$.data.page").value(0))
|
||||
.andExpect(jsonPath("$.data.size").value(50))
|
||||
|
||||
Mockito.verify(facade).getSeriesTab(
|
||||
eqValue(1L),
|
||||
eqValue(viewer),
|
||||
eqValue("INVALID"),
|
||||
eqValue(-1),
|
||||
eqValue(100),
|
||||
anyValue(LocalDateTime.now())
|
||||
)
|
||||
}
|
||||
|
||||
private fun <T> eqValue(value: T): T {
|
||||
return Mockito.eq(value) ?: value
|
||||
}
|
||||
|
||||
private fun <T> anyValue(fallback: T): T {
|
||||
return Mockito.any<T>() ?: fallback
|
||||
}
|
||||
|
||||
private fun createMember(id: Long): Member {
|
||||
return Member(
|
||||
email = "viewer$id@test.com",
|
||||
password = "password",
|
||||
nickname = "viewer$id",
|
||||
role = MemberRole.USER
|
||||
).apply { this.id = id }
|
||||
}
|
||||
|
||||
private fun createResponse(
|
||||
sort: ContentSort = ContentSort.LATEST,
|
||||
page: Int = 0,
|
||||
size: Int = 20
|
||||
): CreatorChannelSeriesTabResponse {
|
||||
return CreatorChannelSeriesTabResponse(
|
||||
seriesCount = 2,
|
||||
series = listOf(
|
||||
CreatorChannelSeriesResponse(
|
||||
seriesId = 101L,
|
||||
title = "series",
|
||||
coverImageUrl = "https://cdn.test/cover.png",
|
||||
publishedDaysOfWeek = "Every Mon, Thu",
|
||||
isOriginal = true,
|
||||
isAdult = false,
|
||||
isProceeding = true,
|
||||
contentCount = 5,
|
||||
purchasedContentCount = 3,
|
||||
paidContentCount = 4,
|
||||
purchasedPaidContentRate = 75
|
||||
)
|
||||
),
|
||||
sort = sort,
|
||||
page = page,
|
||||
size = size,
|
||||
hasNext = false
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user