Skip to content
Permalink
Browse files

[JENKINS-22834] Added tests for classpaths handling in the scriptAppr…

…oval page.
  • Loading branch information...
ikedam committed Aug 3, 2014
1 parent e2b1cb9 commit c2841deb05927bdd31aafabd0350c29031a6ecbc
@@ -263,7 +263,7 @@ public String getHash() {
private final String path;
private final String hash;

PendingClasspath(@Nonnull String path, @Nonnull String hash, @Nonnull ApprovalContext context) {
PendingClasspath(@Nonnull String hash, @Nonnull String path, @Nonnull ApprovalContext context) {
super(context);
/**
* hash should be stored as files located at the classpath can be modified.
@@ -502,7 +502,7 @@ public synchronized void configureingClasspath(@Nonnull String path, @Nonnull Ap
addApprovedClasspath(new ApprovedClasspath(hash, path));
shouldSave = true;
} else {
if (addPendingClasspath(new PendingClasspath(path, hash, context))) {
if (addPendingClasspath(new PendingClasspath(hash, path, context))) {
LOG.info(String.format("%s (%s) is pended.", path, hash));
shouldSave = true;
}
@@ -556,7 +556,7 @@ public synchronized void checkClasspathApproved(@Nonnull String path) throws IOE
// Never approve classpath here.
ApprovalContext context = ApprovalContext.create();
context = context.withCurrentUser().withItemAsKey(currentExecutingItem());
if (addPendingClasspath(new PendingClasspath(path, hash, context))) {
if (addPendingClasspath(new PendingClasspath(hash, path, context))) {
LOG.info(String.format("%s (%s) is pended.", path, hash));
try {
save();
@@ -81,19 +81,19 @@ THE SOFTWARE.
/*
Create a list like:
<p id="pcp-${pcp.hash}">
<button onclick="approveClasspath('${pcp.hash}', '${pcp.path}')">Approve</button> /
<button onclick="denyClasspath('${pcp.hash}', '${pcp.path}')">Deny</button>
<button class="approve" onclick="approveClasspath('${pcp.hash}', '${pcp.path}')">Approve</button> /
<button class="deny" onclick="denyClasspath('${pcp.hash}', '${pcp.path}')">Deny</button>
${pcp.hash} (${pcp.path})
</p>
*/
pendingClasspaths.each(function(e) {
var block = new Element('p', { 'id': 'pcp-' + e.hash });
var approveButton = new Element('button', { 'hash': e.hash, 'path': e.path });
var approveButton = new Element('button', { 'class': 'approve', 'hash': e.hash, 'path': e.path });
approveButton.insert('Approve');
approveButton.observe('click', function() {
approveClasspath(this.readAttribute('hash'), this.readAttribute('path'));
});
var denyButton = new Element('button', { 'hash': e.hash, 'path': e.path });
var denyButton = new Element('button', { 'class': 'deny', 'hash': e.hash, 'path': e.path });
denyButton.insert('Deny');
denyButton.observe('click', function() {
denyClasspath(this.readAttribute('hash'), this.readAttribute('path'));
@@ -120,13 +120,13 @@ THE SOFTWARE.
/*
Create a list like:
<p id="acp-${acp.hash}">
<button onclick="denyApprovedClasspath('${pcp.hash}')">Delete</button>
<button class="delete" onclick="denyApprovedClasspath('${pcp.hash}')">Delete</button>
${acp.hash} (${acp.path})
</p>
*/
approvedClasspaths.each(function(e) {
var block = new Element('p', { 'id': 'acp-' + e.hash });
var deleteButton = new Element('button', { 'hash': e.hash, 'path': e.path });
var deleteButton = new Element('button', { 'class': 'delete', 'hash': e.hash, 'path': e.path });
deleteButton.insert('Delete');
deleteButton.observe('click', function() {
if (confirm('Really delete this approved classpath? Any existing scripts using this classpath will need to be rerun and classpath reapproved.')) {
@@ -29,6 +29,9 @@
import static org.junit.Assert.*;
import org.junit.Rule;
import org.jvnet.hudson.test.JenkinsRule;
import com.gargoylesoftware.htmlunit.ConfirmHandler;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class ScriptApprovalTest {

@@ -52,5 +55,108 @@ private void configureAndUse(String groovy) {
ScriptApproval.get().configuring(groovy, GroovyLanguage.get(), ApprovalContext.create());
assertEquals(groovy, ScriptApproval.get().using(groovy, GroovyLanguage.get()));
}

@Test public void testApproveClasspaths() throws Exception {
final String CLASSPATH_HASH1 = "0000000000000000000000000000000000000000";
final String CLASSPATH_PATH1 = "/path/to/some/jar1.jar";
final String CLASSPATH_HASH2 = "1234567890abcdef1234567890abcdef12345678";
final String CLASSPATH_PATH2 = "/path/to/some/classpath2";
final String CLASSPATH_HASH3 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
final String CLASSPATH_PATH3 = "/path/to/some/jar3.jar";
final String CLASSPATH_HASH4 = "abcdef1234567890abcdef1234567890abcdef12";
final String CLASSPATH_PATH4 = "/path/to/some/classpath4";
final String CLASSPATH_HASH5 = "9999999999999999999999999999999999999999";
final String CLASSPATH_PATH5 = "/path/to/some/jar5.jar";

ApprovalContext context = ApprovalContext.create();

ScriptApproval.get().addApprovedClasspath(new ScriptApproval.ApprovedClasspath(CLASSPATH_HASH1, CLASSPATH_PATH1));
ScriptApproval.get().addApprovedClasspath(new ScriptApproval.ApprovedClasspath(CLASSPATH_HASH2, CLASSPATH_PATH2));
ScriptApproval.get().addPendingClasspath(new ScriptApproval.PendingClasspath(CLASSPATH_HASH3, CLASSPATH_PATH3, context));
ScriptApproval.get().addPendingClasspath(new ScriptApproval.PendingClasspath(CLASSPATH_HASH4, CLASSPATH_PATH4, context));
ScriptApproval.get().addPendingClasspath(new ScriptApproval.PendingClasspath(CLASSPATH_HASH5, CLASSPATH_PATH5, context));

JenkinsRule.WebClient wc = r.createWebClient();

// click "OK" for all confirms.
wc.setConfirmHandler(new ConfirmHandler() {
public boolean handleConfirm(Page page, String message) {
return true;
}
});

HtmlPage page = wc.goTo(ScriptApproval.get().getUrlName());

assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH1)));
assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH2)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH3)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH4)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH5)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH1)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH2)));
assertNotNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH3)));
assertNotNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH4)));
assertNotNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH5)));

// approve a classpath
page.getElementById(String.format("pcp-%s", CLASSPATH_HASH3))
.getElementsByAttribute("button", "class", "approve").get(0).click();

assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH1)));
assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH2)));
assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH3)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH4)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH5)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH1)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH2)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH3)));
assertNotNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH4)));
assertNotNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH5)));

// deny a classpath
page.getElementById(String.format("pcp-%s", CLASSPATH_HASH4))
.getElementsByAttribute("button", "class", "deny").get(0).click();

assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH1)));
assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH2)));
assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH3)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH4)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH5)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH1)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH2)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH3)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH4)));
assertNotNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH5)));

// delete a classpath
page.getElementById(String.format("acp-%s", CLASSPATH_HASH1))
.getElementsByAttribute("button", "class", "delete").get(0).click();

assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH1)));
assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH2)));
assertNotNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH3)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH4)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH5)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH1)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH2)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH3)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH4)));
assertNotNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH5)));

// clear all classpaths
page.getElementById("approvedClasspaths-clear")
.getElementsByTagName("button").get(0).click();

assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH1)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH2)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH3)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH4)));
assertNull(page.getElementById(String.format("acp-%s", CLASSPATH_HASH5)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH1)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH2)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH3)));
assertNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH4)));
assertNotNull(page.getElementById(String.format("pcp-%s", CLASSPATH_HASH5)));
}

}

0 comments on commit c2841de

Please sign in to comment.
You can’t perform that action at this time.