Skip to content

Commit

Permalink
Pass account creation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jultty committed Apr 9, 2024
1 parent 8ce2dc5 commit 3e3c735
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/main/java/mirante/api/security/AuthenticationController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package mirante.api.security;

import jakarta.persistence.EntityNotFoundException;
import mirante.api.account.Account;
import mirante.api.account.AccountDTO;
import mirante.api.account.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
Expand All @@ -23,15 +25,21 @@ public class AuthenticationController {

@PostMapping("/auth/token")
@ResponseStatus(HttpStatus.CREATED)
String getToken(@RequestBody AccountDTO request) {
ResponseEntity<String> getToken(@RequestBody AccountDTO request) {

Optional<String> token =
accountDetailsService.login(request.registration, request.password);
Optional<String> token;

try {
token =
accountDetailsService.login(request.registration, request.password);
} catch (EntityNotFoundException exception) {
return new ResponseEntity<>( "{\"error\":\"No matching account found\"}", HttpStatus.BAD_REQUEST);
}

if (token.isPresent()) {
return "{\"token\":\"" + token.orElseThrow() + "\"}";
return new ResponseEntity<>("{\"token\":\"" + token.orElseThrow() + "\"}", HttpStatus.CREATED);
} else
return "{\"error\":\"Failed to generate token\"}";
return new ResponseEntity<>( "{\"error\":\"Failed to generate token\"}", HttpStatus.BAD_REQUEST);
}

@GetMapping("/auth")
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/mirante/api/security/AccountCreationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ void wrongPasswordForCreatedAccountDoesNotAllowObtainingAToken() {
.bodyValue(account)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().is4xxClientError()
.expectStatus().isBadRequest()
.expectBody()
.jsonPath("$.error").isEqualTo("Failed to generate token");
.jsonPath("$.error").isEqualTo("No matching account found");

}
}

0 comments on commit 3e3c735

Please sign in to comment.