Skip to content

Commit

Permalink
test: enable tests and minor refactoring #2672
Browse files Browse the repository at this point in the history
  • Loading branch information
jgomer2001 committed Oct 28, 2022
1 parent d001704 commit 74dedc5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 51 deletions.
27 changes: 16 additions & 11 deletions agama/engine/src/test/java/io/jans/agama/test/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@

import static java.nio.charset.StandardCharsets.UTF_8;

//import org.testng.annotations.Test;
import static org.testng.Assert.*;

public class BaseTest {

private static String AGAMA_ACR = "agama";
private Map<String, String> map = null;
private static Map<String, String> MAP = null;

Logger logger = LogManager.getLogger(getClass());
WebClient client = null;
Expand All @@ -65,23 +64,23 @@ public void init(ITestContext context) throws IOException {
Properties prop = new Properties();
prop.load(Files.newBufferedReader(Paths.get(propertiesFile), UTF_8));

map = new Hashtable<>();
MAP = new Hashtable<>();
//do not bother about empty keys... but
//If a value is found null, this will throw a NPE since we are using a Hashtable
prop.forEach((key, value) -> map.put(key.toString(), value.toString()));
context.getSuite().getXmlSuite().setParameters(map);
prop.forEach((key, value) -> MAP.put(key.toString(), value.toString()));
context.getSuite().getXmlSuite().setParameters(MAP);

}

String authzRequestUrl(String flowQName, Map<String, Object> inputs) {

OAuthParams p = new OAuthParams();
p.setAuthzEndpoint(map.get("authzEndpoint"));
p.setClientId(map.get("clientId"));
p.setRedirectUri(map.get("redirectUri"));
p.setAuthzEndpoint(MAP.get("authzEndpoint"));
p.setClientId(MAP.get("clientId"));
p.setRedirectUri(MAP.get("redirectUri"));
p.setScopes(Collections.singletonList("openid"));

String queryParam = URLEncoder.encode(map.get("custParamName"), UTF_8);
String queryParam = URLEncoder.encode(MAP.get("custParamName"), UTF_8);

StringBuilder builder = new StringBuilder(flowQName);
if (inputs != null) {
Expand All @@ -108,6 +107,10 @@ String authzRequestUrl(String flowQName, Map<String, Object> inputs) {
}

HtmlPage launch(String flowQName, Map<String, Object> parameters) {
return launch(flowQName, parameters, true);
}

HtmlPage launch(String flowQName, Map<String, Object> parameters, boolean ensureOKStatus) {

//Generate an authn request and launch it in the htmlUnit browser
String url = authzRequestUrl(flowQName, parameters);
Expand All @@ -116,8 +119,10 @@ HtmlPage launch(String flowQName, Map<String, Object> parameters) {
Page p = client.getPage(url);

//Check it is an ok web page
assertTrue(p.isHtmlPage(), "Not an html page");
assertOK(p);
assertTrue(p.isHtmlPage(), "Not an html page");
if (ensureOKStatus) {
assertOK(p);
}
return (HtmlPage) p;

} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MathFlowTest extends BaseTest {

@Test
public void runEmpty() {
HtmlPage page = launch(QNAME, Collections.singletonMap("numbers",Collections.emptyList()));
HtmlPage page = launch(QNAME, Collections.singletonMap("numbers",Collections.emptyList()), false);
logger.info("Landed at {}", page.getUrl());
assertServerError(page);
}
Expand Down
38 changes: 8 additions & 30 deletions agama/engine/src/test/java/io/jans/agama/test/UidOnlyAuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.util.Collections;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

Expand All @@ -15,18 +14,11 @@
public class UidOnlyAuthTest extends BaseTest {

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

@BeforeClass
public void enableJS() {
client.getOptions().setJavaScriptEnabled(true);
}


@Test
public void randUid() {

start("" + Math.random());
HtmlPage page = (HtmlPage) currentPageAfter(2000);

HtmlPage page = (HtmlPage) start("" + Math.random());
assertOK(page);
assertTrue(page.getUrl().toString().endsWith("error.htm"));
assertTextContained(page.getVisibleText().toLowerCase(), "failed", "authenticate");
Expand All @@ -37,36 +29,22 @@ public void randUid() {
@Parameters("redirectUri")
public void adminUid(String redirectUri) {

start("admin");
Page page = currentPageAfter(2000);

Page page = start("admin");
assertOK(page);
assertTrue(page.getUrl().toString().startsWith(redirectUri));

}

private HtmlPage start(String uid) {
private Page 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 Page currentPageAfter(long wait) {

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

logger.debug("Landed at {}", p.getUrl());
return p;
} catch (Exception e) {
fail(e.getMessage(), e);
}
return null;
page = (HtmlPage) doClick(button);
//click on the "Continue" button (at finish template)
button = page.getForms().get(0).getInputByValue("Continue");
return doClick(button);

}

Expand Down
14 changes: 7 additions & 7 deletions agama/engine/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@

<parameter name="propertiesFile" value="target/test-classes/testng.properties" />

<test name="math" enabled="false">
<test name="math" enabled="true">
<classes>
<class name="io.jans.agama.test.MathFlowTest" />
</classes>
</test>

<test name="uid" enabled="false">
<test name="uid" enabled="true">
<classes>
<class name="io.jans.agama.test.UidOnlyAuthTest" />
</classes>
</test>

<test name="say-something" enabled="false">
<test name="say-something" enabled="true">
<classes>
<class name="io.jans.agama.test.SaySomethingFlowTest" />
</classes>
</test>

<test name="say-something-2" enabled="false">
<test name="say-something-2" enabled="true">
<classes>
<class name="io.jans.agama.test.SaySomething2FlowTest" />
</classes>
</test>

<test name="say-something-3" enabled="false">
<test name="say-something-3" enabled="true">
<classes>
<class name="io.jans.agama.test.SaySomething3FlowTest" />
</classes>
</test>

<test name="custom-config" enabled="false">
<test name="custom-config" enabled="true">
<classes>
<class name="io.jans.agama.test.CustomConfigsFlowTest" />
</classes>
</test>

<test name="inexistent" enabled="false">
<test name="inexistent" enabled="true">
<classes>
<class name="io.jans.agama.test.InexistentFlowTest" />
</classes>
Expand Down
5 changes: 3 additions & 2 deletions agama/transpiler/src/test/resources/pass/triggers_calls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ When pepperoni is forgotten
o."-x" = Trigger oh.no some params.here

Trigger $bah.humbug no params
Override templates "pea/body.ftl" "" "pea/media.ftl" "fluff.ftl"
"caravan.ftl" "../whoops.ftlh"
Override templates "pea/body.ftl" "" "pea/media.ftl" "fluff.ftl"

"caravan.ftl" "../whoops.ftlh"

x256 = Call java.lang.Math#incrementExact 255

Expand Down

0 comments on commit 74dedc5

Please sign in to comment.