From a43760d34aaa6c1f65c468e34066210ffec320db Mon Sep 17 00:00:00 2001 From: Sven Merk Date: Mon, 1 Apr 2019 15:59:20 +0200 Subject: [PATCH] Use JenkinsCredentialsRule --- test/groovy/WhitesourceExecuteScanTest.groovy | 49 +-------------- .../groovy/util/JenkinsCredentialsRule.groovy | 59 +++++++++++++++---- 2 files changed, 51 insertions(+), 57 deletions(-) diff --git a/test/groovy/WhitesourceExecuteScanTest.groovy b/test/groovy/WhitesourceExecuteScanTest.groovy index 2544a95f1..02398c917 100644 --- a/test/groovy/WhitesourceExecuteScanTest.groovy +++ b/test/groovy/WhitesourceExecuteScanTest.groovy @@ -36,6 +36,9 @@ class WhitesourceExecuteScanTest extends BasePiperTest { .around(loggingRule) .around(writeFileRule) .around(stepRule) + .around(new JenkinsCredentialsRule(this) + .withCredentials('ID-123456789', 'token-0815') + .withCredentials('ID-9876543', 'token-0816')) def whitesourceOrgAdminRepositoryStub def whitesourceStub @@ -45,52 +48,6 @@ class WhitesourceExecuteScanTest extends BasePiperTest { @Before void init() { - def credentialsStore = ['ID-123456789': 'token-0815', 'ID-9876543': 'token-0816', 'ID-abcdefg': ['testUser', 'testPassword']] - def withCredentialsBindings - helper.registerAllowedMethod('string', [Map], { - m -> - withCredentialsBindings = ["${m.credentialsId}": "${m.variable}"] - return m - }) - helper.registerAllowedMethod('usernamePassword', [Map], { - m -> - withCredentialsBindings = ["${m.credentialsId}": ["${m.usernameVariable}", "${m.passwordVariable}"]] - return m - }) - helper.registerAllowedMethod('withCredentials', [List.class, Closure.class], { - l, body -> - def index = 0 - withCredentialsBindings.each { - entry -> - if(entry.value instanceof List) { - entry.value.each { - subEntry -> - def value = credentialsStore[entry.key] - getBinding().setProperty(subEntry, value[index]) - index++ - - } - } else { - getBinding().setProperty(entry.value, credentialsStore[entry.key]) - } - } - try { - body() - } finally { - withCredentialsBindings.each { - entry -> - if(entry.value instanceof List) { - entry.value.each { - subEntry -> - getBinding().setProperty(subEntry, null) - - } - } else { - getBinding().setProperty(entry.value, null) - } - } - } - }) helper.registerAllowedMethod("archiveArtifacts", [Map.class], { m -> if (m.artifacts == null) { throw new Exception('artifacts cannot be null') diff --git a/test/groovy/util/JenkinsCredentialsRule.groovy b/test/groovy/util/JenkinsCredentialsRule.groovy index dc3388c0d..6b855c843 100644 --- a/test/groovy/util/JenkinsCredentialsRule.groovy +++ b/test/groovy/util/JenkinsCredentialsRule.groovy @@ -14,6 +14,7 @@ import org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException class JenkinsCredentialsRule implements TestRule { Map credentials = [:] + Map bindingTypes = [:] final BasePipelineTest testInstance @@ -26,6 +27,11 @@ class JenkinsCredentialsRule implements TestRule { return this } + JenkinsCredentialsRule withCredentials(String credentialsId, String token) { + credentials.put(credentialsId, [token: token]) + return this + } + @Override Statement apply(Statement base, Description description) { return statement(base) @@ -39,27 +45,58 @@ class JenkinsCredentialsRule implements TestRule { void evaluate() throws Throwable { testInstance.helper.registerAllowedMethod('usernamePassword', [Map.class], - { m -> if (credentials.keySet().contains(m.credentialsId)) return m; - // this is what really happens in case of an unknown credentials id, - // checked with reality using credentials plugin 2.1.18. - throw new CredentialNotFoundException( - "Could not find credentials entry with ID '${m.credentialsId}'") + { m -> + if (credentials.keySet().contains(m.credentialsId)) { bindingTypes[m.credentialsId] = 'usernamePassword'; return m } + // this is what really happens in case of an unknown credentials id, + // checked with reality using credentials plugin 2.1.18. + throw new CredentialNotFoundException( + "Could not find credentials entry with ID '${m.credentialsId}'") + }) + + testInstance.helper.registerAllowedMethod('string', [Map.class], + { m -> + if (credentials.keySet().contains(m.credentialsId)) { bindingTypes[m.credentialsId] = 'string'; return m } + // this is what really happens in case of an unknown credentials id, + // checked with reality using credentials plugin 2.1.18. + throw new CredentialNotFoundException( + "Could not find credentials entry with ID '${m.credentialsId}'") }) testInstance.helper.registerAllowedMethod('withCredentials', [List, Closure], { config, closure -> def credsId = config[0].credentialsId - def passwordVariable = config[0].passwordVariable - def usernameVariable = config[0].usernameVariable + def credentialsBindingType = bindingTypes.get(credsId) def creds = credentials.get(credsId) - binding.setProperty(usernameVariable, creds?.user) - binding.setProperty(passwordVariable, creds?.passwd) + def tokenVariable, usernameVariable, passwordVariable, prepare, destruct + if(credentialsBindingType == "usernamePassword") { + passwordVariable = config[0].passwordVariable + usernameVariable = config[0].usernameVariable + prepare = { + binding.setProperty(usernameVariable, creds?.user) + binding.setProperty(passwordVariable, creds?.passwd) + } + destruct = { + binding.setProperty(usernameVariable, null) + binding.setProperty(passwordVariable, null) + } + } else if(credentialsBindingType == "string") { + tokenVariable = config[0].variable + prepare = { + binding.setProperty(tokenVariable, creds?.token) + } + destruct = { + binding.setProperty(tokenVariable, null) + } + } else { + throw new RuntimeException("Unknown binding type") + } + + prepare() try { closure() } finally { - binding.setProperty(usernameVariable, null) - binding.setProperty(passwordVariable, null) + destruct() } })