Skip to content

Commit

Permalink
[JENKINS-46794] Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Tierno authored and Peter Tierno committed Apr 10, 2018
1 parent b31af31 commit 7f3e243
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
@@ -1,5 +1,8 @@
package com.datapipe.jenkins.vault;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -8,6 +11,7 @@

import org.kohsuke.stapler.DataBoundConstructor;

import com.bettercloud.vault.response.LogicalResponse;
import com.datapipe.jenkins.vault.credentials.VaultAppRoleCredential;
import com.datapipe.jenkins.vault.credentials.VaultCredential;
import com.datapipe.jenkins.vault.model.VaultSecret;
Expand Down Expand Up @@ -44,13 +48,15 @@ public void auth(VaultCredential vaultCredential) {
}

@Override
public Map<String, String> read(String path) {
public LogicalResponse read(String path) {
if (!path.equals("secret/path1")) {
throw new AssertionError("path " + path + " does not match expected: secret/path1");
}
Map<String, String> returnValue = new HashMap<>();
returnValue.put("key1", "some-secret");
return returnValue;
LogicalResponse resp = mock(LogicalResponse.class);
when(resp.getData()).thenReturn(returnValue);
return resp;
}
});
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -25,6 +26,9 @@
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import com.bettercloud.vault.Vault;
import com.bettercloud.vault.VaultConfig;
import com.bettercloud.vault.response.LogicalResponse;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
Expand Down Expand Up @@ -81,7 +85,9 @@ private VaultAccessor mockVaultAccessor() {
VaultAccessor vaultAccessor = mock(VaultAccessor.class);
Map<String, String> returnValue = new HashMap<>();
returnValue.put("key1", "some-secret");
when(vaultAccessor.read("secret/path1")).thenReturn(returnValue);
LogicalResponse resp = mock(LogicalResponse.class);
when(resp.getData()).thenReturn(returnValue);
when(vaultAccessor.read("secret/path1")).thenReturn(resp);
return vaultAccessor;
}

Expand Down Expand Up @@ -289,7 +295,15 @@ public void shouldFailIfCredentialsDoNotExist() throws Exception {
jenkins.assertLogContains("CredentialsUnavailableException", build);
}

public static Credentials createTokenCredential(final String credentialId) {
return new VaultAppRoleCredential(CredentialsScope.GLOBAL, credentialId, "description", "role-id-"+credentialId, Secret.fromString("secret-id-"+credentialId));
public static VaultAppRoleCredential createTokenCredential(final String credentialId) {
Vault vault = mock(Vault.class, withSettings().serializable());
VaultAppRoleCredential cred = mock(VaultAppRoleCredential.class, withSettings().serializable());
when(cred.getId()).thenReturn(credentialId);
when(cred.getDescription()).thenReturn("description");
when(cred.getRoleId()).thenReturn("role-id-" + credentialId);
when(cred.getSecretId()).thenReturn(Secret.fromString("secret-id-" + credentialId));
when(cred.authorizeWithVault((Vault)any(), (VaultConfig)any())).thenReturn(vault);
return cred;

}
}
}
Expand Up @@ -6,7 +6,7 @@
import static com.datapipe.jenkins.vault.it.VaultConfigurationIT.createTokenCredential;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand All @@ -29,6 +29,7 @@
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import com.bettercloud.vault.response.LogicalResponse;
import com.cloudbees.hudson.plugins.folder.Folder;
import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider;
import com.cloudbees.plugins.credentials.Credentials;
Expand Down Expand Up @@ -105,9 +106,11 @@ public void setupJenkins() throws IOException {

private VaultAccessor mockVaultAccessor() {
VaultAccessor vaultAccessor = mock(VaultAccessor.class);
LogicalResponse resp = mock(LogicalResponse.class);
Map<String, String> returnValue = new HashMap<>();
returnValue.put("key1", "some-secret");
when(vaultAccessor.read("secret/path1")).thenReturn(returnValue);
when(resp.getData()).thenReturn(returnValue);
when(vaultAccessor.read("secret/path1")).thenReturn(resp);
return vaultAccessor;
}

Expand Down

0 comments on commit 7f3e243

Please sign in to comment.