|
| 1 | +package dev.typetype.server |
| 2 | + |
| 3 | +import dev.typetype.server.models.SubscriptionItem |
| 4 | +import dev.typetype.server.routes.subscriptionsRoutes |
| 5 | +import dev.typetype.server.services.AuthService |
| 6 | +import dev.typetype.server.services.SubscriptionsService |
| 7 | +import io.ktor.client.request.delete |
| 8 | +import io.ktor.client.request.get |
| 9 | +import io.ktor.client.request.headers |
| 10 | +import io.ktor.client.request.post |
| 11 | +import io.ktor.client.request.setBody |
| 12 | +import io.ktor.client.statement.bodyAsText |
| 13 | +import io.ktor.http.ContentType |
| 14 | +import io.ktor.http.HttpHeaders |
| 15 | +import io.ktor.http.HttpStatusCode |
| 16 | +import io.ktor.serialization.kotlinx.json.json |
| 17 | +import io.ktor.server.application.install |
| 18 | +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation |
| 19 | +import io.ktor.server.routing.routing |
| 20 | +import io.ktor.server.testing.ApplicationTestBuilder |
| 21 | +import io.ktor.server.testing.testApplication |
| 22 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 23 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 24 | +import org.junit.jupiter.api.BeforeAll |
| 25 | +import org.junit.jupiter.api.BeforeEach |
| 26 | +import org.junit.jupiter.api.Test |
| 27 | + |
| 28 | +class SubscriptionsCanonicalizationRoutesTest { |
| 29 | + private val service = SubscriptionsService() |
| 30 | + private val auth = AuthService.fixed(TEST_USER_ID) |
| 31 | + |
| 32 | + companion object { |
| 33 | + @BeforeAll |
| 34 | + @JvmStatic |
| 35 | + fun initDb() = TestDatabase.setup() |
| 36 | + } |
| 37 | + |
| 38 | + @BeforeEach |
| 39 | + fun clean() { |
| 40 | + TestDatabase.truncateAll() |
| 41 | + } |
| 42 | + |
| 43 | + private fun withApp(block: suspend ApplicationTestBuilder.() -> Unit) = testApplication { |
| 44 | + application { |
| 45 | + install(ContentNegotiation) { json() } |
| 46 | + routing { subscriptionsRoutes(service, auth) } |
| 47 | + } |
| 48 | + block() |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun `POST and GET subscriptions return canonical channel url`() = withApp { |
| 53 | + val response = client.post("/subscriptions") { |
| 54 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 55 | + headers.append(HttpHeaders.ContentType, ContentType.Application.Json.toString()) |
| 56 | + setBody("""{"channelUrl":"http://WWW.YouTube.com/channel/UC123/?utm_source=x#frag","name":"Test","avatarUrl":""}""") |
| 57 | + } |
| 58 | + assertEquals(HttpStatusCode.Created, response.status) |
| 59 | + assertTrue(response.bodyAsText().contains("\"channelUrl\":\"https://www.youtube.com/channel/UC123\"")) |
| 60 | + val listBody = client.get("/subscriptions") { |
| 61 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 62 | + }.bodyAsText() |
| 63 | + assertTrue(listBody.contains("\"channelUrl\":\"https://www.youtube.com/channel/UC123\"")) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `DELETE subscriptions canonicalizes channel url`() = withApp { |
| 68 | + service.add( |
| 69 | + TEST_USER_ID, |
| 70 | + SubscriptionItem(channelUrl = "https://www.youtube.com/channel/UC123", name = "Test", avatarUrl = ""), |
| 71 | + ) |
| 72 | + val deleteResponse = client.delete("/subscriptions/http%3A%2F%2FWWW.YouTube.com%2Fchannel%2FUC123%2F%3Futm_source%3Dx%23frag") { |
| 73 | + headers.append(HttpHeaders.Authorization, "Bearer test-jwt") |
| 74 | + } |
| 75 | + assertEquals(HttpStatusCode.NoContent, deleteResponse.status) |
| 76 | + } |
| 77 | +} |
0 commit comments