Skip to content

Commit 7405519

Browse files
committed
Add test for report with embedded script
1 parent 3eb40a0 commit 7405519

11 files changed

Lines changed: 124 additions & 63 deletions

File tree

data/reports/knitr_no_scriptpad.rhtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,11 @@ end.rcode-->
9393
<p>Well, everything seems to be working. Let's ask R what is the
9494
value of &pi;? Of course it is <!--rinline pi -->.</p>
9595

96+
<span>Nonce check: <span id="nonce-check-result">FAIL</span></span>
97+
98+
<script>
99+
document.getElementById('nonce-check-result').innerText = "SUCCESS";
100+
</script>
101+
96102
</body>
97103
</html>

data/reports/knitr_no_scriptpad.rmd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ library(knitr)
113113
knit('knitr-minimal.Rmd')
114114
```
115115

116+
<span>Nonce check: <span id="nonce-check-result">FAIL</span></span>
117+
118+
<script>
119+
document.getElementById('nonce-check-result').innerText = "SUCCESS";
120+
</script>
121+
116122
## Conclusion
117123

118124
Markdown is super easy to write. Go to **knitr** [homepage](http://yihui.name/knitr) for details.

data/reports/nonce_check.rhtml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Test script nonce in Knitr HTML</title>
5+
</head>
6+
<body>
7+
8+
<span>Nonce check: <span id="nonce-check-result">FAIL</span></span>
9+
10+
<script>
11+
document.getElementById('nonce-check-result').innerText = "SUCCESS";
12+
</script>
13+
14+
</body>
15+
</html>

data/reports/plotly.rmd

Lines changed: 0 additions & 12 deletions
This file was deleted.

modules/scriptpad/resources/reports/schemas/script_rhtml.rhtml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,11 @@ end.rcode-->
8787
<p>Well, everything seems to be working. Let's ask R what is the
8888
value of &pi;? Of course it is <!--rinline pi -->.</p>
8989

90+
<span>Nonce check: <span id="nonce-check-result">FAIL</span></span>
91+
92+
<script>
93+
document.getElementById('nonce-check-result').innerText = "SUCCESS";
94+
</script>
95+
9096
</body>
9197
</html>

modules/scriptpad/resources/reports/schemas/script_rmd.rmd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ library(knitr)
108108
knit('knitr-minimal.Rmd')
109109
```
110110

111+
<span>Nonce check: <span id="nonce-check-result">FAIL</span></span>
112+
113+
<script>
114+
document.getElementById('nonce-check-result').innerText = "SUCCESS";
115+
</script>
116+
111117
## Conclusion
112118

113119
Markdown is super easy to write. Go to **knitr** [homepage](http://yihui.name/knitr) for details.

src/org/labkey/test/pages/wiki/EditPage.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,48 @@ public EditPage setTitle(String title)
100100

101101
public EditPage setBody(String body)
102102
{
103+
switchWikiToSourceView();
103104
elementCache().bodyTextArea.set(body);
104105
return this;
105106
}
106107

108+
/**
109+
* Switches the wiki edit page to source view when the format type is HTML.
110+
*/
111+
public void switchWikiToSourceView()
112+
{
113+
String curFormat = executeScript("return LABKEY._wiki.getProps().rendererType;", String.class);
114+
if (curFormat.equalsIgnoreCase("HTML"))
115+
{
116+
if (isElementPresent(Locator.css("#wiki-tab-source.labkey-tab-inactive")))
117+
{
118+
Locator tab = Locator.css("#wiki-tab-source > a");
119+
waitForElementToBeVisible(tab);
120+
click(tab);
121+
waitForElement(Locator.css("#wiki-tab-source.labkey-tab-active"));
122+
}
123+
}
124+
}
125+
126+
public void switchWikiToVisualView()
127+
{
128+
String curFormat = (String) executeScript("return LABKEY._wiki.getProps().rendererType;");
129+
if (curFormat.equalsIgnoreCase("HTML"))
130+
{
131+
if (isElementPresent(Locator.css("#wiki-tab-visual.labkey-tab-inactive")))
132+
{
133+
Locator tab = Locator.css("#wiki-tab-visual > a");
134+
waitForElementToBeVisible(tab);
135+
click(tab);
136+
137+
Locator yesButton = Locator.tagWithText("span","Yes");
138+
waitForElementToBeVisible(yesButton);
139+
waitAndClick(yesButton);
140+
waitForElement(Locator.css("#wiki-tab-visual.labkey-tab-active"));
141+
}
142+
}
143+
}
144+
107145
public EditPage setShouldIndex(boolean shouldIndex)
108146
{
109147
elementCache().shouldIndexCheckbox.set(shouldIndex);

src/org/labkey/test/tests/AbstractKnitrReportTest.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
import org.labkey.test.pages.core.admin.logger.ManagerPage;
2828
import org.labkey.test.pages.reports.ManageViewsPage;
2929
import org.labkey.test.util.CodeMirrorHelper;
30+
import org.labkey.test.util.CspLogUtil;
3031
import org.labkey.test.util.Log4jUtils;
3132
import org.labkey.test.util.LogMethod;
3233
import org.labkey.test.util.LoggedParam;
3334
import org.labkey.test.util.PortalHelper;
3435
import org.labkey.test.util.RReportHelper;
36+
import org.labkey.test.util.WikiHelper;
3537
import org.labkey.test.util.core.admin.CspConfigHelper;
3638
import org.openqa.selenium.WebElement;
3739

@@ -55,7 +57,9 @@ public abstract class AbstractKnitrReportTest extends BaseWebDriverTest
5557
protected static final Path rmdReport_no_scriptpad = TestFileUtils.getSampleData("reports/knitr_no_scriptpad.rmd").toPath();
5658
private static final Path rhtmlReport = scriptpadReports.resolve("script_rhtml.rhtml");
5759
private static final Path rhtmlReport_no_scriptpad = TestFileUtils.getSampleData("reports/knitr_no_scriptpad.rhtml").toPath();
58-
protected static final Path rmdReport_embedded_script = TestFileUtils.getSampleData("reports/plotly.rmd").toPath();
60+
private static final Path rhtmlNonceCheck = TestFileUtils.getSampleData("reports/nonce_check.rhtml").toPath();
61+
private static final Locator.XPathLocator nonceCheckLoc = Locator.id("nonce-check-result");
62+
private static final Locator.XPathLocator nonceCheckSuccessLoc = nonceCheckLoc.withText("SUCCESS");
5963

6064
protected final RReportHelper _rReportHelper = new RReportHelper(this);
6165

@@ -170,7 +174,9 @@ protected void htmlFormat()
170174
Locator.tag("pre").containing("## \"1\",249318596,\"2008-05-17\",86,36,129,76,64"),
171175
Locator.tag("pre").withText("## knitr says hello to HTML!"),
172176
Locator.tag("pre").startsWith("## Error").containing(": non-numeric argument to binary operator"),
173-
Locator.tag("p").startsWith("Well, everything seems to be working. Let's ask R what is the value of \u03C0? Of course it is 3.141")};
177+
Locator.tag("p").startsWith("Well, everything seems to be working. Let's ask R what is the value of \u03C0? Of course it is 3.141"),
178+
nonceCheckSuccessLoc // Inline script should run
179+
};
174180
String[] reportNotContains = {"<html>", // Uninterpreted html
175181
"<!--", // ditto
176182
"A minimal knitr example in HTML", // report title element
@@ -199,7 +205,8 @@ protected void markdownV2()
199205
Locator.tag("h2").withText("R code chunks"),
200206
Locator.tag("code").containing("set.seed(123)"), // Echoed R code
201207
Locator.css("p").containing("2 x pi = 6.283"),
202-
Locator.tag("sup").withText("write") //should not contain the hat markdown v2 closing tag
208+
Locator.tag("sup").withText("write"), //should not contain the hat markdown v2 closing tag
209+
nonceCheckSuccessLoc // Inline script should run
203210
};
204211

205212
String[] reportNotContains = {"```", // Markdown for R code chunks
@@ -233,13 +240,36 @@ protected void moduleReportDependencies()
233240
* Issue 53211: CSP reports when an R/Plotly graph is displayed in Reports web part, same thing wrapped in a wiki works fine with strict csp
234241
*/
235242
@Test
236-
public void reportEmbeddedScript()
243+
public void testEmbeddedReportNonce()
237244
{
238-
Locator[] reportContains = {};
239-
240-
String[] reportNotContains = {};
241-
242-
createAndVerifyKnitrReport(rmdReport_embedded_script, RReportHelper.ReportOption.knitrMarkdown, reportContains,
243-
reportNotContains, true);
245+
String name = "rhtml nonce check";
246+
String success = "SUCCESS";
247+
Locator[] reportContains = {nonceCheckSuccessLoc};
248+
249+
createAndVerifyKnitrReport(rhtmlNonceCheck, RReportHelper.ReportOption.knitrHtml, reportContains,
250+
null, true, name);
251+
CspLogUtil.checkNewCspWarnings(getArtifactCollector());
252+
253+
log("Create wiki with embedded report");
254+
new WikiHelper(this).createNewWikiPage()
255+
.setName(name)
256+
.setBody("""
257+
${labkey.webPart(partName='Report',
258+
reportName='%s',
259+
showFrame='false'
260+
)}
261+
""".formatted(name))
262+
.saveAndClose();
263+
clickAndWait(Locator.linkWithText(name));
264+
assertEquals("Nonce check result", success, getText(nonceCheckLoc));
265+
CspLogUtil.checkNewCspWarnings(getArtifactCollector());
266+
267+
log("Add report webpart");
268+
new PortalHelper(this).doInAdminMode(ph -> {
269+
ph.addTab(name); // Use a separate tab to ensure report isn't run accidentally
270+
ph.addReportWebPart(name);
271+
assertEquals("Nonce check result", success, getText(nonceCheckLoc));
272+
CspLogUtil.checkNewCspWarnings(getArtifactCollector());
273+
});
244274
}
245275
}

src/org/labkey/test/tests/TabTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private void doTabManagementTests()
6666
navigateToFolder(getProjectName(), FOLDER_NAME);
6767

6868
// Move tabs
69-
portalHelper.enableTabEditMode();
69+
portalHelper.enterAdminMode();
7070
portalHelper.moveTab("Tab 1", PortalHelper.Direction.LEFT); // Nothing should happen.
7171
portalHelper.moveTab("Tab 1", PortalHelper.Direction.RIGHT);
7272
List<PortalTab> tabs = PortalTab.findTabs(getDriver());

src/org/labkey/test/util/PortalHelper.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,6 @@ public WebDriver getWrappedDriver()
6262
return _driverWrapper.getWrappedDriver();
6363
}
6464

65-
public void enableTabEditMode()
66-
{
67-
new SiteNavBar(getDriver()).enterPageAdminMode();
68-
}
69-
70-
public void disableTabEditMode()
71-
{
72-
new SiteNavBar(getDriver()).exitPageAdminMode();
73-
}
74-
7565
public PortalTab activateTab(String tabText)
7666
{
7767
tabText = tabText.trim();
@@ -124,18 +114,18 @@ public void hideTab(@LoggedParam String tabText)
124114
{
125115
PortalTab.find(tabText, getDriver()).hide();
126116

127-
disableTabEditMode();
117+
exitAdminMode();
128118
assertElementNotPresent(Locator.xpath("//div[@class='lk-nav-tabs-ct']//ul//li//a[text()='" + tabText +"']"));
129-
enableTabEditMode();
119+
enterAdminMode();
130120
}
131121

132122
@LogMethod(quiet = true)
133123
public void showTab(@LoggedParam String tabText)
134124
{
135125
PortalTab.find(tabText, getDriver()).show();
136-
disableTabEditMode();
126+
exitAdminMode();
137127
assertElementVisible(Locator.xpath("//div[@class='lk-nav-tabs-ct']//ul//li//a[contains(text(),'" + tabText +"')]"));
138-
enableTabEditMode();
128+
enterAdminMode();
139129
}
140130

141131
@LogMethod(quiet = true)

0 commit comments

Comments
 (0)