test: Filter APIのテストを一部復活

This commit is contained in:
usbharu 2024-09-16 01:43:00 +09:00
parent 5cc8267135
commit cb52248b1c
Signed by: usbharu
GPG Key ID: 95CBCF7046307B77
9 changed files with 23 additions and 17 deletions

View File

@ -16,7 +16,10 @@
package dev.usbharu.hideout.core.application.exception package dev.usbharu.hideout.core.application.exception
import dev.usbharu.hideout.core.domain.model.support.principal.Principal
class PermissionDeniedException : RuntimeException { class PermissionDeniedException : RuntimeException {
constructor(principal: Principal) : super("Permission Denied $principal")
constructor() : super() constructor() : super()
constructor(message: String?) : super(message) constructor(message: String?) : super(message)
constructor(message: String?, cause: Throwable?) : super(message, cause) constructor(message: String?, cause: Throwable?) : super(message, cause)

View File

@ -34,7 +34,8 @@ class UserGetFilterApplicationService(private val filterRepository: FilterReposi
) { ) {
override suspend fun internalExecute(command: GetFilter, principal: LocalUser): Filter { override suspend fun internalExecute(command: GetFilter, principal: LocalUser): Filter {
val filter = val filter =
filterRepository.findByFilterId(FilterId(command.filterId)) ?: throw IllegalArgumentException("Not Found") filterRepository.findByFilterId(FilterId(command.filterId))
?: throw IllegalArgumentException("Filter ${command.filterId} not found.")
if (filter.userDetailId != principal.userDetailId) { if (filter.userDetailId != principal.userDetailId) {
throw PermissionDeniedException() throw PermissionDeniedException()
} }

View File

@ -33,7 +33,7 @@ class DeleteFilterV1ApplicationService(private val filterRepository: FilterRepos
) { ) {
override suspend fun internalExecute(command: DeleteFilterV1, principal: LocalUser) { override suspend fun internalExecute(command: DeleteFilterV1, principal: LocalUser) {
val filter = filterRepository.findByFilterKeywordId(FilterKeywordId(command.filterKeywordId)) val filter = filterRepository.findByFilterKeywordId(FilterKeywordId(command.filterKeywordId))
?: throw IllegalArgumentException("Filter ${command.filterKeywordId} not found") ?: throw IllegalArgumentException("Filter ${command.filterKeywordId} by KeywordId not found")
if (principal.userDetailId != filter.userDetailId) { if (principal.userDetailId != filter.userDetailId) {
throw PermissionDeniedException() throw PermissionDeniedException()
} }

View File

@ -39,7 +39,7 @@ class GetFilterV1ApplicationService(private val filterRepository: FilterReposito
?: throw IllegalArgumentException("Filter ${command.filterKeywordId} not found") ?: throw IllegalArgumentException("Filter ${command.filterKeywordId} not found")
if (filter.userDetailId != principal.userDetailId) { if (filter.userDetailId != principal.userDetailId) {
throw PermissionDeniedException() throw PermissionDeniedException(principal)
} }
val filterKeyword = filter.filterKeywords.find { it.id.id == command.filterKeywordId } val filterKeyword = filter.filterKeywords.find { it.id.id == command.filterKeywordId }

View File

@ -84,6 +84,7 @@ class SpringFilterApi(
account -> FilterContext.ACCOUNT account -> FilterContext.ACCOUNT
} }
}.toSet() }.toSet()
val principal = principalContextHolder.getPrincipal()
val filter = userRegisterFilterApplicationService.execute( val filter = userRegisterFilterApplicationService.execute(
RegisterFilter( RegisterFilter(
v1FilterPostRequest.phrase, v1FilterPostRequest.phrase,
@ -91,12 +92,12 @@ class SpringFilterApi(
FilterAction.WARN, FilterAction.WARN,
setOf(RegisterFilterKeyword(v1FilterPostRequest.phrase, filterMode)) setOf(RegisterFilterKeyword(v1FilterPostRequest.phrase, filterMode))
), ),
principalContextHolder.getPrincipal() principal
) )
return ResponseEntity.ok( return ResponseEntity.ok(
getFilterV1ApplicationService.execute( getFilterV1ApplicationService.execute(
GetFilterV1(filter.filterKeywords.first().id), GetFilterV1(filter.filterKeywords.first().id),
principalContextHolder.getPrincipal() principal
) )
) )
} }

View File

@ -418,7 +418,7 @@ class FilterTest {
) )
} }
.asyncDispatch() .asyncDispatch()
.andExpect { status { isOk() } } .andExpect { status { isNotFound() } }
} }
@Test @Test
@ -431,7 +431,7 @@ class FilterTest {
) )
} }
.asyncDispatch() .asyncDispatch()
.andExpect { status { isOk() } } .andExpect { status { isNotFound() } }
} }
@Test @Test
@ -456,7 +456,7 @@ class FilterTest {
) )
} }
.asyncDispatch() .asyncDispatch()
.andExpect { status { isOk() } } .andExpect { status { isNotFound() } }
} }
@Test @Test
@ -469,7 +469,7 @@ class FilterTest {
) )
} }
.asyncDispatch() .asyncDispatch()
.andExpect { status { isOk() } } .andExpect { status { isNotFound() } }
} }
@Test @Test
@ -494,7 +494,7 @@ class FilterTest {
) )
} }
.asyncDispatch() .asyncDispatch()
.andExpect { status { isOk() } } .andExpect { status { isNotFound() } }
} }
@Test @Test
@ -507,7 +507,7 @@ class FilterTest {
) )
} }
.asyncDispatch() .asyncDispatch()
.andExpect { status { isOk() } } .andExpect { status { isNotFound() } }
} }
@Test @Test
@ -664,6 +664,7 @@ class FilterTest {
} }
@Test @Test
@Sql("/sql/filter/test-filter.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
fun `apiV1FiltersIdDelete writeで削除できる`() { fun `apiV1FiltersIdDelete writeで削除できる`() {
mockMvc mockMvc
.delete("/api/v1/filters/1") { .delete("/api/v1/filters/1") {
@ -676,6 +677,7 @@ class FilterTest {
} }
@Test @Test
@Sql("/sql/filter/test-filter.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
fun `apiV1FiltersIdDelete write_filtersで削除できる`() { fun `apiV1FiltersIdDelete write_filtersで削除できる`() {
mockMvc mockMvc
.delete("/api/v1/filters/1") { .delete("/api/v1/filters/1") {
@ -692,11 +694,10 @@ class FilterTest {
mockMvc mockMvc
.delete("/api/v1/filters/1") { .delete("/api/v1/filters/1") {
with( with(
jwt().jwt { it.claim("uid", "1") }.authorities(SimpleGrantedAuthority("SCOPE_write")) jwt().jwt { it.claim("uid", "1") }.authorities(SimpleGrantedAuthority("SCOPE_read"))
) )
} }
.asyncDispatch() .andExpect { status { isForbidden() } }
.andExpect { status { isOk() } }
} }
companion object { companion object {

View File

@ -49,7 +49,7 @@ import org.springframework.web.context.WebApplicationContext
@SpringBootTest(classes = [SpringApplication::class]) @SpringBootTest(classes = [SpringApplication::class])
@AutoConfigureMockMvc @AutoConfigureMockMvc
@Transactional @Transactional
@Sql("/sql/actors.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS) @Sql("/sql/actors.sql", "/sql/userdetail.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
@Sql("/sql/posts.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS) @Sql("/sql/posts.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
@Sql("/sql/test-custom-emoji.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS) @Sql("/sql/test-custom-emoji.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
class StatusTest { class StatusTest {

View File

@ -37,7 +37,7 @@ import org.springframework.web.context.WebApplicationContext
@SpringBootTest(classes = [SpringApplication::class]) @SpringBootTest(classes = [SpringApplication::class])
@Transactional @Transactional
@Sql("/sql/actors.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS) @Sql("/sql/actors.sql", "/sql/userdetail.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
class TimelineApiTest { class TimelineApiTest {
@Autowired @Autowired
private lateinit var context: WebApplicationContext private lateinit var context: WebApplicationContext

View File

@ -1,4 +1,4 @@
insert into filters (id, user_id, name, context, action) insert into filters (id, user_id, name, context, action)
VALUES (1, 1, 'test filter', 'home', 'warn'); VALUES (1, 1, 'test filter', 'HOME', 'WARN');
insert into filter_keywords(id, filter_id, keyword, mode) insert into filter_keywords(id, filter_id, keyword, mode)
VALUES (1, 1, 'hoge', 'NONE') VALUES (1, 1, 'hoge', 'NONE')