Skip to content

Commit

Permalink
Unit test for about, Announcement
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoii committed Sep 28, 2023
1 parent 390665d commit 21a50f5
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 2 deletions.
22 changes: 20 additions & 2 deletions mirai-api-http/src/test/kotlin/framework/extend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<VerifyRetDTO>(pathsVerify, VerifyDTO("verifyKey")).also {
assertEquals(StateCode.Success.code, it.code)
assertNotNull(it.session)
}

// bind
postJsonData<StateCode>(pathsBind, BindDTO(SetupMockBot.ID).withSession(verifyRet.session)).also {
assertEquals(StateCode.Success.code, it.code)
}

client.get(Paths.sessionInfo){ parameter("sessionKey", verifyRet.session) }.body<ElementResult>().also {
val session = it.data.jsonElementParseOrNull<SessionDTO>()
assertNotNull(session)
assertEquals(verifyRet.session, session.sessionKey)
}
}

@Test
fun testOnGetBotList() = testMahApplication {
installHttpAdapter()

client.get(Paths.botList).body<LongListRestfulResult>().also {
assertEquals(1, it.data.size)
assertEquals(SetupMockBot.ID, it.data[0])
}
}
}
Original file line number Diff line number Diff line change
@@ -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<AnnouncementList>().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<ElementResult>(
Paths.httpPath(Paths.announcementPublish), PublishAnnouncementDTO(
SetupMockBot.BEST_GROUP_ID,
"new announcement content",
)
).also {
assertEquals(StateCode.Success.code, it.code)
val announcement = it.data.jsonElementParseOrNull<AnnouncementDTO>()

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<StateCode>(
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<StateCode>(
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
)
}
}
}

0 comments on commit 21a50f5

Please sign in to comment.