From 9c8aef6c4444caa1eece0dd273022a537da3da16 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Sat, 20 May 2023 15:04:56 +0900 Subject: [PATCH] =?UTF-8?q?test:=20Users=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 --- .../routing/api/internal/v1/UsersTest.kt | 567 +++++++++++++++++- 1 file changed, 558 insertions(+), 9 deletions(-) diff --git a/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/UsersTest.kt b/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/UsersTest.kt index 7e411c49..2fe03d92 100644 --- a/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/UsersTest.kt +++ b/src/test/kotlin/dev/usbharu/hideout/routing/api/internal/v1/UsersTest.kt @@ -1,24 +1,27 @@ package dev.usbharu.hideout.routing.api.internal.v1 +import com.auth0.jwt.interfaces.Claim +import com.auth0.jwt.interfaces.Payload import com.fasterxml.jackson.module.kotlin.readValue import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.domain.model.hideout.dto.UserResponse import dev.usbharu.hideout.domain.model.hideout.entity.User import dev.usbharu.hideout.domain.model.hideout.form.UserCreate +import dev.usbharu.hideout.plugins.TOKEN_AUTH import dev.usbharu.hideout.plugins.configureSecurity import dev.usbharu.hideout.plugins.configureSerialization +import dev.usbharu.hideout.service.IUserApiService import dev.usbharu.hideout.service.impl.IUserService import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* +import io.ktor.server.auth.* +import io.ktor.server.auth.jwt.* import io.ktor.server.config.* import io.ktor.server.routing.* import io.ktor.server.testing.* import org.junit.jupiter.api.Test -import org.mockito.kotlin.any -import org.mockito.kotlin.anyOrNull -import org.mockito.kotlin.doReturn -import org.mockito.kotlin.mock +import org.mockito.kotlin.* import utils.JsonObjectMapper import java.time.Instant import kotlin.test.assertEquals @@ -50,15 +53,15 @@ class UsersTest { Instant.now().toEpochMilli() ), ) - val userService = mock { - onBlocking { findAllForUser(anyOrNull(), anyOrNull()) } doReturn users + val userService = mock { + onBlocking { findAll(anyOrNull(), anyOrNull()) } doReturn users } application { configureSerialization() configureSecurity(mock(), mock(), mock(), mock(), mock()) routing { route("/api/internal/v1") { - users(userService) + users(mock(), userService) } } } @@ -96,7 +99,7 @@ class UsersTest { configureSecurity(mock(), mock(), mock(), mock(), mock()) routing { route("/api/internal/v1") { - users(userService) + users(userService, mock()) } } } @@ -127,7 +130,7 @@ class UsersTest { configureSecurity(mock(), mock(), mock(), mock(), mock()) routing { route("/api/internal/v1") { - users(userService) + users(userService, mock()) } } } @@ -139,4 +142,550 @@ class UsersTest { assertEquals(HttpStatusCode.BadRequest, status) } } + + @Test + fun `users name にGETしたらユーザーを取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + val userResponse = UserResponse( + 1234, + "test1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + val userApiService = mock { + onBlocking { findByAcct(any()) } doReturn userResponse + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/test1").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(userResponse, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } + + @Test + fun `users id にGETしたらユーザーを取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + val userResponse = UserResponse( + 1234, + "test1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + val userApiService = mock { + onBlocking { findById(any()) } doReturn userResponse + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/1234").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(userResponse, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } + + @Test + fun `users name@domain にGETしたらユーザーを取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + val userResponse = UserResponse( + 1234, + "test1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + val userApiService = mock { + onBlocking { findByAcct(any()) } doReturn userResponse + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/test1").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(userResponse, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } + + @Test + fun `users @name@domain にGETしたらユーザーを取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + val userResponse = UserResponse( + 1234, + "test1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + val userApiService = mock { + onBlocking { findByAcct(any()) } doReturn userResponse + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/test1").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(userResponse, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } + + @Test + fun `users name followers にGETしたらフォロワー一覧を取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + + val followers = listOf( + UserResponse( + 1235, + "follower1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ), UserResponse( + 1236, + "follower2", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + ) + val userApiService = mock { + onBlocking { findFollowersByAcct(any()) } doReturn followers + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/test1/followers").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(followers, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } + + @Test + fun `users name@domain followers にGETしたらフォロワー一覧を取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + + val followers = listOf( + UserResponse( + 1235, + "follower1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ), UserResponse( + 1236, + "follower2", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + ) + val userApiService = mock { + onBlocking { findFollowersByAcct(any()) } doReturn followers + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/@test1@example.com/followers").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(followers, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } + + @Test + fun `users id followers にGETしたらフォロワー一覧を取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + + val followers = listOf( + UserResponse( + 1235, + "follower1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ), UserResponse( + 1236, + "follower2", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + ) + val userApiService = mock { + onBlocking { findFollowers(any()) } doReturn followers + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/1234/followers").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(followers, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } + + @Test + fun `users name followers に認証情報ありでGETしたらフォローできる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + + val claim = mock { + on { asLong() } doReturn 1234 + } + val payload = mock { + on { getClaim(eq("uid")) } doReturn claim + } + + val userApiService = mock { + onBlocking { findByAcct(any()) } doReturn UserResponse( + 1235, + "follower1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + } + val userService = mock { + onBlocking { addFollowers(eq(1235), eq(1234)) } doReturn true + } + application { + configureSerialization() + authentication { + bearer(TOKEN_AUTH) { + authenticate { + JWTPrincipal(payload) + } + } + } + routing { + route("/api/internal/v1") { + users(userService, userApiService) + } + } + } + + client.post("/api/internal/v1/users/test1/followers") { + header(HttpHeaders.Authorization, "Bearer test") + }.apply { + assertEquals(HttpStatusCode.OK, status) + } + } + + @Test + fun `users name followers に認証情報ありでGETしたらフォロー処理受付になることもある`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + + val claim = mock { + on { asLong() } doReturn 1234 + } + val payload = mock { + on { getClaim(eq("uid")) } doReturn claim + } + + val userApiService = mock { + onBlocking { findByAcct(any()) } doReturn UserResponse( + 1235, + "follower1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + } + val userService = mock { + onBlocking { addFollowers(eq(1235), eq(1234)) } doReturn false + } + application { + configureSerialization() + authentication { + bearer(TOKEN_AUTH) { + authenticate { + JWTPrincipal(payload) + } + } + } + routing { + route("/api/internal/v1") { + users(userService, userApiService) + } + } + } + + client.post("/api/internal/v1/users/test1/followers") { + header(HttpHeaders.Authorization, "Bearer test") + }.apply { + assertEquals(HttpStatusCode.Accepted, status) + } + } + + @Test + fun `users id followers に認証情報ありでGETしたらフォロー処理受付になることもある`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + + val claim = mock { + on { asLong() } doReturn 1234 + } + val payload = mock { + on { getClaim(eq("uid")) } doReturn claim + } + + val userApiService = mock { + onBlocking { findById(any()) } doReturn UserResponse( + 1235, + "follower1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + } + val userService = mock { + onBlocking { addFollowers(eq(1235), eq(1234)) } doReturn false + } + application { + configureSerialization() + authentication { + bearer(TOKEN_AUTH) { + authenticate { + JWTPrincipal(payload) + } + } + } + routing { + route("/api/internal/v1") { + users(userService, userApiService) + } + } + } + + client.post("/api/internal/v1/users/1235/followers") { + header(HttpHeaders.Authorization, "Bearer test") + }.apply { + assertEquals(HttpStatusCode.Accepted, status) + } + } + + @Test + fun `users name following にGETしたらフォロイー一覧を取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + + val followers = listOf( + UserResponse( + 1235, + "follower1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ), UserResponse( + 1236, + "follower2", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + ) + val userApiService = mock { + onBlocking { findFollowingsByAcct(any()) } doReturn followers + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/test1/following").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(followers, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } + + @Test + fun `users name@domain following にGETしたらフォロイー一覧を取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + + val followers = listOf( + UserResponse( + 1235, + "follower1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ), UserResponse( + 1236, + "follower2", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + ) + val userApiService = mock { + onBlocking { findFollowingsByAcct(any()) } doReturn followers + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/test1@domain/following").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(followers, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } + + @Test + fun `users id following にGETしたらフォロイー一覧を取得できる`() = testApplication { + environment { + config = ApplicationConfig("empty.conf") + } + + val followers = listOf( + UserResponse( + 1235, + "follower1", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ), UserResponse( + 1236, + "follower2", + "example.com", + "test", + "test User", + "https://example.com/test", + Instant.now().toEpochMilli() + ) + ) + val userApiService = mock { + onBlocking { findFollowings(any()) } doReturn followers + } + application { + configureSerialization() + configureSecurity(mock(), mock(), mock(), mock(), mock()) + routing { + route("/api/internal/v1") { + users(mock(), userApiService) + } + } + } + + client.get("/api/internal/v1/users/1234/following").apply { + assertEquals(HttpStatusCode.OK, status) + assertEquals(followers, JsonObjectMapper.objectMapper.readValue(bodyAsText())) + } + } }