Skip to content

Commit

Permalink
Change Keycloak logout test to support multichunk cookies (quarkus-qe…
Browse files Browse the repository at this point in the history
  • Loading branch information
mocenas authored Jan 8, 2024
1 parent 4307176 commit 0ef1ac5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

Expand Down Expand Up @@ -53,11 +53,11 @@ public void singlePageAppLogoutFlow() throws IOException {
page = form.getInputByName("login").click();

assertEquals("alice, cache size: 0", page.getBody().asNormalizedText());
assertNotNull(getSessionCookie(webClient, "code-flow"));
assertTrue(isCodeFlowCookiePresent(webClient));

page = webClient.getPage(app.getURI(Protocol.HTTP).toString() + "/code-flow/logout");
assertThat(page.getBody().asNormalizedText(), containsString("You are logged out"));
assertNull(getSessionCookie(webClient, "code-flow"));
assertFalse(isCodeFlowCookiePresent(webClient));
// Clear the post logout cookie
webClient.getCookieManager().clearCookies();
}
Expand All @@ -69,7 +69,19 @@ private WebClient createWebClient() {
return webClient;
}

private Cookie getSessionCookie(WebClient webClient, String tenantId) {
return webClient.getCookieManager().getCookie("q_session" + (tenantId == null ? "" : "_" + tenantId));
/**
* Search for "q_session_code-flow" cookie.
* This might be one cookie, or it might be chunked into several ones,
* which are named "q_session_code-flow_chunk_1" etc.
*
* @return True if cookie is present, chunked or not. False otherwise.
*/
private boolean isCodeFlowCookiePresent(WebClient webClient) {
for (Cookie cookie : webClient.getCookieManager().getCookies()) {
if (cookie.getName().startsWith("q_session_code-flow")) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import static io.quarkus.test.bootstrap.KeycloakService.DEFAULT_REALM_BASE_PATH;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
Expand Down Expand Up @@ -60,11 +59,11 @@ public void singlePageAppLogoutFlow() throws IOException {
.getContentAsString();

assertThat(content, containsString("alice, cache size: 0"));
assertNotNull(getSessionCookie(webClient, null));
assertTrue(isSessionCookiePresent(webClient));

HtmlPage page = webClient.getPage(app.getURI(Protocol.HTTP).toString() + "/code-flow/logout");
assertThat(page.getBody().asNormalizedText(), containsString("You are logged out"));
assertNull(getSessionCookie(webClient, "code-flow"));
assertFalse(isSessionCookiePresent(webClient));

// double-check session is not valid anymore
boolean isLoginPage = isRedirectedToLoginPage(webClient, "/code-flow/authenticated");
Expand Down Expand Up @@ -96,7 +95,19 @@ private WebClient createWebClient() {
return webClient;
}

private Cookie getSessionCookie(WebClient webClient, String tenantId) {
return webClient.getCookieManager().getCookie("q_session" + (tenantId == null ? "" : "_" + tenantId));
/**
* Search for "q_session" cookie.
* This might be one cookie, or it might be chunked into several ones,
* which are named "q_session_chunk_1" etc.
*
* @return True if cookie is present, chunked or not. False otherwise.
*/
private boolean isSessionCookiePresent(WebClient webClient) {
for (Cookie cookie : webClient.getCookieManager().getCookies()) {
if (cookie.getName().startsWith("q_session")) {
return true;
}
}
return false;
}
}

0 comments on commit 0ef1ac5

Please sign in to comment.