From fd8887be198e30aa19e149ea601d90c809f5de93 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 14 Mar 2021 11:45:08 +0100 Subject: [PATCH] WIP --- .../pwreset/adapters/in/WebController.java | 73 +++++++++++++++++++ .../out/RemoteUsersServiceConsumer.java | 13 +++- .../api/domain/PasswordRequestResult.java | 7 -- .../api/domain/PasswordResetRequest.java | 19 ++++- .../api/domain/PasswordRestService.java | 5 +- .../out/RemoteUsersServiceConsumerIT.java | 25 +++++-- 6 files changed, 119 insertions(+), 23 deletions(-) create mode 100644 pwreset/src/main/java/de/juplo/demos/pwreset/adapters/in/WebController.java delete mode 100644 pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRequestResult.java diff --git a/pwreset/src/main/java/de/juplo/demos/pwreset/adapters/in/WebController.java b/pwreset/src/main/java/de/juplo/demos/pwreset/adapters/in/WebController.java new file mode 100644 index 0000000..79ad14e --- /dev/null +++ b/pwreset/src/main/java/de/juplo/demos/pwreset/adapters/in/WebController.java @@ -0,0 +1,73 @@ +package de.juplo.demos.pwreset.adapters.in; + +import de.juplo.demos.pwreset.api.domain.PasswordResetRequest; +import de.juplo.demos.pwreset.api.domain.PasswordRestService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.UUID; + + +@Controller +@RequiredArgsConstructor +public class WebController +{ + private final PasswordRestService passwordRestService; + + + @GetMapping("/") + public String requestForm() + { + return "request"; + } + + @PostMapping("/") + public void resetRequest( + @RequestParam String email, + HttpServletResponse httpServletResponse) + throws + IOException + { + PasswordResetRequest resetRequest = passwordRestService.requestPasswordReset(email); + httpServletResponse.sendRedirect(resetRequest.getUserId().toString()); + } + + @GetMapping("/{id}") + public ModelAndView getResetRequest(@PathVariable UUID id) + { + PasswordResetRequest resetRequest = passwordRestService.getPasswordReset(id); + ModelAndView mav = new ModelAndView("confirm"); + return "confirm"; + } + + @PutMapping("/{id") + public ModelAndView confirmResetRequest( + @PathVariable UUID id, + @RequestParam String code, + @RequestParam String password) + { + ModelAndView mav = new ModelAndView("confirm"); + mav.addObject("code", code); + mav.addObject("password", password); + + try + { + PasswordResetRequest resetRequest = + passwordRestService.confirmPasswordRequest( + id, + code, + password); + + } + catch (Exception e) + { + mav.addObject("message", e.getMessage()); + } + + return mav; + } +} diff --git a/pwreset/src/main/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumer.java b/pwreset/src/main/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumer.java index 6755255..78c5548 100644 --- a/pwreset/src/main/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumer.java +++ b/pwreset/src/main/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumer.java @@ -18,24 +18,31 @@ public class RemoteUsersServiceConsumer implements UsersService @Override - public Optional getUserByEmail(String email) { + public Optional getUserByEmail(String email) + { ResponseEntity response = restTemplate.getForEntity(uriTemplate, UserDTO.class, email); + HttpStatus status = response.getStatusCode(); UserDTO dto = response.getBody(); - switch (response.getStatusCode()) { + + switch (response.getStatusCode()) + { case OK: return Optional.of(dto.id); + case NOT_FOUND: return Optional.empty(); + default: throw new RuntimeException(status.toString()); } } @Override - public void setPassword(UUID userId, String password) { + public void setPassword(UUID userId, String password) + { } } diff --git a/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRequestResult.java b/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRequestResult.java deleted file mode 100644 index 5085f09..0000000 --- a/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRequestResult.java +++ /dev/null @@ -1,7 +0,0 @@ -package de.juplo.demos.pwreset.api.domain; - -public enum PasswordRequestResult -{ - SUCCESSFUL, - WRONG_CODE -} diff --git a/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordResetRequest.java b/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordResetRequest.java index 165c860..a5508d8 100644 --- a/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordResetRequest.java +++ b/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordResetRequest.java @@ -1,13 +1,24 @@ package de.juplo.demos.pwreset.api.domain; +import lombok.Data; + import java.util.Optional; import java.util.UUID; +@Data public class PasswordResetRequest { - UUID id; - Optional userId; - String code; - String password; + public enum Status + { + CREATED, + CONFIRMED, + ACCOMPLISHED + } + + private final UUID id; + private final Optional userId; + private String code; + private String password; + private Status status; } diff --git a/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRestService.java b/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRestService.java index d1d9d7a..af124c7 100644 --- a/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRestService.java +++ b/pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRestService.java @@ -5,6 +5,7 @@ import java.util.UUID; public interface PasswordRestService { - PasswordResetRequest requestReset(UUID userId); - PasswordRequestResult performRequest(PasswordResetRequest request); + PasswordResetRequest requestPasswordReset(String email); + PasswordResetRequest getPasswordReset(); + PasswordResetRequest confirmPasswordRequest(UUID requestId, String code, String password); } diff --git a/pwreset/src/test/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumerIT.java b/pwreset/src/test/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumerIT.java index e84280c..2f58b21 100644 --- a/pwreset/src/test/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumerIT.java +++ b/pwreset/src/test/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumerIT.java @@ -24,7 +24,8 @@ import static org.assertj.core.api.Assertions.fail; public class RemoteUsersServiceConsumerIT { @Pact(consumer="EmailServicesConsumer") - public RequestResponsePact getExistingUserByEmail(PactDslWithProvider builder) { + public RequestResponsePact getExistingUserByEmail(PactDslWithProvider builder) + { return builder .given("User with email pact@juplo.de exists") .uponReceiving("Request for user with email pact@juplo.de") @@ -40,16 +41,26 @@ public class RemoteUsersServiceConsumerIT @Test @PactTestFor(pactMethod = "getExistingUserByEmail") - public void testGetExistingUserByEmail(MockServer mockServer) { - RestTemplate restTemplate = new RestTemplateBuilder().rootUri(mockServer.getUrl()).build(); - RemoteUsersServiceConsumer usersService = new RemoteUsersServiceConsumer("/get?email={email}", restTemplate); - try { - Optional result = usersService.getUserByEmail("pact@juplo.de"); + public void testGetExistingUserByEmail(MockServer mockServer) + { + RestTemplate restTemplate = + new RestTemplateBuilder() + .rootUri(mockServer.getUrl()) + .build(); + RemoteUsersServiceConsumer usersService = + new RemoteUsersServiceConsumer( + "/get?email={email}", + restTemplate); + try + { + Optional result = + usersService.getUserByEmail("pact@juplo.de"); assertThat(result.isPresent()).isTrue(); assertThat(result.get()).isEqualTo(UUID.fromString("123e4567-e89b-12d3-a456-426614174000")); } - catch (Exception e) { + catch (Exception e) + { fail("Unexpected exception", e); } } -- 2.20.1