Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import org.junit.jupiter.api.Test;

import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;
import static uk.gov.di.orchestration.shared.helpers.PersistentIdHelper.isValidPersistentSessionCookieWithDoubleDashedTimestamp;

class PersistentIdHelperTest {
private static final String ARBITRARY_UNIX_TIMESTAMP = "1700558480962";
private static final String NEW_ID = "lML1nhHXgGC9o-7efoVoFBJGve0";
private static final String PERSISTENT_SESSION_ID_COOKIE_VALUE =
IdGenerator.generate() + "--" + ARBITRARY_UNIX_TIMESTAMP;

Expand Down Expand Up @@ -131,13 +131,19 @@ void shouldAppendTimestampToPersistentIdWhenMissing() {

@Test
void shouldGenerateNewPersistentIdFromGetExistingOrCreateWhenMissing() {
String cookieString =
"Version=1; gs=session-id.456;cookies_preferences_set={\"analytics\":true};name=ts";
Map<String, String> inputHeaders = Map.of(CookieHelper.REQUEST_COOKIE_HEADER, cookieString);
String persistentId =
PersistentIdHelper.getExistingOrCreateNewPersistentSessionId(inputHeaders);
try (var mockIdGenerator = mockStatic(IdGenerator.class)) {
mockIdGenerator.when(IdGenerator::generate).thenReturn(NEW_ID);

String cookieString =
"Version=1; gs=session-id.456;cookies_preferences_set={\"analytics\":true};name=ts";
Map<String, String> inputHeaders =
Map.of(CookieHelper.REQUEST_COOKIE_HEADER, cookieString);

String persistentId =
PersistentIdHelper.getExistingOrCreateNewPersistentSessionId(inputHeaders);

assertThat(persistentId, not(equalTo("a-persistent-id")));
assertThat(Base64.getUrlDecoder().decode(persistentId.split("--")[0]).length, equalTo(20));
assertTrue(isValidPersistentSessionCookieWithDoubleDashedTimestamp(persistentId));
assertTrue(persistentId.startsWith(NEW_ID));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

import org.junit.jupiter.api.Test;

import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;
import static uk.gov.di.authentication.shared.helpers.PersistentIdHelper.isValidPersistentSessionCookieWithDoubleDashedTimestamp;

class PersistentIdHelperTest {
private static final String ARBITRARY_UNIX_TIMESTAMP = "1700558480962";
private static final String NEW_ID = "lML1nhHXgGC9o-7efoVoFBJGve0";
private static final String PERSISTENT_SESSION_ID_COOKIE_VALUE =
IdGenerator.generate() + "--" + ARBITRARY_UNIX_TIMESTAMP;

Expand Down Expand Up @@ -131,13 +131,19 @@ void shouldAppendTimestampToPersistentIdWhenMissing() {

@Test
void shouldGenerateNewPersistentIdFromGetExistingOrCreateWhenMissing() {
String cookieString =
"Version=1; gs=session-id.456;cookies_preferences_set={\"analytics\":true};name=ts";
Map<String, String> inputHeaders = Map.of(CookieHelper.REQUEST_COOKIE_HEADER, cookieString);
String persistentId =
PersistentIdHelper.getExistingOrCreateNewPersistentSessionId(inputHeaders);
try (var mockIdGenerator = mockStatic(IdGenerator.class)) {
mockIdGenerator.when(IdGenerator::generate).thenReturn(NEW_ID);

String cookieString =
"Version=1; gs=session-id.456;cookies_preferences_set={\"analytics\":true};name=ts";
Map<String, String> inputHeaders =
Map.of(CookieHelper.REQUEST_COOKIE_HEADER, cookieString);

String persistentId =
PersistentIdHelper.getExistingOrCreateNewPersistentSessionId(inputHeaders);

assertThat(persistentId, not(equalTo("a-persistent-id")));
assertThat(Base64.getUrlDecoder().decode(persistentId.split("--")[0]).length, equalTo(20));
assertTrue(isValidPersistentSessionCookieWithDoubleDashedTimestamp(persistentId));
assertTrue(persistentId.startsWith(NEW_ID));
}
}
}
Loading