-
Notifications
You must be signed in to change notification settings - Fork 234
/
CredentialsMatchersTest.java
111 lines (94 loc) · 5.17 KB
/
CredentialsMatchersTest.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
103
104
105
106
107
108
109
110
111
package com.cloudbees.plugins.credentials;
import com.cloudbees.plugins.credentials.common.UsernameCredentials;
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;
import com.cloudbees.plugins.credentials.matchers.CQLSyntaxException;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class CredentialsMatchersTest {
@Test
public void describe() throws Exception {
assertThat(CredentialsMatchers.describe(CredentialsMatchers.always()), is("true"));
assertThat(CredentialsMatchers.describe(CredentialsMatchers.never()), is("false"));
assertThat(CredentialsMatchers.describe(CredentialsMatchers.withId("target=\"foo\"")), is("(id == \"target=\\\"foo\\\"\")"));
assertThat(CredentialsMatchers.describe(CredentialsMatchers.allOf(
CredentialsMatchers.anyOf(
CredentialsMatchers.instanceOf(UsernameCredentials.class),
CredentialsMatchers.withScopes(CredentialsScope.GLOBAL, CredentialsScope.USER)
),
CredentialsMatchers.not(CredentialsMatchers.withUsername("bob")))), is(
"(((instanceof com.cloudbees.plugins.credentials.common.UsernameCredentials) "
+ "|| ((scope == com.cloudbees.plugins.credentials.CredentialsScope.GLOBAL) "
+ "|| (scope == com.cloudbees.plugins.credentials.CredentialsScope.USER))"
+ ") && !(username == \"bob\"))"));
}
@Test
public void parse() throws Exception {
CredentialsMatcher matcher = CredentialsMatchers.allOf(
CredentialsMatchers.anyOf(
CredentialsMatchers.instanceOf(UsernameCredentials.class),
CredentialsMatchers.withScopes(CredentialsScope.GLOBAL, CredentialsScope.USER)
),
CredentialsMatchers.not(CredentialsMatchers.withUsername("bob")));
assertThat(CredentialsMatchers.describe(CredentialsMatchers.parse(CredentialsMatchers.describe(matcher))), is(CredentialsMatchers.describe(matcher)));
matcher = CredentialsMatchers.allOf(
CredentialsMatchers.anyOf(
CredentialsMatchers.withProperty("foo", 57.0),
CredentialsMatchers.withScopes(CredentialsScope.SYSTEM)
),
CredentialsMatchers.not(CredentialsMatchers.withUsername("bob")));
assertThat(CredentialsMatchers.describe(CredentialsMatchers.parse(CredentialsMatchers.describe(matcher))), is(CredentialsMatchers.describe(matcher)));
}
@Test
public void parseTrue() throws Exception {
assertThat(CredentialsMatchers.parse(CredentialsMatchers.describe(CredentialsMatchers.always())), is(CredentialsMatchers.always()));
}
@Test
public void parsePropertyTest() throws Exception {
CredentialsMatcher parsedMatcher =
CredentialsMatchers.parse(CredentialsMatchers.describe(CredentialsMatchers.withUsername("b\tob")));
assertThat(parsedMatcher.matches(new MyBaseStandardCredentials("b\tob")), is(true));
assertThat(parsedMatcher.matches(new MyBaseStandardCredentials("bob")), is(false));
}
@Test(expected = CQLSyntaxException.class)
public void parseInvalid() throws Exception {
CredentialsMatchers.parse("\"bob\" == username");
}
@Test(expected = CQLSyntaxException.class)
public void parseInvalid2() throws Exception {
CredentialsMatchers.parse("id == \"id-1\" || \"id-2\"");
}
@Test(expected = CQLSyntaxException.class)
public void parseInvalidMultiline() throws Exception {
CredentialsMatchers.parse("id == \"id-1\"\n|| \"id-2\"\n&& instanceof Boolean");
}
@Test
public void parseEmpty() throws Exception {
assertThat(CredentialsMatchers.parse(""), is(CredentialsMatchers.always()));
}
@Test
public void parseNegativePropertyTest() throws Exception {
CredentialsMatcher parsedMatcher =
CredentialsMatchers.parse(CredentialsMatchers.describe(CredentialsMatchers.not(CredentialsMatchers.withUsername("b\tob"))));
assertThat(parsedMatcher.matches(new MyBaseStandardCredentials("b\tob")), is(false));
assertThat(parsedMatcher.matches(new MyBaseStandardCredentials("bob")), is(true));
}
@Test
public void beanMatcher() throws Exception {
CredentialsMatcher instance = CredentialsMatchers.withProperty("username", "bob");
assertThat(CredentialsMatchers.describe(instance), is("(username == \"bob\")"));
assertThat(instance.matches(new MyBaseStandardCredentials("bob")), is(true));
assertThat(instance.matches(new MyBaseStandardCredentials("ben")), is(false));
assertThat(instance.matches(new BaseStandardCredentials(null, null) {}), is(false));
}
public static class MyBaseStandardCredentials extends BaseStandardCredentials {
private final String username;
public MyBaseStandardCredentials(String username) {
super(null, null);
this.username = username;
}
public String getUsername() {
return username;
}
}
}