-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGitChangeSetTest.java
More file actions
234 lines (201 loc) · 11.2 KB
/
GitChangeSetTest.java
File metadata and controls
234 lines (201 loc) · 11.2 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package hudson.plugins.git;
import hudson.model.User;
import hudson.tasks.Mailer;
import hudson.tasks.Mailer.UserProperty;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.MockedStatic;
import org.springframework.security.authentication.DisabledException;
import java.io.IOException;
import java.util.Collections;
import java.util.Random;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
public class GitChangeSetTest {
@Rule
public JenkinsRule jenkins = new JenkinsRule();
private final Random random = new Random();
@Test
public void testFindOrCreateUser() {
final GitChangeSet committerCS = GitChangeSetUtil.genChangeSet(false, false);
final String email = "jauthor@nospam.com";
final boolean createAccountBasedOnEmail = true;
final boolean useExistingAccountBasedOnEmail = false;
User user = committerCS.findOrCreateUser(GitChangeSetUtil.AUTHOR_NAME, email, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertNotNull(user);
UserProperty property = user.getProperty(Mailer.UserProperty.class);
assertNotNull(property);
String address = property.getAddress();
assertNotNull(address);
assertEquals(email, address);
assertEquals(User.getUnknown(), committerCS.findOrCreateUser(null, email, false, useExistingAccountBasedOnEmail));
assertEquals(User.getUnknown(), committerCS.findOrCreateUser(null, email, true, useExistingAccountBasedOnEmail));
}
@Test
public void testFindOrCreateUserNullAuthorEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = true;
final boolean useExistingAccountBasedOnEmail = random.nextBoolean();
final String csAuthor = "ada";
final String csAuthorEmail = null;
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user, is(User.getUnknown()));
}
@Test
public void testFindOrCreateUserEmptyAuthorEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = true;
final boolean useExistingAccountBasedOnEmail = random.nextBoolean();
final String csAuthor = "babbage";
final String csAuthorEmail = "";
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user, is(User.getUnknown()));
}
@Test
public void testFindOrCreateUserEmptyAuthorEmailDoNotCreateAccountBasedOnEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = false;
final boolean useExistingAccountBasedOnEmail = random.nextBoolean();
final String csAuthor = "babbage-do-not-create";
final String csAuthorEmail = "";
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user, is(User.getUnknown()));
}
@Test
public void testFindOrCreateUserEmptyAuthorDoNotCreateAccountBasedOnEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = false;
final boolean useExistingAccountBasedOnEmail = random.nextBoolean();
final String csAuthor = "";
final String csAuthorEmail = "babbage-empty-author@example.com";
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user, is(User.getUnknown()));
}
@Test
public void testFindOrCreateUserBadAuthorEmailDoNotCreateAccountBasedOnEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = false;
final boolean useExistingAccountBasedOnEmail = random.nextBoolean();
final String csAuthor = "babbage-do-not-create";
final String csAuthorEmail = "@";
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user, is(User.getUnknown()));
}
@Test
public void testFindOrCreateUserOKAuthorEmailDoNotCreateAccountBasedOnEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = false;
final boolean useExistingAccountBasedOnEmail = random.nextBoolean();
final String csAuthor = "babbage-will-be-created";
final String csAuthorEmail = csAuthor + "@";
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user.getFullName(), is(csAuthor));
}
@Test
public void testFindOrCreateUserBlankAuthorEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = true;
final boolean useExistingAccountBasedOnEmail = false;
final String csAuthor = "candide";
final String csAuthorEmail = " ";
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user.getFullName(), is(csAuthor));
}
@Test
public void testFindOrCreateUserBlankAuthorEmailUseExistingAccountBasedOnEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = true;
final boolean useExistingAccountBasedOnEmail = true;
final String csAuthor = "cosimo";
final String csAuthorEmail = " ";
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user.getFullName(), is(csAuthor));
}
@Test
public void testFindOrCreateUserValidAuthorEmailUseExistingAccountBasedOnEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = true;
final boolean useExistingAccountBasedOnEmail = true;
final String csAuthor = "dante";
final String csAuthorEmail = "alighieri@example.com";
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user.getFullName(), is(csAuthor));
}
@Test
public void testFindOrCreateUserUseExistingAuthorEmailUseExistingAccountBasedOnEmail() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
final boolean createAccountBasedOnEmail = true;
final boolean useExistingAccountBasedOnEmail = true;
final String csAuthor = "ecco";
final String csAuthorEmail = "umberto@example.com";
User user = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(user.getFullName(), is(csAuthor));
/* Confirm that second search returns user created by first search */
User existing = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertThat(existing, is(user));
}
@Test
public void testFindOrCreateUserHandlesAuthenticationException() {
final GitChangeSet changeset = GitChangeSetUtil.genChangeSet(random.nextBoolean(), random.nextBoolean());
// TODO this only test one code path, there are several code paths using User.get()
final boolean createAccountBasedOnEmail = false;
final boolean useExistingAccountBasedOnEmail = true;
final String csAuthor = "disabled_user";
final String csAuthorEmail = "disabled_user@example.com";
try (MockedStatic<User> user = mockStatic(User.class)) {
user.when(() -> User.get("disabled_user", createAccountBasedOnEmail, Collections.emptyMap()))
.thenThrow(new DisabledException("The user \"disabled_user\" is administratively disabled"));
User actual = changeset.findOrCreateUser(csAuthor, csAuthorEmail, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertEquals(User.getUnknown(), actual);
user.verify(
() -> User.get("disabled_user", createAccountBasedOnEmail, Collections.emptyMap()),
times(1)
);
user.verify(
() -> User.getUnknown(),
times(2)
);
}
}
@Test
@Deprecated // Test deprecated User.get()
public void testFindOrCreateUserBasedOnExistingUsersEmail() throws IOException {
final GitChangeSet committerCS = GitChangeSetUtil.genChangeSet(true, false);
final String existingUserId = "An existing user";
final String existingUserFullName = "Some FullName";
final String email = "jcommitter@nospam.com";
final boolean createAccountBasedOnEmail = random.nextBoolean();
final boolean useExistingAccountBasedOnEmail = true;
assertNull(User.get(email, false));
User existingUser = User.get(existingUserId, true);
assertThat(existingUser, is(not(nullValue())));
existingUser.setFullName(existingUserFullName);
existingUser.addProperty(new Mailer.UserProperty(email));
User user = committerCS.findOrCreateUser(GitChangeSetUtil.COMMITTER_NAME, email, createAccountBasedOnEmail, useExistingAccountBasedOnEmail);
assertNotNull(user);
assertEquals(user.getId(), existingUserId);
assertEquals(user.getFullName(), existingUserFullName);
UserProperty property = user.getProperty(Mailer.UserProperty.class);
assertNotNull(property);
String address = property.getAddress();
assertNotNull(address);
assertEquals(email, address);
assertEquals(User.getUnknown(), committerCS.findOrCreateUser(null, email, false, useExistingAccountBasedOnEmail));
assertEquals(User.getUnknown(), committerCS.findOrCreateUser(null, email, true, useExistingAccountBasedOnEmail));
}
@Test
@Deprecated // Testing deprecated User.get
public void findOrCreateByFullName() throws Exception {
GitChangeSet cs = GitChangeSetUtil.genChangeSet(false, false);
User user = User.get("john");
user.setFullName(GitChangeSetUtil.COMMITTER_NAME);
user.addProperty(new Mailer.UserProperty(GitChangeSetUtil.COMMITTER_EMAIL));
assertEquals(user, cs.getAuthor());
}
}