From 7e6b3854f859d5763af6b6fac8c53182bf1d03fd Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Thu, 7 Dec 2023 11:53:02 +0900 Subject: [PATCH] =?UTF-8?q?test:=20=E3=83=95=E3=82=A9=E3=83=AD=E3=83=BC?= =?UTF-8?q?=E3=81=AE=E7=B5=90=E5=90=88=E3=83=86=E3=82=B9=E3=83=88=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/mastodon/account/AccountApiTest.kt | 22 +++++++++++++++++++ src/intTest/resources/application.yml | 2 +- ...iV1AccountsIdFollowPost フォローできる.sql | 18 +++++++++++++++ .../core/service/user/UserServiceImpl.kt | 3 +++ .../resources/db/migration/V1__Init_DB.sql | 2 +- 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/intTest/resources/sql/accounts/apiV1AccountsIdFollowPost フォローできる.sql diff --git a/src/intTest/kotlin/mastodon/account/AccountApiTest.kt b/src/intTest/kotlin/mastodon/account/AccountApiTest.kt index fa419c24..4798f574 100644 --- a/src/intTest/kotlin/mastodon/account/AccountApiTest.kt +++ b/src/intTest/kotlin/mastodon/account/AccountApiTest.kt @@ -1,8 +1,10 @@ package mastodon.account import dev.usbharu.hideout.SpringApplication +import dev.usbharu.hideout.core.infrastructure.exposedquery.FollowerQueryServiceImpl import dev.usbharu.hideout.core.infrastructure.exposedquery.UserQueryServiceImpl import kotlinx.coroutines.test.runTest +import org.assertj.core.api.Assertions.assertThat import org.flywaydb.core.Flyway import org.junit.jupiter.api.AfterAll import org.junit.jupiter.api.BeforeEach @@ -33,9 +35,13 @@ import org.springframework.web.context.WebApplicationContext @Sql("/sql/test-user2.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS) class AccountApiTest { + @Autowired + private lateinit var followerQueryServiceImpl: FollowerQueryServiceImpl + @Autowired private lateinit var userQueryServiceImpl: UserQueryServiceImpl + @Autowired private lateinit var context: WebApplicationContext @@ -259,6 +265,22 @@ class AccountApiTest { .andExpect { status { isForbidden() } } } + @Test + @Sql("/sql/accounts/apiV1AccountsIdFollowPost フォローできる.sql") + fun `apiV1AccountsIdFollowPost フォローできる`() = runTest { + mockMvc + .post("/api/v1/accounts/3733363/follow") { + contentType = MediaType.APPLICATION_JSON + with(jwt().jwt { it.claim("uid", "37335363") }.authorities(SimpleGrantedAuthority("SCOPE_write"))) + } + .asyncDispatch() + .andExpect { status { isOk() } } + + val alreadyFollow = followerQueryServiceImpl.alreadyFollow(3733363, 37335363) + + assertThat(alreadyFollow).isTrue() + } + companion object { @JvmStatic @AfterAll diff --git a/src/intTest/resources/application.yml b/src/intTest/resources/application.yml index c73fc1f3..51622edd 100644 --- a/src/intTest/resources/application.yml +++ b/src/intTest/resources/application.yml @@ -1,5 +1,5 @@ hideout: - url: "https://localhost:8080" + url: "https://example.com" use-mongodb: true security: jwt: diff --git a/src/intTest/resources/sql/accounts/apiV1AccountsIdFollowPost フォローできる.sql b/src/intTest/resources/sql/accounts/apiV1AccountsIdFollowPost フォローできる.sql new file mode 100644 index 00000000..53ea2830 --- /dev/null +++ b/src/intTest/resources/sql/accounts/apiV1AccountsIdFollowPost フォローできる.sql @@ -0,0 +1,18 @@ +insert into "USERS" (id, name, domain, screen_name, description, password, inbox, outbox, url, public_key, private_key, + created_at, key_id, following, followers, instance) +VALUES (3733363, 'follow-test-user-1', 'example.com', 'follow-test-user-1-name', '', + '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8', + 'https://example.com/users/follow-test-user-1/inbox', + 'https://example.com/users/follow-test-user-1/outbox', 'https://example.com/users/follow-test-user-1', + '-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----', + '-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678, + 'https://example.com/users/follow-test-user-1#pubkey', 'https://example.com/users/follow-test-user-1/following', + 'https://example.com/users/follow-test-user-1/followers', null), + (37335363, 'follow-test-user-2', 'example.com', 'follow-test-user-2-name', '', + '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8', + 'https://example.com/users/follow-test-user-2/inbox', + 'https://example.com/users/follow-test-user-2/outbox', 'https://example.com/users/follow-test-user-2', + '-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----', + '-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678, + 'https://example.com/users/follow-test-user-2#pubkey', 'https://example.com/users/follow-test-user-2/following', + 'https://example.com/users/follow-test-user-2/followers', null); diff --git a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt index d2a2e45e..e70ec782 100644 --- a/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/core/service/user/UserServiceImpl.kt @@ -114,10 +114,13 @@ class UserServiceImpl( } override suspend fun follow(id: Long, followerId: Long) { + logger.debug("START Follow id: {} → target: {}", followerId, id) followerQueryService.appendFollower(id, followerId) if (userRepository.findFollowRequestsById(id, followerId)) { + logger.debug("Follow request is accepted! ") userRepository.deleteFollowRequest(id, followerId) } + logger.debug("SUCCESS Follow id: {} → target: {}", followerId, id) } override suspend fun unfollow(id: Long, followerId: Long): Boolean { diff --git a/src/main/resources/db/migration/V1__Init_DB.sql b/src/main/resources/db/migration/V1__Init_DB.sql index e0188588..1440fb61 100644 --- a/src/main/resources/db/migration/V1__Init_DB.sql +++ b/src/main/resources/db/migration/V1__Init_DB.sql @@ -31,7 +31,7 @@ create table if not exists users "following" varchar(1000) null, followers varchar(1000) null, "instance" bigint null, - unique (name, domain), + unique ("name", "domain"), constraint fk_users_instance__id foreign key ("instance") references instance (id) on delete restrict on update restrict ); create table if not exists follow_requests