Source:
public void testGlobalBindingWithAuthorization() { story.addStep(new Statement() { @Override public void evaluate() throws Throwable { story.j.jenkins.setSecurityRealm(story.j.createDummySecurityRealm()); story.j.jenkins.setAuthorizationStrategy(new FullControlOnceLoggedInAuthorizationStrategy()); story.j.jenkins.getUser("dummy"); Map<String, Boolean> strategies = new HashMap<String, Boolean>(); strategies.put(story.j.jenkins.getDescriptor(SpecificUsersAuthorizationStrategy.class).getId(), true); QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new ProjectQueueItemAuthenticator(strategies)); story.j.jenkins.getExtensionList(Whitelist.class).add(new BlanketWhitelist()); String credentialsId = "creds"; String secret = "s3cr3t"; CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), new StringCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "sample", Secret.fromString(secret))); WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("" + "node {\n" + " def authentication = Jenkins.getAuthentication()\n" + " echo \"running as user: $authentication.principal\"\n" + " withCredentials([[$class: 'StringBinding', credentialsId: '" + credentialsId + "', variable: 'SECRET']]) {\n" + " echo 'this will fail if the credentials can not be found'\n" + " }\n" + "}", true)); p.addProperty(new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("dummy", true))); WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0).get()); story.j.assertLogContains("running as user: dummy", b); } }); }

Target:
public void testGlobalBindingWithAuthorization() { story.addStep(new Statement() { @Override public void evaluate() throws Throwable { story.j.jenkins.setSecurityRealm(story.j.createDummySecurityRealm()); story.j.jenkins.setAuthorizationStrategy(new FullControlOnceLoggedInAuthorizationStrategy()); story.j.jenkins.getUser("dummy"); Map<String, Boolean> strategies = new HashMap<String, Boolean>(); strategies.put(story.j.jenkins.getDescriptor(SpecificUsersAuthorizationStrategy.class).getId(), true); QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new ProjectQueueItemAuthenticator(strategies)); story.j.jenkins.getExtensionList(Whitelist.class).add(new BlanketWhitelist()); String credentialsId = "creds"; String secret = "s3cr3t"; CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), new StringCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "sample", Secret.fromString(secret))); WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("" + "node {\n" + " def authentication = Jenkins.getAuthentication()\n" + " echo \"running as user: $authentication.principal\"\n" + " withCredentials([[$class: 'StringBinding', credentialsId: '" + credentialsId + "', variable: 'SECRET']]) {\n" + " writeFile file:'test', text: \"$env.SECRET\"\n" + " def content = readFile 'test'\n" + " if (\"$content\" != \"" + secret + "\") {\n" + " error 'The credential was not bound into the workflow correctly'\n" + " }\n" + " }\n" + "}", true)); p.addProperty(new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("dummy", true))); WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0).get()); story.j.assertLogContains("running as user: dummy", b); } }); }