forked from mtusi2026/mtusi2026lab2
Completed for a grade of three
This commit is contained in:
+151
@@ -0,0 +1,151 @@
|
||||
package com.mtusi.messenger.main.infrastructure.controllers.authentication
|
||||
|
||||
import com.mtusi.messenger.main.extensions.ErrorBody
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.authentication.responses.SessionResponse
|
||||
import com.mtusi.messenger.main.support.TEST_PASSWORD
|
||||
import com.mtusi.messenger.main.support.auth
|
||||
import com.mtusi.messenger.main.support.createUser
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.basicAuth
|
||||
import io.ktor.client.request.bearerAuth
|
||||
import io.ktor.client.request.delete
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
private const val BASIC_CHALLENGE = "Basic realm=\"messenger\", charset=\"UTF-8\""
|
||||
|
||||
class AuthenticationControllerTest {
|
||||
|
||||
@Test
|
||||
fun `valid Basic credentials open a session`() = runApp { client ->
|
||||
val user = client.createUser("alice")
|
||||
|
||||
val response = client.post("/api/v1/auth/sessions") {
|
||||
basicAuth(user.username, TEST_PASSWORD)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Created, response.status)
|
||||
|
||||
val session = response.body<SessionResponse>()
|
||||
assertEquals("Bearer", session.tokenType)
|
||||
assertEquals(user, session.user)
|
||||
assertTrue(session.token.length >= 43, "token must carry at least 256 bits")
|
||||
assertTrue(session.expiresAt > Clock.System.now() + 59.minutes)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `each login issues a different token`() = runApp { client ->
|
||||
val user = client.createUser("alice")
|
||||
|
||||
val first = client.post("/api/v1/auth/sessions") { basicAuth(user.username, TEST_PASSWORD) }
|
||||
val second = client.post("/api/v1/auth/sessions") { basicAuth(user.username, TEST_PASSWORD) }
|
||||
|
||||
assertNotEquals(first.body<SessionResponse>().token, second.body<SessionResponse>().token)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `password containing a colon and non-ASCII characters works`() = runApp { client ->
|
||||
val password = "пароль:с-двоеточием"
|
||||
val user = client.createUser("colon", password = password)
|
||||
|
||||
val response = client.post("/api/v1/auth/sessions") {
|
||||
basicAuth(user.username, password)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Created, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `wrong password and unknown user get the same 401`() = runApp { client ->
|
||||
val user = client.createUser("alice")
|
||||
|
||||
val wrongPassword = client.post("/api/v1/auth/sessions") {
|
||||
basicAuth(user.username, "wrong-password-123")
|
||||
}
|
||||
val unknownUser = client.post("/api/v1/auth/sessions") {
|
||||
basicAuth("nobody_${user.username}".take(32), TEST_PASSWORD)
|
||||
}
|
||||
|
||||
for (response in listOf(wrongPassword, unknownUser)) {
|
||||
assertEquals(HttpStatusCode.Unauthorized, response.status)
|
||||
assertEquals(BASIC_CHALLENGE, response.headers[HttpHeaders.WWWAuthenticate])
|
||||
assertEquals("unauthorized", response.body<ErrorBody>().error.code)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `missing or malformed Basic header returns 401`() = runApp { client ->
|
||||
val missing = client.post("/api/v1/auth/sessions")
|
||||
val malformed = client.post("/api/v1/auth/sessions") {
|
||||
header(HttpHeaders.Authorization, "Basic !!!not-base64")
|
||||
}
|
||||
|
||||
for (response in listOf(missing, malformed)) {
|
||||
assertEquals(HttpStatusCode.Unauthorized, response.status)
|
||||
assertEquals(BASIC_CHALLENGE, response.headers[HttpHeaders.WWWAuthenticate])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Bearer token cannot open a session`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
|
||||
val response = client.post("/api/v1/auth/sessions") {
|
||||
auth(alice)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Unauthorized, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `logout revokes only the current token`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val secondSession = client.post("/api/v1/auth/sessions") {
|
||||
basicAuth(alice.user.username, alice.password)
|
||||
}.body<SessionResponse>()
|
||||
|
||||
val logout = client.delete("/api/v1/auth/sessions/current") { auth(alice) }
|
||||
assertEquals(HttpStatusCode.NoContent, logout.status)
|
||||
|
||||
assertEquals(HttpStatusCode.Unauthorized, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
assertEquals(HttpStatusCode.Unauthorized, client.delete("/api/v1/auth/sessions/current") { auth(alice) }.status)
|
||||
|
||||
val other = client.get("/api/v1/users/me") { bearerAuth(secondSession.token) }
|
||||
assertEquals(HttpStatusCode.OK, other.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unknown token returns 401`() = runApp { client ->
|
||||
val response = client.get("/api/v1/users/me") {
|
||||
bearerAuth("this-token-was-never-issued-by-the-server-at-all")
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Unauthorized, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `session expires after SESSION_TTL_SECONDS`() = runApp(sessionTtlSeconds = 2) { client ->
|
||||
val alice = client.register("alice")
|
||||
|
||||
assertEquals(HttpStatusCode.OK, client.get("/api/v1/users/me") { auth(alice) }.status)
|
||||
|
||||
delay(2.5.seconds)
|
||||
|
||||
val expired = client.get("/api/v1/users/me") { auth(alice) }
|
||||
assertEquals(HttpStatusCode.Unauthorized, expired.status)
|
||||
assertEquals("Bearer realm=\"messenger\"", expired.headers[HttpHeaders.WWWAuthenticate])
|
||||
}
|
||||
}
|
||||
+121
-12
@@ -1,27 +1,64 @@
|
||||
package com.mtusi.messenger.main.infrastructure.controllers.chat
|
||||
|
||||
import com.mtusi.messenger.main.extensions.ErrorBody
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest
|
||||
import com.mtusi.messenger.main.infrastructure.dto.Chat
|
||||
import com.mtusi.messenger.main.infrastructure.dto.PageResponse
|
||||
import com.mtusi.messenger.main.infrastructure.models.chat.ChatModel
|
||||
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.createUser
|
||||
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.header
|
||||
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 org.jetbrains.exposed.v1.core.eq
|
||||
import org.jetbrains.exposed.v1.jdbc.selectAll
|
||||
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
|
||||
import java.util.UUID
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class ChatControllerTest {
|
||||
|
||||
private suspend fun HttpClient.postChat(actor: TestUser, title: String, memberIds: List<String>): HttpResponse =
|
||||
post("/api/v1/chats") {
|
||||
auth(actor)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateChatRequest(title = title, memberIds = memberIds))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `member can read own chat and members keep their order`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val bob = client.register("bob")
|
||||
val created = client.createChat(alice, title = "notes", memberIds = listOf(bob.id, alice.id))
|
||||
|
||||
val response = client.get("/api/v1/chats/${created.id}") {
|
||||
auth(bob)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
assertEquals(created, response.body<Chat>())
|
||||
assertEquals(listOf(bob.id, alice.id), created.memberIds)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `foreign chat returns 404 for non-member`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val bob = client.createUser("bob")
|
||||
val alice = client.register("alice")
|
||||
val eve = client.register("eve")
|
||||
val chat = client.createChat(alice, title = "secret", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.get("/api/v1/chats/${chat.id}") {
|
||||
header("X-User-Id", bob.id)
|
||||
auth(eve)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.NotFound, response.status)
|
||||
@@ -29,15 +66,87 @@ class ChatControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `member can read own chat`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val created = client.createChat(alice, title = "notes", memberIds = listOf(alice.id))
|
||||
fun `invalid member lists are rejected with 400`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val bob = client.register("bob")
|
||||
|
||||
val response = client.get("/api/v1/chats/${created.id}") {
|
||||
header("X-User-Id", alice.id)
|
||||
val cases = listOf(
|
||||
listOf(bob.id.toString()), // creator is not a member
|
||||
listOf(alice.id.toString(), alice.id.toString()), // duplicates
|
||||
listOf(alice.id.toString(), "not-a-uuid"), // malformed id
|
||||
listOf(alice.id.toString(), UUID.randomUUID().toString()), // unknown user
|
||||
emptyList(),
|
||||
)
|
||||
|
||||
for (memberIds in cases) {
|
||||
val response = client.postChat(alice, "bad", memberIds)
|
||||
assertEquals(HttpStatusCode.BadRequest, response.status, "member_ids=$memberIds")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `failed chat creation leaves no partial rows`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val title = "atomic-${UUID.randomUUID()}"
|
||||
|
||||
val response = client.postChat(alice, title, listOf(alice.id.toString(), UUID.randomUUID().toString()))
|
||||
assertEquals(HttpStatusCode.BadRequest, response.status)
|
||||
|
||||
val chats = transaction {
|
||||
ChatModel.selectAll().where(ChatModel.title eq title).count()
|
||||
}
|
||||
assertEquals(0, chats)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `chat list contains only own chats and is paginated`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val bob = client.register("bob")
|
||||
val eve = client.register("eve")
|
||||
|
||||
val created = (0 until 5).map {
|
||||
client.createChat(alice, title = "c$it", memberIds = listOf(alice.id, bob.id))
|
||||
}
|
||||
client.createChat(eve, title = "eve only", memberIds = listOf(eve.id))
|
||||
|
||||
val collected = mutableListOf<Chat>()
|
||||
var cursor: String? = null
|
||||
|
||||
do {
|
||||
val page = client.get("/api/v1/chats") {
|
||||
auth(bob)
|
||||
url {
|
||||
parameters.append("limit", "2")
|
||||
cursor?.let { parameters.append("cursor", it) }
|
||||
}
|
||||
}.body<PageResponse<Chat>>()
|
||||
|
||||
collected += page.items
|
||||
cursor = page.nextCursor
|
||||
} while (cursor != null)
|
||||
|
||||
assertEquals(created, collected)
|
||||
|
||||
val evePage = client.get("/api/v1/chats") { auth(eve) }.body<PageResponse<Chat>>()
|
||||
assertEquals(listOf("eve only"), evePage.items.map { it.title })
|
||||
assertNull(evePage.nextCursor)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `cursor issued to another user is rejected`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val bob = client.register("bob")
|
||||
repeat(2) { client.createChat(alice, title = "c$it", memberIds = listOf(alice.id)) }
|
||||
|
||||
val cursor = client.get("/api/v1/chats?limit=1") { auth(alice) }
|
||||
.body<PageResponse<Chat>>()
|
||||
.nextCursor!!
|
||||
|
||||
val response = client.get("/api/v1/chats") {
|
||||
auth(bob)
|
||||
url { parameters.append("cursor", cursor) }
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
assertEquals(created.id, response.body<Chat>().id)
|
||||
assertEquals(HttpStatusCode.BadRequest, response.status)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@ class HealthControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ready returns 200`() = runApp { client ->
|
||||
fun `ready returns 200 when database is reachable`() = runApp { client ->
|
||||
val response = client.get("/health/ready")
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
}
|
||||
|
||||
+8
-7
@@ -1,13 +1,14 @@
|
||||
package com.mtusi.messenger.main.infrastructure.controllers.message
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.message.responses.MessagePageResponse
|
||||
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.createUser
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.http.ContentType
|
||||
@@ -23,7 +24,7 @@ class MessageConcurrencyTest {
|
||||
|
||||
@Test
|
||||
fun `parallel senders produce exactly N unique messages`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, title = "load", memberIds = listOf(alice.id))
|
||||
|
||||
val workers = 10
|
||||
@@ -35,7 +36,7 @@ class MessageConcurrencyTest {
|
||||
async {
|
||||
repeat(perWorker) { i ->
|
||||
val response = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "w$w-i$i"))
|
||||
}
|
||||
@@ -50,7 +51,7 @@ class MessageConcurrencyTest {
|
||||
|
||||
do {
|
||||
val response = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
url {
|
||||
parameters.append("limit", "100")
|
||||
if (cursor != null) parameters.append("cursor", cursor!!)
|
||||
@@ -58,7 +59,7 @@ class MessageConcurrencyTest {
|
||||
}
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
|
||||
val page = response.body<MessagePageResponse>()
|
||||
val page = response.body<PageResponse<Message>>()
|
||||
collected += page.items.map { it.text }
|
||||
cursor = page.nextCursor
|
||||
} while (cursor != null)
|
||||
|
||||
+30
-29
@@ -2,13 +2,14 @@ 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.controllers.message.responses.MessagePageResponse
|
||||
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.createUser
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.http.ContentType
|
||||
@@ -23,11 +24,11 @@ class MessageControllerTest {
|
||||
|
||||
@Test
|
||||
fun `empty text returns 400 invalid_request`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = ""))
|
||||
}
|
||||
@@ -38,12 +39,12 @@ class MessageControllerTest {
|
||||
|
||||
@Test
|
||||
fun `foreign chat returns 404 when sending`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val bob = client.createUser("bob")
|
||||
val alice = client.register("alice")
|
||||
val bob = client.register("bob")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", bob.id)
|
||||
auth(bob)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "hi"))
|
||||
}
|
||||
@@ -54,12 +55,12 @@ class MessageControllerTest {
|
||||
|
||||
@Test
|
||||
fun `foreign chat returns 404 when listing`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val bob = client.createUser("bob")
|
||||
val alice = client.register("alice")
|
||||
val bob = client.register("bob")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", bob.id)
|
||||
auth(bob)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.NotFound, response.status)
|
||||
@@ -67,13 +68,13 @@ class MessageControllerTest {
|
||||
|
||||
@Test
|
||||
fun `pagination walks all messages exactly once`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val total = 25
|
||||
repeat(total) { i ->
|
||||
val r = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "m$i"))
|
||||
}
|
||||
@@ -86,7 +87,7 @@ class MessageControllerTest {
|
||||
|
||||
do {
|
||||
val response = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
url {
|
||||
parameters.append("limit", "10")
|
||||
if (cursor != null) parameters.append("cursor", cursor!!)
|
||||
@@ -94,7 +95,7 @@ class MessageControllerTest {
|
||||
}
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
|
||||
val page = response.body<MessagePageResponse>()
|
||||
val page = response.body<PageResponse<Message>>()
|
||||
collected += page.items.map { it.text }
|
||||
cursor = page.nextCursor
|
||||
pages++
|
||||
@@ -108,32 +109,32 @@ class MessageControllerTest {
|
||||
|
||||
@Test
|
||||
fun `history that is an exact multiple of limit ends with null next_cursor`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
repeat(20) { i ->
|
||||
client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "m$i"))
|
||||
}
|
||||
}
|
||||
|
||||
val first = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
url { parameters.append("limit", "10") }
|
||||
}.body<MessagePageResponse>()
|
||||
}.body<PageResponse<Message>>()
|
||||
|
||||
val cursor = first.nextCursor
|
||||
assertNotNull(cursor)
|
||||
|
||||
val second = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
url {
|
||||
parameters.append("limit", "10")
|
||||
parameters.append("cursor", cursor)
|
||||
}
|
||||
}.body<MessagePageResponse>()
|
||||
}.body<PageResponse<Message>>()
|
||||
|
||||
assertEquals((10 until 20).map { "m$it" }, second.items.map { it.text })
|
||||
assertNull(second.nextCursor)
|
||||
@@ -141,43 +142,43 @@ class MessageControllerTest {
|
||||
|
||||
@Test
|
||||
fun `last page has null next_cursor`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val alice = client.register("alice")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
val page = response.body<MessagePageResponse>()
|
||||
val page = response.body<PageResponse<Message>>()
|
||||
assertEquals(0, page.items.size)
|
||||
assertNull(page.nextCursor)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `foreign cursor returns 400`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val alice = client.register("alice")
|
||||
val chatA = client.createChat(alice, title = "a", memberIds = listOf(alice.id))
|
||||
val chatB = client.createChat(alice, title = "b", memberIds = listOf(alice.id))
|
||||
|
||||
repeat(2) { i ->
|
||||
client.post("/api/v1/chats/${chatA.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "a$i"))
|
||||
}
|
||||
}
|
||||
|
||||
val firstA = client.get("/api/v1/chats/${chatA.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
url { parameters.append("limit", "1") }
|
||||
}.body<MessagePageResponse>()
|
||||
}.body<PageResponse<Message>>()
|
||||
|
||||
val cursor = firstA.nextCursor
|
||||
assertNotNull(cursor)
|
||||
|
||||
val response = client.get("/api/v1/chats/${chatB.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
auth(alice)
|
||||
url {
|
||||
parameters.append("limit", "1")
|
||||
parameters.append("cursor", cursor)
|
||||
|
||||
+120
-12
@@ -2,30 +2,48 @@ package com.mtusi.messenger.main.infrastructure.controllers.user
|
||||
|
||||
import com.mtusi.messenger.main.extensions.ErrorBody
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest
|
||||
import com.mtusi.messenger.main.support.createUser
|
||||
import com.mtusi.messenger.main.infrastructure.dto.User
|
||||
import com.mtusi.messenger.main.support.TEST_PASSWORD
|
||||
import com.mtusi.messenger.main.support.auth
|
||||
import com.mtusi.messenger.main.support.register
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import com.mtusi.messenger.main.support.uniqueUsername
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.basicAuth
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
class UserControllerTest {
|
||||
|
||||
@Test
|
||||
fun `create user returns 201 and echoes fields`() = runApp { client ->
|
||||
fun `create user returns 201 without password`() = runApp { client ->
|
||||
val username = uniqueUsername("alice")
|
||||
|
||||
val response = client.post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateUserRequest(username = "alice", displayName = "Alice"))
|
||||
setBody(CreateUserRequest(username = username, displayName = "Alice", password = TEST_PASSWORD))
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Created, response.status)
|
||||
|
||||
val body = response.bodyAsText()
|
||||
assertFalse("password" in body)
|
||||
assertFalse(TEST_PASSWORD in body)
|
||||
assertEquals(username, response.body<User>().username)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -43,30 +61,120 @@ class UserControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `duplicate username returns 409 conflict`() = runApp { client ->
|
||||
client.createUser(username = "bob")
|
||||
|
||||
fun `missing password returns 400`() = runApp { client ->
|
||||
val response = client.post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateUserRequest(username = "bob", displayName = "Bob 2"))
|
||||
setBody("""{"username":"${uniqueUsername("nopass")}","display_name":"x"}""")
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Conflict, response.status)
|
||||
assertEquals("conflict", response.body<ErrorBody>().error.code)
|
||||
assertEquals(HttpStatusCode.BadRequest, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `missing X-User-Id on protected endpoint returns 401`() = runApp { client ->
|
||||
fun `password shorter than 12 code points returns 400`() = runApp { client ->
|
||||
val response = client.post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateUserRequest(username = uniqueUsername("short"), displayName = "x", password = "elevenchars"))
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.BadRequest, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `duplicate username returns 409 conflict`() = runApp { client ->
|
||||
val username = uniqueUsername("bob")
|
||||
|
||||
val request = CreateUserRequest(username = username, displayName = "Bob", password = TEST_PASSWORD)
|
||||
|
||||
val first = client.post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(request)
|
||||
}
|
||||
assertEquals(HttpStatusCode.Created, first.status)
|
||||
|
||||
val second = client.post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(request.copy(displayName = "Bob 2"))
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Conflict, second.status)
|
||||
assertEquals("conflict", second.body<ErrorBody>().error.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `concurrent registrations of one username create exactly one user`() = runApp { client ->
|
||||
val username = uniqueUsername("race")
|
||||
val attempts = 10
|
||||
|
||||
val statuses = coroutineScope {
|
||||
(0 until attempts).map {
|
||||
async {
|
||||
client.post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateUserRequest(username = username, displayName = "r$it", password = TEST_PASSWORD))
|
||||
}.status
|
||||
}
|
||||
}.awaitAll()
|
||||
}
|
||||
|
||||
assertEquals(1, statuses.count { it == HttpStatusCode.Created })
|
||||
assertEquals(attempts - 1, statuses.count { it == HttpStatusCode.Conflict })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `me returns the session owner`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
|
||||
val response = client.get("/api/v1/users/me") {
|
||||
auth(alice)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
assertEquals(alice.user, response.body<User>())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `missing token returns 401 with Bearer challenge`() = runApp { client ->
|
||||
val response = client.get("/api/v1/users/me")
|
||||
|
||||
assertEquals(HttpStatusCode.Unauthorized, response.status)
|
||||
assertEquals("unauthorized", response.body<ErrorBody>().error.code)
|
||||
assertEquals("Bearer realm=\"messenger\"", response.headers[HttpHeaders.WWWAuthenticate])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unknown X-User-Id returns 401`() = runApp { client ->
|
||||
fun `X-User-Id alone gives no access`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
|
||||
val response = client.get("/api/v1/users/me") {
|
||||
header("X-User-Id", "00000000-0000-0000-0000-000000000000")
|
||||
header("X-User-Id", alice.id.toString())
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Unauthorized, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `X-User-Id does not override bearer identity`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
val eve = client.register("eve")
|
||||
|
||||
val response = client.get("/api/v1/users/me") {
|
||||
auth(alice)
|
||||
header("X-User-Id", eve.id.toString())
|
||||
}
|
||||
|
||||
assertEquals(alice.id, response.body<User>().id)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Basic credentials are rejected on business endpoints`() = runApp { client ->
|
||||
val alice = client.register("alice")
|
||||
|
||||
val response = client.get("/api/v1/users/me") {
|
||||
basicAuth(alice.user.username, alice.password)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Unauthorized, response.status)
|
||||
assertEquals("Bearer realm=\"messenger\"", response.headers[HttpHeaders.WWWAuthenticate])
|
||||
}
|
||||
}
|
||||
|
||||
-58
@@ -1,58 +0,0 @@
|
||||
package com.mtusi.messenger.main.infrastructure.repositories.message
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.dto.Message
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
private const val CHAT_ID = "chat"
|
||||
|
||||
class InMemoryMessageRepositoryTest {
|
||||
|
||||
private fun repositoryWith(size: Int): InMemoryMessageRepository {
|
||||
|
||||
val repository = InMemoryMessageRepository()
|
||||
|
||||
repeat(size) { i ->
|
||||
repository.append(
|
||||
CHAT_ID,
|
||||
Message(
|
||||
id = "m$i",
|
||||
chatId = CHAT_ID,
|
||||
senderId = "u",
|
||||
text = "t$i",
|
||||
createdAt = "2026-01-01T00:00:00.000Z",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return repository
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns requested window in append order`() {
|
||||
val range = repositoryWith(10).findRangeByChatId(CHAT_ID, offset = 3, count = 4)
|
||||
assertEquals(listOf("m3", "m4", "m5", "m6"), range.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `window is clipped at the end of history`() {
|
||||
val range = repositoryWith(10).findRangeByChatId(CHAT_ID, offset = 8, count = 5)
|
||||
assertEquals(listOf("m8", "m9"), range.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `offset past the end returns empty`() {
|
||||
assertTrue(repositoryWith(10).findRangeByChatId(CHAT_ID, offset = 10, count = 5).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `huge offset does not overflow`() {
|
||||
assertTrue(repositoryWith(10).findRangeByChatId(CHAT_ID, offset = Int.MAX_VALUE - 1, count = 51).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unknown chat returns empty`() {
|
||||
assertTrue(InMemoryMessageRepository().findRangeByChatId("missing", offset = 0, count = 50).isEmpty())
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.mtusi.messenger.main.infrastructure.services.hashing
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.services.hashing.types.HashingAlgorithm
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertNotEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class DefaultHashingServiceTest {
|
||||
|
||||
private val service = DefaultHashingService()
|
||||
|
||||
@Test
|
||||
fun `hash is a salted Argon2id PHC string`() {
|
||||
val first = service.computeHash(HashingAlgorithm.Argon2id, "correct-horse-battery")
|
||||
val second = service.computeHash(HashingAlgorithm.Argon2id, "correct-horse-battery")
|
||||
|
||||
assertTrue(first.startsWith("\$argon2id\$v=19\$m=19456,t=2,p=1\$"))
|
||||
assertFalse("correct-horse-battery" in first)
|
||||
assertNotEquals(first, second, "same password must produce different hashes (random salt)")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `validates the right password and rejects a wrong one`() {
|
||||
val hash = service.computeHash(HashingAlgorithm.Argon2id, "correct-horse-battery")
|
||||
|
||||
assertTrue(service.validateHash(HashingAlgorithm.Argon2id, "correct-horse-battery", hash))
|
||||
assertFalse(service.validateHash(HashingAlgorithm.Argon2id, "correct-horse-batterY", hash))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `passwords differing only after 72 bytes are distinguished`() {
|
||||
// bcrypt silently truncates input to 72 bytes; Argon2id must not.
|
||||
val prefix = "ж".repeat(40) // 80 bytes in UTF-8
|
||||
val hash = service.computeHash(HashingAlgorithm.Argon2id, prefix + "a")
|
||||
|
||||
assertFalse(service.validateHash(HashingAlgorithm.Argon2id, prefix + "b", hash))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `malformed hash is rejected instead of throwing`() {
|
||||
assertFalse(service.validateHash(HashingAlgorithm.Argon2id, "whatever", "not-a-hash"))
|
||||
assertFalse(service.validateHash(HashingAlgorithm.Argon2id, "whatever", "\$argon2id\$v=19\$m=x\$a\$b"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.mtusi.messenger.main.plugins
|
||||
|
||||
import com.mtusi.messenger.main.support.TestDatabase
|
||||
import com.mtusi.messenger.main.support.createUser
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import com.zaxxer.hikari.HikariDataSource
|
||||
import org.flywaydb.core.Flyway
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class DatabaseMigrationTest {
|
||||
|
||||
@Test
|
||||
fun `re-running migrations applies nothing and keeps data`() = runApp { client ->
|
||||
client.createUser("keep")
|
||||
|
||||
HikariDataSource().apply {
|
||||
jdbcUrl = TestDatabase.container.jdbcUrl
|
||||
username = TestDatabase.USERNAME
|
||||
password = TestDatabase.PASSWORD
|
||||
}.use { dataSource ->
|
||||
|
||||
val usersBefore = countUsers(dataSource)
|
||||
|
||||
applyDatabaseMigrations(dataSource)
|
||||
applyDatabaseMigrations(dataSource)
|
||||
|
||||
val flyway = Flyway.configure()
|
||||
.dataSource(dataSource)
|
||||
.locations("classpath:migrations")
|
||||
.load()
|
||||
|
||||
assertEquals(0, flyway.info().pending().size)
|
||||
assertEquals(flyway.info().applied().size, flyway.info().all().size)
|
||||
assertEquals(usersBefore, countUsers(dataSource))
|
||||
}
|
||||
}
|
||||
|
||||
private fun countUsers(dataSource: HikariDataSource): Long {
|
||||
return dataSource.connection.use { connection ->
|
||||
connection.createStatement().use { statement ->
|
||||
statement.executeQuery("SELECT count(*) FROM users").use {
|
||||
it.next()
|
||||
it.getLong(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,31 @@
|
||||
package com.mtusi.messenger.main.support
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.dto.Chat
|
||||
import com.mtusi.messenger.main.infrastructure.dto.User
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.authentication.responses.SessionResponse
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest
|
||||
import com.mtusi.messenger.main.infrastructure.dto.Chat
|
||||
import com.mtusi.messenger.main.infrastructure.dto.User
|
||||
import com.mtusi.messenger.main.module
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.HttpRequestBuilder
|
||||
import io.ktor.client.request.basicAuth
|
||||
import io.ktor.client.request.bearerAuth
|
||||
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.HttpHeaders
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import io.ktor.server.config.HoconApplicationConfig
|
||||
import io.ktor.server.testing.ApplicationTestBuilder
|
||||
import io.ktor.server.testing.testApplication
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonNamingStrategy
|
||||
import java.util.UUID
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
val testJson = Json {
|
||||
@@ -30,7 +34,40 @@ val testJson = Json {
|
||||
namingStrategy = JsonNamingStrategy.SnakeCase
|
||||
}
|
||||
|
||||
fun runApp(block: suspend ApplicationTestBuilder.(HttpClient) -> Unit) = testApplication {
|
||||
const val TEST_PASSWORD = "correct-horse-battery"
|
||||
|
||||
data class TestUser(
|
||||
val user: User,
|
||||
val password: String,
|
||||
val token: String,
|
||||
) {
|
||||
val id: UUID
|
||||
get() = user.id
|
||||
}
|
||||
|
||||
private fun testConfig(sessionTtlSeconds: Long) = HoconApplicationConfig(
|
||||
ConfigFactory.parseMap(
|
||||
mapOf(
|
||||
"database.host" to TestDatabase.container.host,
|
||||
"database.port" to TestDatabase.container.firstMappedPort,
|
||||
"database.name" to TestDatabase.NAME,
|
||||
"database.username" to TestDatabase.USERNAME,
|
||||
"database.password" to TestDatabase.PASSWORD,
|
||||
"database.maxPoolSize" to 10,
|
||||
"database.connectionTimeoutMs" to 10000,
|
||||
"authentication.realm" to "messenger",
|
||||
"authentication.sessionTtlSeconds" to sessionTtlSeconds,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
fun runApp(
|
||||
sessionTtlSeconds: Long = 3600,
|
||||
block: suspend ApplicationTestBuilder.(HttpClient) -> Unit
|
||||
) = testApplication {
|
||||
environment {
|
||||
config = testConfig(sessionTtlSeconds)
|
||||
}
|
||||
application {
|
||||
module()
|
||||
}
|
||||
@@ -42,30 +79,50 @@ fun runApp(block: suspend ApplicationTestBuilder.(HttpClient) -> Unit) = testApp
|
||||
block(client)
|
||||
}
|
||||
|
||||
suspend fun HttpClient.createUser(username: String, displayName: String = username): User {
|
||||
/**
|
||||
* Usernames are unique per call: all tests share one database.
|
||||
*/
|
||||
fun uniqueUsername(prefix: String): String = "${prefix}_${UUID.randomUUID().toString().replace("-", "").take(12)}"
|
||||
|
||||
suspend fun HttpClient.createUser(prefix: String, password: String = TEST_PASSWORD): User {
|
||||
val username = uniqueUsername(prefix)
|
||||
val response = post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateUserRequest(username = username, displayName = displayName))
|
||||
setBody(CreateUserRequest(username = username, displayName = prefix, password = password))
|
||||
}
|
||||
check(response.status == HttpStatusCode.Created) { "createUser failed: ${response.status}" }
|
||||
return response.body()
|
||||
}
|
||||
|
||||
suspend fun HttpClient.login(username: String, password: String): SessionResponse {
|
||||
val response = post("/api/v1/auth/sessions") {
|
||||
basicAuth(username, password)
|
||||
}
|
||||
check(response.status == HttpStatusCode.Created) { "login failed: ${response.status}" }
|
||||
return response.body()
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a user and opens a session for it.
|
||||
*/
|
||||
suspend fun HttpClient.register(prefix: String): TestUser {
|
||||
val user = createUser(prefix)
|
||||
val session = login(user.username, TEST_PASSWORD)
|
||||
return TestUser(user, TEST_PASSWORD, session.token)
|
||||
}
|
||||
|
||||
fun HttpRequestBuilder.auth(user: TestUser) = bearerAuth(user.token)
|
||||
|
||||
suspend fun HttpClient.createChat(
|
||||
creator: User,
|
||||
creator: TestUser,
|
||||
title: String,
|
||||
memberIds: List<String>,
|
||||
memberIds: List<UUID>,
|
||||
): Chat {
|
||||
val response = post("/api/v1/chats") {
|
||||
header("X-User-Id", creator.id)
|
||||
auth(creator)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateChatRequest(title = title, memberIds = memberIds))
|
||||
setBody(CreateChatRequest(title = title, memberIds = memberIds.map(UUID::toString)))
|
||||
}
|
||||
check(response.status == HttpStatusCode.Created) { "createChat failed: ${response.status}" }
|
||||
return response.body()
|
||||
}
|
||||
|
||||
fun HttpResponse.header(name: String): String? = headers[name]
|
||||
|
||||
val HttpResponse.requestId: String?
|
||||
get() = headers[HttpHeaders.XRequestId]
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.mtusi.messenger.main.support
|
||||
|
||||
import org.testcontainers.containers.PostgreSQLContainer
|
||||
import org.testcontainers.utility.DockerImageName
|
||||
|
||||
/**
|
||||
* One PostgreSQL container per test JVM, same image as compose.yaml.
|
||||
* Tests share it and stay independent by using unique usernames.
|
||||
*/
|
||||
object TestDatabase {
|
||||
|
||||
const val NAME = "messenger"
|
||||
const val USERNAME = "messenger"
|
||||
const val PASSWORD = "messenger"
|
||||
|
||||
val container: PostgreSQLContainer<*> by lazy {
|
||||
PostgreSQLContainer(DockerImageName.parse("postgres:17.6-alpine3.22").asCompatibleSubstituteFor("postgres"))
|
||||
.withDatabaseName(NAME)
|
||||
.withUsername(USERNAME)
|
||||
.withPassword(PASSWORD)
|
||||
.also { it.start() }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user