Completed for a grade of three
This commit is contained in:
+2
@@ -5,6 +5,7 @@ import com.mtusi.messenger.main.infrastructure.dto.Message
|
||||
import com.mtusi.messenger.main.infrastructure.dto.PageResponse
|
||||
import com.mtusi.messenger.main.support.auth
|
||||
import com.mtusi.messenger.main.support.createChat
|
||||
import com.mtusi.messenger.main.support.idempotencyKey
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.call.body
|
||||
@@ -37,6 +38,7 @@ class MessageConcurrencyTest {
|
||||
repeat(perWorker) { i ->
|
||||
val response = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
auth(alice)
|
||||
idempotencyKey()
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "w$w-i$i"))
|
||||
}
|
||||
|
||||
+6
@@ -6,6 +6,7 @@ import com.mtusi.messenger.main.infrastructure.dto.Message
|
||||
import com.mtusi.messenger.main.infrastructure.dto.PageResponse
|
||||
import com.mtusi.messenger.main.support.auth
|
||||
import com.mtusi.messenger.main.support.createChat
|
||||
import com.mtusi.messenger.main.support.idempotencyKey
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.call.body
|
||||
@@ -29,6 +30,7 @@ class MessageControllerTest {
|
||||
|
||||
val response = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
auth(alice)
|
||||
idempotencyKey()
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = ""))
|
||||
}
|
||||
@@ -45,6 +47,7 @@ class MessageControllerTest {
|
||||
|
||||
val response = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
auth(bob)
|
||||
idempotencyKey()
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "hi"))
|
||||
}
|
||||
@@ -75,6 +78,7 @@ class MessageControllerTest {
|
||||
repeat(total) { i ->
|
||||
val r = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
auth(alice)
|
||||
idempotencyKey()
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "m$i"))
|
||||
}
|
||||
@@ -115,6 +119,7 @@ class MessageControllerTest {
|
||||
repeat(20) { i ->
|
||||
client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
auth(alice)
|
||||
idempotencyKey()
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "m$i"))
|
||||
}
|
||||
@@ -164,6 +169,7 @@ class MessageControllerTest {
|
||||
repeat(2) { i ->
|
||||
client.post("/api/v1/chats/${chatA.id}/messages") {
|
||||
auth(alice)
|
||||
idempotencyKey()
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "a$i"))
|
||||
}
|
||||
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
package com.mtusi.messenger.main.infrastructure.controllers.message
|
||||
|
||||
import com.mtusi.messenger.main.extensions.ErrorBody
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest
|
||||
import com.mtusi.messenger.main.infrastructure.dto.Chat
|
||||
import com.mtusi.messenger.main.infrastructure.dto.Message
|
||||
import com.mtusi.messenger.main.infrastructure.dto.PageResponse
|
||||
import com.mtusi.messenger.main.support.TestUser
|
||||
import com.mtusi.messenger.main.support.auth
|
||||
import com.mtusi.messenger.main.support.createChat
|
||||
import com.mtusi.messenger.main.support.idempotencyKey
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.UUID
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
|
||||
class MessageIdempotencyTest {
|
||||
|
||||
private suspend fun HttpClient.send(user: TestUser, chat: Chat, text: String, key: String?): HttpResponse {
|
||||
return post("/api/v1/chats/${chat.id}/messages") {
|
||||
auth(user)
|
||||
if (key != null) {
|
||||
idempotencyKey(key)
|
||||
}
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = text))
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun HttpClient.history(user: TestUser, chat: Chat): List<Message> {
|
||||
return get("/api/v1/chats/${chat.id}/messages?limit=100") { auth(user) }.body<PageResponse<Message>>().items
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `retry with the same key and body returns the original message`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, "retry", listOf(alice.id))
|
||||
val key = UUID.randomUUID().toString()
|
||||
|
||||
val first = client.send(alice, chat, "hello", key)
|
||||
val retry = client.send(alice, chat, "hello", key)
|
||||
|
||||
assertEquals(HttpStatusCode.Created, first.status)
|
||||
assertEquals(HttpStatusCode.Created, retry.status)
|
||||
assertEquals(first.body<Message>(), retry.body<Message>())
|
||||
assertEquals(1, client.history(alice, chat).size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `same key with another body is a conflict`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, "conflict", listOf(alice.id))
|
||||
val key = UUID.randomUUID().toString()
|
||||
|
||||
client.send(alice, chat, "first", key)
|
||||
val response = client.send(alice, chat, "second", key)
|
||||
|
||||
assertEquals(HttpStatusCode.Conflict, response.status)
|
||||
assertEquals("conflict", response.body<ErrorBody>().error.code)
|
||||
assertEquals(listOf("first"), client.history(alice, chat).map { it.text })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `missing or malformed key is rejected before anything is written`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, "keys", listOf(alice.id))
|
||||
|
||||
for (key in listOf(null, "", "has space", "кириллица", "a".repeat(129))) {
|
||||
val response = client.send(alice, chat, "text", key)
|
||||
assertEquals(HttpStatusCode.BadRequest, response.status, "key: $key")
|
||||
assertEquals("invalid_request", response.body<ErrorBody>().error.code)
|
||||
}
|
||||
assertEquals(HttpStatusCode.Created, client.send(alice, chat, "ok", "A-z_0.9:" + "x".repeat(120)).status)
|
||||
assertEquals(1, client.history(alice, chat).size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `key scope is user and chat`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val bob = client.register("bob")
|
||||
val first = client.createChat(alice, "one", listOf(alice.id, bob.id))
|
||||
val second = client.createChat(alice, "two", listOf(alice.id, bob.id))
|
||||
val key = UUID.randomUUID().toString()
|
||||
|
||||
val ids = listOf(
|
||||
client.send(alice, first, "same", key),
|
||||
client.send(bob, first, "same", key),
|
||||
client.send(alice, second, "same", key)
|
||||
).map {
|
||||
assertEquals(HttpStatusCode.Created, it.status)
|
||||
it.body<Message>().id
|
||||
}
|
||||
|
||||
assertEquals(3, ids.toSet().size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `retry by a former outsider is still checked for access`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val eve = client.register("eve")
|
||||
val chat = client.createChat(alice, "access", listOf(alice.id))
|
||||
val key = UUID.randomUUID().toString()
|
||||
|
||||
client.send(alice, chat, "hello", key)
|
||||
|
||||
assertEquals(HttpStatusCode.NotFound, client.send(eve, chat, "hello", key).status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `concurrent retries create exactly one message`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, "concurrent", listOf(alice.id))
|
||||
val key = UUID.randomUUID().toString()
|
||||
|
||||
val responses = withContext(Dispatchers.Default) {
|
||||
coroutineScope {
|
||||
(0 until 20).map { async { client.send(alice, chat, "once", key) } }.awaitAll()
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(List(20) { HttpStatusCode.Created }, responses.map { it.status })
|
||||
assertEquals(1, responses.map { it.body<Message>() }.toSet().size)
|
||||
assertEquals(1, client.history(alice, chat).size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `expired key can be used for a new message`() = runApp(idempotencyTtlSeconds = 1) { client ->
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, "ttl", listOf(alice.id))
|
||||
val key = UUID.randomUUID().toString()
|
||||
|
||||
val first = client.send(alice, chat, "hello", key).body<Message>()
|
||||
delay(1500)
|
||||
val second = client.send(alice, chat, "hello again", key)
|
||||
|
||||
assertEquals(HttpStatusCode.Created, second.status)
|
||||
assertNotEquals(first.id, second.body<Message>().id)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.mtusi.messenger.main.plugins
|
||||
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class CorsTest {
|
||||
|
||||
@Test
|
||||
fun `page of the API itself may call it`() = runApp { client ->
|
||||
val response = client.get("/health/ready") { header("Origin", "http://localhost") }
|
||||
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unknown origin is rejected`() = runApp { client ->
|
||||
val response = client.get("/health/ready") { header("Origin", "http://evil.example") }
|
||||
|
||||
assertEquals(HttpStatusCode.Forbidden, response.status)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import com.mtusi.messenger.main.support.TestRedis
|
||||
import com.mtusi.messenger.main.support.TestUser
|
||||
import com.mtusi.messenger.main.support.auth
|
||||
import com.mtusi.messenger.main.support.createChat
|
||||
import com.mtusi.messenger.main.support.idempotencyKey
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import com.mtusi.messenger.main.utils.computeSha256
|
||||
@@ -176,6 +177,7 @@ class DependencyFailureTest {
|
||||
HttpStatusCode.Unauthorized,
|
||||
client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
auth(alice)
|
||||
idempotencyKey()
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "after logout"))
|
||||
}.status
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.mtusi.messenger.main.plugins
|
||||
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class InstanceIdTest {
|
||||
|
||||
@Test
|
||||
fun `every response names the replica`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
|
||||
val responses = listOf(
|
||||
client.get("/health/live"),
|
||||
client.get("/health/ready"),
|
||||
client.get("/api/v1/users/me") { header("Authorization", "Bearer ${alice.token}") },
|
||||
client.get("/api/v1/users/me"),
|
||||
client.get("/api/v1/chats/not-a-uuid") { header("Authorization", "Bearer ${alice.token}") },
|
||||
client.post("/api/v1/chats") { header("Authorization", "Bearer ${alice.token}") },
|
||||
client.get("/no/such/route")
|
||||
)
|
||||
|
||||
assertEquals(List(responses.size) { "test-instance" }, responses.map { it.headers["X-Instance-Id"] })
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.request.HttpRequestBuilder
|
||||
import io.ktor.client.request.basicAuth
|
||||
import io.ktor.client.request.bearerAuth
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.http.ContentType
|
||||
@@ -56,6 +57,7 @@ private fun testConfig(
|
||||
database: PostgreSQLContainer<*>,
|
||||
databaseConnectionTimeoutMs: Long,
|
||||
redis: RedisEndpoint,
|
||||
idempotencyTtlSeconds: Long,
|
||||
) = HoconApplicationConfig(
|
||||
ConfigFactory.parseMap(
|
||||
mapOf(
|
||||
@@ -75,6 +77,9 @@ private fun testConfig(
|
||||
"authentication.realm" to "messenger",
|
||||
"authentication.sessionTtlSeconds" to sessionTtlSeconds,
|
||||
"authentication.sessionCacheTtlSeconds" to 60,
|
||||
"messages.idempotencyTtlSeconds" to idempotencyTtlSeconds,
|
||||
"instance.id" to "test-instance",
|
||||
"cors.allowSameOrigin" to true,
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -87,10 +92,11 @@ fun runApp(
|
||||
database: PostgreSQLContainer<*> = TestDatabase.container,
|
||||
databaseConnectionTimeoutMs: Long = 10000,
|
||||
redis: RedisEndpoint = TestRedis.endpoint(),
|
||||
idempotencyTtlSeconds: Long = 86400,
|
||||
block: suspend ApplicationTestBuilder.(HttpClient) -> Unit
|
||||
) = testApplication {
|
||||
environment {
|
||||
config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis)
|
||||
config = testConfig(sessionTtlSeconds, database, databaseConnectionTimeoutMs, redis, idempotencyTtlSeconds)
|
||||
}
|
||||
application {
|
||||
module()
|
||||
@@ -137,6 +143,11 @@ suspend fun HttpClient.register(prefix: String): TestUser {
|
||||
|
||||
fun HttpRequestBuilder.auth(user: TestUser) = bearerAuth(user.token)
|
||||
|
||||
/**
|
||||
* Every message POST needs its own key since lab3; pass the same key to model a retry.
|
||||
*/
|
||||
fun HttpRequestBuilder.idempotencyKey(key: String = UUID.randomUUID().toString()) = header("Idempotency-Key", key)
|
||||
|
||||
suspend fun HttpClient.createChat(
|
||||
creator: TestUser,
|
||||
title: String,
|
||||
|
||||
Reference in New Issue
Block a user