From 85a331c28d5c465fcbe14637a56d96750e46f0de Mon Sep 17 00:00:00 2001 From: Klaus Date: Wed, 17 Jun 2026 20:19:48 +0900 Subject: [PATCH] =?UTF-8?q?feat(creator):=20=EC=B1=84=EB=84=90=20=EB=9D=BC?= =?UTF-8?q?=EC=9D=B4=EB=B8=8C=20=ED=83=AD=20=EC=A1=B0=ED=9A=8C=20API?= =?UTF-8?q?=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../in/web/CreatorChannelLiveController.kt | 42 ++++ .../web/CreatorChannelLiveControllerTest.kt | 217 ++++++++++++++++++ 2 files changed, 259 insertions(+) create mode 100644 src/main/kotlin/kr/co/vividnext/sodalive/v2/api/creator/channel/live/adapter/in/web/CreatorChannelLiveController.kt create mode 100644 src/test/kotlin/kr/co/vividnext/sodalive/v2/api/creator/channel/live/adapter/in/web/CreatorChannelLiveControllerTest.kt diff --git a/src/main/kotlin/kr/co/vividnext/sodalive/v2/api/creator/channel/live/adapter/in/web/CreatorChannelLiveController.kt b/src/main/kotlin/kr/co/vividnext/sodalive/v2/api/creator/channel/live/adapter/in/web/CreatorChannelLiveController.kt new file mode 100644 index 00000000..66e4e8da --- /dev/null +++ b/src/main/kotlin/kr/co/vividnext/sodalive/v2/api/creator/channel/live/adapter/in/web/CreatorChannelLiveController.kt @@ -0,0 +1,42 @@ +package kr.co.vividnext.sodalive.v2.api.creator.channel.live.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.live.application.CreatorChannelLiveFacade +import kr.co.vividnext.sodalive.v2.common.domain.ContentSort +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 CreatorChannelLiveController( + private val creatorChannelLiveFacade: CreatorChannelLiveFacade +) { + @GetMapping("/{creatorId}/live") + fun getLiveTab( + @PathVariable creatorId: Long, + @RequestParam(defaultValue = "LATEST") sort: ContentSort, + @RequestParam(defaultValue = "0") page: Int, + @RequestParam(defaultValue = "20") size: Int, + @AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : member") member: Member? + ) = run { + ApiResponse.ok( + creatorChannelLiveFacade.getLiveTab( + 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") + } +} diff --git a/src/test/kotlin/kr/co/vividnext/sodalive/v2/api/creator/channel/live/adapter/in/web/CreatorChannelLiveControllerTest.kt b/src/test/kotlin/kr/co/vividnext/sodalive/v2/api/creator/channel/live/adapter/in/web/CreatorChannelLiveControllerTest.kt new file mode 100644 index 00000000..a37a38f8 --- /dev/null +++ b/src/test/kotlin/kr/co/vividnext/sodalive/v2/api/creator/channel/live/adapter/in/web/CreatorChannelLiveControllerTest.kt @@ -0,0 +1,217 @@ +package kr.co.vividnext.sodalive.v2.api.creator.channel.live.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.live.application.CreatorChannelLiveFacade +import kr.co.vividnext.sodalive.v2.api.creator.channel.live.dto.CreatorChannelAudioContentResponse +import kr.co.vividnext.sodalive.v2.api.creator.channel.live.dto.CreatorChannelLiveResponse +import kr.co.vividnext.sodalive.v2.api.creator.channel.live.dto.CreatorChannelLiveTabResponse +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(CreatorChannelLiveController::class) +@Import(CreatorChannelLiveControllerTest.TestSecurityConfig::class) +class CreatorChannelLiveControllerTest @Autowired constructor( + private val mockMvc: MockMvc +) { + @MockBean + private lateinit var facade: CreatorChannelLiveFacade + + @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 shouldRejectAnonymousCreatorChannelLiveRequest() { + mockMvc.perform( + get("/api/v2/creator-channels/1/live") + .with(anonymous()) + ) + .andExpect(status().isUnauthorized) + } + + @Test + @DisplayName("크리에이터 채널 라이브 탭 조회는 기본 정렬과 page 정보를 facade에 전달하고 성공 응답을 반환한다") + fun shouldReturnCreatorChannelLiveTabForAuthenticatedMember() { + val viewer = createMember(id = 10L) + Mockito.doReturn(createResponse()).`when`(facade).getLiveTab( + eqValue(1L), + eqValue(viewer), + eqValue(ContentSort.LATEST), + eqValue(0), + eqValue(20), + anyValue(LocalDateTime.now()) + ) + + mockMvc.perform( + get("/api/v2/creator-channels/1/live") + .with(user(MemberAdapter(viewer))) + ) + .andExpect(status().isOk) + .andExpect(jsonPath("$.success").value(true)) + .andExpect(jsonPath("$.data.liveReplayContentCount").value(1)) + .andExpect(jsonPath("$.data.currentLive").exists()) + .andExpect(jsonPath("$.data.liveReplayContents").isArray) + .andExpect(jsonPath("$.data.sort").value("LATEST")) + .andExpect(jsonPath("$.data.page").value(0)) + .andExpect(jsonPath("$.data.size").value(20)) + .andExpect(jsonPath("$.data.hasNext").value(false)) + .andExpect(jsonPath("$.data.liveReplayContents[0].isOwned").value(true)) + .andExpect(jsonPath("$.data.liveReplayContents[0].isRented").value(false)) + + Mockito.verify(facade).getLiveTab( + eqValue(1L), + eqValue(viewer), + eqValue(ContentSort.LATEST), + eqValue(0), + eqValue(20), + anyValue(LocalDateTime.now()) + ) + } + + @Test + @DisplayName("크리에이터 채널 라이브 탭 조회는 잘못된 page 요청을 기존 오류 응답으로 반환한다") + fun shouldReturnErrorResponseWhenPageIsInvalid() { + val viewer = createMember(id = 10L) + Mockito.doThrow(kr.co.vividnext.sodalive.common.SodaException(messageKey = "common.error.invalid_request")) + .`when`(facade).getLiveTab( + eqValue(1L), + eqValue(viewer), + eqValue(ContentSort.LATEST), + eqValue(-1), + eqValue(20), + anyValue(LocalDateTime.now()) + ) + + mockMvc.perform( + get("/api/v2/creator-channels/1/live") + .param("page", "-1") + .with(user(MemberAdapter(viewer))) + ) + .andExpect(status().isOk) + .andExpect(jsonPath("$.success").value(false)) + } + + @Test + @DisplayName("크리에이터 채널 라이브 탭 조회는 잘못된 size 요청을 기존 오류 응답으로 반환한다") + fun shouldReturnErrorResponseWhenSizeIsInvalid() { + val viewer = createMember(id = 10L) + Mockito.doThrow(kr.co.vividnext.sodalive.common.SodaException(messageKey = "common.error.invalid_request")) + .`when`(facade).getLiveTab( + eqValue(1L), + eqValue(viewer), + eqValue(ContentSort.LATEST), + eqValue(0), + eqValue(0), + anyValue(LocalDateTime.now()) + ) + + mockMvc.perform( + get("/api/v2/creator-channels/1/live") + .param("size", "0") + .with(user(MemberAdapter(viewer))) + ) + .andExpect(status().isOk) + .andExpect(jsonPath("$.success").value(false)) + } + + private fun eqValue(value: T): T { + return Mockito.eq(value) ?: value + } + + private fun anyValue(fallback: T): T { + return Mockito.any() ?: 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(): CreatorChannelLiveTabResponse { + return CreatorChannelLiveTabResponse( + liveReplayContentCount = 1, + currentLive = CreatorChannelLiveResponse( + liveId = 101L, + title = "live", + coverImageUrl = "live.png", + beginDateTimeUtc = "2026-06-17T01:00:00Z", + price = 20, + isAdult = true + ), + liveReplayContents = listOf( + CreatorChannelAudioContentResponse( + audioContentId = 201L, + title = "audio", + duration = "00:10:00", + imageUrl = "audio.png", + price = 30, + isAdult = false, + isPointAvailable = true, + isFirstContent = true, + seriesName = "series", + isOriginalSeries = true, + isOwned = true, + isRented = false + ) + ), + sort = ContentSort.LATEST, + page = 0, + size = 20, + hasNext = false + ) + } +}