feat: new endpoint for seeing remainnig screentime, test integration

This commit is contained in:
Oliver Wallisch 2026-09-11 21:46:00 +02:00
parent 263f182bab
commit cc9b381577
2 changed files with 44 additions and 0 deletions

View file

@ -133,6 +133,23 @@ fun Application.configureRouting(repository: KidioRepository, jwtService : JwtSe
)
}
}
get("/api/v1/screentime") {
val principal = call.principal<UserPrincipal>()
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") {

View file

@ -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<SyncResponse>(response.bodyAsText())
assertEquals(1800, json.remainingSeconds)
assertFalse(json.isBlocked)
}
@Test
fun `test protected endpoints reject unauthorized requests`() = testApplication {
val jwtService = JwtService()