-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathCredentialsHelperTest.java
More file actions
88 lines (74 loc) · 3.94 KB
/
CredentialsHelperTest.java
File metadata and controls
88 lines (74 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package hudson.plugins.jira;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.domains.DomainSpecification;
import com.cloudbees.plugins.credentials.domains.HostnameSpecification;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.model.Descriptor.FormException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
/**
* @author Zhenlei Huang
*/
public class CredentialsHelperTest {
@Rule
public JenkinsRule r = new JenkinsRule();
@Test
public void lookupSystemCredentials() throws IOException, FormException {
assertNull(CredentialsHelper.lookupSystemCredentials("nonexistent-credentials-id", null));
StandardUsernamePasswordCredentials c =
new UsernamePasswordCredentialsImpl(CredentialsScope.SYSTEM, null, null, "username", "password");
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c);
assertEquals(c, CredentialsHelper.lookupSystemCredentials(c.getId(), null));
assertEquals(c, CredentialsHelper.lookupSystemCredentials(c.getId(), new URL("http://example.org")));
}
@Test
public void lookupSystemCredentialsWithDomainRestriction() throws IOException, FormException {
Domain domain = new Domain(
"example",
"test domain",
Arrays.<DomainSpecification>asList(new HostnameSpecification("example.org", null)));
StandardUsernamePasswordCredentials c =
new UsernamePasswordCredentialsImpl(CredentialsScope.SYSTEM, null, null, "username", "password");
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addDomain(domain, c);
assertEquals(c, CredentialsHelper.lookupSystemCredentials(c.getId(), null));
assertEquals(c, CredentialsHelper.lookupSystemCredentials(c.getId(), new URL("http://example.org")));
assertNull(CredentialsHelper.lookupSystemCredentials(c.getId(), new URL("http://nonexistent.url")));
}
@Test
public void migrateCredentials() throws MalformedURLException, FormException {
assertThat(
CredentialsProvider.lookupStores(r.jenkins).iterator().next().getCredentials(Domain.global()), empty());
StandardUsernamePasswordCredentials c =
CredentialsHelper.migrateCredentials("username", "password", new URL("http://example.org"));
assertEquals("Migrated by Jira Plugin", c.getDescription());
assertThat(
CredentialsProvider.lookupStores(r.jenkins).iterator().next().getCredentials(Domain.global()),
hasSize(1));
}
@Test
public void migrateCredentialsWithExsitingCredentials() throws IOException, FormException {
Domain domain = new Domain(
"example",
"test domain",
Arrays.<DomainSpecification>asList(new HostnameSpecification("example.org", null)));
StandardUsernamePasswordCredentials c =
new UsernamePasswordCredentialsImpl(CredentialsScope.SYSTEM, null, null, "username", "password");
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addDomain(domain, c);
StandardUsernamePasswordCredentials cred =
CredentialsHelper.migrateCredentials("username", "password", new URL("http://example.org"));
assertEquals(c, cred);
}
}