diff --git a/src/main/kotlin/com/oliver/kidio/backend/plugins/Routing.kt b/src/main/kotlin/com/oliver/kidio/backend/plugins/Routing.kt index 0654cc5..3fbec59 100644 --- a/src/main/kotlin/com/oliver/kidio/backend/plugins/Routing.kt +++ b/src/main/kotlin/com/oliver/kidio/backend/plugins/Routing.kt @@ -133,6 +133,23 @@ fun Application.configureRouting(repository: KidioRepository, jwtService : JwtSe ) } } + get("/api/v1/screentime") { + val principal = call.principal() + val userid = principal?.userId?: run { + call.respond(HttpStatusCode.Unauthorized, mapOf("error" to "Ungültiger Token")) + return@get + } + val remainingSeconds = repository.getScreentime(userid) + call.respond( + HttpStatusCode.OK, + SyncResponse( + remainingSeconds = remainingSeconds, + isBlocked = remainingSeconds <= 0 + ) + ) + + + } // 3. Bildschirmzeit synchronisieren post("/api/v1/screentime/sync") { diff --git a/src/test/kotlin/ServerTest.kt b/src/test/kotlin/ServerTest.kt index 0acddd7..fe23153 100644 --- a/src/test/kotlin/ServerTest.kt +++ b/src/test/kotlin/ServerTest.kt @@ -34,6 +34,33 @@ class ServerTest { assertTrue(response.bodyAsText().contains("Kidio Backend läuft!")) } + @Test + fun `test get screentime returns current status`() = testApplication { + val jwtService = JwtService() + val repository = InMemoryKidioRepository() + + application { + install(ServerContentNegotiation) { json() } + configureSecurity(jwtService) + configureRouting(repository, jwtService) + } + + val token = jwtService.generateToken("default_user") + + // Ohne Token -> 401 Unauthorized + val unauthResponse = client.get("/api/v1/screentime") + assertEquals(HttpStatusCode.Unauthorized, unauthResponse.status) + + // Mit Token -> 200 OK + val response = client.get("/api/v1/screentime") { + header(HttpHeaders.Authorization, "Bearer $token") + } + assertEquals(HttpStatusCode.OK, response.status) + val json = Json.decodeFromString(response.bodyAsText()) + assertEquals(1800, json.remainingSeconds) + assertFalse(json.isBlocked) + } + @Test fun `test protected endpoints reject unauthorized requests`() = testApplication { val jwtService = JwtService()