From 40fcb741c0f5011c30cb8de488efa6726058852a Mon Sep 17 00:00:00 2001 From: Pedro Oliveira Date: Sun, 23 Feb 2025 22:47:04 +0000 Subject: [PATCH] Fixed invalid error message --- .../pt/up/fe/ni/website/backend/service/AccountService.kt | 2 ++ .../kotlin/pt/up/fe/ni/website/backend/service/AuthService.kt | 1 + .../up/fe/ni/website/backend/controller/AuthControllerTest.kt | 4 ++-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/pt/up/fe/ni/website/backend/service/AccountService.kt b/src/main/kotlin/pt/up/fe/ni/website/backend/service/AccountService.kt index 6cbb525f..d922af87 100644 --- a/src/main/kotlin/pt/up/fe/ni/website/backend/service/AccountService.kt +++ b/src/main/kotlin/pt/up/fe/ni/website/backend/service/AccountService.kt @@ -39,6 +39,8 @@ class AccountService( fun doesAccountExist(id: Long): Boolean = repository.findByIdOrNull(id) != null + fun doesAccountExist(email: String): Boolean = repository.findByEmail(email) != null + fun updateAccountById(id: Long, dto: UpdateAccountDto): Account { val account = getAccountById(id) diff --git a/src/main/kotlin/pt/up/fe/ni/website/backend/service/AuthService.kt b/src/main/kotlin/pt/up/fe/ni/website/backend/service/AuthService.kt index 9148b7fd..4ac6257b 100644 --- a/src/main/kotlin/pt/up/fe/ni/website/backend/service/AuthService.kt +++ b/src/main/kotlin/pt/up/fe/ni/website/backend/service/AuthService.kt @@ -54,6 +54,7 @@ class AuthService( } fun authenticate(email: String, password: String): Account { + if (!accountService.doesAccountExist(email)) throw InvalidBearerTokenException(ErrorMessages.invalidCredentials) val account = accountService.getAccountByEmail(email) if (!passwordEncoder.matches(password, account.password)) { throw InvalidBearerTokenException(ErrorMessages.invalidCredentials) diff --git a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AuthControllerTest.kt b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AuthControllerTest.kt index 59d4ec44..7fc3d5aa 100644 --- a/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AuthControllerTest.kt +++ b/src/test/kotlin/pt/up/fe/ni/website/backend/controller/AuthControllerTest.kt @@ -102,8 +102,8 @@ class AuthControllerTest @Autowired constructor( ) ).andExpectAll( - status().isNotFound, - jsonPath("$.errors[0].message").value("account not found with email president@niaefeup.pt") + status().isUnauthorized, + jsonPath("$.errors[0].message").value("invalid credentials") ).andDocumentErrorResponse(documentation, hasRequestPayload = true) }