briar-headless: Add more controller tests

Current controller line coverage: 100%
This commit is contained in:
Torsten Grote
2018-08-27 17:37:10 -03:00
parent 159fd34c0c
commit 98d1ea7730
17 changed files with 573 additions and 126 deletions

View File

@@ -4,6 +4,8 @@ import io.javalin.BadRequestResponse
import io.javalin.Context
import org.briarproject.bramble.api.identity.IdentityManager
import org.briarproject.bramble.api.system.Clock
import org.briarproject.bramble.util.StringUtils
import org.briarproject.briar.api.blog.BlogConstants.MAX_BLOG_POST_BODY_LENGTH
import org.briarproject.briar.api.blog.BlogManager
import org.briarproject.briar.api.blog.BlogPostFactory
import javax.annotation.concurrent.Immutable
@@ -33,7 +35,9 @@ constructor(
fun createPost(ctx: Context): Context {
val text = ctx.formParam("text")
if (text == null || text.isEmpty())
throw BadRequestResponse("Expecting Blog text")
throw BadRequestResponse("Expecting blog post text")
if (StringUtils.toUtf8(text).size > MAX_BLOG_POST_BODY_LENGTH)
throw BadRequestResponse("Too long blog post text")
val author = identityManager.localAuthor
val blog = blogManager.getPersonalBlog(author)

View File

@@ -2,6 +2,8 @@ package org.briarproject.briar.headless.forums
import io.javalin.BadRequestResponse
import io.javalin.Context
import org.briarproject.bramble.util.StringUtils
import org.briarproject.briar.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH
import org.briarproject.briar.api.forum.ForumManager
import javax.annotation.concurrent.Immutable
import javax.inject.Inject
@@ -18,8 +20,10 @@ constructor(private val forumManager: ForumManager) {
fun create(ctx: Context): Context {
val name = ctx.formParam("name")
if (name == null || name.isEmpty())
if (name == null || name.isNullOrEmpty())
throw BadRequestResponse("Expecting Forum Name")
if (StringUtils.toUtf8(name).size > MAX_FORUM_NAME_LENGTH)
throw BadRequestResponse("Forum name is too long")
return ctx.json(forumManager.addForum(name).output())
}

View File

@@ -4,7 +4,7 @@ import org.briarproject.briar.api.forum.Forum
import javax.annotation.concurrent.Immutable
@Immutable
internal class OutputForum(
internal data class OutputForum(
val name: String,
val id: ByteArray
) {

View File

@@ -14,10 +14,10 @@ import org.briarproject.briar.api.sharing.InvitationResponse
internal fun PrivateMessageHeader.output(
contactId: ContactId,
body: String?
) = OutputPrivateMessage(this, contactId, body)
) = OutputPrivateMessageHeader(this, contactId, body)
internal fun PrivateMessage.output(contactId: ContactId, body: String) =
OutputPrivateMessage(this, contactId, body)
OutputPrivateMessageHeader(this, contactId, body)
internal fun PrivateMessageReceivedEvent<*>.output(body: String) =
messageHeader.output(contactId, body)

View File

@@ -20,7 +20,7 @@ import javax.annotation.concurrent.Immutable
import javax.inject.Inject
import javax.inject.Singleton
private const val EVENT_PRIVATE_MESSAGE =
internal const val EVENT_PRIVATE_MESSAGE =
"org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent"
@Immutable
@@ -79,7 +79,11 @@ constructor(
private fun getContact(ctx: Context): Contact {
val contactString = ctx.pathParam("contactId")
val contactInt = Integer.parseInt(contactString)
val contactInt = try {
Integer.parseInt(contactString)
} catch (e: NumberFormatException) {
throw NotFoundResponse()
}
val contactId = ContactId(contactInt)
return try {
contactManager.getContact(contactId)

View File

@@ -1,5 +1,3 @@
@file:Suppress("MemberVisibilityCanBePrivate", "unused")
package org.briarproject.briar.headless.messaging
import org.briarproject.bramble.api.contact.ContactId
@@ -8,47 +6,45 @@ import org.briarproject.briar.api.messaging.PrivateMessageHeader
import javax.annotation.concurrent.Immutable
@Immutable
internal open class OutputPrivateMessage(
val body: String?,
val timestamp: Long,
val read: Boolean,
val seen: Boolean,
val sent: Boolean,
val local: Boolean,
val id: ByteArray,
val groupId: ByteArray,
val contactId: Int
internal abstract class OutputPrivateMessage(
protected open val iHeader: PrivateMessageHeader,
protected open val iContactId: ContactId,
open val body: String?
) {
open val type = "org.briarproject.briar.api.messaging.PrivateMessageHeader"
internal constructor(
header: PrivateMessageHeader,
contactId: ContactId,
body: String?
) : this(
body = body,
timestamp = header.timestamp,
read = header.isRead,
seen = header.isSeen,
sent = header.isSent,
local = header.isLocal,
id = header.id.bytes,
groupId = header.groupId.bytes,
contactId = contactId.int
)
open val type: String get() = throw NotImplementedError()
val contactId: Int get() = iContactId.int
val timestamp: Long get() = iHeader.timestamp
val read: Boolean get() = iHeader.isRead
val seen: Boolean get() = iHeader.isSeen
val sent: Boolean get() = iHeader.isSent
val local: Boolean get() = iHeader.isLocal
val id: ByteArray get() = iHeader.id.bytes
val groupId: ByteArray get() = iHeader.groupId.bytes
}
@Immutable
internal data class OutputPrivateMessageHeader(
override val iHeader: PrivateMessageHeader,
override val iContactId: ContactId,
override val body: String?
) : OutputPrivateMessage(iHeader, iContactId, body) {
override val type = "org.briarproject.briar.api.messaging.PrivateMessageHeader"
/**
* Only meant for own [PrivateMessage]s directly after creation.
*/
internal constructor(m: PrivateMessage, contactId: ContactId, body: String) : this(
body = body,
timestamp = m.message.timestamp,
read = true,
seen = true,
sent = true,
local = true,
id = m.message.id.bytes,
groupId = m.message.groupId.bytes,
contactId = contactId.int
PrivateMessageHeader(
m.message.id,
m.message.groupId,
m.message.timestamp,
true,
true,
true,
true
), contactId, body
)
}

View File

@@ -1,4 +1,4 @@
@file:Suppress("MemberVisibilityCanBePrivate", "unused")
@file:Suppress("unused")
package org.briarproject.briar.headless.messaging
@@ -21,22 +21,28 @@ internal abstract class OutputPrivateRequest(header: PrivateRequest<*>, contactI
}
@Immutable
internal class OutputIntroductionRequest(header: IntroductionRequest, contactId: ContactId) :
OutputPrivateRequest(header, contactId) {
internal data class OutputIntroductionRequest(
override val iHeader: IntroductionRequest,
override val iContactId: ContactId
) : OutputPrivateRequest(iHeader, iContactId) {
override val type = "org.briarproject.briar.api.introduction.IntroductionRequest"
val alreadyContact = header.isContact
val alreadyContact get() = iHeader.isContact
}
@Immutable
internal class OutputInvitationRequest(header: InvitationRequest<*>, contactId: ContactId) :
OutputPrivateRequest(header, contactId) {
internal data class OutputInvitationRequest(
override val iHeader: InvitationRequest<*>,
override val iContactId: ContactId
) : OutputPrivateRequest(iHeader, iContactId) {
override val type = when (header) {
override val type = when (iHeader) {
is ForumInvitationRequest -> "org.briarproject.briar.api.forum.ForumInvitationRequest"
is BlogInvitationRequest -> "org.briarproject.briar.api.blog.BlogInvitationRequest"
is GroupInvitationRequest -> "org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest"
else -> throw AssertionError("Unknown InvitationRequest")
}
val canBeOpened = header.canBeOpened()
val canBeOpened get() = iHeader.canBeOpened()
}

View File

@@ -21,23 +21,27 @@ internal abstract class OutputPrivateResponse(header: PrivateResponse, contactId
}
@Immutable
internal class OutputIntroductionResponse(header: IntroductionResponse, contactId: ContactId) :
OutputPrivateResponse(header, contactId) {
internal data class OutputIntroductionResponse(
override val iHeader: IntroductionResponse,
override val iContactId: ContactId
) : OutputPrivateResponse(iHeader, iContactId) {
override val type = "org.briarproject.briar.api.introduction.IntroductionResponse"
val introducedAuthor = header.introducedAuthor.output()
val introducer = header.isIntroducer
val introducedAuthor get() = iHeader.introducedAuthor.output()
val introducer get() = iHeader.isIntroducer
}
@Immutable
internal class OutputInvitationResponse(header: InvitationResponse, contactId: ContactId) :
OutputPrivateResponse(header, contactId) {
internal data class OutputInvitationResponse(
override val iHeader: InvitationResponse,
override val iContactId: ContactId
) : OutputPrivateResponse(iHeader, iContactId) {
override val type = when (header) {
override val type = when (iHeader) {
is ForumInvitationResponse -> "org.briarproject.briar.api.forum.ForumInvitationResponse"
is BlogInvitationResponse -> "org.briarproject.briar.api.blog.BlogInvitationResponse"
is GroupInvitationResponse -> "org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse"
else -> throw AssertionError("Unknown InvitationResponse")
}
val shareableId: ByteArray = header.shareableId.bytes
val shareableId: ByteArray get() = iHeader.shareableId.bytes
}