WIP
authorKai Moritz <kai@juplo.de>
Sun, 14 Mar 2021 10:45:08 +0000 (11:45 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 14 Mar 2021 10:45:08 +0000 (11:45 +0100)
pwreset/src/main/java/de/juplo/demos/pwreset/adapters/in/WebController.java [new file with mode: 0644]
pwreset/src/main/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumer.java
pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRequestResult.java [deleted file]
pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordResetRequest.java
pwreset/src/main/java/de/juplo/demos/pwreset/api/domain/PasswordRestService.java
pwreset/src/test/java/de/juplo/demos/pwreset/adapters/out/RemoteUsersServiceConsumerIT.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 (file)
index 0000000..79ad14e
--- /dev/null
@@ -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;
+  }
+}
index 6755255..78c5548 100644 (file)
@@ -18,24 +18,31 @@ public class RemoteUsersServiceConsumer implements UsersService
 
 
   @Override
-  public Optional<UUID> getUserByEmail(String email) {
+  public Optional<UUID> getUserByEmail(String email)
+  {
     ResponseEntity<UserDTO> 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 (file)
index 5085f09..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-package de.juplo.demos.pwreset.api.domain;
-
-public enum PasswordRequestResult
-{
-  SUCCESSFUL,
-  WRONG_CODE
-}
index 165c860..a5508d8 100644 (file)
@@ -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<UUID> userId;
-  String code;
-  String password;
+  public enum Status
+  {
+    CREATED,
+    CONFIRMED,
+    ACCOMPLISHED
+  }
+
+  private final UUID id;
+  private final Optional<UUID> userId;
+  private String code;
+  private String password;
+  private Status status;
 }
index d1d9d7a..af124c7 100644 (file)
@@ -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);
 }
index e84280c..2f58b21 100644 (file)
@@ -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<UUID> 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<UUID> 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);
     }
   }