James Moger
2012-10-01 eb1405f736f2f98e14215774dd53eea9b9a77017
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
73     public void testSendMail() throws Exception {
74         MockGitblit gitblit = new MockGitblit();
75         MockLogger logger = new MockLogger();
a612e6 76         MockClientLogger clientLogger = new MockClientLogger();
357109 77         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
JM 78         commands.add(new ReceiveCommand(ObjectId
79                 .fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
80                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
5e8e7e 81         commands.add(new ReceiveCommand(ObjectId
JM 82                 .fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
83                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master2"));
357109 84
ff148d 85         RepositoryModel repository = GitBlit.self().getRepositoryModel("helloworld.git");
JM 86         repository.mailingLists.add("list@helloworld.git");
87
a612e6 88         test("sendmail.groovy", gitblit, logger, clientLogger, commands, repository);
357109 89         assertEquals(1, logger.messages.size());
JM 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     }
ff148d 95     
JM 96     @Test
4e7813 97     public void testProtectRefsCreateBranch() throws Exception {
PLM 98         MockGitblit gitblit = new MockGitblit();
99         MockLogger logger = new MockLogger();
a612e6 100         MockClientLogger clientLogger = new MockClientLogger();
4e7813 101         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
PLM 102         commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
103                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
104         
105         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
106
a612e6 107         test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
4e7813 108     }
PLM 109     
110     @Test
111     public void testProtectRefsCreateTag() throws Exception {
112         MockGitblit gitblit = new MockGitblit();
113         MockLogger logger = new MockLogger();
a612e6 114         MockClientLogger clientLogger = new MockClientLogger();
4e7813 115         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
PLM 116         commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
117                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/tags/v1.0"));
118         
119         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
120
a612e6 121         test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
4e7813 122         assertEquals(0, logger.messages.size());
PLM 123     }
124     
125     @Test
126     public void testProtectRefsFastForward() throws Exception {
127         MockGitblit gitblit = new MockGitblit();
128         MockLogger logger = new MockLogger();
a612e6 129         MockClientLogger clientLogger = new MockClientLogger();
4e7813 130         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
PLM 131         commands.add(new ReceiveCommand(ObjectId
132                 .fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
133                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
134         
135         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
136
a612e6 137         test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
4e7813 138         assertEquals(0, logger.messages.size());
PLM 139     }
140     
141     @Test
142     public void testProtectRefsDeleteMasterBranch() throws Exception {
143         MockGitblit gitblit = new MockGitblit();
144         MockLogger logger = new MockLogger();
a612e6 145         MockClientLogger clientLogger = new MockClientLogger();
4e7813 146         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
PLM 147         ReceiveCommand command = new ReceiveCommand(ObjectId
148                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), ObjectId.zeroId(),
149                 "refs/heads/master");
150         commands.add(command);
151         
152         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
153
a612e6 154         test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
4e7813 155         assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
PLM 156         assertEquals(0, logger.messages.size());
157     }
158     
159     @Test
160     public void testProtectRefsDeleteOtherBranch() throws Exception {
161         MockGitblit gitblit = new MockGitblit();
162         MockLogger logger = new MockLogger();
a612e6 163         MockClientLogger clientLogger = new MockClientLogger();
4e7813 164         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
PLM 165         commands.add(new ReceiveCommand(ObjectId
166                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), ObjectId.zeroId(),
167                 "refs/heads/other"));
168         
169         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
170
a612e6 171         test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
4e7813 172         assertEquals(0, logger.messages.size());
PLM 173     }
174     
175     @Test
176     public void testProtectRefsDeleteTag() throws Exception {
177         MockGitblit gitblit = new MockGitblit();
178         MockLogger logger = new MockLogger();
a612e6 179         MockClientLogger clientLogger = new MockClientLogger();
4e7813 180         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
PLM 181         ReceiveCommand command = new ReceiveCommand(ObjectId
182                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), ObjectId.zeroId(),
183                 "refs/tags/v1.0");
184         commands.add(command);
185         
186         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
187
a612e6 188         test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
4e7813 189         assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
PLM 190         assertEquals(0, logger.messages.size());
191     }
192     
193     @Test
ff148d 194     public void testBlockPush() throws Exception {
JM 195         MockGitblit gitblit = new MockGitblit();
196         MockLogger logger = new MockLogger();
a612e6 197         MockClientLogger clientLogger = new MockClientLogger();
ff148d 198         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
JM 199         commands.add(new ReceiveCommand(ObjectId
200                 .fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
201                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
202         
203         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
204
205         try {
a612e6 206             test("blockpush.groovy", gitblit, logger, clientLogger, commands, repository);
ff148d 207             assertTrue("blockpush should have failed!", false);
JM 208         } catch (GitBlitException e) {
209             assertTrue(e.getMessage().contains("failed"));
210         }
211     }
a612e6 212     
JC 213     @Test
214     public void testClientLogging() throws Exception {
215         MockGitblit gitblit = new MockGitblit();
216         MockLogger logger = new MockLogger();
217         MockClientLogger clientLogger = new MockClientLogger();
218         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
219         commands.add(new ReceiveCommand(ObjectId
220                 .fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
221                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
222         
223         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
224         
225         File groovyDir = GitBlit.getGroovyScriptsFolder();
226         File tempScript = File.createTempFile("testClientLogging", "groovy", groovyDir);
227         tempScript.deleteOnExit();
228         
229         BufferedWriter writer = new BufferedWriter(new FileWriter(tempScript));
230         
2f5d15 231         writer.write("clientLogger.info('this is a test message')\n");
a612e6 232         writer.flush();
JC 233         writer.close();
357109 234
a612e6 235         test(tempScript.getName(), gitblit, logger, clientLogger, commands, repository);
JC 236         assertTrue("Message Missing", clientLogger.messages.contains("this is a test message"));
237     }
238
239     private void test(String script, MockGitblit gitblit, MockLogger logger, MockClientLogger clientLogger,
ff148d 240             List<ReceiveCommand> commands, RepositoryModel repository) throws Exception {
357109 241
JM 242         UserModel user = new UserModel("mock");
243
244         String gitblitUrl = GitBlitSuite.url;
245
246         File groovyDir = GitBlit.getGroovyScriptsFolder();
247         GroovyScriptEngine gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
248
249         Binding binding = new Binding();
250         binding.setVariable("gitblit", gitblit);
251         binding.setVariable("repository", repository);
252         binding.setVariable("user", user);
253         binding.setVariable("commands", commands);
254         binding.setVariable("url", gitblitUrl);
255         binding.setVariable("logger", logger);
a612e6 256         binding.setVariable("clientLogger", clientLogger);
357109 257
JM 258         Object result = gse.run(script, binding);
259         if (result instanceof Boolean) {
260             if (!((Boolean) result)) {
261                 throw new GitBlitException(MessageFormat.format(
262                         "Groovy script {0} has failed!  Hook scripts aborted.", script));
263             }
264         }
265     }
266
267     class MockGitblit {
268         List<MockMail> messages = new ArrayList<MockMail>();
269
270         public Repository getRepository(String name) throws Exception {
271             return GitBlitSuite.getHelloworldRepository();
272         }
273
274         public List<String> getStrings(String key) {
275             return Arrays.asList("alpha@aaa.com", "beta@bee.com", "gamma@see.com");
276         }
277
278         public List<String> getRepositoryTeams(RepositoryModel repository) {
279             return Arrays.asList("testteam");
280         }
281
282         public TeamModel getTeamModel(String name) {
283             TeamModel model = new TeamModel(name);
284             model.mailingLists.add("list@" + name + ".com");
285             return model;
286         }
287
288         public String getString(String key, String dv) {
289             return dv;
290         }
291
292         public boolean getBoolean(String key, boolean dv) {
293             return dv;
294         }
295
296         public void sendMail(String subject, String message, Collection<String> toAddresses) {
297             messages.add(new MockMail(subject, message, toAddresses));
298         }
299     }
300
301     class MockLogger {
302         List<String> messages = new ArrayList<String>();
303
304         public void info(String message) {
305             messages.add(message);
306         }
307     }
a612e6 308     
JC 309     class MockClientLogger {
310         List<String> messages = new ArrayList<String>();
311
2f5d15 312         public void info(String message) {
a612e6 313             messages.add(message);
JC 314         }
2f5d15 315         
JM 316         public void error(String message) {
317             messages.add(message);
318         }
319         
320         public void error(String message, Throwable t) {
321             PrintWriter writer = new PrintWriter(new StringWriter());
322             if (!StringUtils.isEmpty(message)) {
323                 writer.append(message);
324                 writer.append('\n');
325             }
326             t.printStackTrace(writer);
327             messages.add(writer.toString());
328         }
a612e6 329     }
357109 330
JM 331     class MockMail {
332         final Collection<String> toAddresses;
333         final String subject;
334         final String message;
335
336         MockMail(String subject, String message, Collection<String> toAddresses) {
337             this.subject = subject;
338             this.message = message;
339             this.toAddresses = toAddresses;
340         }
341
342         @Override
343         public String toString() {
344             return StringUtils.flattenStrings(toAddresses, ", ") + "\n\n" + subject + "\n\n"
345                     + message;
346         }
347     }
a612e6 348 }