feat: new endpoint for seeing remainnig screentime, test integration #8

Merged
DrunkenPinguin merged 8 commits from feat/add-new-endpoint-for-sreentime into main 2026-09-14 08:15:26 +00:00
2 changed files with 44 additions and 0 deletions
Showing only changes of commit cc9b381577 - Show all commits

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()