Skip to content

Commit

Permalink
fixed and simplified tests after Tapestry upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
derkoe committed Apr 19, 2024
1 parent b5d8e12 commit 63e39a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package at.porscheinformatik.tapestry.csrfprotection.tests.auto;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import java.util.List;

import org.apache.tapestry5.dom.Document;
import org.apache.tapestry5.dom.Element;
import org.apache.tapestry5.internal.test.TestableResponse;
import org.apache.tapestry5.test.PageTester;
import org.jaxen.JaxenException;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -59,10 +60,9 @@ public void sendRemoveRowLink() throws JaxenException

org.apache.tapestry5.dom.Document doc = tester.renderPage("AjaxFormLoop");

//obtain anti CSRF token from dummy link
//obtain anti CSRF token from dummy link
List<Element> dummyLinkElements = TapestryXPath.xpath("id('dummy')").selectElements(doc);
assertTrue(dummyLinkElements.size() == 1,
"There should be only one dummy link used to extract an anti CSRF token.");
assertEquals(dummyLinkElements.size(), 1, "There should be only one dummy link used to extract an anti CSRF token.");

Element element = dummyLinkElements.get(0);
String token =
Expand All @@ -71,15 +71,15 @@ public void sendRemoveRowLink() throws JaxenException
+ (CsrfConstants.DEFAULT_CSRF_TOKEN_PARAMETER_NAME + "=").length());

List<Element> selectElements = TapestryXPath.xpath("id('removeRowLink')").selectElements(doc);
assertTrue(selectElements.size() == 1, "There should be only one remove row link.");
assertEquals(selectElements.size(), 1, "There should be only one remove row link.");

Element removeRowLink = selectElements.get(0);
String href = removeRowLink.getAttribute("href");
href += "&" + CsrfConstants.DEFAULT_CSRF_TOKEN_PARAMETER_NAME + "=" + token;
removeRowLink.forceAttributes("href", href);
Document response = tester.clickLink(removeRowLink);
TestableResponse response = tester.clickLinkAndReturnResponse(removeRowLink);

assertTrue(response.toString().contains("A component event handler method returned the value {}"),
"AjaxFormLoop component should does not work properly anymore (removeRowLink).");
assertEquals(response.getStatus(), 200);
assertEquals(response.getOutput(), "{}", "AjaxFormLoop component should does not work properly anymore (removeRowLink).");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package at.porscheinformatik.tapestry.csrfprotection.tests.off;

import static at.porscheinformatik.tapestry.csrfprotection.CsrfConstants.DEFAULT_CSRF_TOKEN_PARAMETER_NAME;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.*;
import static org.testng.Assert.assertEquals;

import java.util.List;

import org.apache.tapestry5.dom.Document;
import org.apache.tapestry5.dom.Element;
import org.apache.tapestry5.internal.test.TestableResponse;
import org.apache.tapestry5.test.PageTester;
import org.jaxen.JaxenException;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -38,7 +38,7 @@ public void testForTokenNotPresent()

/**
* Tests if the remove row link is still working.
*
*
* @throws JaxenException .
*/
@Test
Expand All @@ -48,10 +48,9 @@ public void sendRemoveRowLink() throws JaxenException

org.apache.tapestry5.dom.Document doc = tester.renderPage("AjaxFormLoop");

//obtain anti CSRF token from dummy link
//obtain anti CSRF token from dummy link
List<Element> dummyLinkElements = TapestryXPath.xpath("id('dummy')").selectElements(doc);
assertTrue(dummyLinkElements.size() == 1,
"There should be only one dummy link used to extract an anti CSRF token.");
assertEquals(dummyLinkElements.size(), 1, "There should be only one dummy link used to extract an anti CSRF token.");

Element element = dummyLinkElements.get(0);
String token =
Expand All @@ -60,15 +59,15 @@ public void sendRemoveRowLink() throws JaxenException
+ (DEFAULT_CSRF_TOKEN_PARAMETER_NAME + "=").length());

List<Element> selectElements = TapestryXPath.xpath("id('removeRowLink')").selectElements(doc);
assertTrue(selectElements.size() == 1, "There should be only one remove row link.");
assertEquals(selectElements.size(), 1, "There should be only one remove row link.");

Element removeRowLink = selectElements.get(0);
String href = removeRowLink.getAttribute("href");
href += "?" + DEFAULT_CSRF_TOKEN_PARAMETER_NAME + "=" + token;
removeRowLink.attribute("href", href);
Document response = tester.clickLink(removeRowLink);
TestableResponse response = tester.clickLinkAndReturnResponse(removeRowLink);

assertTrue(response.toString().contains("A component event handler method returned the value {}"),
"AjaxFormLoop component should does not work properly anymore (removeRowLink).");
assertEquals(response.getStatus(), 200);
assertEquals(response.getOutput(), "{}", "AjaxFormLoop component should does not work properly anymore (removeRowLink).");
}
}

0 comments on commit 63e39a6

Please sign in to comment.