From c93a00cd4b078a1f362303d48e74cc5673010723 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Fri, 28 Apr 2023 17:54:59 +0900 Subject: [PATCH] =?UTF-8?q?test:=20UserService=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hideout/service/impl/UserServiceTest.kt | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/test/kotlin/dev/usbharu/hideout/service/impl/UserServiceTest.kt diff --git a/src/test/kotlin/dev/usbharu/hideout/service/impl/UserServiceTest.kt b/src/test/kotlin/dev/usbharu/hideout/service/impl/UserServiceTest.kt new file mode 100644 index 00000000..45965180 --- /dev/null +++ b/src/test/kotlin/dev/usbharu/hideout/service/impl/UserServiceTest.kt @@ -0,0 +1,88 @@ +@file:OptIn(ExperimentalCoroutinesApi::class) + +package dev.usbharu.hideout.service.impl + +import dev.usbharu.hideout.config.Config +import dev.usbharu.hideout.config.ConfigData +import dev.usbharu.hideout.domain.model.hideout.dto.RemoteUserCreateDto +import dev.usbharu.hideout.domain.model.hideout.dto.UserCreateDto +import dev.usbharu.hideout.repository.IUserRepository +import dev.usbharu.hideout.service.IUserAuthService +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.anyString +import org.mockito.kotlin.* +import java.security.KeyPairGenerator +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class UserServiceTest{ + @Test + fun `createLocalUser ローカルユーザーを作成できる`() = runTest { + Config.configData = ConfigData(domain = "example.com", url = "https://example.com") + val userRepository = mock { + onBlocking { nextId() } doReturn 110001L + } + val generateKeyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair() + val userAuthService = mock { + onBlocking { hash(anyString()) } doReturn "hashedPassword" + onBlocking { generateKeyPair() } doReturn generateKeyPair + } + val userService = UserService(userRepository, userAuthService) + userService.createLocalUser(UserCreateDto("test", "testUser", "XXXXXXXXXXXXX", "test")) + verify(userRepository, times(1)).save(any()) + argumentCaptor { + verify(userRepository, times(1)).save(capture()) + assertEquals("test", firstValue.name) + assertEquals("testUser", firstValue.screenName) + assertEquals("XXXXXXXXXXXXX", firstValue.description) + assertEquals("hashedPassword", firstValue.password) + assertEquals(110001L, firstValue.id) + assertEquals("https://example.com/users/110001", firstValue.url) + assertEquals("example.com", firstValue.domain) + assertEquals("https://example.com/users/110001/inbox", firstValue.inbox) + assertEquals("https://example.com/users/110001/outbox", firstValue.outbox) + assertEquals(generateKeyPair.public.toPem(),firstValue.publicKey) + assertEquals(generateKeyPair.private.toPem(),firstValue.privateKey) + } + } + + @Test + fun `createRemoteUser リモートユーザーを作成できる`() = runTest { + + Config.configData = ConfigData(domain = "example.com", url = "https://example.com") + + + val userRepository = mock{ + onBlocking { nextId() } doReturn 113345L + } + val userService = UserService(userRepository,mock()) + val user = RemoteUserCreateDto( + "test", + "example.com", + "testUser", + "test user", + "https://example.com/inbox", + "https://example.com/outbox", + "https://example.com", + "-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----" + ) + userService.createRemoteUser(user) + verify(userRepository, times(1)).save(any()) + argumentCaptor { + verify(userRepository, times(1)).save(capture()) + assertEquals("test", firstValue.name) + assertEquals("testUser", firstValue.screenName) + assertEquals("test user", firstValue.description) + assertNull(firstValue.password) + assertEquals(113345L, firstValue.id) + assertEquals("https://example.com", firstValue.url) + assertEquals("example.com", firstValue.domain) + assertEquals("https://example.com/inbox", firstValue.inbox) + assertEquals("https://example.com/outbox", firstValue.outbox) + assertEquals("-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----",firstValue.publicKey) + assertNull(firstValue.privateKey) + } + } +}