Compare commits
3 Commits
008af10d0e
...
37a9eb46ae
Author | SHA1 | Date | |
---|---|---|---|
37a9eb46ae | |||
bfc4f6567b | |||
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)
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
package ovh.herisson.Clyde.EndPoints;
|
||||
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
|
||||
@ -10,7 +13,7 @@ import ovh.herisson.Clyde.Services.UserService;
|
||||
import ovh.herisson.Clyde.Tables.Role;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
import java.security.Key;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -40,7 +43,7 @@ public class UserController {
|
||||
@PostMapping("/user")
|
||||
public ResponseEntity<String> postUser(@RequestBody User user,@RequestHeader("Authorization") String authorization){
|
||||
|
||||
if (authServ.isNotSecretaryOrAdmin(authorization))
|
||||
if (!isSecretaryOrAdmin(authorization))
|
||||
return new UnauthorizedResponse<>(null);
|
||||
|
||||
userService.save(user);
|
||||
@ -50,7 +53,7 @@ public class UserController {
|
||||
@GetMapping("/users")
|
||||
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllUsers(@RequestHeader("Authorization") String authorization){
|
||||
|
||||
if (authServ.isNotSecretaryOrAdmin(authorization))
|
||||
if (!isSecretaryOrAdmin(authorization))
|
||||
return new UnauthorizedResponse<>(null);
|
||||
|
||||
Iterable<User> users = userService.getAll();
|
||||
@ -75,21 +78,6 @@ public class UserController {
|
||||
return new ResponseEntity<>("data modified", HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/teachers")
|
||||
public ResponseEntity<Iterable<HashMap<String,Object>>> getAllTeachers(@RequestHeader("Authorization") String token){
|
||||
if (authServ.getUserFromToken(token) == null)
|
||||
return new UnauthorizedResponse<>(null);
|
||||
Iterable<User> teachers = userService.getAllTeachers();
|
||||
ArrayList<HashMap<String, Object>> withoutPassword = new ArrayList<>();
|
||||
|
||||
for (User t: teachers){
|
||||
withoutPassword.add(userWithoutPassword(t));
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(withoutPassword, HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** return user's data except password
|
||||
* @param user the user to return
|
||||
@ -97,6 +85,7 @@ public class UserController {
|
||||
*/
|
||||
private HashMap<String,Object> userWithoutPassword(User user){
|
||||
HashMap<String,Object> toReturn = new HashMap<>();
|
||||
|
||||
toReturn.put("regNo",user.getRegNo());
|
||||
toReturn.put("firstName",user.getFirstName());
|
||||
toReturn.put("lastName",user.getLastName());
|
||||
@ -104,7 +93,18 @@ public class UserController {
|
||||
toReturn.put("country",user.getCountry());
|
||||
toReturn.put("address",user.getAddress());
|
||||
toReturn.put("role",user.getRole());
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private boolean isSecretaryOrAdmin(String authorization){
|
||||
if (authorization ==null)
|
||||
return false;
|
||||
|
||||
User poster = authServ.getUserFromToken(authorization);
|
||||
if (poster == null) return false;
|
||||
|
||||
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…
x
Reference in New Issue
Block a user