Skip to content

Commit

Permalink
chore(agama): add test cases for agama engine (#2691)
Browse files Browse the repository at this point in the history
* chore: refactor test cases #2672

* chore: minor updates #2672
  • Loading branch information
jgomer2001 authored Oct 21, 2022
1 parent 415e7c5 commit 993c47b
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 29 deletions.
31 changes: 23 additions & 8 deletions agama/engine/src/test/java/io/jans/agama/test/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
Expand Down Expand Up @@ -49,7 +50,10 @@ public class BaseTest {
WebClientOptions options = client.getOptions();

options.setThrowExceptionOnFailingStatusCode(false);
//prevents the finish page to autosubmit the POST to AS's postlogin endpoint
//skip downloading of external resources to avoid time waste
options.setCssEnabled(false);
options.setDownloadImages(false);
//prevent the finish page to autosubmit the POST to AS's postlogin endpoint
options.setJavaScriptEnabled(false);

}
Expand Down Expand Up @@ -130,7 +134,7 @@ void validateFinishPage(HtmlPage page, boolean success) {
String title = page.getTitleText().toLowerCase();

if (success) {
assertTrue(title.contains("redirect"), "'redirect' word not found in page title");
assertTextContained(title, true, "redirect");

List<HtmlForm> forms = page.getForms();
assertEquals(forms.size(), 1, "Page should have one and only one form");
Expand All @@ -140,12 +144,8 @@ void validateFinishPage(HtmlPage page, boolean success) {
assertEquals(form.getMethodAttribute().toLowerCase(), "post", "Form does not use POST");

} else {

assertTrue(title.contains("error"), "'error' word not found in page title");

String text = page.getVisibleText().toLowerCase();
assertTrue(text.contains("authentication"), "'authentication' word not found in page text");
assertTrue(text.contains("failed"), "'failed' word not found in page text");
assertTextContained(title, true, "error");
assertTextContained(page.getVisibleText().toLowerCase(), true, "authentication", "failed");
}

}
Expand All @@ -158,6 +158,21 @@ void assertServerError(Page page) {
assertEquals(page.getWebResponse().getStatusCode(), WebResponse.INTERNAL_SERVER_ERROR);
}

void assertTextContained(String text, String ...words) {
assertTextContained(text, false, words);
}

void assertTextContained(String text, boolean includeMessageInAssert, String ...words) {

List<String> list = Arrays.asList(words);
if (includeMessageInAssert) {
list.forEach(w -> assertTrue(text.contains(w), String.format("'%s' word not found in page text", w)));
} else {
list.forEach(w -> assertTrue(text.contains(w)));
}

}

<P extends Page> P doClick(DomElement el) {

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

import static org.testng.Assert.*;

@org.testng.annotations.Ignore
public class CustomConfigsFlowTest extends BaseTest {

private static final String QNAME = "io.jans.agama.test.showConfig";

@Test
public void withTimeout() {

//Waiting 10 seconds is enough due to time skew (see FlowService#ensureTimeNotExceeded)
HtmlPage page = launchAndWait(10);
//Flow should have timed out now - see flow impl
//The page currently shown may correspond to the Agama timeout template or to
Expand All @@ -27,13 +27,10 @@ public void withTimeout() {

if (status == WebResponse.OK) {
//See timeout.ftlh
assertTrue(text.contains("took"));
assertTrue(text.contains("more"));
assertTrue(text.contains("expected"));
assertTextContained(text, "took", "more", "expected");
} else if (status == WebResponse.NOT_FOUND) {
//See mismatch.ftlh
assertTrue(text.contains("not"));
assertTrue(text.contains("found"));
assertTextContained(text, "not", "found");
} else {
fail("Unexpected status code " + status);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.testng.Assert.*;

@org.testng.annotations.Ignore
public class SaySomething2FlowTest extends BaseTest {

private static final String QNAME = "org.gluu.flow1";
Expand All @@ -40,7 +39,7 @@ private HtmlPage run(String text) {
page = doClick(button);

assertOK(page);
assertTrue(page.getVisibleText().contains("Gluu")); //see f1/*.ftl
assertTextContained(page.getVisibleText(), "Gluu"); //see f1/*.ftl

HtmlForm form = page.getForms().get(0);
if (text != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.testng.Assert.*;

@org.testng.annotations.Ignore
public class SaySomething3FlowTest extends BaseTest {

private static final String QNAME = "org.gluu.flow3";
Expand All @@ -28,7 +27,7 @@ public void test() {
page = doClick(button);

assertOK(page);
assertTrue(page.getVisibleText().contains("Agama")); //see me/myindex.ftl and f1/index2.ftl
assertTextContained(page.getVisibleText(), "Agama"); //see me/myindex.ftl and f1/index2.ftl

button = page.getForms().get(0).getInputByValue("Continue");
page = doClick(button);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.testng.Assert.*;

@org.testng.annotations.Ignore
public class SaySomethingFlowTest extends BaseTest {

private static final String QNAME = "org.gluu.flow2";
Expand Down Expand Up @@ -42,7 +41,7 @@ private HtmlPage run(String name, String text) {
boolean nameEmpty = name == null;
HtmlPage page = launch(QNAME, nameEmpty ? null : Collections.singletonMap("val", name));

if (!nameEmpty) assertTrue(page.getVisibleText().contains(name));
if (!nameEmpty) assertTextContained(page.getVisibleText(), name);

HtmlForm form = page.getForms().get(0);

Expand Down
44 changes: 40 additions & 4 deletions agama/engine/src/test/java/io/jans/agama/test/UidOnlyAuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static org.testng.Assert.*;

@org.testng.annotations.Ignore
public class UidOnlyAuthTest extends BaseTest {

private static final String QNAME = "io.jans.agama.test.auth.uidOnly";
Expand All @@ -26,17 +25,54 @@ public void enableJS() {

@Test
public void randUid() {
HtmlPage page = run("" + Math.random());
validateFinishPage(page, false);

HtmlPage page = start("" + Math.random());
page = proceed(page);
assertOK(page);

assertTrue(page.getUrl().toString().endsWith("error.htm"));
assertTextContained(page.getVisibleText().toLowerCase(), "failed", "authenticate");

}

@Test(dependsOnMethods = "randUid", alwaysRun = true)
@Parameters("redirectUri")
public void adminUid(String redirectUri) {

HtmlPage page = start("admin");
page = proceed(page);
assertOK(page);

if (!page.getUrl().toString().startsWith(redirectUri)) {
//check if this is the consent page
assertTextContained(page.getVisibleText().toLowerCase(), "permission", "allow");
}

}

private HtmlPage run(String uid) {
private HtmlPage start(String uid) {

HtmlPage page = launch(QNAME, Collections.singletonMap("uid", uid));
//click on the "Continue" button
HtmlSubmitInput button = page.getForms().get(0).getInputByValue("Continue");
return doClick(button);

}

private HtmlPage proceed(HtmlPage page) {

try {
//wait for the auto-submitting javascript to execute (see finished.ftlh) and redirections to take place
Thread.sleep(2000);
page = (HtmlPage) client.getCurrentWindow().getEnclosedPage();

logger.debug("Landed at {}",page.getUrl());
return page;
} catch (Exception e) {
fail(e.getMessage(), e);
}
return null;

}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Flow io.jans.agama.test.showConfig
Basepath ""
Timeout 10 seconds
Timeout 15 seconds
Configs conf

RRF "printConfigs.ftlh" conf
RRF "custom/printConfigs.ftlh" conf

Finish false
Finish false
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<body>

<#!-- rendering this page crashes if neither joke nor phrase are defined -->
<#-- rendering this page crashes if neither joke nor phrase are defined -->
<p>${joke}
<p>${phrase}

Expand Down

0 comments on commit 993c47b

Please sign in to comment.