From cb52248b1c24619c1a7e2cfb95374afa18dabb37 Mon Sep 17 00:00:00 2001 From: usbharu Date: Mon, 16 Sep 2024 01:43:00 +0900 Subject: [PATCH] =?UTF-8?q?test:=20Filter=20API=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E4=B8=80=E9=83=A8=E5=BE=A9=E6=B4=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/PermissionDeniedException.kt | 3 +++ .../filter/UserGetFilterApplicationService.kt | 3 ++- .../DeleteFilterV1ApplicationService.kt | 2 +- .../filter/GetFilterV1ApplicationService.kt | 2 +- .../interfaces/api/SpringFilterApi.kt | 5 +++-- .../test/kotlin/mastodon/filter/FilterTest.kt | 19 ++++++++++--------- .../test/kotlin/mastodon/status/StatusTest.kt | 2 +- .../mastodon/timelines/TimelineApiTest.kt | 2 +- .../test/resources/sql/filter/test-filter.sql | 2 +- 9 files changed, 23 insertions(+), 17 deletions(-) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/exception/PermissionDeniedException.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/exception/PermissionDeniedException.kt index e59663be..53613621 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/exception/PermissionDeniedException.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/exception/PermissionDeniedException.kt @@ -16,7 +16,10 @@ package dev.usbharu.hideout.core.application.exception +import dev.usbharu.hideout.core.domain.model.support.principal.Principal + class PermissionDeniedException : RuntimeException { + constructor(principal: Principal) : super("Permission Denied $principal") constructor() : super() constructor(message: String?) : super(message) constructor(message: String?, cause: Throwable?) : super(message, cause) diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/filter/UserGetFilterApplicationService.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/filter/UserGetFilterApplicationService.kt index e97209fc..b6682c64 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/filter/UserGetFilterApplicationService.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/application/filter/UserGetFilterApplicationService.kt @@ -34,7 +34,8 @@ class UserGetFilterApplicationService(private val filterRepository: FilterReposi ) { override suspend fun internalExecute(command: GetFilter, principal: LocalUser): 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) { throw PermissionDeniedException() } diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/filter/DeleteFilterV1ApplicationService.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/filter/DeleteFilterV1ApplicationService.kt index 4961d383..53ed42ff 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/filter/DeleteFilterV1ApplicationService.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/filter/DeleteFilterV1ApplicationService.kt @@ -33,7 +33,7 @@ class DeleteFilterV1ApplicationService(private val filterRepository: FilterRepos ) { override suspend fun internalExecute(command: DeleteFilterV1, principal: LocalUser) { 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) { throw PermissionDeniedException() } diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/filter/GetFilterV1ApplicationService.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/filter/GetFilterV1ApplicationService.kt index 6f485361..25e7f658 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/filter/GetFilterV1ApplicationService.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/application/filter/GetFilterV1ApplicationService.kt @@ -39,7 +39,7 @@ class GetFilterV1ApplicationService(private val filterRepository: FilterReposito ?: throw IllegalArgumentException("Filter ${command.filterKeywordId} not found") if (filter.userDetailId != principal.userDetailId) { - throw PermissionDeniedException() + throw PermissionDeniedException(principal) } val filterKeyword = filter.filterKeywords.find { it.id.id == command.filterKeywordId } diff --git a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringFilterApi.kt b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringFilterApi.kt index 1734be87..8e92209e 100644 --- a/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringFilterApi.kt +++ b/hideout-mastodon/src/main/kotlin/dev/usbharu/hideout/mastodon/interfaces/api/SpringFilterApi.kt @@ -84,6 +84,7 @@ class SpringFilterApi( account -> FilterContext.ACCOUNT } }.toSet() + val principal = principalContextHolder.getPrincipal() val filter = userRegisterFilterApplicationService.execute( RegisterFilter( v1FilterPostRequest.phrase, @@ -91,12 +92,12 @@ class SpringFilterApi( FilterAction.WARN, setOf(RegisterFilterKeyword(v1FilterPostRequest.phrase, filterMode)) ), - principalContextHolder.getPrincipal() + principal ) return ResponseEntity.ok( getFilterV1ApplicationService.execute( GetFilterV1(filter.filterKeywords.first().id), - principalContextHolder.getPrincipal() + principal ) ) } diff --git a/hideout-mastodon/src/test/kotlin/mastodon/filter/FilterTest.kt b/hideout-mastodon/src/test/kotlin/mastodon/filter/FilterTest.kt index 73615a07..8052c258 100644 --- a/hideout-mastodon/src/test/kotlin/mastodon/filter/FilterTest.kt +++ b/hideout-mastodon/src/test/kotlin/mastodon/filter/FilterTest.kt @@ -418,7 +418,7 @@ class FilterTest { ) } .asyncDispatch() - .andExpect { status { isOk() } } + .andExpect { status { isNotFound() } } } @Test @@ -431,7 +431,7 @@ class FilterTest { ) } .asyncDispatch() - .andExpect { status { isOk() } } + .andExpect { status { isNotFound() } } } @Test @@ -456,7 +456,7 @@ class FilterTest { ) } .asyncDispatch() - .andExpect { status { isOk() } } + .andExpect { status { isNotFound() } } } @Test @@ -469,7 +469,7 @@ class FilterTest { ) } .asyncDispatch() - .andExpect { status { isOk() } } + .andExpect { status { isNotFound() } } } @Test @@ -494,7 +494,7 @@ class FilterTest { ) } .asyncDispatch() - .andExpect { status { isOk() } } + .andExpect { status { isNotFound() } } } @Test @@ -507,7 +507,7 @@ class FilterTest { ) } .asyncDispatch() - .andExpect { status { isOk() } } + .andExpect { status { isNotFound() } } } @Test @@ -664,6 +664,7 @@ class FilterTest { } @Test + @Sql("/sql/filter/test-filter.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) fun `apiV1FiltersIdDelete writeで削除できる`() { mockMvc .delete("/api/v1/filters/1") { @@ -676,6 +677,7 @@ class FilterTest { } @Test + @Sql("/sql/filter/test-filter.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) fun `apiV1FiltersIdDelete write_filtersで削除できる`() { mockMvc .delete("/api/v1/filters/1") { @@ -692,11 +694,10 @@ class FilterTest { mockMvc .delete("/api/v1/filters/1") { with( - jwt().jwt { it.claim("uid", "1") }.authorities(SimpleGrantedAuthority("SCOPE_write")) + jwt().jwt { it.claim("uid", "1") }.authorities(SimpleGrantedAuthority("SCOPE_read")) ) } - .asyncDispatch() - .andExpect { status { isOk() } } + .andExpect { status { isForbidden() } } } companion object { diff --git a/hideout-mastodon/src/test/kotlin/mastodon/status/StatusTest.kt b/hideout-mastodon/src/test/kotlin/mastodon/status/StatusTest.kt index 790c3cc6..35a4a8a6 100644 --- a/hideout-mastodon/src/test/kotlin/mastodon/status/StatusTest.kt +++ b/hideout-mastodon/src/test/kotlin/mastodon/status/StatusTest.kt @@ -49,7 +49,7 @@ import org.springframework.web.context.WebApplicationContext @SpringBootTest(classes = [SpringApplication::class]) @AutoConfigureMockMvc @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/test-custom-emoji.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS) class StatusTest { diff --git a/hideout-mastodon/src/test/kotlin/mastodon/timelines/TimelineApiTest.kt b/hideout-mastodon/src/test/kotlin/mastodon/timelines/TimelineApiTest.kt index 4d20bcfb..3f6bf40a 100644 --- a/hideout-mastodon/src/test/kotlin/mastodon/timelines/TimelineApiTest.kt +++ b/hideout-mastodon/src/test/kotlin/mastodon/timelines/TimelineApiTest.kt @@ -37,7 +37,7 @@ import org.springframework.web.context.WebApplicationContext @SpringBootTest(classes = [SpringApplication::class]) @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 { @Autowired private lateinit var context: WebApplicationContext diff --git a/hideout-mastodon/src/test/resources/sql/filter/test-filter.sql b/hideout-mastodon/src/test/resources/sql/filter/test-filter.sql index d06d6bc0..8eab045a 100644 --- a/hideout-mastodon/src/test/resources/sql/filter/test-filter.sql +++ b/hideout-mastodon/src/test/resources/sql/filter/test-filter.sql @@ -1,4 +1,4 @@ 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) VALUES (1, 1, 'hoge', 'NONE') \ No newline at end of file