feat: new endpoint for seeing remainnig screentime, test integration #8
|
|
@ -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
|
// 3. Bildschirmzeit synchronisieren
|
||||||
post("/api/v1/screentime/sync") {
|
post("/api/v1/screentime/sync") {
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,33 @@ class ServerTest {
|
||||||
assertTrue(response.bodyAsText().contains("Kidio Backend läuft!"))
|
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
|
@Test
|
||||||
fun `test protected endpoints reject unauthorized requests`() = testApplication {
|
fun `test protected endpoints reject unauthorized requests`() = testApplication {
|
||||||
val jwtService = JwtService()
|
val jwtService = JwtService()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue