|
| 1 | +package dev.typetype.server |
| 2 | + |
| 3 | +import dev.typetype.server.routes.settingsRoutes |
| 4 | +import dev.typetype.server.services.AuthService |
| 5 | +import dev.typetype.server.services.SettingsService |
| 6 | +import io.ktor.client.request.get |
| 7 | +import io.ktor.client.request.headers |
| 8 | +import io.ktor.client.request.put |
| 9 | +import io.ktor.client.request.setBody |
| 10 | +import io.ktor.client.statement.bodyAsText |
| 11 | +import io.ktor.http.ContentType |
| 12 | +import io.ktor.http.HttpHeaders |
| 13 | +import io.ktor.http.HttpStatusCode |
| 14 | +import io.ktor.serialization.kotlinx.json.json |
| 15 | +import io.ktor.server.application.install |
| 16 | +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation |
| 17 | +import io.ktor.server.routing.routing |
| 18 | +import io.ktor.server.testing.ApplicationTestBuilder |
| 19 | +import io.ktor.server.testing.testApplication |
| 20 | +import kotlinx.serialization.json.Json |
| 21 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 22 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 23 | +import org.junit.jupiter.api.BeforeAll |
| 24 | +import org.junit.jupiter.api.BeforeEach |
| 25 | +import org.junit.jupiter.api.Test |
| 26 | + |
| 27 | +class SettingsPrivacyControlsRoutesTest { |
| 28 | + private val service = SettingsService() |
| 29 | + private val auth = AuthService.fixed(TEST_USER_ID) |
| 30 | + |
| 31 | + companion object { |
| 32 | + @BeforeAll |
| 33 | + @JvmStatic |
| 34 | + fun initDb() { |
| 35 | + TestDatabase.setup() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + fun clean() { |
| 41 | + TestDatabase.truncateAll() |
| 42 | + } |
| 43 | + |
| 44 | + private fun withApp(block: suspend ApplicationTestBuilder.() -> Unit) = testApplication { |
| 45 | + application { |
| 46 | + install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true; encodeDefaults = true }) } |
| 47 | + routing { settingsRoutes(service, auth) } |
| 48 | + } |
| 49 | + block() |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `GET settings returns SponsorBlock and privacy defaults`() = withApp { |
| 54 | + val body = client.get("/settings") { |
| 55 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 56 | + }.bodyAsText() |
| 57 | + |
| 58 | + assertContainsAll( |
| 59 | + body, |
| 60 | + listOf( |
| 61 | + "\"sponsorBlockMode\":\"auto_skip\"", |
| 62 | + "\"hideHomeRecommendations\":false", |
| 63 | + "\"hideRelatedVideos\":false", |
| 64 | + "\"hideComments\":false", |
| 65 | + "\"hideShorts\":false", |
| 66 | + ), |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `PUT settings persists SponsorBlock and privacy controls`() = withApp { |
| 72 | + val put = client.put("/settings") { |
| 73 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 74 | + headers.append(HttpHeaders.ContentType, ContentType.Application.Json.toString()) |
| 75 | + setBody(settingsBody()) |
| 76 | + } |
| 77 | + assertEquals(HttpStatusCode.OK, put.status) |
| 78 | + |
| 79 | + val body = client.get("/settings") { |
| 80 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 81 | + }.bodyAsText() |
| 82 | + assertContainsAll( |
| 83 | + body, |
| 84 | + listOf( |
| 85 | + "\"sponsorBlockMode\":\"mark_only\"", |
| 86 | + "\"hideHomeRecommendations\":true", |
| 87 | + "\"hideRelatedVideos\":true", |
| 88 | + "\"hideComments\":true", |
| 89 | + "\"hideShorts\":true", |
| 90 | + ), |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun `PUT settings rejects invalid SponsorBlock mode`() = withApp { |
| 96 | + val response = client.put("/settings") { |
| 97 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 98 | + headers.append(HttpHeaders.ContentType, ContentType.Application.Json.toString()) |
| 99 | + setBody(settingsBody(sponsorBlockMode = "skip_everything")) |
| 100 | + } |
| 101 | + |
| 102 | + assertEquals(HttpStatusCode.BadRequest, response.status) |
| 103 | + } |
| 104 | + |
| 105 | + private fun assertContainsAll(body: String, values: List<String>) = |
| 106 | + values.forEach { assertTrue(body.contains(it)) } |
| 107 | + |
| 108 | + private fun settingsBody(sponsorBlockMode: String = "mark_only"): String = """ |
| 109 | + {"defaultService":0,"defaultQuality":"1080p","autoplay":true,"volume":1.0,"muted":false,"sponsorBlockMode":"$sponsorBlockMode","hideHomeRecommendations":true,"hideRelatedVideos":true,"hideComments":true,"hideShorts":true} |
| 110 | + """.trimIndent() |
| 111 | +} |
0 commit comments