From d9dc281dfcefaeab6ca76a114f6da85e1b77b5e2 Mon Sep 17 00:00:00 2001 From: alyssa Date: Wed, 30 Oct 2024 18:52:44 +0000 Subject: [PATCH] finish session controller tests --- .../routes/session/SessionControllerTest.kt | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/server/src/test/kotlin/routes/session/SessionControllerTest.kt b/server/src/test/kotlin/routes/session/SessionControllerTest.kt index 0339353..11eef3a 100644 --- a/server/src/test/kotlin/routes/session/SessionControllerTest.kt +++ b/server/src/test/kotlin/routes/session/SessionControllerTest.kt @@ -1,5 +1,6 @@ package routes.session +import http.CustomHeader import http.Routes import io.kotest.matchers.collections.shouldHaveSize import io.kotest.matchers.shouldBe @@ -7,16 +8,18 @@ import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* import io.ktor.server.testing.* +import java.util.UUID import org.junit.jupiter.api.Test import testCore.shouldMatchJson import util.ApplicationTest import util.OnlineConstants import util.ServerGlobals.sessionStore +import util.makeSession class SessionControllerTest : ApplicationTest() { @Test fun `Should respond to a begin session request`() = testApplication { - val response = client.post(Routes.BEGIN_SESSION, ::buildRequest) + val response = client.post(Routes.BEGIN_SESSION, ::buildSessionRequest) response.status shouldBe HttpStatusCode.OK sessionStore.getAll().shouldHaveSize(1) @@ -33,9 +36,44 @@ class SessionControllerTest : ApplicationTest() { .trimIndent() } - @Test fun `Should reject an update achievement call without a session`() = testApplication {} + @Test + fun `Should reject an update achievement call without a session`() = testApplication { + val response = client.post(Routes.ACHIEVEMENT_COUNT, ::buildAchievementUpdateRequest) + response.status shouldBe HttpStatusCode.Unauthorized + } + + @Test + fun `Should be able to update achievement count`() = testApplication { + val session = makeSession(achievementCount = 4) + sessionStore.put(session) + + val response = + client.post(Routes.ACHIEVEMENT_COUNT) { + buildAchievementUpdateRequest(this, session.id) + } + response.status shouldBe HttpStatusCode.NoContent + + val updatedSession = sessionStore.get(session.id) + updatedSession.achievementCount shouldBe 8 + } + + private fun buildAchievementUpdateRequest( + builder: HttpRequestBuilder, + sessionId: UUID? = null, + ) { + builder.contentType(ContentType.Application.Json) + sessionId?.let { builder.header(CustomHeader.SESSION_ID, sessionId) } + builder.setBody( + """ + { + "achievementCount": 8 + } + """ + .trimIndent() + ) + } - private fun buildRequest(builder: HttpRequestBuilder) { + private fun buildSessionRequest(builder: HttpRequestBuilder) { builder.contentType(ContentType.Application.Json) builder.setBody( """