Leo/Backend/UnitTest #138
@ -16,7 +16,6 @@ public class UserService {
|
||||
this.userRepo = userRepo;
|
||||
}
|
||||
|
||||
|
||||
public User getUser(String identifier){
|
||||
if (identifier == null) return null;
|
||||
try {
|
||||
|
@ -1,2 +1,3 @@
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
|
||||
spring.sql.init.mode=always
|
@ -8,6 +8,7 @@ class ClydeApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,39 @@
|
||||
package ovh.herisson.Clyde.Repositories;
|
||||
|
||||
import org.junit.Assert;
|
||||
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;
|
||||
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
@DataJpaTest
|
||||
@TestPropertySource(properties = {
|
||||
"spring.test.database.replace=none",
|
||||
"spring.datasource.url=jdbc:tc:postgresql:16-alpine:///db"
|
||||
})
|
||||
public class UserRepoTest {
|
||||
|
||||
@Autowired
|
||||
UserRepository userRepo;
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usertest(){
|
||||
Assert.assertEquals("brine", userRepo.findById(1).getLastName());
|
||||
Assert.assertTrue(new GregorianCalendar(2005, 4, 3).getTime().equals(userRepo.findById(1).getBirthDate()));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package ovh.herisson.Clyde.Services;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
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.TokenRepository;
|
||||
import ovh.herisson.Clyde.Repositories.UserRepository;
|
||||
import ovh.herisson.Clyde.Tables.Role;
|
||||
import ovh.herisson.Clyde.Tables.Token;
|
||||
import ovh.herisson.Clyde.Tables.User;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@DataJpaTest
|
||||
@TestPropertySource(properties = {
|
||||
"spring.test.database.replace=none",
|
||||
"spring.datasource.url=jdbc:tc:postgresql:16-alpine:///db"
|
||||
})
|
||||
|
||||
class TokenServiceTest {
|
||||
|
||||
@Autowired
|
||||
TokenRepository tokenRepository;
|
||||
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
void saveToken() {
|
||||
User herobrine = userRepository.findById(1);
|
||||
|
||||
User herobrinenul = new User("brine","heronul","pasadmin@student.com","in your WalLs","ShadowsLand", new GregorianCalendar(2005, 4, 3).getTime(), null, Role.Admin,"admin");
|
||||
|
||||
TokenService tokenService = new TokenService(tokenRepository);
|
||||
Token testToken = new Token(herobrine, tokenService.generateNewToken(), new Date());
|
||||
tokenService.saveToken(testToken);
|
||||
|
||||
Iterable<Token> t = tokenService.getAllTokens();
|
||||
Token tok = t.iterator().next();
|
||||
|
||||
Assert.assertEquals(herobrine, tok.getUser());
|
||||
Assert.assertNotEquals(herobrinenul, tok.getUser());
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveTokenLimit(){
|
||||
TokenService tokenService = new TokenService(tokenRepository);
|
||||
|
||||
//On va stocker les token qu'on va sauvegarder au préalable dans une liste pour tester que les tokens remplacés sont bien ceux avec la date d'expi la plus jeune
|
||||
//A la fin il ne devrait donc rester que les 5 derniers tokens de tokenList
|
||||
|
||||
ArrayList<Token> tokenList = new ArrayList<>();
|
||||
GregorianCalendar gc = new GregorianCalendar();
|
||||
|
||||
User malveillant = new User("mechant", "veutdestoken", "donnezmoidestoken@mail.com", "secret", "secret", null, null, null, "secret");
|
||||
userRepository.save(malveillant);
|
||||
|
||||
for (int i = 0; i < 20; i++){
|
||||
gc.add(Calendar.DAY_OF_WEEK, 1);
|
||||
|
||||
Token t = new Token(malveillant, tokenService.generateNewToken(), gc.getTime());
|
||||
tokenList.add(t);
|
||||
|
||||
tokenService.saveToken(t);
|
||||
}
|
||||
|
||||
//Testons les tokens
|
||||
ArrayList <Token> resp = tokenRepository.getByUserOrderByExpirationDate(malveillant);
|
||||
Assert.assertTrue(resp.size() == 5);
|
||||
|
||||
for (int i = 1; i <= resp.size(); i++){
|
||||
Assert.assertEquals(tokenList.get(tokenList.size()-i), resp.get(resp.size()-i));
|
||||
}
|
||||
}
|
||||
}
|
3
backend/src/test/resources/application.properties
Normal file
3
backend/src/test/resources/application.properties
Normal file
@ -0,0 +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
|
Loading…
Reference in New Issue
Block a user