| @@ -8,6 +8,7 @@ import kotlinx.serialization.json.jsonObject |
| | import kotlinx.serialization.json.jsonPrimitive |
| | import kotlinx.serialization.json.put |
| | import org.junit.Assert.assertEquals |
| | import org.junit.Assert.assertFalse |
| | import org.junit.Assert.assertNull |
| | import org.junit.Assert.assertTrue |
| | import org.junit.Test |
| @@ -22,6 +23,7 @@ import social.grain.data.api.GalleryService |
| | import social.grain.data.api.PinnedFeed |
| | import social.grain.data.api.ReportSubject |
| | import social.grain.data.api.StoryService |
| | import social.grain.data.api.XrpcException |
| | import social.grain.data.api.applyWrites |
| | import social.grain.data.api.createReport |
| | import social.grain.data.api.createRecord |
| @@ -53,6 +55,7 @@ import social.grain.data.api.markNotificationsSeen |
| | import social.grain.data.api.muteActor |
| | import social.grain.data.api.putPreference |
| | import social.grain.data.api.putRecord |
| | import social.grain.data.api.isMethodMissing |
| | import social.grain.data.api.registerPushToken |
| | import social.grain.data.api.searchActorsTypeahead |
| | import social.grain.data.api.searchGalleries |
| @@ -533,18 +536,111 @@ class EndpointsTest { |
| | |
| | // MARK: - Push |
| | |
| | private val registerPush = "social.grain.notification.registerPush" |
| | private val unregisterPush = "social.grain.notification.unregisterPush" |
| | private val registerToken = "dev.hatk.push.registerToken" |
| | private val unregisterToken = "dev.hatk.push.unregisterToken" |
| | |
| | /** The answers a server gives for a method it does not serve. */ |
| | private val methodMissing = listOf( |
| | 404 to "Not Found", |
| | 501 to """{"error":"MethodNotImplemented"}""", |
| | 400 to """{"error":"MethodNotImplemented","message":"Method Not Implemented"}""", |
| | 400 to """{"error":"MethodNotFound"}""", |
| | ) |
| | |
| | /** Answers about the request rather than the method. */ |
| | private val otherFailures = listOf( |
| | 401 to """{"error":"AuthenticationRequired"}""", |
| | 400 to """{"error":"InvalidRequest","message":"Input/token must be a string"}""", |
| | 500 to "nope", |
| | ) |
| | |
| | private fun assertPushRegistration(sent: JsonObject) { |
| | assertEquals(setOf("serviceDid", "token", "platform", "appId"), sent.keys) |
| | assertEquals("did:web:grain.social", sent["serviceDid"]!!.jsonPrimitive.content) |
| | assertEquals("fcm-token", sent["token"]!!.jsonPrimitive.content) |
| | assertEquals("android", sent["platform"]!!.jsonPrimitive.content) |
| | assertEquals("social.grain", sent["appId"]!!.jsonPrimitive.content) |
| | } |
| | |
| | @Test |
| | fun pushTokensRegisterAndUnregisterForThisPlatform() = runTest { |
| | fun pushTokensRegisterAndUnregisterWithRegisterPush() = runTest { |
| | client.registerPushToken("fcm-token", auth) |
| | val registered = body("dev.hatk.push.registerToken") |
| | assertEquals("fcm-token", registered["token"]!!.jsonPrimitive.content) |
| | assertEquals("fcm", registered["platform"]!!.jsonPrimitive.content) |
| | val registered = server.lastCall(registerPush) |
| | assertEquals("POST", registered.method) |
| | assertEquals("DPoP test-access-token", registered.authorization) |
| | assertPushRegistration(registered.json) |
| | |
| | client.unregisterPushToken("fcm-token", auth) |
| | assertEquals( |
| | "fcm-token", |
| | body("dev.hatk.push.unregisterToken")["token"]!!.jsonPrimitive.content, |
| | ) |
| | assertPushRegistration(body(unregisterPush)) |
| | |
| | assertEquals(0, server.countOf(registerToken)) |
| | assertEquals(0, server.countOf(unregisterToken)) |
| | } |
| | |
| | @Test |
| | fun pushRegistrationFallsBackWhenTheServerLacksRegisterPush() = runTest { |
| | for ((code, reply) in methodMissing) { |
| | val server = StubServer().on(registerPush, reply, code = code) |
| | server.xrpc().registerPushToken("fcm-token", auth) |
| | |
| | val label = "$code $reply" |
| | assertEquals(label, listOf(registerPush, registerToken), server.calls.map { it.nsid }) |
| | val sent = server.lastCall(registerToken).json |
| | assertEquals(label, setOf("token", "platform"), sent.keys) |
| | assertEquals(label, "fcm-token", sent["token"]!!.jsonPrimitive.content) |
| | assertEquals(label, "fcm", sent["platform"]!!.jsonPrimitive.content) |
| | server.shutdown() |
| | } |
| | } |
| | |
| | @Test |
| | fun pushUnregistrationFallsBackWhenTheServerLacksUnregisterPush() = runTest { |
| | for ((code, reply) in methodMissing) { |
| | val server = StubServer().on(unregisterPush, reply, code = code) |
| | server.xrpc().unregisterPushToken("fcm-token", auth) |
| | |
| | val label = "$code $reply" |
| | assertEquals(label, listOf(unregisterPush, unregisterToken), server.calls.map { it.nsid }) |
| | val sent = server.lastCall(unregisterToken).json |
| | assertEquals(label, setOf("token"), sent.keys) |
| | assertEquals(label, "fcm-token", sent["token"]!!.jsonPrimitive.content) |
| | server.shutdown() |
| | } |
| | } |
| | |
| | @Test |
| | fun otherPushFailuresAreTheAnswerAndNotFallenBackFrom() = runTest { |
| | for ((code, reply) in otherFailures) { |
| | val server = StubServer() |
| | .on(registerPush, reply, code = code) |
| | .on(unregisterPush, reply, code = code) |
| | val client = server.xrpc() |
| | |
| | val label = "$code $reply" |
| | val registering = runCatching { client.registerPushToken("fcm-token", auth) } |
| | assertTrue(label, registering.exceptionOrNull() is XrpcException) |
| | val unregistering = runCatching { client.unregisterPushToken("fcm-token", auth) } |
| | assertTrue(label, unregistering.exceptionOrNull() is XrpcException) |
| | |
| | assertEquals(label, listOf(registerPush, unregisterPush), server.calls.map { it.nsid }) |
| | server.shutdown() |
| | } |
| | } |
| | |
| | @Test |
| | fun onlyAMissingMethodCountsAsMissing() { |
| | assertTrue(isMethodMissing(XrpcException.Http(404, null))) |
| | assertTrue(isMethodMissing(XrpcException.Http(501, ""))) |
| | assertTrue(isMethodMissing(XrpcException.Http(400, """{"error":"MethodNotImplemented"}"""))) |
| | assertTrue(isMethodMissing(XrpcException.Http(400, """{"error":"MethodNotFound"}"""))) |
| | assertFalse(isMethodMissing(XrpcException.Http(400, """{"error":"InvalidRequest"}"""))) |
| | assertFalse(isMethodMissing(XrpcException.Http(400, "garbage"))) |
| | assertFalse(isMethodMissing(XrpcException.Http(400, null))) |
| | assertFalse(isMethodMissing(XrpcException.Http(500, "{}"))) |
| | assertFalse(isMethodMissing(XrpcException.Unauthorized)) |
| | assertFalse(isMethodMissing(java.io.IOException("offline"))) |
| | } |
| | |
| | // MARK: - Moderation lists |
| |