Add output of DeletionResult to deleteAllMessages call

This commit is contained in:
Nico Alt
2020-10-08 14:42:40 +02:00
parent 6c6dbfd357
commit c017a813b0
3 changed files with 37 additions and 3 deletions

View File

@@ -109,11 +109,11 @@ constructor(
override fun deleteAllMessages(ctx: Context): Context { override fun deleteAllMessages(ctx: Context): Context {
val contactId = ctx.getContactIdFromPathParam() val contactId = ctx.getContactIdFromPathParam()
try { try {
conversationManager.deleteAllMessages(contactId) val result = conversationManager.deleteAllMessages(contactId)
return ctx.json(result.output())
} catch (e: NoSuchContactException) { } catch (e: NoSuchContactException) {
throw NotFoundResponse() throw NotFoundResponse()
} }
return ctx
} }
override fun eventOccurred(e: Event) { override fun eventOccurred(e: Event) {

View File

@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.sync.MessageId
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent import org.briarproject.bramble.api.sync.event.MessagesAckedEvent
import org.briarproject.bramble.api.sync.event.MessagesSentEvent import org.briarproject.bramble.api.sync.event.MessagesSentEvent
import org.briarproject.briar.api.conversation.ConversationMessageHeader import org.briarproject.briar.api.conversation.ConversationMessageHeader
import org.briarproject.briar.api.conversation.DeletionResult
import org.briarproject.briar.api.messaging.PrivateMessage import org.briarproject.briar.api.messaging.PrivateMessage
import org.briarproject.briar.api.messaging.PrivateMessageHeader import org.briarproject.briar.api.messaging.PrivateMessageHeader
import org.briarproject.briar.headless.json.JsonDict import org.briarproject.briar.headless.json.JsonDict
@@ -47,6 +48,15 @@ internal fun PrivateMessage.output(contactId: ContactId, text: String) = JsonDic
"text" to text "text" to text
) )
internal fun DeletionResult.output() = JsonDict(
"allDeleted" to allDeleted(),
"hasIntroductionSessionInProgress" to hasIntroductionSessionInProgress(),
"hasInvitationSessionInProgress" to hasInvitationSessionInProgress(),
"hasNotAllIntroductionSelected" to hasNotAllIntroductionSelected(),
"hasNotAllInvitationSelected" to hasNotAllInvitationSelected(),
"hasNotFullyDownloaded" to hasNotFullyDownloaded()
)
internal fun MessagesAckedEvent.output() = JsonDict( internal fun MessagesAckedEvent.output() = JsonDict(
"contactId" to contactId.int, "contactId" to contactId.int,
"messageIds" to messageIds.toJson() "messageIds" to messageIds.toJson()

View File

@@ -33,6 +33,7 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.spongycastle.util.encoders.Base64 import org.spongycastle.util.encoders.Base64
import kotlin.random.Random
internal class MessagingControllerImplTest : ControllerTest() { internal class MessagingControllerImplTest : ControllerTest() {
@@ -346,8 +347,10 @@ internal class MessagingControllerImplTest : ControllerTest() {
@Test @Test
fun testDeleteAllMessages() { fun testDeleteAllMessages() {
val result = DeletionResult()
every { ctx.pathParam("contactId") } returns "1" every { ctx.pathParam("contactId") } returns "1"
every { conversationManager.deleteAllMessages(ContactId(1)) } returns DeletionResult() every { conversationManager.deleteAllMessages(ContactId(1)) } returns result
every { ctx.json(result.output()) } returns ctx
controller.deleteAllMessages(ctx) controller.deleteAllMessages(ctx)
} }
@@ -368,6 +371,27 @@ internal class MessagingControllerImplTest : ControllerTest() {
} }
} }
@Test
fun testOutputDeletionResult() {
val result = DeletionResult()
if (Random.nextBoolean()) result.addInvitationNotAllSelected()
if (Random.nextBoolean()) result.addInvitationSessionInProgress()
if (Random.nextBoolean()) result.addIntroductionNotAllSelected()
if (Random.nextBoolean()) result.addIntroductionSessionInProgress()
if (Random.nextBoolean()) result.addNotFullyDownloaded()
val json = """
{
"allDeleted": ${result.allDeleted()},
"hasIntroductionSessionInProgress": ${result.hasIntroductionSessionInProgress()},
"hasInvitationSessionInProgress": ${result.hasInvitationSessionInProgress()},
"hasNotAllIntroductionSelected": ${result.hasNotAllIntroductionSelected()},
"hasNotAllInvitationSelected": ${result.hasNotAllInvitationSelected()},
"hasNotFullyDownloaded": ${result.hasNotFullyDownloaded()}
}
"""
assertJsonEquals(json, result.output())
}
private fun expectGetContact() { private fun expectGetContact() {
every { ctx.pathParam("contactId") } returns contact.id.int.toString() every { ctx.pathParam("contactId") } returns contact.id.int.toString()
every { contactManager.getContact(contact.id) } returns contact every { contactManager.getContact(contact.id) } returns contact