From 24fa574c63e16402c21d5d3ed9e28f41d74e3c89 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 12 Mar 2020 16:57:50 +0100 Subject: [PATCH] Added email confirmation test --- build.gradle | 1 + .../models/ConfirmationTokenRepository.java | 2 + .../smarthut/AuthenticationTests.java | 36 ++++++++------- .../sanmarinoes/smarthut/SmartHutTest.java | 44 +++++++++++++++++++ src/test/resources/application.properties | 2 +- 5 files changed, 69 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index 5140ea7..303f5e7 100644 --- a/build.gradle +++ b/build.gradle @@ -32,6 +32,7 @@ dependencies { compile 'io.springfox:springfox-swagger2:2.9.2' compile 'io.springfox:springfox-swagger-ui:2.9.2' compile 'org.springframework.boot:spring-boot-configuration-processor' + testCompile 'org.springframework.boot:spring-boot-starter-webflux' implementation('org.springframework.boot:spring-boot-starter-web') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-json' diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenRepository.java index 9bf3791..40c6a17 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ConfirmationTokenRepository.java @@ -6,6 +6,8 @@ import org.springframework.data.repository.CrudRepository; public interface ConfirmationTokenRepository extends CrudRepository { ConfirmationToken findByConfirmationToken(String confirmationToken); + ConfirmationToken findByUser(User user); + @Transactional void deleteByUserAndResetPassword(User user, boolean resetPassword); } 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 60761cd..d13104f 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 @@ -8,6 +8,8 @@ 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 ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.Map; @@ -25,6 +27,10 @@ public class AuthenticationTests extends SmartHutTest { @Autowired private TestRestTemplate restTemplate; + @Autowired private UserRepository userRepository; + + @Autowired private ConfirmationTokenRepository tokenRepository; + private UserRegistrationRequest getDisabledUser() { final UserRegistrationRequest disabledUser = new UserRegistrationRequest(); disabledUser.setName("Disabled User"); @@ -34,15 +40,6 @@ public class AuthenticationTests extends SmartHutTest { 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 ResponseEntity res = @@ -50,12 +47,7 @@ public class AuthenticationTests extends SmartHutTest { this.url("/register"), getDisabledUser(), OkResponse.class); assertThat(res.getStatusCode().equals(HttpStatus.OK)); - final ResponseEntity res2 = - this.restTemplate.postForEntity( - this.url("/register"), enabledUser, OkResponse.class); - assertThat(res2.getStatusCode().equals(HttpStatus.OK)); - - // TODO: email confirmation for enabledUser + registerTestUser(restTemplate, userRepository, tokenRepository); } @Test @@ -230,4 +222,18 @@ public class AuthenticationTests extends SmartHutTest { assertThat(res.getBody() != null); assertThat(res.getBody().isUserDisabled()); } + + @Test + public void loginShouldReturnTokenWithEnabledUser() { + final JWTRequest request = new JWTRequest(); + request.setUsernameOrEmail("enabled"); + request.setPassword("password"); + + final ResponseEntity res = + this.restTemplate.postForEntity(url("/auth/login"), request, JWTResponse.class); + assertThat(res.getStatusCode().equals(HttpStatus.OK)); + assertThat(res.getBody() != null); + assertThat(res.getBody().getToken() != null); + assertThat(!res.getBody().getToken().isEmpty()); + } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java index 5c6e097..f2b737a 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmartHutTest.java @@ -1,6 +1,18 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; +import static org.assertj.core.api.Assertions.assertThat; + +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.models.ConfirmationToken; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ConfirmationTokenRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; import org.junit.jupiter.api.BeforeEach; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.reactive.function.client.WebClient; public abstract class SmartHutTest { private boolean setupDone = false; @@ -15,6 +27,38 @@ public abstract class SmartHutTest { protected void setUp() {} + protected static final UserRegistrationRequest enabledUser = new UserRegistrationRequest(); + + static { + enabledUser.setName("Enabled User"); + enabledUser.setEmail("enabled@example.com"); + enabledUser.setUsername("enabled"); + enabledUser.setPassword("password"); + } + + protected void registerTestUser( + final TestRestTemplate restTemplate, + final UserRepository userRepository, + final ConfirmationTokenRepository tokenRepository) { + final ResponseEntity res2 = + restTemplate.postForEntity(this.url("/register"), enabledUser, OkResponse.class); + assertThat(res2.getStatusCode().equals(HttpStatus.OK)); + + final User persistedEnabledUser = userRepository.findByUsername("enabled"); + final ConfirmationToken token = tokenRepository.findByUser(persistedEnabledUser); + + final ResponseEntity res3 = + WebClient.create(getBaseURL()) + .get() + .uri("/register/confirm-account?token=" + token.getConfirmationToken()) + .retrieve() + .toBodilessEntity() + .block(); + + assertThat(res3.getStatusCode().is2xxSuccessful()); + assertThat(userRepository.findByUsername("enabled").getEnabled()); + } + @BeforeEach void setUpHack() { if (!setupDone) { diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index ce6fe39..673d02a 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -28,7 +28,7 @@ server.port = 2000 email.registrationSubject=Complete your SmartHut.sm registration email.registration=To confirm your registration, please click here: -email.registrationPath=http://localhost:8080/register/confirm-account?token= +email.registrationPath=http://localhost:2000/register/confirm-account?token= email.resetpasswordSubject=SmartHut.sm password reset email.resetpassword=To reset your password, please click here: