From c8b7930a8e7540eb5da33a45acd63e9045a05328 Mon Sep 17 00:00:00 2001 From: LeoMoulin Date: Fri, 15 Mar 2024 11:20:23 +0100 Subject: [PATCH] Setup unit tests and implements test for the token limit and trivial tests for userRepository --- .../herisson/Clyde/Services/UserService.java | 1 - .../src/main/resources/application.properties | 1 + .../herisson/Clyde/ClydeApplicationTests.java | 1 + .../Clyde/Repositories/UserRepoTest.java | 39 +++++++++ .../Clyde/Services/TokenServiceTest.java | 85 +++++++++++++++++++ .../src/test/resources/application.properties | 3 + 6 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java create mode 100644 backend/src/test/java/ovh/herisson/Clyde/Services/TokenServiceTest.java create mode 100644 backend/src/test/resources/application.properties diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java index 55a2f92..097dbaa 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/UserService.java @@ -16,7 +16,6 @@ public class UserService { this.userRepo = userRepo; } - public User getUser(String identifier){ if (identifier == null) return null; try { diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index df288a6..5d00d8e 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1,2 +1,3 @@ spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect +spring.sql.init.mode=always \ No newline at end of file diff --git a/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java b/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java index 4714cbc..860e110 100644 --- a/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java +++ b/backend/src/test/java/ovh/herisson/Clyde/ClydeApplicationTests.java @@ -8,6 +8,7 @@ class ClydeApplicationTests { @Test void contextLoads() { + } } diff --git a/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java b/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java new file mode 100644 index 0000000..64ba33e --- /dev/null +++ b/backend/src/test/java/ovh/herisson/Clyde/Repositories/UserRepoTest.java @@ -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())); + } + +} diff --git a/backend/src/test/java/ovh/herisson/Clyde/Services/TokenServiceTest.java b/backend/src/test/java/ovh/herisson/Clyde/Services/TokenServiceTest.java new file mode 100644 index 0000000..cdb26f9 --- /dev/null +++ b/backend/src/test/java/ovh/herisson/Clyde/Services/TokenServiceTest.java @@ -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 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 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 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)); + } + } +} \ No newline at end of file diff --git a/backend/src/test/resources/application.properties b/backend/src/test/resources/application.properties new file mode 100644 index 0000000..e20ff1a --- /dev/null +++ b/backend/src/test/resources/application.properties @@ -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 \ No newline at end of file