From e28d7b507399e97ecd4c4c1fb7503be6da5890d7 Mon Sep 17 00:00:00 2001 From: Okke Harsta Date: Thu, 16 Nov 2023 11:11:34 +0100 Subject: [PATCH] Test validity --- .../java/oidc/secure/ResourceCleanerTest.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/test/java/oidc/secure/ResourceCleanerTest.java b/src/test/java/oidc/secure/ResourceCleanerTest.java index 88bb1e07..e03e9de5 100644 --- a/src/test/java/oidc/secure/ResourceCleanerTest.java +++ b/src/test/java/oidc/secure/ResourceCleanerTest.java @@ -2,6 +2,7 @@ import oidc.AbstractIntegrationTest; import oidc.SeedUtils; +import oidc.endpoints.OidcEndpoint; import oidc.model.AccessToken; import oidc.model.AuthenticationRequest; import oidc.model.AuthorizationCode; @@ -9,6 +10,7 @@ import oidc.model.RefreshToken; import oidc.model.User; import oidc.model.UserConsent; +import oidc.repository.AccessTokenRepository; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -20,14 +22,12 @@ import java.time.LocalDateTime; import java.time.ZoneId; import java.time.temporal.ChronoUnit; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.UUID; +import java.util.*; import java.util.stream.Stream; import static java.util.Collections.emptyList; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { @@ -36,11 +36,14 @@ "cron.node-cron-job-responsible=true" }) @SuppressWarnings("unchecked") -public class ResourceCleanerTest extends AbstractIntegrationTest implements SeedUtils { +public class ResourceCleanerTest extends AbstractIntegrationTest implements SeedUtils, OidcEndpoint { @Autowired private ResourceCleaner subject; + @Autowired + private AccessTokenRepository accessTokenRepository; + @Test public void clean() throws URISyntaxException { Class[] classes = {User.class, UserConsent.class, AccessToken.class, RefreshToken.class, AuthorizationCode.class, AuthenticationRequest.class}; @@ -62,6 +65,26 @@ public void clean() throws URISyntaxException { Stream.of(classes).forEach(clazz -> assertEquals(0, mongoTemplate.findAll(clazz).size())); } + @Test + public void cleanEagerNegative() { + String jwtId = UUID.randomUUID().toString(); + coCleanEager(jwtId, -5, false); + } + + @Test + public void cleanEagerPositive() { + String jwtId = UUID.randomUUID().toString(); + coCleanEager(jwtId, 15, true); + } + + private void coCleanEager(String jwtId, int validity, boolean present) { + AccessToken accessToken = accessToken(jwtId, tokenValidity(validity)); + accessTokenRepository.insert(accessToken); + subject.clean(); + Optional optionalAccessToken = accessTokenRepository.findByJwtId(jwtId); + assertEquals(present, optionalAccessToken.isPresent()); + } + private UserConsent userConsent() { UserConsent userConsent = new UserConsent(new User("sub", "unspecifiedNameId", "http://mockidp", "clientId", Collections.emptyMap(), Collections.emptyList()), Arrays.asList("openid", "profile"), new OpenIDClient());