From 960b316d993b66625b48a2cb32f440f3da817b33 Mon Sep 17 00:00:00 2001 From: Tim Chamberlain Date: Thu, 21 Jul 2022 11:24:08 -0500 Subject: [PATCH] QQQ-27: fixed fragile test --- .../Auth0AuthenticationModule.java | 32 ++----------------- .../Auth0AuthenticationModuleTest.java | 27 ++++++++++------ 2 files changed, 21 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModule.java b/src/main/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModule.java index db644312..0bf8d5f8 100644 --- a/src/main/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModule.java +++ b/src/main/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModule.java @@ -113,7 +113,7 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface /////////////////////////////////////////////////////////////////// StateProviderInterface spi = getStateProvider(); Auth0StateKey key = new Auth0StateKey(qSession.getIdReference()); - spi.put(key, getNow()); + spi.put(key, Instant.now()); return (qSession); } @@ -174,7 +174,7 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface // - so this is basically saying, if the time between the last time we checked the token and // // right now is more than ID_TOKEN_VALIDATION_INTERVAL_SECTIONS, then session needs revalidated // /////////////////////////////////////////////////////////////////////////////////////////////////// - return (Duration.between(lastTimeChecked, getNow()).compareTo(Duration.ofSeconds(ID_TOKEN_VALIDATION_INTERVAL_SECONDS)) < 0); + return (Duration.between(lastTimeChecked, Instant.now()).compareTo(Duration.ofSeconds(ID_TOKEN_VALIDATION_INTERVAL_SECONDS)) < 0); } return (false); @@ -182,32 +182,6 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface - /******************************************************************************* - ** public method so that 'now' can be used for testing purposes - ** - defaults to real 'now' - *******************************************************************************/ - public Instant getNow() - { - if(now == null) - { - now = Instant.now(); - } - - return (now); - } - - - - /******************************************************************************* - ** public method so that 'now' can be set for testing purposes - *******************************************************************************/ - public void setNow(Instant now) - { - this.now = now; - } - - - /******************************************************************************* ** makes request to check if a token is still valid and build new qSession if it is ** @@ -283,7 +257,7 @@ public class Auth0AuthenticationModule implements QAuthenticationModuleInterface /******************************************************************************* ** *******************************************************************************/ - private static class Auth0StateKey extends AbstractStateKey + public static class Auth0StateKey extends AbstractStateKey { private final String key; diff --git a/src/test/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModuleTest.java b/src/test/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModuleTest.java index 80e10109..99cbb6b6 100644 --- a/src/test/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModuleTest.java +++ b/src/test/java/com/kingsrook/qqq/backend/core/modules/authentication/Auth0AuthenticationModuleTest.java @@ -30,6 +30,8 @@ import com.kingsrook.qqq.backend.core.model.metadata.QInstance; import com.kingsrook.qqq.backend.core.model.session.QSession; import com.kingsrook.qqq.backend.core.modules.authentication.metadata.Auth0AuthenticationMetaData; import com.kingsrook.qqq.backend.core.modules.authentication.metadata.QAuthenticationMetaData; +import com.kingsrook.qqq.backend.core.state.InMemoryStateProvider; +import com.kingsrook.qqq.backend.core.state.StateProviderInterface; import com.kingsrook.qqq.backend.core.utils.TestUtils; import org.junit.jupiter.api.Test; import static com.kingsrook.qqq.backend.core.modules.authentication.Auth0AuthenticationModule.AUTH0_ID_TOKEN_KEY; @@ -61,21 +63,28 @@ public class Auth0AuthenticationModuleTest ** *******************************************************************************/ @Test - public void testValidToken() throws QAuthenticationException + public void testLastTimeChecked() throws QAuthenticationException { - Map context = new HashMap<>(); - context.put(AUTH0_ID_TOKEN_KEY, VALID_TOKEN); - ////////////////////////////////////////////////////////// // Tuesday, July 19, 2022 12:40:27.299 PM GMT-05:00 DST // ////////////////////////////////////////////////////////// - Instant now = Instant.ofEpochMilli(1658252427299L); + Instant now = Instant.now(); + + ///////////////////////////////////////////////////////// + // put the 'now' from the past into the state provider // + ///////////////////////////////////////////////////////// + StateProviderInterface spi = InMemoryStateProvider.getInstance(); + Auth0AuthenticationModule.Auth0StateKey key = new Auth0AuthenticationModule.Auth0StateKey(VALID_TOKEN); + spi.put(key, now); + + ////////////////////// + // build up session // + ////////////////////// + QSession session = new QSession(); + session.setIdReference(VALID_TOKEN); Auth0AuthenticationModule auth0AuthenticationModule = new Auth0AuthenticationModule(); - auth0AuthenticationModule.setNow(now); - QSession session = auth0AuthenticationModule.createSession(getQInstance(), context); - assertEquals("tim.chamberlain@kingsrook.com", session.getUser().getIdReference(), "Id should be Tim's email."); - assertEquals("Tim Chamberlain", session.getUser().getFullName(), "Full name should be Tim's full name (well without the middle name)."); + assertEquals(true, auth0AuthenticationModule.isSessionValid(session), "Session should return as still valid."); }