From d423a57fa0e760af930e3a00fb470143dcc7e7ab Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Sun, 17 Mar 2024 15:40:14 +0100 Subject: [PATCH] Add tests for UserController get and post --- backend/build.gradle.kts | 3 + .../Clyde/EndPoints/UserController.java | 2 +- .../Clyde/Endpoints/UserControllerTest.java | 114 ++++++++++++++++++ .../Clyde/Repositories/UserRepoTest.java | 12 +- .../Clyde/Services/StorageServiceTest.java | 5 + .../Clyde/Services/TokenServiceTest.java | 8 ++ .../src/test/resources/application.properties | 2 +- 7 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 backend/src/test/java/ovh/herisson/Clyde/Endpoints/UserControllerTest.java diff --git a/backend/build.gradle.kts b/backend/build.gradle.kts index 6557b82..b7cabcd 100644 --- a/backend/build.gradle.kts +++ b/backend/build.gradle.kts @@ -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) } diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java index 844e82e..59412c1 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/UserController.java @@ -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; } } diff --git a/backend/src/test/java/ovh/herisson/Clyde/Endpoints/UserControllerTest.java b/backend/src/test/java/ovh/herisson/Clyde/Endpoints/UserControllerTest.java new file mode 100644 index 0000000..aebb474 --- /dev/null +++ b/backend/src/test/java/ovh/herisson/Clyde/Endpoints/UserControllerTest.java @@ -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")); + } + +} diff --git a/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java b/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java index 64ba33e..f99ff2f 100644 --- a/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java +++ b/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java @@ -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()); diff --git a/backend/src/test/java/ovh/herisson/Clyde/Services/StorageServiceTest.java b/backend/src/test/java/ovh/herisson/Clyde/Services/StorageServiceTest.java index b44454b..317ed91 100644 --- a/backend/src/test/java/ovh/herisson/Clyde/Services/StorageServiceTest.java +++ b/backend/src/test/java/ovh/herisson/Clyde/Services/StorageServiceTest.java @@ -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(){ diff --git a/backend/src/test/java/ovh/herisson/Clyde/Services/TokenServiceTest.java b/backend/src/test/java/ovh/herisson/Clyde/Services/TokenServiceTest.java index cdb26f9..c0314ea 100644 --- a/backend/src/test/java/ovh/herisson/Clyde/Services/TokenServiceTest.java +++ b/backend/src/test/java/ovh/herisson/Clyde/Services/TokenServiceTest.java @@ -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); diff --git a/backend/src/test/resources/application.properties b/backend/src/test/resources/application.properties index e20ff1a..5500350 100644 --- a/backend/src/test/resources/application.properties +++ b/backend/src/test/resources/application.properties @@ -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 \ No newline at end of file +spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect