Merge branch '22-test-controllers' into 'dev'

Resolve "Test all new controllers and websocket"

Closes #31

See merge request sa4-2020/the-sanmarinoes/backend!39
This commit is contained in:
Claudio Maggioni 2020-03-17 14:02:26 +01:00
commit 3096c7862c
5 changed files with 69 additions and 16 deletions

View file

@ -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'

View file

@ -6,6 +6,8 @@ import org.springframework.data.repository.CrudRepository;
public interface ConfirmationTokenRepository extends CrudRepository<ConfirmationToken, String> {
ConfirmationToken findByConfirmationToken(String confirmationToken);
ConfirmationToken findByUser(User user);
@Transactional
void deleteByUserAndResetPassword(User user, boolean resetPassword);
}

View file

@ -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<OkResponse> res =
@ -50,12 +47,7 @@ public class AuthenticationTests extends SmartHutTest {
this.url("/register"), getDisabledUser(), OkResponse.class);
assertThat(res.getStatusCode().equals(HttpStatus.OK));
final ResponseEntity<OkResponse> 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<JWTResponse> 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());
}
}

View file

@ -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<OkResponse> 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<Void> 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) {

View file

@ -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: