From 748c5ba4b4cc9a4427b00f9c7344723d695046b1 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 5 Mar 2020 15:03:02 +0100 Subject: [PATCH] Added more tests on login/registration --- .../sanmarinoes/smarthut/dto/JWTRequest.java | 18 ++-- .../smarthut/AuthenticationTests.java | 92 ++++++++++++++++--- 2 files changed, 89 insertions(+), 21 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java index 52fa5c3..da11bc3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/JWTRequest.java @@ -1,8 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; +import javax.validation.constraints.NotNull; + public class JWTRequest { - private String usernameOrEmail; - private String password; + @NotNull private String usernameOrEmail; + @NotNull private String password; public String getUsernameOrEmail() { return this.usernameOrEmail; @@ -22,9 +24,13 @@ public class JWTRequest { @Override public String toString() { - return "JWTRequest{" + - "usernameOrEmail='" + usernameOrEmail + '\'' + - ", password='" + password + '\'' + - '}'; + return "JWTRequest{" + + "usernameOrEmail='" + + usernameOrEmail + + '\'' + + ", password='" + + password + + '\'' + + '}'; } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java index 96b67ee..60761cd 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/AuthenticationTests.java @@ -6,6 +6,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTResponse; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.OkResponse; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.UserRegistrationRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateRegistrationException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UnauthorizedException; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -24,29 +25,37 @@ public class AuthenticationTests extends SmartHutTest { @Autowired private TestRestTemplate restTemplate; + private UserRegistrationRequest getDisabledUser() { + final UserRegistrationRequest disabledUser = new UserRegistrationRequest(); + disabledUser.setName("Disabled User"); + disabledUser.setEmail("disabled@example.com"); + disabledUser.setUsername("disabled"); + disabledUser.setPassword("password"); + return disabledUser; + } + + private static final UserRegistrationRequest enabledUser = new UserRegistrationRequest(); + + static { + enabledUser.setName("Enabled User"); + enabledUser.setEmail("enabled@example.com"); + enabledUser.setUsername("enabled"); + enabledUser.setPassword("password"); + } + @Override protected void setUp() { - final UserRegistrationRequest request = new UserRegistrationRequest(); - request.setName("Disabled User"); - request.setEmail("disabled@example.com"); - request.setUsername("disabled"); - request.setPassword("password"); - final ResponseEntity res = - this.restTemplate.postForEntity(this.url("/register"), request, OkResponse.class); + this.restTemplate.postForEntity( + this.url("/register"), getDisabledUser(), OkResponse.class); assertThat(res.getStatusCode().equals(HttpStatus.OK)); - final UserRegistrationRequest request2 = new UserRegistrationRequest(); - request2.setName("Enabled User"); - request2.setEmail("enabled@example.com"); - request2.setUsername("enabled"); - request2.setPassword("password"); - final ResponseEntity res2 = - this.restTemplate.postForEntity(this.url("/register"), request, OkResponse.class); + this.restTemplate.postForEntity( + this.url("/register"), enabledUser, OkResponse.class); assertThat(res2.getStatusCode().equals(HttpStatus.OK)); - // TODO: email confirmation for res2 + // TODO: email confirmation for enabledUser } @Test @@ -130,6 +139,59 @@ public class AuthenticationTests extends SmartHutTest { assertThat(errors.get(0).getAsJsonObject().get("field").getAsString().equals("username")); } + @Test + public void registrationShouldReturnBadRequestWithDuplicateData() { + { + final ResponseEntity res = + this.restTemplate.postForEntity( + url("/register"), + getDisabledUser(), + DuplicateRegistrationException.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + } + + { + final UserRegistrationRequest disabledUserDifferentMail = getDisabledUser(); + enabledUser.setEmail("another@example.com"); + + final ResponseEntity res = + this.restTemplate.postForEntity( + url("/register"), + disabledUserDifferentMail, + DuplicateRegistrationException.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + } + + { + final UserRegistrationRequest disabledUserDifferentUsername = getDisabledUser(); + enabledUser.setUsername("another"); + + final ResponseEntity res = + this.restTemplate.postForEntity( + url("/register"), + disabledUserDifferentUsername, + DuplicateRegistrationException.class); + assertThat(res.getStatusCode().equals(HttpStatus.BAD_REQUEST)); + assertThat(res.getBody() != null); + } + } + + @Test + public void registrationShouldReturnOkWithCorrectData() { + final UserRegistrationRequest request = new UserRegistrationRequest(); + request.setName("Registration Test"); + request.setUsername("smarthut"); + request.setEmail("smarthut.sm@example.com"); + request.setPassword("password"); + + final ResponseEntity res = + this.restTemplate.postForEntity(url("/register"), request, OkResponse.class); + assertThat(res.getStatusCode().equals(HttpStatus.OK)); + assertThat(res.getBody() != null); + } + @Test public void loginShouldReturnBadRequestWithIncorrectFields() { final Map badJSON = Map.of("badkey", 3, "password", "ciaomamma");