Skip to content

Commit

Permalink
Implement a UserDetails DTO
Browse files Browse the repository at this point in the history
  • Loading branch information
jultty committed Apr 9, 2024
1 parent 3e3c735 commit 5d06596
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/main/java/mirante/api/security/AccountDetailsDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mirante.api.security;

public class AccountDetailsDTO {
public String registration;
public String password;

public AccountDetailsDTO(String registration, String password) {
this.registration = registration;
this.password = password;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ public class AuthenticationController {

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

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);
return new ResponseEntity<>( "{\"error\":\"Failed to retrieve token\"}", HttpStatus.BAD_REQUEST);
}

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

@GetMapping("/auth")
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/mirante/api/security/AccountCreationTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package mirante.api.security;

import mirante.api.account.AccountDTO;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.springframework.web.reactive.function.client.ExchangeFilterFunctions.basicAuthentication;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class AccountCreationTest {

@Value("${server.port:8889}")
Expand All @@ -23,6 +21,7 @@ class AccountCreationTest {
WebTestClient authenticatedClient;
WebTestClient invalidAuthenticationClient;
AccountDTO account;
AccountDetailsDTO details;

@BeforeAll
void setUp() {
Expand All @@ -46,6 +45,8 @@ void setUp() {
account = new AccountDTO(
"jc222222", "Tania Wolfgramm", "tania@mirante.dev", "xyz6060"
);

details = new AccountDetailsDTO(account.registration, account.password);
}

@Test @Order(1)
Expand Down Expand Up @@ -76,7 +77,7 @@ void createdAccountDetailsAllowObtainingAToken() {
unauthenticatedClient
.post().uri("/auth/token")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(account)
.bodyValue(details)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isCreated()
Expand All @@ -86,12 +87,12 @@ void createdAccountDetailsAllowObtainingAToken() {
@Test @Order(5)
void wrongPasswordForCreatedAccountDoesNotAllowObtainingAToken() {

account.password = "xyz0000";
details.password = "xyz0000";

invalidAuthenticationClient
.post().uri("/auth/token")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(account)
.bodyValue(details)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isBadRequest()
Expand Down

0 comments on commit 5d06596

Please sign in to comment.