- password validation
All checks were successful
CI Pipeline / lint-and-test (pull_request) Successful in 2m12s

- Adding and refactoring tests
This commit is contained in:
aldemirm 2026-09-14 12:10:33 +02:00
parent aabf94941a
commit 0eb3ce33d0
2 changed files with 193 additions and 194 deletions

View file

@ -17,7 +17,8 @@ fun isValidPassword(password: String): Boolean {
if (password.length < 8) return false
val hasUppercase = password.any { it.isUpperCase() }
val hasEmoji = password.any { char ->
val hasEmoji =
password.any { char ->
val type = Character.getType(char).toByte()
type == Character.SURROGATE ||
type == Character.OTHER_SYMBOL ||
@ -32,7 +33,6 @@ fun isValidPassword(password: String): Boolean {
return hasUppercase && hasSpecialChar && !hasEmoji
}
fun Application.configureRouting(repository: KidioRepository, jwtService : JwtService) {
fun Application.configureRouting(
repository: KidioRepository,
jwtService: JwtService,
@ -47,7 +47,8 @@ fun Application.configureRouting(repository: KidioRepository, jwtService : JwtSe
if (!isValidPassword(registerRequest.passwordPlain)) {
call.respond(
HttpStatusCode.BadRequest,
"Das Passwort muss mindestens 8 Zeichen lang sein, mindestens einen Großbuchstaben und ein Sonderzeichen enthalten und darf keine Emojis enthalten."
"Das Passwort muss mindestens 8 Zeichen lang sein, " +
"mindestens einen Großbuchstaben und ein Sonderzeichen enthalten.",
)
return@post
}
@ -115,12 +116,9 @@ fun Application.configureRouting(repository: KidioRepository, jwtService : JwtSe
),
)
return@post
}
authenticate("auth-jwt") {
// 1. Alle verfügbaren Aufgaben abrufen
get("/api/v1/tasks") {
val tasks = repository.getAllTasks()
@ -135,7 +133,7 @@ fun Application.configureRouting(repository: KidioRepository, jwtService : JwtSe
if (userId == null) {
call.respond(
HttpStatusCode.Unauthorized,
mapOf("error" to "Ungültiges oder abgelaufenes Token")
mapOf("error" to "Ungültiges oder abgelaufenes Token"),
)
return@post
}
@ -219,5 +217,3 @@ fun Application.configureRouting(repository: KidioRepository, jwtService : JwtSe
}
}
}
}

View file

@ -112,7 +112,7 @@ class ServerTest {
setBody(
"""{
"email": "test@family.de",
"passwordPlain": "strengGeheim1",
"passwordPlain": "StrengGeheim1!",
"familyName": "Familie Schmidt",
"childAge": 8,
"parentPin": "4321"
@ -140,7 +140,8 @@ class ServerTest {
}
@Test
fun `test registration rejects invalid passwords`() = testApplication {
fun `test registration rejects invalid passwords`() =
testApplication {
val jwtService = JwtService()
val repository = InMemoryKidioRepository()
@ -152,15 +153,17 @@ class ServerTest {
configureRouting(repository, jwtService)
}
val invalidPasswords = listOf(
val invalidPasswords =
listOf(
"Kurz1!",
"keingrossbuch1!",
"KeinSonder12",
"Passwort123!😊"
"Passwort123!😊",
)
for (password in invalidPasswords) {
val response = client.post("/api/v1/auth/register") {
val response =
client.post("/api/v1/auth/register") {
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(
"""{
@ -169,13 +172,13 @@ class ServerTest {
"familyName": "Familie Schmidt",
"childAge": 8,
"parentPin": "4321"
}"""
}""",
)
}
assertEquals(
HttpStatusCode.BadRequest,
response.status,
"Passwort '$password' hätte abgelehnt werden müssen!"
"Passwort '$password' hätte abgelehnt werden müssen!",
)
}
}