Add tests for UserController get and post
This commit is contained in:
parent
189e664f37
commit
d423a57fa0
@ -31,8 +31,11 @@ dependencies {
|
||||
testImplementation("org.springframework.boot:spring-boot-testcontainers")
|
||||
testImplementation("org.testcontainers:junit-jupiter")
|
||||
testImplementation("org.testcontainers:postgresql")
|
||||
testImplementation("io.rest-assured:rest-assured")
|
||||
testImplementation("org.hamcrest:hamcrest")
|
||||
}
|
||||
|
||||
|
||||
tasks.register("run") {
|
||||
dependsOn(tasks.bootRun)
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ public class UserController {
|
||||
User poster = authServ.getUserFromToken(authorization);
|
||||
if (poster == null) return false;
|
||||
|
||||
return poster.getRole() == Role.Secretary && poster.getRole() == Role.Admin;
|
||||
return poster.getRole() == Role.Secretary || poster.getRole() == Role.Admin;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,114 @@
|
||||
package ovh.herisson.Clyde.Endpoints;
|
||||
|
||||
import com.github.dockerjava.api.model.ExposedPort;
|
||||
import com.github.dockerjava.api.model.HostConfig;
|
||||
import com.github.dockerjava.api.model.PortBinding;
|
||||
import com.github.dockerjava.api.model.Ports;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import ovh.herisson.Clyde.ClydeApplication;
|
||||
import ovh.herisson.Clyde.Repositories.TokenRepository;
|
||||
import ovh.herisson.Clyde.Repositories.UserRepository;
|
||||
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
|
||||
import ovh.herisson.Clyde.Services.TokenService;
|
||||
import ovh.herisson.Clyde.Tables.Role;
|
||||
import ovh.herisson.Clyde.Tables.Token;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import static io.restassured.RestAssured.with;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@Testcontainers
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class UserControllerTest {
|
||||
@LocalServerPort
|
||||
private Integer port;
|
||||
|
||||
@Autowired
|
||||
private ClydeApplication controller;
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private TokenRepository tokenRepository;
|
||||
@Container
|
||||
@ServiceConnection
|
||||
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:alpine")
|
||||
.withDatabaseName("clyde")
|
||||
.withUsername("devel")
|
||||
.withPassword("devel")
|
||||
.withCreateContainerCmdModifier(cmd -> cmd.withHostConfig(new HostConfig().withPortBindings(new PortBinding(Ports.Binding.bindPort(5432), new ExposedPort(5432)))));
|
||||
|
||||
@BeforeAll
|
||||
static void beforeAll(){
|
||||
postgres.start();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterAll(){
|
||||
postgres.stop();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void setup(){
|
||||
RestAssured.baseURI = "http://localhost:" + port;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void aftereach(){
|
||||
tokenRepository.deleteAll();
|
||||
userRepository.deleteAll();
|
||||
}
|
||||
@Test
|
||||
//Verifie qu'un user qui n'a pas les permissions admin ou secretaire ne peut pas post
|
||||
public void userPostTest(){
|
||||
User god = new User("god","god","admin@admin.com","everywhere","every",new Date(0), null, Role.Admin,"goddoesntneedpassword");
|
||||
Token godToken = new Token(god, tokenService.generateNewToken(), new Date());
|
||||
userRepository.save(god);
|
||||
tokenService.saveToken(godToken);
|
||||
|
||||
//Can god post herobrine himself ?
|
||||
User herobrine = new User("brine","hero","herobrine@admin.com","in your WalLs","ShadowsLand",new Date(0), null,Role.Student,"test");
|
||||
|
||||
with().body(herobrine).contentType(ContentType.JSON).header("Authorization", godToken.getToken()).when().request("POST", "/user").then().statusCode(201);
|
||||
|
||||
userRepository.delete(herobrine);
|
||||
|
||||
//Can noob post herobrine without authorizations (no)
|
||||
User noob = new User("boon","noob","noob@admintkt.com","everywhere","every",new Date(0), null, Role.Student,"noob");
|
||||
Token noobToken = new Token(noob, tokenService.generateNewToken(), new Date());
|
||||
userRepository.save(noob);
|
||||
tokenService.saveToken(noobToken);
|
||||
|
||||
with().body(herobrine).contentType(ContentType.JSON).header("Authorization", noobToken.getToken()).when().request("POST", "/user").then().statusCode(401);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userGetTest(){
|
||||
User herobrine = new User("brine","hero","herobrine@admin.com","in your WalLs","ShadowsLand",new Date(0), null,Role.Student,"test");
|
||||
userRepository.save(herobrine);
|
||||
|
||||
Token t = new Token(herobrine, tokenService.generateNewToken(), new Date());
|
||||
tokenRepository.save(t);
|
||||
|
||||
|
||||
with().header("Authorization", t.getToken()).when().request("GET", "/user").then().assertThat().statusCode(200).body("firstName",equalTo("hero"));
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +1,12 @@
|
||||
package ovh.herisson.Clyde.Repositories;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import ovh.herisson.Clyde.Repositories.UserRepository;
|
||||
import ovh.herisson.Clyde.Tables.Role;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
@ -24,12 +24,14 @@ public class UserRepoTest {
|
||||
|
||||
@BeforeEach
|
||||
public void setup(){
|
||||
if (userRepo.findById(1) == null){
|
||||
User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand", new GregorianCalendar(2005, 4, 3).getTime(), null, Role.Admin,"admin");
|
||||
userRepo.save(herobrine);
|
||||
}
|
||||
User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand", new GregorianCalendar(2005, 4, 3).getTime(), null, Role.Admin,"admin");
|
||||
userRepo.save(herobrine);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void clean(){
|
||||
userRepo.deleteAll();
|
||||
}
|
||||
@Test
|
||||
public void usertest(){
|
||||
Assert.assertEquals("brine", userRepo.findById(1).getLastName());
|
||||
|
@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Services;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -39,6 +40,10 @@ public class StorageServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void aftereach(){
|
||||
fileRepo.deleteAll();
|
||||
}
|
||||
@Test
|
||||
//Check si le fichier est bien sauvegardé dans la DB et si le fichier est bien sauvegardé au bon endroit
|
||||
public void saveFile(){
|
||||
|
@ -2,6 +2,7 @@ package ovh.herisson.Clyde.Services;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -35,6 +36,12 @@ class TokenServiceTest {
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
|
||||
@AfterEach
|
||||
public void aftereach(){
|
||||
tokenRepository.deleteAll();
|
||||
userRepository.deleteAll();
|
||||
}
|
||||
@Test
|
||||
void saveToken() {
|
||||
User herobrine = userRepository.findById(1);
|
||||
@ -52,6 +59,7 @@ class TokenServiceTest {
|
||||
Assert.assertNotEquals(herobrinenul, tok.getUser());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void saveTokenLimit(){
|
||||
TokenService tokenService = new TokenService(tokenRepository);
|
||||
|
@ -1,3 +1,3 @@
|
||||
spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
|
||||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
|
||||
|
Loading…
Reference in New Issue
Block a user