Skip to content

Commit

Permalink
finish session controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssaruth committed Oct 30, 2024
1 parent a3d6d1b commit d9dc281
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions server/src/test/kotlin/routes/session/SessionControllerTest.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package routes.session

import http.CustomHeader
import http.Routes
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
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)
Expand All @@ -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(
"""
Expand Down

0 comments on commit d9dc281

Please sign in to comment.