Compare commits

...

2 Commits

Author SHA1 Message Date
9328601d2d Fix unit tests 2024-03-21 14:59:50 +01:00
79f4b84b70 Customisation of the RegNo 2024-03-21 14:39:17 +01:00
3 changed files with 17 additions and 10 deletions

View File

@ -3,10 +3,11 @@ package ovh.herisson.Clyde;
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class RegNoGenerator implements IdentifierGenerator {
private final UserSaveCounter usv = UserSaveCounter.getInstance();
@ -24,9 +25,9 @@ public class RegNoGenerator implements IdentifierGenerator {
ResultSet set = statement.executeQuery(query);
long resp = 0;
Calendar c = new GregorianCalendar();
if(set.next()){
resp = set.getLong(1)+1000;
System.out.println(resp);
resp = set.getLong(1)+((c.get(Calendar.YEAR)%1000)*10000);
}
conn.close();

View File

@ -23,6 +23,7 @@ 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.Services.UserService;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User;
@ -47,6 +48,8 @@ public class UserControllerTest {
@Autowired
private TokenService tokenService;
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Autowired
@ -79,12 +82,13 @@ public class UserControllerTest {
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);
userService.save(god);
tokenService.saveToken(godToken);
//Can god post herobrine himself ?
@ -97,7 +101,7 @@ public class UserControllerTest {
//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");
Token noobToken = new Token(noob, tokenService.generateNewToken(), new Date());
userRepository.save(noob);
userService.save(noob);
tokenService.saveToken(noobToken);
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.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import ovh.herisson.Clyde.Services.UserService;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.User;
@ -24,6 +25,7 @@ public class UserRepoTest {
@BeforeEach
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");
userRepo.save(herobrine);
}
@ -34,8 +36,8 @@ public class UserRepoTest {
}
@Test
public void usertest(){
Assert.assertEquals("brine", userRepo.findById(1).getLastName());
Assert.assertTrue(new GregorianCalendar(2005, 4, 3).getTime().equals(userRepo.findById(1).getBirthDate()));
Assert.assertEquals("brine", userRepo.findByEmail("admin@admin.com").getLastName());
Assert.assertTrue(new GregorianCalendar(2005, 4, 3).getTime().equals(userRepo.findByEmail("admin@admin.com").getBirthDate()));
}
}