QQQ-27: fixed fragile test

This commit is contained in:
Tim Chamberlain
2022-07-21 11:24:08 -05:00
parent 7a9a83a348
commit 960b316d99
2 changed files with 21 additions and 38 deletions

View File

@ -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;

View File

@ -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<String, String> 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.");
}