Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rooije committed May 17, 2024
1 parent 4c6ab9e commit cddc9bf
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import org.keycloak.authentication.RequiredActionContext;
import org.keycloak.models.UserModel;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import static nl.deltares.keycloak.mocking.TestUtils.getRequiredActionContext;
import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -15,7 +16,7 @@
public class LoginStatsRecordingRequiredActionProviderTest {

@Test
public void testEvaluateTriggers(){
public void testEvaluateTriggers() throws ParseException {

LoginStatsRecordingRequiredActionProvider provider = new LoginStatsRecordingRequiredActionProvider();
RequiredActionContext context = getRequiredActionContext("evaluateTriggers");
Expand All @@ -25,22 +26,25 @@ public void testEvaluateTriggers(){

UserModel user = context.getUser();

long startTime = LocalDateTime.now(ZoneId.of("GMT")).toEpochSecond(ZoneOffset.UTC);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
long startTime = System.currentTimeMillis();

final String clientId = context.getAuthenticationSession().getClient().getClientId();
String firstLoginDate = user.getFirstAttribute(LoginStatsRecordingRequiredActionProvider.LOGIN_FIRST_LOGIN_DATE + '.' + clientId);
assertNotNull(firstLoginDate);
LocalDateTime dateTime = LocalDateTime.parse(firstLoginDate);
long timeStamp = dateTime.toEpochSecond(ZoneOffset.UTC);
assertEquals(startTime, timeStamp, 10);
Date dateTime = dateFormat.parse(firstLoginDate);
long timeStamp = dateTime.getTime();
assertEquals(startTime, timeStamp, 5000);

String loginCount = user.getFirstAttribute(LoginStatsRecordingRequiredActionProvider.LOGIN_LOGIN_COUNT + '.' + clientId);
assertNotNull(loginCount);
assertEquals(1, Integer.parseInt(loginCount));

String recentLoginDate = user.getFirstAttribute(LoginStatsRecordingRequiredActionProvider.LOGIN_RECENT_LOGIN_DATE + '.' + clientId);
assertNotNull(recentLoginDate);
timeStamp = dateTime.toEpochSecond(ZoneOffset.UTC);
dateTime = dateFormat.parse(recentLoginDate);
timeStamp = dateTime.getTime();
assertEquals(startTime, timeStamp, 10);
}

Expand Down

0 comments on commit cddc9bf

Please sign in to comment.