1
0
forked from PGL/Clyde

Fix unit tests

This commit is contained in:
LeoMoulin 2024-03-21 14:59:50 +01:00
parent 79f4b84b70
commit 9328601d2d
3 changed files with 10 additions and 5 deletions

View File

@ -28,7 +28,6 @@ public class RegNoGenerator implements IdentifierGenerator {
Calendar c = new GregorianCalendar(); Calendar c = new GregorianCalendar();
if(set.next()){ if(set.next()){
resp = set.getLong(1)+((c.get(Calendar.YEAR)%1000)*10000); resp = set.getLong(1)+((c.get(Calendar.YEAR)%1000)*10000);
System.out.println(resp);
} }
conn.close(); conn.close();

View File

@ -23,6 +23,7 @@ import ovh.herisson.Clyde.Repositories.TokenRepository;
import ovh.herisson.Clyde.Repositories.UserRepository; import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse; import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.TokenService; import ovh.herisson.Clyde.Services.TokenService;
import ovh.herisson.Clyde.Services.UserService;
import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
@ -47,6 +48,8 @@ public class UserControllerTest {
@Autowired @Autowired
private TokenService tokenService; private TokenService tokenService;
@Autowired
private UserService userService;
@Autowired @Autowired
private UserRepository userRepository; private UserRepository userRepository;
@Autowired @Autowired
@ -79,12 +82,13 @@ public class UserControllerTest {
tokenRepository.deleteAll(); tokenRepository.deleteAll();
userRepository.deleteAll(); userRepository.deleteAll();
} }
@Test @Test
//Verifie qu'un user qui n'a pas les permissions admin ou secretaire ne peut pas post //Verifie qu'un user qui n'a pas les permissions admin ou secretaire ne peut pas post
public void userPostTest(){ public void userPostTest(){
User god = new User("god","god","admin@admin.com","everywhere","every",new Date(0), null, Role.Admin,"goddoesntneedpassword"); 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()); Token godToken = new Token(god, tokenService.generateNewToken(), new Date());
userRepository.save(god); userService.save(god);
tokenService.saveToken(godToken); tokenService.saveToken(godToken);
//Can god post herobrine himself ? //Can god post herobrine himself ?
@ -97,7 +101,7 @@ public class UserControllerTest {
//Can noob post herobrine without authorizations (no) //Can noob post herobrine without authorizations (no)
User noob = new User("boon","noob","noob@student.com","everywhere","every",new Date(0), null, Role.Student,"noob"); User noob = new User("boon","noob","noob@student.com","everywhere","every",new Date(0), null, Role.Student,"noob");
Token noobToken = new Token(noob, tokenService.generateNewToken(), new Date()); Token noobToken = new Token(noob, tokenService.generateNewToken(), new Date());
userRepository.save(noob); userService.save(noob);
tokenService.saveToken(noobToken); tokenService.saveToken(noobToken);
with().body(herobrine).contentType(ContentType.JSON).header("Authorization", noobToken.getToken()).when().request("POST", "/user").then().statusCode(401); with().body(herobrine).contentType(ContentType.JSON).header("Authorization", noobToken.getToken()).when().request("POST", "/user").then().statusCode(401);

View File

@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
import ovh.herisson.Clyde.Services.UserService;
import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User; import ovh.herisson.Clyde.Tables.User;
@ -24,6 +25,7 @@ public class UserRepoTest {
@BeforeEach @BeforeEach
public void setup(){ public void setup(){
userRepo.deleteAll();
User herobrine = new User("brine","hero","admin@admin.com","in your WalLs","ShadowsLand", new GregorianCalendar(2005, 4, 3).getTime(), null, Role.Admin,"admin"); 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); userRepo.save(herobrine);
} }
@ -34,8 +36,8 @@ public class UserRepoTest {
} }
@Test @Test
public void usertest(){ public void usertest(){
Assert.assertEquals("brine", userRepo.findById(1).getLastName()); Assert.assertEquals("brine", userRepo.findByEmail("admin@admin.com").getLastName());
Assert.assertTrue(new GregorianCalendar(2005, 4, 3).getTime().equals(userRepo.findById(1).getBirthDate())); Assert.assertTrue(new GregorianCalendar(2005, 4, 3).getTime().equals(userRepo.findByEmail("admin@admin.com").getBirthDate()));
} }
} }