forked from jenkinsci/acceptance-test-harness
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/org/jenkinsci/test/acceptance/junit/CspRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.jenkinsci.test.acceptance.junit; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Injector; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
import org.jenkinsci.test.acceptance.plugins.csp.ContentSecurityPolicyReport; | ||
import org.jenkinsci.test.acceptance.po.Jenkins; | ||
import org.jenkinsci.test.acceptance.update_center.PluginSpec; | ||
import org.junit.rules.TestRule; | ||
import org.junit.runner.Description; | ||
import org.junit.runners.model.Statement; | ||
|
||
@GlobalRule | ||
public final class CspRule implements TestRule { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(CspRule.class.getName()); | ||
|
||
@Inject | ||
Injector injector; | ||
|
||
@Override | ||
public Statement apply(final Statement base, final Description d) { | ||
return new Statement() { | ||
private Jenkins jenkins; | ||
|
||
@Override | ||
public void evaluate() throws Throwable { | ||
jenkins = injector.getInstance(Jenkins.class); | ||
final PluginSpec plugin = new PluginSpec("csp"); | ||
LOGGER.info("Installing plugin for test: " + plugin); | ||
jenkins.getPluginManager().installPlugins(plugin); | ||
// LOGGER.info("Configuring CSP plugin for test"); | ||
// final GlobalSecurityConfig security = new GlobalSecurityConfig(jenkins); | ||
// security.open(); | ||
// security.disableCspReportOnly(); | ||
// security.save(); | ||
try { | ||
base.evaluate(); | ||
} finally { | ||
ContentSecurityPolicyReport csp = new ContentSecurityPolicyReport(jenkins); | ||
csp.open(); | ||
List<String> lines = csp.getReport(); | ||
if (lines.size() > 2) { | ||
throw new AssertionError(String.join("\n", csp.getReport())); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/org/jenkinsci/test/acceptance/plugins/csp/ContentSecurityPolicyReport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.jenkinsci.test.acceptance.plugins.csp; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.jenkinsci.test.acceptance.po.Jenkins; | ||
import org.jenkinsci.test.acceptance.po.PageObject; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.NoSuchElementException; | ||
import org.openqa.selenium.WebElement; | ||
|
||
public class ContentSecurityPolicyReport extends PageObject { | ||
public ContentSecurityPolicyReport(Jenkins context) { | ||
super(context, context.url("content-security-policy-reports/")); | ||
} | ||
|
||
public List<String> getReport() { | ||
List<String> lines = new ArrayList<>(); | ||
WebElement table; | ||
try { | ||
table = find(By.className("bigtable")); | ||
} catch (NoSuchElementException e) { | ||
try { | ||
getContext().getJenkins().logout(); | ||
} catch (Exception ignored) { | ||
// Ignore | ||
} | ||
getContext().getJenkins().login().doLogin("admin"); | ||
table = find(By.className("bigtable")); | ||
} | ||
List<WebElement> headers = table.findElements(By.tagName("th")); | ||
StringBuilder sb = new StringBuilder(); | ||
for (WebElement header : headers) { | ||
sb.append(header.getText()).append("\t"); | ||
} | ||
lines.add(sb.toString()); | ||
sb = new StringBuilder(); | ||
List<WebElement> rows = table.findElements(By.tagName("tr")); | ||
for (WebElement row : rows) { | ||
List<WebElement> cells = row.findElements(By.tagName("td")); | ||
for (WebElement cell : cells) { | ||
sb.append(cell.getText()).append("\t"); | ||
} | ||
lines.add(sb.toString()); | ||
sb = new StringBuilder(); | ||
} | ||
return lines; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters