Added more tests on login/registration

This commit is contained in:
Claudio Maggioni 2020-03-05 15:03:02 +01:00
parent e8a55ae131
commit 748c5ba4b4
2 changed files with 89 additions and 21 deletions

View file

@ -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
+ '\''
+ '}';
}
}

View file

@ -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<OkResponse> 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<OkResponse> 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<DuplicateRegistrationException> 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<DuplicateRegistrationException> 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<DuplicateRegistrationException> 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<OkResponse> 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<String, Object> badJSON = Map.of("badkey", 3, "password", "ciaomamma");