mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-11 18:29:05 +01:00
Add method to change contact alias to REST API
Needed for https://code.briarproject.org/briar/briar-gtk/-/issues/14 and https://code.briarproject.org/briar/python-briar-wrapper/-/issues/6. Fixes #1781
This commit is contained in:
@@ -183,6 +183,18 @@ Note that it's also possible to add contacts nearby via Bluetooth/Wifi or
|
||||
introductions. In these cases contacts omit the `pendingContact` state and
|
||||
directly become `contact`s.
|
||||
|
||||
### Changing alias of a contact
|
||||
|
||||
`PUT /v1/contacts/{contactId}/alias`
|
||||
|
||||
The alias should be posted as a JSON object:
|
||||
|
||||
```json
|
||||
{
|
||||
"alias": "A nickname for the new contact"
|
||||
}
|
||||
```
|
||||
|
||||
### Removing a contact
|
||||
|
||||
`DELETE /v1/contacts/{contactId}`
|
||||
|
||||
@@ -81,6 +81,9 @@ constructor(
|
||||
path("/:contactId") {
|
||||
delete { ctx -> contactController.delete(ctx) }
|
||||
}
|
||||
path("/:contactId/alias") {
|
||||
put { ctx -> contactController.setContactAlias(ctx) }
|
||||
}
|
||||
}
|
||||
path("/messages/:contactId") {
|
||||
get { ctx -> messagingController.list(ctx) }
|
||||
|
||||
@@ -9,6 +9,7 @@ interface ContactController {
|
||||
fun addPendingContact(ctx: Context): Context
|
||||
fun listPendingContacts(ctx: Context): Context
|
||||
fun removePendingContact(ctx: Context): Context
|
||||
fun setContactAlias(ctx: Context): Context
|
||||
fun delete(ctx: Context): Context
|
||||
|
||||
}
|
||||
|
||||
@@ -92,9 +92,7 @@ constructor(
|
||||
val link = ctx.getFromJson(objectMapper, "link")
|
||||
val alias = ctx.getFromJson(objectMapper, "alias")
|
||||
if (!LINK_REGEX.matcher(link).find()) throw BadRequestResponse("Invalid Link")
|
||||
val aliasUtf8 = toUtf8(alias)
|
||||
if (aliasUtf8.isEmpty() || aliasUtf8.size > MAX_AUTHOR_NAME_LENGTH)
|
||||
throw BadRequestResponse("Invalid Alias")
|
||||
checkAliasLength(alias)
|
||||
val pendingContact = contactManager.addPendingContact(link, alias)
|
||||
return ctx.json(pendingContact.output())
|
||||
}
|
||||
@@ -125,6 +123,18 @@ constructor(
|
||||
return ctx
|
||||
}
|
||||
|
||||
override fun setContactAlias(ctx: Context): Context {
|
||||
val contactId = ctx.getContactIdFromPathParam()
|
||||
val alias = ctx.getFromJson(objectMapper, "alias")
|
||||
checkAliasLength(alias)
|
||||
try {
|
||||
contactManager.setContactAlias(contactId, alias)
|
||||
} catch (e: NoSuchContactException) {
|
||||
throw NotFoundResponse()
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
override fun delete(ctx: Context): Context {
|
||||
val contactId = ctx.getContactIdFromPathParam()
|
||||
try {
|
||||
@@ -135,4 +145,10 @@ constructor(
|
||||
return ctx
|
||||
}
|
||||
|
||||
private fun checkAliasLength(alias: String) {
|
||||
val aliasUtf8 = toUtf8(alias)
|
||||
if (aliasUtf8.isEmpty() || aliasUtf8.size > MAX_AUTHOR_NAME_LENGTH)
|
||||
throw BadRequestResponse("Invalid Alias")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import io.javalin.plugin.json.JavalinJson.toJson
|
||||
import io.mockk.Runs
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockkStatic
|
||||
import io.mockk.runs
|
||||
import org.briarproject.bramble.api.Pair
|
||||
import org.briarproject.bramble.api.contact.Contact
|
||||
@@ -27,6 +28,7 @@ import org.briarproject.bramble.test.TestUtils.getPendingContact
|
||||
import org.briarproject.bramble.test.TestUtils.getRandomBytes
|
||||
import org.briarproject.bramble.util.StringUtils.getRandomString
|
||||
import org.briarproject.briar.headless.ControllerTest
|
||||
import org.briarproject.briar.headless.getFromJson
|
||||
import org.briarproject.briar.headless.json.JsonDict
|
||||
import org.junit.jupiter.api.Assertions.assertNotNull
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
@@ -194,6 +196,66 @@ internal class ContactControllerTest : ControllerTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetContactAlias() {
|
||||
mockkStatic("org.briarproject.briar.headless.RouterKt")
|
||||
every { ctx.pathParam("contactId") } returns "1"
|
||||
every { ctx.getFromJson(objectMapper, "alias") } returns "foo"
|
||||
every { contactManager.setContactAlias(ContactId(1), "foo") } just Runs
|
||||
controller.setContactAlias(ctx)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetContactAliasInvalidId() {
|
||||
mockkStatic("org.briarproject.briar.headless.RouterKt")
|
||||
every { ctx.pathParam("contactId") } returns "foo"
|
||||
every { ctx.getFromJson(objectMapper, "alias") } returns "bar"
|
||||
assertThrows(NotFoundResponse::class.java) {
|
||||
controller.setContactAlias(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetContactAliasNonexistentId() {
|
||||
mockkStatic("org.briarproject.briar.headless.RouterKt")
|
||||
every { ctx.pathParam("contactId") } returns "1"
|
||||
every { ctx.getFromJson(objectMapper, "alias") } returns "foo"
|
||||
every { contactManager.setContactAlias(ContactId(1), "foo") } throws NotFoundResponse()
|
||||
assertThrows(NotFoundResponse::class.java) {
|
||||
controller.setContactAlias(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetContactAliasInvalid() {
|
||||
mockkStatic("org.briarproject.briar.headless.RouterKt")
|
||||
every { ctx.pathParam("contactId") } returns "1"
|
||||
every { ctx.getFromJson(objectMapper, "alias") } returns getRandomString(MAX_AUTHOR_NAME_LENGTH + 1)
|
||||
assertThrows(BadRequestResponse::class.java) {
|
||||
controller.setContactAlias(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetContactAliasEmpty() {
|
||||
mockkStatic("org.briarproject.briar.headless.RouterKt")
|
||||
every { ctx.pathParam("contactId") } returns "1"
|
||||
every { ctx.getFromJson(objectMapper, "alias") } returns ""
|
||||
assertThrows(BadRequestResponse::class.java) {
|
||||
controller.setContactAlias(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSetContactAliasMissing() {
|
||||
mockkStatic("org.briarproject.briar.headless.RouterKt")
|
||||
every { ctx.pathParam("contactId") } returns "1"
|
||||
every { ctx.body() } returns ""
|
||||
assertThrows(BadRequestResponse::class.java) {
|
||||
controller.setContactAlias(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDelete() {
|
||||
every { ctx.pathParam("contactId") } returns "1"
|
||||
|
||||
Reference in New Issue
Block a user