5151import static org .hamcrest .MatcherAssert .assertThat ;
5252import static org .hamcrest .Matchers .nullValue ;
5353import static org .junit .Assert .assertEquals ;
54+ import static org .junit .Assert .assertFalse ;
55+ import static org .junit .Assert .assertNotNull ;
56+ import static org .junit .Assert .assertNull ;
5457import static org .junit .Assert .assertThrows ;
58+ import static org .junit .Assert .assertTrue ;
5559import static org .junit .Assert .fail ;
5660
5761public class ScriptApprovalTest extends AbstractApprovalTest <ScriptApprovalTest .Script > {
@@ -65,6 +69,41 @@ public class ScriptApprovalTest extends AbstractApprovalTest<ScriptApprovalTest.
6569 private static final String WHITELISTED_SIGNATURE = "method java.lang.String trim" ;
6670 private static final String DANGEROUS_SIGNATURE = "staticMethod hudson.model.User current" ;
6771
72+ private String approveIfExists (String script , ScriptApproval sa ) throws IOException {
73+ for (ScriptApproval .PendingScript pending : sa .getPendingScripts ()) {
74+ if (pending .script .equals (script )) {
75+ String hash = pending .getHash ();
76+ sa .approveScript (hash );
77+ return hash ;
78+ }
79+ }
80+ return null ;
81+ }
82+
83+ @ Test
84+ public void reapprovingShouldFail () throws Exception {
85+ configureSecurity ();
86+ final ScriptApproval sa = ScriptApproval .get ();
87+ FreeStyleProject p = r .createFreeStyleProject ();
88+ String testScript = "jenkins.model.Jenkins.instance" ;
89+ p .getPublishersList ().add (new TestGroovyRecorder (
90+ new SecureGroovyScript (testScript , false , null )));
91+
92+ String approvedHash = approveIfExists (testScript , sa );
93+ assertNotNull (approvedHash );
94+ r .assertBuildStatus (Result .SUCCESS , p .scheduleBuild2 (0 ).get ());
95+
96+ sa .denyScript (approvedHash );
97+
98+ /*
99+ * The script is not approved but should be pending.
100+ */
101+ r .assertBuildStatus (Result .FAILURE , p .scheduleBuild2 (0 ).get ());
102+ approvedHash = approveIfExists (testScript , sa );
103+ assertNotNull (approvedHash );
104+ r .assertBuildStatus (Result .SUCCESS , p .scheduleBuild2 (0 ).get ());
105+ }
106+
68107 @ Test public void emptyScript () throws Exception {
69108 configureSecurity ();
70109 script ("" ).use ();
0 commit comments