mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
[headless] expose ContactManager methods for adding contacts remotely
This commit is contained in:
@@ -4,6 +4,7 @@ import dagger.Component
|
||||
import org.briarproject.bramble.BrambleCoreEagerSingletons
|
||||
import org.briarproject.bramble.BrambleCoreModule
|
||||
import org.briarproject.bramble.account.AccountModule
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent
|
||||
import org.briarproject.bramble.event.DefaultEventExecutorModule
|
||||
import org.briarproject.bramble.test.TestSecureRandomModule
|
||||
import org.briarproject.briar.BriarCoreEagerSingletons
|
||||
@@ -25,5 +26,7 @@ import javax.inject.Singleton
|
||||
internal interface BriarHeadlessTestApp : BrambleCoreEagerSingletons, BriarCoreEagerSingletons {
|
||||
fun getRouter(): Router
|
||||
|
||||
fun getCryptoComponent(): CryptoComponent
|
||||
|
||||
fun getTestDataCreator(): TestDataCreator
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import io.javalin.Javalin
|
||||
import io.javalin.core.util.Header.AUTHORIZATION
|
||||
import khttp.responses.Response
|
||||
import org.briarproject.bramble.BrambleCoreModule
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent
|
||||
import org.briarproject.briar.BriarCoreModule
|
||||
import org.briarproject.briar.api.test.TestDataCreator
|
||||
import org.junit.jupiter.api.AfterAll
|
||||
@@ -22,6 +23,7 @@ abstract class IntegrationTest {
|
||||
private val dataDir = File("tmp")
|
||||
|
||||
protected lateinit var api: Javalin
|
||||
protected lateinit var crypto: CryptoComponent
|
||||
protected lateinit var testDataCreator: TestDataCreator
|
||||
private lateinit var router: Router
|
||||
|
||||
@@ -33,6 +35,7 @@ abstract class IntegrationTest {
|
||||
BrambleCoreModule.initEagerSingletons(app)
|
||||
BriarCoreModule.initEagerSingletons(app)
|
||||
router = app.getRouter()
|
||||
crypto = app.getCryptoComponent()
|
||||
testDataCreator = app.getTestDataCreator()
|
||||
|
||||
api = router.start(token, port, false)
|
||||
@@ -52,10 +55,18 @@ abstract class IntegrationTest {
|
||||
return khttp.get(url, getAuthTokenHeader("wrongToken"))
|
||||
}
|
||||
|
||||
protected fun post(url: String, data: String) : Response {
|
||||
return khttp.post(url, getAuthTokenHeader(token), data = data)
|
||||
}
|
||||
|
||||
protected fun delete(url: String) : Response {
|
||||
return khttp.delete(url, getAuthTokenHeader(token))
|
||||
}
|
||||
|
||||
protected fun delete(url: String, data: String) : Response {
|
||||
return khttp.delete(url, getAuthTokenHeader(token), data = data)
|
||||
}
|
||||
|
||||
protected fun deleteWithWrongToken(url: String) : Response {
|
||||
return khttp.delete(url, getAuthTokenHeader("wrongToken"))
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package org.briarproject.briar.headless.contact
|
||||
|
||||
import org.briarproject.bramble.api.contact.HandshakeLinkConstants.BASE32_LINK_BYTES
|
||||
import org.briarproject.briar.headless.IntegrationTest
|
||||
import org.briarproject.briar.headless.url
|
||||
import org.briarproject.briar.test.BriarTestUtils.getRealHandshakeLink
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class ContactControllerIntegrationTest: IntegrationTest() {
|
||||
@@ -33,6 +36,51 @@ class ContactControllerIntegrationTest: IntegrationTest() {
|
||||
assertEquals(testContactName, author.getString("name"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns own handshake link`() {
|
||||
val response = get("$url/contacts/add/link")
|
||||
assertEquals(200, response.statusCode)
|
||||
val link = response.jsonObject.getString("link")
|
||||
assertTrue(link.startsWith("briar://"))
|
||||
assertEquals(BASE32_LINK_BYTES + 8, link.length)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns list of pending contacts`() {
|
||||
// retrieve empty list of pending contacts
|
||||
var response = get("$url/contacts/add/pending")
|
||||
assertEquals(200, response.statusCode)
|
||||
assertEquals(0, response.jsonArray.length())
|
||||
|
||||
// add one pending contact
|
||||
val alias = "AliasFoo"
|
||||
val json = """{
|
||||
"link": "${getRealHandshakeLink(crypto)}",
|
||||
"alias": "$alias"
|
||||
}"""
|
||||
response = post("$url/contacts/add", json)
|
||||
assertEquals(200, response.statusCode)
|
||||
|
||||
// get added contact as only list item
|
||||
response = get("$url/contacts/add/pending")
|
||||
assertEquals(200, response.statusCode)
|
||||
assertEquals(1, response.jsonArray.length())
|
||||
val jsonObject = response.jsonArray.getJSONObject(0)
|
||||
assertEquals(alias, jsonObject.getString("alias"))
|
||||
assertEquals("waiting_for_connection", jsonObject.getString("state"))
|
||||
|
||||
// remove pending contact again
|
||||
val idString = jsonObject.getString("pendingContactId")
|
||||
val deleteJson = """{"pendingContactId": "$idString"}"""
|
||||
response = delete("$url/contacts/add/pending", deleteJson)
|
||||
assertEquals(200, response.statusCode)
|
||||
|
||||
// list of pending contacts should be empty now
|
||||
response = get("$url/contacts/add/pending")
|
||||
assertEquals(200, response.statusCode)
|
||||
assertEquals(0, response.jsonArray.length())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `deleting contact need authentication token`() {
|
||||
val response = deleteWithWrongToken("$url/contacts/1")
|
||||
|
||||
@@ -7,15 +7,21 @@ import io.mockk.every
|
||||
import io.mockk.just
|
||||
import org.briarproject.bramble.api.contact.Contact
|
||||
import org.briarproject.bramble.api.contact.ContactId
|
||||
import org.briarproject.bramble.api.contact.PendingContactId
|
||||
import org.briarproject.bramble.api.db.NoSuchContactException
|
||||
import org.briarproject.bramble.api.db.NoSuchPendingContactException
|
||||
import org.briarproject.bramble.identity.output
|
||||
import org.briarproject.bramble.test.TestUtils.getPendingContact
|
||||
import org.briarproject.bramble.test.TestUtils.getRandomBytes
|
||||
import org.briarproject.briar.headless.ControllerTest
|
||||
import org.briarproject.briar.headless.json.JsonDict
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class ContactControllerTest : ControllerTest() {
|
||||
|
||||
private val controller = ContactControllerImpl(contactManager)
|
||||
private val controller = ContactControllerImpl(contactManager, objectMapper)
|
||||
private val pendingContact = getPendingContact()
|
||||
|
||||
@Test
|
||||
fun testEmptyContactList() {
|
||||
@@ -31,6 +37,79 @@ internal class ContactControllerTest : ControllerTest() {
|
||||
controller.list(ctx)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testLink() {
|
||||
val link = "briar://link"
|
||||
every { contactManager.handshakeLink } returns link
|
||||
every { ctx.json(JsonDict("link" to link)) } returns ctx
|
||||
controller.link(ctx)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddPendingContact() {
|
||||
val link = "briar://link123"
|
||||
val alias = "Alias123"
|
||||
val body = """{
|
||||
"link": "$link",
|
||||
"alias": "$alias"
|
||||
}"""
|
||||
every { ctx.body() } returns body
|
||||
every { contactManager.addPendingContact(link, alias) } returns pendingContact
|
||||
every { ctx.json(pendingContact.output()) } returns ctx
|
||||
controller.addPendingContact(ctx)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testListPendingContacts() {
|
||||
every { contactManager.pendingContacts } returns listOf(pendingContact)
|
||||
every { ctx.json(listOf(pendingContact.output())) } returns ctx
|
||||
controller.listPendingContacts(ctx)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRemovePendingContact() {
|
||||
val id = pendingContact.id
|
||||
every { ctx.body() } returns """{"pendingContactId": ${toJson(id.bytes)}}"""
|
||||
every { contactManager.removePendingContact(id) } just Runs
|
||||
controller.removePendingContact(ctx)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRemovePendingContactInvalidId() {
|
||||
every { ctx.body() } returns """{"pendingContactId": "foo"}"""
|
||||
assertThrows(NotFoundResponse::class.java) {
|
||||
controller.removePendingContact(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRemovePendingContactTooShortId() {
|
||||
val bytes = getRandomBytes(PendingContactId.LENGTH - 1)
|
||||
every { ctx.body() } returns """{"pendingContactId": ${toJson(bytes)}}"""
|
||||
assertThrows(NotFoundResponse::class.java) {
|
||||
controller.removePendingContact(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRemovePendingContactTooLongId() {
|
||||
val bytes = getRandomBytes(PendingContactId.LENGTH + 1)
|
||||
every { ctx.body() } returns """{"pendingContactId": ${toJson(bytes)}}"""
|
||||
assertThrows(NotFoundResponse::class.java) {
|
||||
controller.removePendingContact(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRemovePendingContactNonexistentId() {
|
||||
val id = pendingContact.id
|
||||
every { ctx.body() } returns """{"pendingContactId": ${toJson(id.bytes)}}"""
|
||||
every { contactManager.removePendingContact(id) } throws NoSuchPendingContactException()
|
||||
assertThrows(NotFoundResponse::class.java) {
|
||||
controller.removePendingContact(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete() {
|
||||
every { ctx.pathParam("contactId") } returns "1"
|
||||
@@ -80,4 +159,17 @@ internal class ContactControllerTest : ControllerTest() {
|
||||
assertJsonEquals(json, author.output())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOutputPendingContact() {
|
||||
val json = """
|
||||
{
|
||||
"pendingContactId": ${toJson(pendingContact.id.bytes)},
|
||||
"alias": "${pendingContact.alias}",
|
||||
"state": "${pendingContact.state.name.toLowerCase()}",
|
||||
"timestamp": ${pendingContact.timestamp}
|
||||
}
|
||||
"""
|
||||
assertJsonEquals(json, pendingContact.output())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user