From 993c47bb104a96278434ac624a0cddf5951ca748 Mon Sep 17 00:00:00 2001 From: Jose Date: Fri, 21 Oct 2022 08:50:02 -0500 Subject: [PATCH] chore(agama): add test cases for agama engine (#2691) * chore: refactor test cases #2672 * chore: minor updates #2672 --- .../java/io/jans/agama/test/BaseTest.java | 31 +++++++++---- .../agama/test/CustomConfigsFlowTest.java | 11 ++--- .../agama/test/SaySomething2FlowTest.java | 3 +- .../agama/test/SaySomething3FlowTest.java | 3 +- .../jans/agama/test/SaySomethingFlowTest.java | 3 +- .../io/jans/agama/test/UidOnlyAuthTest.java | 44 +++++++++++++++++-- .../flows/io.jans.agama.test.showConfig | 6 +-- .../templates/custom/printConfigs.ftlh | 2 +- 8 files changed, 74 insertions(+), 29 deletions(-) diff --git a/agama/engine/src/test/java/io/jans/agama/test/BaseTest.java b/agama/engine/src/test/java/io/jans/agama/test/BaseTest.java index e3fb8e1cb1c..cbfee164568 100644 --- a/agama/engine/src/test/java/io/jans/agama/test/BaseTest.java +++ b/agama/engine/src/test/java/io/jans/agama/test/BaseTest.java @@ -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; @@ -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); } @@ -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 forms = page.getForms(); assertEquals(forms.size(), 1, "Page should have one and only one form"); @@ -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"); } } @@ -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 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 doClick(DomElement el) { try { diff --git a/agama/engine/src/test/java/io/jans/agama/test/CustomConfigsFlowTest.java b/agama/engine/src/test/java/io/jans/agama/test/CustomConfigsFlowTest.java index 6e511e5df9b..83ce021096a 100644 --- a/agama/engine/src/test/java/io/jans/agama/test/CustomConfigsFlowTest.java +++ b/agama/engine/src/test/java/io/jans/agama/test/CustomConfigsFlowTest.java @@ -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 @@ -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); } diff --git a/agama/engine/src/test/java/io/jans/agama/test/SaySomething2FlowTest.java b/agama/engine/src/test/java/io/jans/agama/test/SaySomething2FlowTest.java index 4a8ee169cfa..ca163472546 100644 --- a/agama/engine/src/test/java/io/jans/agama/test/SaySomething2FlowTest.java +++ b/agama/engine/src/test/java/io/jans/agama/test/SaySomething2FlowTest.java @@ -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"; @@ -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) { diff --git a/agama/engine/src/test/java/io/jans/agama/test/SaySomething3FlowTest.java b/agama/engine/src/test/java/io/jans/agama/test/SaySomething3FlowTest.java index 1edc5ec8723..997665dd571 100644 --- a/agama/engine/src/test/java/io/jans/agama/test/SaySomething3FlowTest.java +++ b/agama/engine/src/test/java/io/jans/agama/test/SaySomething3FlowTest.java @@ -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"; @@ -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); diff --git a/agama/engine/src/test/java/io/jans/agama/test/SaySomethingFlowTest.java b/agama/engine/src/test/java/io/jans/agama/test/SaySomethingFlowTest.java index 509a7f9f17a..9a57032b0e7 100644 --- a/agama/engine/src/test/java/io/jans/agama/test/SaySomethingFlowTest.java +++ b/agama/engine/src/test/java/io/jans/agama/test/SaySomethingFlowTest.java @@ -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"; @@ -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); diff --git a/agama/engine/src/test/java/io/jans/agama/test/UidOnlyAuthTest.java b/agama/engine/src/test/java/io/jans/agama/test/UidOnlyAuthTest.java index 0c7720f5afa..ef04eb25d94 100644 --- a/agama/engine/src/test/java/io/jans/agama/test/UidOnlyAuthTest.java +++ b/agama/engine/src/test/java/io/jans/agama/test/UidOnlyAuthTest.java @@ -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"; @@ -26,11 +25,32 @@ 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 @@ -38,5 +58,21 @@ private HtmlPage run(String uid) { 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; + + } } \ No newline at end of file diff --git a/agama/engine/src/test/resources/flows/io.jans.agama.test.showConfig b/agama/engine/src/test/resources/flows/io.jans.agama.test.showConfig index 616d4dcf80c..a0ba0158b2f 100644 --- a/agama/engine/src/test/resources/flows/io.jans.agama.test.showConfig +++ b/agama/engine/src/test/resources/flows/io.jans.agama.test.showConfig @@ -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 \ No newline at end of file +Finish false diff --git a/agama/engine/src/test/resources/templates/custom/printConfigs.ftlh b/agama/engine/src/test/resources/templates/custom/printConfigs.ftlh index 5193bcbe4c9..abcf7512ebf 100644 --- a/agama/engine/src/test/resources/templates/custom/printConfigs.ftlh +++ b/agama/engine/src/test/resources/templates/custom/printConfigs.ftlh @@ -2,7 +2,7 @@ -<#!-- rendering this page crashes if neither joke nor phrase are defined --> +<#-- rendering this page crashes if neither joke nor phrase are defined -->

${joke}

${phrase}