From 1a5c07561b0c1dbfa97de3e85f9874c8db3cf5c3 Mon Sep 17 00:00:00 2001 From: phjppo0918 Date: Mon, 20 Nov 2023 16:16:19 +0900 Subject: [PATCH 1/3] =?UTF-8?q?delete=20:=20image=20api=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qbox/qboxserver/domain/image/ImageCtrl.kt | 19 ------------------- .../qboxserver/domain/image/ImageCtrlTest.kt | 7 ------- 2 files changed, 26 deletions(-) delete mode 100644 src/main/kotlin/site/qbox/qboxserver/domain/image/ImageCtrl.kt delete mode 100644 src/test/kotlin/site/qbox/qboxserver/domain/image/ImageCtrlTest.kt diff --git a/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageCtrl.kt b/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageCtrl.kt deleted file mode 100644 index f133b7b..0000000 --- a/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageCtrl.kt +++ /dev/null @@ -1,19 +0,0 @@ -package site.qbox.qboxserver.domain.image - -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RequestPart -import org.springframework.web.bind.annotation.RestController -import org.springframework.web.multipart.MultipartFile -import site.qbox.qboxserver.domain.image.dto.ImageRes - -@RestController -@RequestMapping("images") -class ImageCtrl( - private val imageSvc: ImageSvc, -) { - - @PostMapping - fun save(@RequestPart file: MultipartFile): ImageRes = - imageSvc.save(file) -} \ No newline at end of file diff --git a/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageCtrlTest.kt b/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageCtrlTest.kt deleted file mode 100644 index 23f63aa..0000000 --- a/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageCtrlTest.kt +++ /dev/null @@ -1,7 +0,0 @@ -package site.qbox.qboxserver.domain.image - -import site.qbox.qboxserver.config.WebClientDocsTest - -class ImageCtrlTest : WebClientDocsTest() { - -} From ddbf76177eeffeeecb7be40bb9ed3d4418975d3b Mon Sep 17 00:00:00 2001 From: phjppo0918 Date: Mon, 20 Nov 2023 16:16:43 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat=20:=20local-storage=20=ED=99=98?= =?UTF-8?q?=EA=B2=BD=20=EB=B3=80=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-local.yml | 4 ++++ src/main/resources/application.yml | 6 +++++- src/test/resources/application.yml | 6 +++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 9b8c581..aac4c1d 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -43,3 +43,7 @@ logging: service: url: http://localhost:5172 + +local-storage: + directory: http://localhost/qbox/image/ + url: http://localhost:5172 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b2d8421..1431976 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -45,4 +45,8 @@ logging: config: classpath:logback-spring.xml service: - url: ${SERVICE_URL} \ No newline at end of file + url: ${SERVICE_URL} + +local-storage: + directory: ${LOCAL_STORAGE_DIRECTORY} + url: ${LOCAL_STORAGE_URL} \ No newline at end of file diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 075aa87..f360944 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -42,4 +42,8 @@ logging: config: classpath:logback-spring.xml service: - url: https://aaa.bb.com \ No newline at end of file + url: https://aaa.bb.com + +local-storage: + directory: http://localhost/qbox/image/ + url: http://localhost:5172 \ No newline at end of file From f369f1c9c606c8f00b91074a0f00072626be1fd8 Mon Sep 17 00:00:00 2001 From: phjppo0918 Date: Mon, 20 Nov 2023 16:47:11 +0900 Subject: [PATCH 3/3] =?UTF-8?q?test=20:=20image=20=EC=A0=80=EC=9E=A5=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EB=8B=A8=EC=9C=84=20test=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 1 + .../qbox/qboxserver/domain/image/ImageSvc.kt | 2 +- .../qboxserver/domain/image/ImageValidator.kt | 6 +-- .../domain/member/query/MemberQuery.kt | 2 +- .../qboxserver/domain/image/ImageSvcTest.kt | 27 ++++++++++++++ .../domain/image/ImageValidatorTest.kt | 37 +++++++++++++++++++ .../site/qbox/qboxserver/study/RequireTest.kt | 27 ++++++++++++++ 7 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 src/test/kotlin/site/qbox/qboxserver/study/RequireTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 27babf3..92f189f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -157,6 +157,7 @@ koverReport { classes("**.QboxServerApplicationKt") classes("**.**Config") classes("**.**Query") + classes("**.local.**") classes("site.qbox.qboxserver.domain.member.email.svc.MailSvc") classes("site.qbox.qboxserver.domain.image.local.LocalStorageImageRepo") classes("**.dto.**") diff --git a/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageSvc.kt b/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageSvc.kt index 3259afd..91f2fe4 100644 --- a/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageSvc.kt +++ b/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageSvc.kt @@ -19,5 +19,5 @@ class ImageSvc( "${UUID.randomUUID()}.${extractExtension(file)}" private fun extractExtension(file: MultipartFile): String = - file.originalFilename!!.substringAfterLast(".") + file.name.substringAfterLast(".") } \ No newline at end of file diff --git a/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageValidator.kt b/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageValidator.kt index 9428bf1..767150f 100644 --- a/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageValidator.kt +++ b/src/main/kotlin/site/qbox/qboxserver/domain/image/ImageValidator.kt @@ -7,15 +7,13 @@ import site.qbox.qboxserver.domain.image.exception.NotAllowedImageException @Component class ImageValidator { companion object { - val ALLOW_EXTENSIONS = listOf("png", "jpg", "jpeg", "gif", "bmp", "webp", "ico", "tiff", "tif", "svg") + private val ALLOW_EXTENSIONS = listOf("png", "jpg", "jpeg", "gif", "bmp", "webp", "ico", "tiff", "tif", "svg") } fun validate(file: MultipartFile) { - file.originalFilename ?: NotAllowedImageException() - file.contentType ?: NotAllowedImageException() require(extractExtension(file) in ALLOW_EXTENSIONS) { throw NotAllowedImageException() } } private fun extractExtension(file: MultipartFile): String = - file.originalFilename!!.substringAfterLast(".") + file.name.substringAfterLast(".") } diff --git a/src/main/kotlin/site/qbox/qboxserver/domain/member/query/MemberQuery.kt b/src/main/kotlin/site/qbox/qboxserver/domain/member/query/MemberQuery.kt index d871a59..8d02332 100644 --- a/src/main/kotlin/site/qbox/qboxserver/domain/member/query/MemberQuery.kt +++ b/src/main/kotlin/site/qbox/qboxserver/domain/member/query/MemberQuery.kt @@ -4,7 +4,7 @@ import com.querydsl.core.types.dsl.StringPath import site.qbox.qboxserver.domain.member.command.entity.QMember.member import site.qbox.qboxserver.domain.member.query.dto.QMemberSummary -class MemberQuery { +interface MemberQuery { companion object { val id: StringPath = member.email.email val summary = QMemberSummary(id, member.nickname) diff --git a/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageSvcTest.kt b/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageSvcTest.kt index d7cc592..ae80afd 100644 --- a/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageSvcTest.kt +++ b/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageSvcTest.kt @@ -1,7 +1,34 @@ package site.qbox.qboxserver.domain.image +import com.ninjasquad.springmockk.MockkBean import io.kotest.core.spec.style.DescribeSpec +import io.kotest.extensions.spring.SpringExtension +import io.kotest.matchers.shouldBe +import io.mockk.every +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.mock.web.MockMultipartFile +@SpringBootTest class ImageSvcTest : DescribeSpec() { + override fun extensions() = listOf(SpringExtension) + + @Autowired + lateinit var imageSvc: ImageSvc + + @MockkBean + lateinit var imageRepo: ImageRepo + + val image = MockMultipartFile("asdfasdf.jpg", byteArrayOf(9)) + init { + it("저장을 수행한다.") { + val savedUrl = "https://hello.com/user/kkkkk-sdf.jpg" + every { imageRepo.save(image, any(String::class)) } returns savedUrl + + val result = imageSvc.save(image) + result.location shouldBe savedUrl + } + } + } diff --git a/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageValidatorTest.kt b/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageValidatorTest.kt index da78257..86bbee9 100644 --- a/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageValidatorTest.kt +++ b/src/test/kotlin/site/qbox/qboxserver/domain/image/ImageValidatorTest.kt @@ -1,7 +1,44 @@ package site.qbox.qboxserver.domain.image +import io.kotest.assertions.throwables.shouldNotThrow +import io.kotest.assertions.throwables.shouldThrow import io.kotest.core.spec.style.DescribeSpec +import io.kotest.extensions.spring.SpringExtension +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.mock.web.MockMultipartFile +import site.qbox.qboxserver.domain.image.exception.NotAllowedImageException +@SpringBootTest class ImageValidatorTest : DescribeSpec() { + override fun extensions() = listOf(SpringExtension) + + @Autowired + lateinit var imageValidator: ImageValidator + + init { + it("검증을 수행한다") { + val image = MockMultipartFile("hello.jpg", byteArrayOf(9)) + shouldNotThrow { + imageValidator.validate(image) + } + } + + it("확장자가 명시되어있지 않으면 예외를 반환한다") { + val image = MockMultipartFile("asdfasdf", byteArrayOf(9)) + shouldThrow { + imageValidator.validate(image) + } + } + + it("허용하지 않은 확장자면 예외를 반환한다") { + val image = MockMultipartFile("hello.txt", byteArrayOf(9)) + shouldThrow { + imageValidator.validate(image) + } + } + + } + } diff --git a/src/test/kotlin/site/qbox/qboxserver/study/RequireTest.kt b/src/test/kotlin/site/qbox/qboxserver/study/RequireTest.kt new file mode 100644 index 0000000..599103a --- /dev/null +++ b/src/test/kotlin/site/qbox/qboxserver/study/RequireTest.kt @@ -0,0 +1,27 @@ +package site.qbox.qboxserver.study + +import io.kotest.assertions.throwables.shouldNotThrow +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.core.spec.style.DescribeSpec + +class RequireTest : DescribeSpec() { + + + fun requireIn(value: String) { + val list = listOf("A", "B", "C", "D", "E", "F") + require(value in list) + } + + init { + it("in 값에 포함되면 예외를 반환하지 않는다.") { + shouldNotThrow { + requireIn("A") + } + } + it("in 값에 포함되지 않으면 예외를 반환한다.") { + shouldThrow { + requireIn("KK") + } + } + } +} \ No newline at end of file