commit | author | age
|
357109
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
|
16 |
package com.gitblit.tests;
|
|
17 |
|
|
18 |
import static org.junit.Assert.assertEquals;
|
|
19 |
import static org.junit.Assert.assertTrue;
|
|
20 |
import groovy.lang.Binding;
|
|
21 |
import groovy.util.GroovyScriptEngine;
|
|
22 |
|
a612e6
|
23 |
import java.io.BufferedWriter;
|
357109
|
24 |
import java.io.File;
|
a612e6
|
25 |
import java.io.FileWriter;
|
2f5d15
|
26 |
import java.io.PrintWriter;
|
JM |
27 |
import java.io.StringWriter;
|
357109
|
28 |
import java.text.MessageFormat;
|
JM |
29 |
import java.util.ArrayList;
|
|
30 |
import java.util.Arrays;
|
|
31 |
import java.util.Collection;
|
ff148d
|
32 |
import java.util.Date;
|
357109
|
33 |
import java.util.List;
|
JM |
34 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
35 |
|
|
36 |
import org.eclipse.jgit.lib.ObjectId;
|
|
37 |
import org.eclipse.jgit.lib.Repository;
|
|
38 |
import org.eclipse.jgit.transport.ReceiveCommand;
|
|
39 |
import org.junit.AfterClass;
|
|
40 |
import org.junit.BeforeClass;
|
|
41 |
import org.junit.Test;
|
|
42 |
|
|
43 |
import com.gitblit.GitBlit;
|
|
44 |
import com.gitblit.GitBlitException;
|
|
45 |
import com.gitblit.models.RepositoryModel;
|
|
46 |
import com.gitblit.models.TeamModel;
|
|
47 |
import com.gitblit.models.UserModel;
|
|
48 |
import com.gitblit.utils.StringUtils;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Test class for Groovy scripts. Mostly this is to facilitate development.
|
|
52 |
*
|
|
53 |
* @author James Moger
|
|
54 |
*
|
|
55 |
*/
|
|
56 |
public class GroovyScriptTest {
|
|
57 |
|
|
58 |
private static final AtomicBoolean started = new AtomicBoolean(false);
|
|
59 |
|
|
60 |
@BeforeClass
|
|
61 |
public static void startGitblit() throws Exception {
|
|
62 |
started.set(GitBlitSuite.startGitblit());
|
|
63 |
}
|
|
64 |
|
|
65 |
@AfterClass
|
|
66 |
public static void stopGitblit() throws Exception {
|
|
67 |
if (started.get()) {
|
|
68 |
GitBlitSuite.stopGitblit();
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
@Test
|
d60a42
|
73 |
public void testSendHtmlMail() throws Exception {
|
GS |
74 |
MockGitblit gitblit = new MockGitblit();
|
|
75 |
MockLogger logger = new MockLogger();
|
|
76 |
MockClientLogger clientLogger = new MockClientLogger();
|
|
77 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
|
78 |
commands.add(new ReceiveCommand(ObjectId
|
|
79 |
.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
|
|
80 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
|
|
81 |
commands.add(new ReceiveCommand(ObjectId
|
|
82 |
.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
|
|
83 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master2"));
|
|
84 |
|
|
85 |
RepositoryModel repository = GitBlit.self().getRepositoryModel("helloworld.git");
|
|
86 |
repository.mailingLists.add("list@helloworld.git");
|
|
87 |
|
|
88 |
test("sendmail-html.groovy", gitblit, logger, clientLogger, commands, repository);
|
|
89 |
assertEquals(1, logger.messages.size());
|
|
90 |
assertEquals(1, gitblit.messages.size());
|
|
91 |
MockMail m = gitblit.messages.get(0);
|
|
92 |
assertEquals(5, m.toAddresses.size());
|
|
93 |
assertTrue(m.message.contains("BIT"));
|
|
94 |
assertTrue(m.message.contains("<html>"));
|
|
95 |
}
|
|
96 |
|
|
97 |
@Test
|
357109
|
98 |
public void testSendMail() throws Exception {
|
JM |
99 |
MockGitblit gitblit = new MockGitblit();
|
|
100 |
MockLogger logger = new MockLogger();
|
a612e6
|
101 |
MockClientLogger clientLogger = new MockClientLogger();
|
357109
|
102 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
JM |
103 |
commands.add(new ReceiveCommand(ObjectId
|
|
104 |
.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
|
|
105 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
|
5e8e7e
|
106 |
commands.add(new ReceiveCommand(ObjectId
|
JM |
107 |
.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
|
|
108 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master2"));
|
357109
|
109 |
|
ff148d
|
110 |
RepositoryModel repository = GitBlit.self().getRepositoryModel("helloworld.git");
|
JM |
111 |
repository.mailingLists.add("list@helloworld.git");
|
|
112 |
|
a612e6
|
113 |
test("sendmail.groovy", gitblit, logger, clientLogger, commands, repository);
|
357109
|
114 |
assertEquals(1, logger.messages.size());
|
JM |
115 |
assertEquals(1, gitblit.messages.size());
|
|
116 |
MockMail m = gitblit.messages.get(0);
|
|
117 |
assertEquals(5, m.toAddresses.size());
|
|
118 |
assertTrue(m.message.contains("BIT"));
|
|
119 |
}
|
ff148d
|
120 |
|
JM |
121 |
@Test
|
4e7813
|
122 |
public void testProtectRefsCreateBranch() throws Exception {
|
PLM |
123 |
MockGitblit gitblit = new MockGitblit();
|
|
124 |
MockLogger logger = new MockLogger();
|
a612e6
|
125 |
MockClientLogger clientLogger = new MockClientLogger();
|
4e7813
|
126 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
PLM |
127 |
commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
|
|
128 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
|
|
129 |
|
|
130 |
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
|
|
131 |
|
a612e6
|
132 |
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
|
4e7813
|
133 |
}
|
PLM |
134 |
|
|
135 |
@Test
|
|
136 |
public void testProtectRefsCreateTag() throws Exception {
|
|
137 |
MockGitblit gitblit = new MockGitblit();
|
|
138 |
MockLogger logger = new MockLogger();
|
a612e6
|
139 |
MockClientLogger clientLogger = new MockClientLogger();
|
4e7813
|
140 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
PLM |
141 |
commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
|
|
142 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/tags/v1.0"));
|
|
143 |
|
|
144 |
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
|
|
145 |
|
a612e6
|
146 |
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
|
4e7813
|
147 |
assertEquals(0, logger.messages.size());
|
PLM |
148 |
}
|
|
149 |
|
|
150 |
@Test
|
|
151 |
public void testProtectRefsFastForward() throws Exception {
|
|
152 |
MockGitblit gitblit = new MockGitblit();
|
|
153 |
MockLogger logger = new MockLogger();
|
a612e6
|
154 |
MockClientLogger clientLogger = new MockClientLogger();
|
4e7813
|
155 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
PLM |
156 |
commands.add(new ReceiveCommand(ObjectId
|
|
157 |
.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
|
|
158 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
|
|
159 |
|
|
160 |
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
|
|
161 |
|
a612e6
|
162 |
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
|
4e7813
|
163 |
assertEquals(0, logger.messages.size());
|
PLM |
164 |
}
|
|
165 |
|
|
166 |
@Test
|
|
167 |
public void testProtectRefsDeleteMasterBranch() throws Exception {
|
|
168 |
MockGitblit gitblit = new MockGitblit();
|
|
169 |
MockLogger logger = new MockLogger();
|
a612e6
|
170 |
MockClientLogger clientLogger = new MockClientLogger();
|
4e7813
|
171 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
PLM |
172 |
ReceiveCommand command = new ReceiveCommand(ObjectId
|
|
173 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), ObjectId.zeroId(),
|
|
174 |
"refs/heads/master");
|
|
175 |
commands.add(command);
|
|
176 |
|
|
177 |
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
|
|
178 |
|
a612e6
|
179 |
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
|
4e7813
|
180 |
assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
|
PLM |
181 |
assertEquals(0, logger.messages.size());
|
|
182 |
}
|
|
183 |
|
|
184 |
@Test
|
|
185 |
public void testProtectRefsDeleteOtherBranch() throws Exception {
|
|
186 |
MockGitblit gitblit = new MockGitblit();
|
|
187 |
MockLogger logger = new MockLogger();
|
a612e6
|
188 |
MockClientLogger clientLogger = new MockClientLogger();
|
4e7813
|
189 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
PLM |
190 |
commands.add(new ReceiveCommand(ObjectId
|
|
191 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), ObjectId.zeroId(),
|
|
192 |
"refs/heads/other"));
|
|
193 |
|
|
194 |
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
|
|
195 |
|
a612e6
|
196 |
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
|
4e7813
|
197 |
assertEquals(0, logger.messages.size());
|
PLM |
198 |
}
|
|
199 |
|
|
200 |
@Test
|
|
201 |
public void testProtectRefsDeleteTag() throws Exception {
|
|
202 |
MockGitblit gitblit = new MockGitblit();
|
|
203 |
MockLogger logger = new MockLogger();
|
a612e6
|
204 |
MockClientLogger clientLogger = new MockClientLogger();
|
4e7813
|
205 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
PLM |
206 |
ReceiveCommand command = new ReceiveCommand(ObjectId
|
|
207 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), ObjectId.zeroId(),
|
|
208 |
"refs/tags/v1.0");
|
|
209 |
commands.add(command);
|
|
210 |
|
|
211 |
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
|
|
212 |
|
a612e6
|
213 |
test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
|
4e7813
|
214 |
assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
|
PLM |
215 |
assertEquals(0, logger.messages.size());
|
|
216 |
}
|
|
217 |
|
|
218 |
@Test
|
ff148d
|
219 |
public void testBlockPush() throws Exception {
|
JM |
220 |
MockGitblit gitblit = new MockGitblit();
|
|
221 |
MockLogger logger = new MockLogger();
|
a612e6
|
222 |
MockClientLogger clientLogger = new MockClientLogger();
|
ff148d
|
223 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
JM |
224 |
commands.add(new ReceiveCommand(ObjectId
|
|
225 |
.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
|
|
226 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
|
|
227 |
|
|
228 |
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
|
|
229 |
|
|
230 |
try {
|
a612e6
|
231 |
test("blockpush.groovy", gitblit, logger, clientLogger, commands, repository);
|
ff148d
|
232 |
assertTrue("blockpush should have failed!", false);
|
JM |
233 |
} catch (GitBlitException e) {
|
|
234 |
assertTrue(e.getMessage().contains("failed"));
|
|
235 |
}
|
|
236 |
}
|
a612e6
|
237 |
|
JC |
238 |
@Test
|
|
239 |
public void testClientLogging() throws Exception {
|
|
240 |
MockGitblit gitblit = new MockGitblit();
|
|
241 |
MockLogger logger = new MockLogger();
|
|
242 |
MockClientLogger clientLogger = new MockClientLogger();
|
|
243 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
|
244 |
commands.add(new ReceiveCommand(ObjectId
|
|
245 |
.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
|
|
246 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
|
|
247 |
|
|
248 |
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
|
|
249 |
|
|
250 |
File groovyDir = GitBlit.getGroovyScriptsFolder();
|
|
251 |
File tempScript = File.createTempFile("testClientLogging", "groovy", groovyDir);
|
|
252 |
tempScript.deleteOnExit();
|
|
253 |
|
|
254 |
BufferedWriter writer = new BufferedWriter(new FileWriter(tempScript));
|
|
255 |
|
2f5d15
|
256 |
writer.write("clientLogger.info('this is a test message')\n");
|
a612e6
|
257 |
writer.flush();
|
JC |
258 |
writer.close();
|
357109
|
259 |
|
a612e6
|
260 |
test(tempScript.getName(), gitblit, logger, clientLogger, commands, repository);
|
JC |
261 |
assertTrue("Message Missing", clientLogger.messages.contains("this is a test message"));
|
|
262 |
}
|
|
263 |
|
|
264 |
private void test(String script, MockGitblit gitblit, MockLogger logger, MockClientLogger clientLogger,
|
ff148d
|
265 |
List<ReceiveCommand> commands, RepositoryModel repository) throws Exception {
|
357109
|
266 |
|
JM |
267 |
UserModel user = new UserModel("mock");
|
|
268 |
|
|
269 |
String gitblitUrl = GitBlitSuite.url;
|
|
270 |
|
|
271 |
File groovyDir = GitBlit.getGroovyScriptsFolder();
|
|
272 |
GroovyScriptEngine gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
|
|
273 |
|
|
274 |
Binding binding = new Binding();
|
|
275 |
binding.setVariable("gitblit", gitblit);
|
|
276 |
binding.setVariable("repository", repository);
|
|
277 |
binding.setVariable("user", user);
|
|
278 |
binding.setVariable("commands", commands);
|
|
279 |
binding.setVariable("url", gitblitUrl);
|
|
280 |
binding.setVariable("logger", logger);
|
a612e6
|
281 |
binding.setVariable("clientLogger", clientLogger);
|
357109
|
282 |
|
JM |
283 |
Object result = gse.run(script, binding);
|
|
284 |
if (result instanceof Boolean) {
|
|
285 |
if (!((Boolean) result)) {
|
|
286 |
throw new GitBlitException(MessageFormat.format(
|
|
287 |
"Groovy script {0} has failed! Hook scripts aborted.", script));
|
|
288 |
}
|
|
289 |
}
|
|
290 |
}
|
|
291 |
|
|
292 |
class MockGitblit {
|
|
293 |
List<MockMail> messages = new ArrayList<MockMail>();
|
|
294 |
|
|
295 |
public Repository getRepository(String name) throws Exception {
|
|
296 |
return GitBlitSuite.getHelloworldRepository();
|
|
297 |
}
|
|
298 |
|
|
299 |
public List<String> getStrings(String key) {
|
|
300 |
return Arrays.asList("alpha@aaa.com", "beta@bee.com", "gamma@see.com");
|
|
301 |
}
|
|
302 |
|
|
303 |
public List<String> getRepositoryTeams(RepositoryModel repository) {
|
|
304 |
return Arrays.asList("testteam");
|
|
305 |
}
|
|
306 |
|
|
307 |
public TeamModel getTeamModel(String name) {
|
|
308 |
TeamModel model = new TeamModel(name);
|
|
309 |
model.mailingLists.add("list@" + name + ".com");
|
|
310 |
return model;
|
|
311 |
}
|
|
312 |
|
|
313 |
public String getString(String key, String dv) {
|
|
314 |
return dv;
|
|
315 |
}
|
|
316 |
|
|
317 |
public boolean getBoolean(String key, boolean dv) {
|
|
318 |
return dv;
|
|
319 |
}
|
|
320 |
|
|
321 |
public void sendMail(String subject, String message, Collection<String> toAddresses) {
|
|
322 |
messages.add(new MockMail(subject, message, toAddresses));
|
|
323 |
}
|
d60a42
|
324 |
public void sendHtmlMail(String subject, String message, Collection<String> toAddresses) {
|
GS |
325 |
messages.add(new MockMail(subject, message, toAddresses));
|
|
326 |
}
|
357109
|
327 |
}
|
JM |
328 |
|
|
329 |
class MockLogger {
|
|
330 |
List<String> messages = new ArrayList<String>();
|
|
331 |
|
|
332 |
public void info(String message) {
|
|
333 |
messages.add(message);
|
|
334 |
}
|
|
335 |
}
|
a612e6
|
336 |
|
JC |
337 |
class MockClientLogger {
|
|
338 |
List<String> messages = new ArrayList<String>();
|
|
339 |
|
2f5d15
|
340 |
public void info(String message) {
|
a612e6
|
341 |
messages.add(message);
|
JC |
342 |
}
|
2f5d15
|
343 |
|
JM |
344 |
public void error(String message) {
|
|
345 |
messages.add(message);
|
|
346 |
}
|
|
347 |
|
|
348 |
public void error(String message, Throwable t) {
|
|
349 |
PrintWriter writer = new PrintWriter(new StringWriter());
|
|
350 |
if (!StringUtils.isEmpty(message)) {
|
|
351 |
writer.append(message);
|
|
352 |
writer.append('\n');
|
|
353 |
}
|
|
354 |
t.printStackTrace(writer);
|
|
355 |
messages.add(writer.toString());
|
|
356 |
}
|
a612e6
|
357 |
}
|
357109
|
358 |
|
JM |
359 |
class MockMail {
|
|
360 |
final Collection<String> toAddresses;
|
|
361 |
final String subject;
|
|
362 |
final String message;
|
|
363 |
|
|
364 |
MockMail(String subject, String message, Collection<String> toAddresses) {
|
|
365 |
this.subject = subject;
|
|
366 |
this.message = message;
|
|
367 |
this.toAddresses = toAddresses;
|
|
368 |
}
|
|
369 |
|
|
370 |
@Override
|
|
371 |
public String toString() {
|
|
372 |
return StringUtils.flattenStrings(toAddresses, ", ") + "\n\n" + subject + "\n\n"
|
|
373 |
+ message;
|
|
374 |
}
|
|
375 |
}
|
a612e6
|
376 |
}
|