--- /dev/null
+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;
+ }
+}
@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)
+ {
}
}
+++ /dev/null
-package de.juplo.demos.pwreset.api.domain;
-
-public enum PasswordRequestResult
-{
- SUCCESSFUL,
- WRONG_CODE
-}
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;
}
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);
}
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")
@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);
}
}