From 21a50f591b9ffbd53fc16e25fa9c802ce40f6cd3 Mon Sep 17 00:00:00 2001 From: ryoii Date: Thu, 28 Sep 2023 18:44:27 +0800 Subject: [PATCH] Unit test for about, Announcement --- .../src/test/kotlin/framework/extend.kt | 22 ++- .../integration/action/AboutActionTest.kt | 57 ++++++++ .../action/AnnouncementActionTest.kt | 127 ++++++++++++++++++ 3 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 mirai-api-http/src/test/kotlin/integration/action/AboutActionTest.kt create mode 100644 mirai-api-http/src/test/kotlin/integration/action/AnnouncementActionTest.kt diff --git a/mirai-api-http/src/test/kotlin/framework/extend.kt b/mirai-api-http/src/test/kotlin/framework/extend.kt index 4b4eb7fe..73022589 100644 --- a/mirai-api-http/src/test/kotlin/framework/extend.kt +++ b/mirai-api-http/src/test/kotlin/framework/extend.kt @@ -23,11 +23,29 @@ class SetupMockBot : BeforeAllCallback { .id(ID) .create() - bot.addFriend(FRIEND_ID, "friend") + bot.addFriend(BEST_FRIEND_ID, "best_friend") + bot.addFriend(WORST_FRIEND_ID, "worst_friend") + bot.addGroup(BEST_GROUP_ID, "best_group").apply { + addMember(BEST_MEMBER_ID, "best_member") + addMember(GOOD_MEMBER_ID, "good_member") + } + bot.addGroup(WORST_GROUP_ID, "worst_group").apply { + addMember(WORST_MEMBER_ID, "worst_member") + addMember(BAD_MEMBER_ID, "bad_member") + } } companion object { const val ID = 1L - const val FRIEND_ID = 11L + const val BEST_FRIEND_ID = 11L + const val WORST_FRIEND_ID = 99L + + const val BEST_GROUP_ID = 111L + const val BEST_MEMBER_ID = 11111L + const val GOOD_MEMBER_ID = 11122L + + const val WORST_GROUP_ID = 999L + const val WORST_MEMBER_ID = 99999L + const val BAD_MEMBER_ID = 99988L } } \ No newline at end of file diff --git a/mirai-api-http/src/test/kotlin/integration/action/AboutActionTest.kt b/mirai-api-http/src/test/kotlin/integration/action/AboutActionTest.kt new file mode 100644 index 00000000..585a5ef9 --- /dev/null +++ b/mirai-api-http/src/test/kotlin/integration/action/AboutActionTest.kt @@ -0,0 +1,57 @@ +package integration.action + +import framework.SetupMockBot +import framework.testMahApplication +import integration.withSession +import io.ktor.client.call.* +import io.ktor.client.request.* +import net.mamoe.mirai.api.http.adapter.common.StateCode +import net.mamoe.mirai.api.http.adapter.internal.consts.Paths +import net.mamoe.mirai.api.http.adapter.internal.dto.* +import net.mamoe.mirai.api.http.adapter.internal.serializer.jsonElementParseOrNull +import org.junit.jupiter.api.extension.ExtendWith +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull + + +@ExtendWith(SetupMockBot::class) +class AboutActionTest { + + private val pathsVerify = Paths.httpPath("verify") + private val pathsBind = Paths.httpPath("bind") + + @Test + fun testOnGetSessionInfo() = testMahApplication( + enableVerify = true, + singleMode = false, + ) { + installHttpAdapter() + + val verifyRet = postJsonData(pathsVerify, VerifyDTO("verifyKey")).also { + assertEquals(StateCode.Success.code, it.code) + assertNotNull(it.session) + } + + // bind + postJsonData(pathsBind, BindDTO(SetupMockBot.ID).withSession(verifyRet.session)).also { + assertEquals(StateCode.Success.code, it.code) + } + + client.get(Paths.sessionInfo){ parameter("sessionKey", verifyRet.session) }.body().also { + val session = it.data.jsonElementParseOrNull() + assertNotNull(session) + assertEquals(verifyRet.session, session.sessionKey) + } + } + + @Test + fun testOnGetBotList() = testMahApplication { + installHttpAdapter() + + client.get(Paths.botList).body().also { + assertEquals(1, it.data.size) + assertEquals(SetupMockBot.ID, it.data[0]) + } + } +} \ No newline at end of file diff --git a/mirai-api-http/src/test/kotlin/integration/action/AnnouncementActionTest.kt b/mirai-api-http/src/test/kotlin/integration/action/AnnouncementActionTest.kt new file mode 100644 index 00000000..50f93ed5 --- /dev/null +++ b/mirai-api-http/src/test/kotlin/integration/action/AnnouncementActionTest.kt @@ -0,0 +1,127 @@ +package integration.action + +import framework.ExtendWith +import framework.SetupMockBot +import framework.testMahApplication +import io.ktor.client.call.* +import io.ktor.client.request.* +import kotlinx.coroutines.flow.first +import net.mamoe.mirai.Bot +import net.mamoe.mirai.api.http.adapter.common.StateCode +import net.mamoe.mirai.api.http.adapter.internal.consts.Paths +import net.mamoe.mirai.api.http.adapter.internal.dto.AnnouncementDTO +import net.mamoe.mirai.api.http.adapter.internal.dto.ElementResult +import net.mamoe.mirai.api.http.adapter.internal.dto.parameter.AnnouncementDeleteDTO +import net.mamoe.mirai.api.http.adapter.internal.dto.parameter.AnnouncementList +import net.mamoe.mirai.api.http.adapter.internal.dto.parameter.PublishAnnouncementDTO +import net.mamoe.mirai.api.http.adapter.internal.serializer.jsonElementParseOrNull +import net.mamoe.mirai.console.util.cast +import net.mamoe.mirai.contact.announcement.AnnouncementParameters +import net.mamoe.mirai.contact.getMemberOrFail +import net.mamoe.mirai.mock.MockBot +import net.mamoe.mirai.mock.contact.announcement.MockOnlineAnnouncement +import net.mamoe.mirai.utils.MiraiInternalApi +import org.junit.jupiter.api.BeforeAll +import kotlin.test.* + +@ExtendWith(SetupMockBot::class) +class AnnouncementActionTest { + + @Test + fun testOnListAnnouncement() = testMahApplication { + installHttpAdapter() + + client.get(Paths.httpPath(Paths.announcementList)) { + parameter("id", SetupMockBot.BEST_GROUP_ID) + }.body().also { + assertEquals(1, it.data.size) + + val announcement = it.data[0] + assertEquals("announcement content", announcement.content) + assertEquals(SetupMockBot.BEST_MEMBER_ID, announcement.senderId) + assertEquals(SetupMockBot.BEST_GROUP_ID, announcement.group.id) + assertEquals(1, announcement.publicationTime) + } + } + + @Test + fun testOnPublishAnnouncement() = testMahApplication { + installHttpAdapter() + + postJsonData( + Paths.httpPath(Paths.announcementPublish), PublishAnnouncementDTO( + SetupMockBot.BEST_GROUP_ID, + "new announcement content", + ) + ).also { + assertEquals(StateCode.Success.code, it.code) + val announcement = it.data.jsonElementParseOrNull() + + assertNotNull(announcement) + assertEquals("new announcement content", announcement.content) + assertEquals(SetupMockBot.ID, announcement.senderId) + assertEquals(SetupMockBot.BEST_GROUP_ID, announcement.group.id) + + Bot.getInstance(SetupMockBot.ID).groups[SetupMockBot.BEST_GROUP_ID]!!.announcements.get(announcement.fid) + .also { groupAnnouncement -> + assertNotNull(groupAnnouncement) + assertEquals("new announcement content", groupAnnouncement.content) + assertEquals(SetupMockBot.ID, announcement.senderId) + assertEquals(SetupMockBot.BEST_GROUP_ID, groupAnnouncement.group.id) + } + } + } + + @Test + fun testOnDeleteAnnouncement() = testMahApplication { + installHttpAdapter() + + val fid = Bot.getInstance(SetupMockBot.ID).groups[SetupMockBot.BEST_GROUP_ID]!!.announcements.asFlow().first().fid + + postJsonData( + Paths.httpPath(Paths.announcementDelete), + AnnouncementDeleteDTO( + SetupMockBot.BEST_GROUP_ID, + fid, + ) + ).also { + assertEquals(StateCode.Success.code, it.code) + + Bot.getInstance(SetupMockBot.ID).groups[SetupMockBot.BEST_GROUP_ID]!!.announcements.get(fid) + .also(::assertNull) + } + + postJsonData( + Paths.httpPath(Paths.announcementDelete), + AnnouncementDeleteDTO( + SetupMockBot.BEST_GROUP_ID, + fid, + ) + ).also { + assertEquals(StateCode.NoElement.code, it.code) + } + } + + companion object { + @JvmStatic + @OptIn(MiraiInternalApi::class) + @BeforeAll + fun setUpAnnouncement() { + val bot: MockBot = Bot.getInstance(SetupMockBot.ID).cast() + bot.groups[SetupMockBot.BEST_GROUP_ID]!!.announcements + .mockPublish( + MockOnlineAnnouncement( + "announcement content", + AnnouncementParameters.DEFAULT, + SetupMockBot.BEST_MEMBER_ID, + "mock announcement fid", + false, + 0, + 1, + ), + bot.getGroupOrFail(SetupMockBot.BEST_GROUP_ID).getMemberOrFail(SetupMockBot.BEST_MEMBER_ID), + false + ) + } + } +} \ No newline at end of file