forked from jenkinsci/bitbucket-branch-source-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitbucketAuthenticatorTest.java
102 lines (84 loc) · 4.92 KB
/
BitbucketAuthenticatorTest.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.cloudbees.jenkins.plugins.bitbucket;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.api.credentials.BitbucketUsernamePasswordAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.impl.CertificateCredentialsImpl;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Collections;
import java.util.List;
import jenkins.authentication.tokens.api.AuthenticationTokenContext;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.jvnet.hudson.test.JenkinsRule;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
public class BitbucketAuthenticatorTest {
@ClassRule
public static JenkinsRule j = new JenkinsRule();
@Rule
public TestName currentTestName = new TestName();
@Test
public void authenticationContextTest() {
AuthenticationTokenContext nullCloudContext = BitbucketAuthenticator.authenticationContext(null);
AuthenticationTokenContext cloudContext = BitbucketAuthenticator.authenticationContext(BitbucketCloudEndpoint.SERVER_URL);
AuthenticationTokenContext httpContext = BitbucketAuthenticator.authenticationContext("http://git.example.com");
AuthenticationTokenContext httpsContext = BitbucketAuthenticator.authenticationContext("https://git.example.com");
assertThat(nullCloudContext.mustHave(BitbucketAuthenticator.SCHEME, "https"), is(true));
assertThat(nullCloudContext.mustHave(BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE,
BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE_CLOUD), is(true));
assertThat(cloudContext.mustHave(BitbucketAuthenticator.SCHEME, "https"), is(true));
assertThat(cloudContext.mustHave(BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE,
BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE_CLOUD), is(true));
assertThat(httpContext.mustHave(BitbucketAuthenticator.SCHEME, "http"), is(true));
assertThat(httpContext.mustHave(BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE,
BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE_SERVER), is(true));
assertThat(httpsContext.mustHave(BitbucketAuthenticator.SCHEME, "https"), is(true));
assertThat(httpsContext.mustHave(BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE,
BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE_SERVER), is(true));
}
@Test
public void passwordCredentialsTest() {
List<Credentials> list = Collections.<Credentials>singletonList(new UsernamePasswordCredentialsImpl(
CredentialsScope.SYSTEM, "dummy", "dummy", "user", "pass"));
AuthenticationTokenContext ctx = BitbucketAuthenticator.authenticationContext((null));
Credentials c = CredentialsMatchers.firstOrNull(list, AuthenticationTokens.matcher(ctx));
assertThat(c, notNullValue());
Object a = AuthenticationTokens.convert(ctx, c);
assertThat(a, notNullValue());
assertThat(a, isA(BitbucketUsernamePasswordAuthenticator.class));
}
@Test
public void certCredentialsTest() {
List<Credentials> list = Collections.<Credentials>singletonList(new CertificateCredentialsImpl(
CredentialsScope.SYSTEM, "dummy", "dummy", "password", new DummyKeyStoreSource()));
AuthenticationTokenContext ctx = BitbucketAuthenticator.authenticationContext(null);
Credentials c = CredentialsMatchers.firstOrNull(list, AuthenticationTokens.matcher(ctx));
assertThat(c, nullValue());
ctx = BitbucketAuthenticator.authenticationContext("http://git.example.com");
c = CredentialsMatchers.firstOrNull(list, AuthenticationTokens.matcher(ctx));
assertThat(c, nullValue());
ctx = BitbucketAuthenticator.authenticationContext("https://git.example.com");
c = CredentialsMatchers.firstOrNull(list, AuthenticationTokens.matcher(ctx));
assertThat(c, notNullValue());
assertThat(AuthenticationTokens.convert(ctx, c), notNullValue());
}
private static class DummyKeyStoreSource extends CertificateCredentialsImpl.KeyStoreSource {
@NonNull
@Override
public byte[] getKeyStoreBytes() { return new byte[0]; }
@Override
public long getKeyStoreLastModified() { return 0; }
@Override
public boolean isSnapshotSource() { return true; }
}
}