James Moger
2012-03-23 5b29c53b326b5fd283cb06356e94df5f78508860
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
23 import java.io.File;
24 import java.text.MessageFormat;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
ff148d 28 import java.util.Date;
357109 29 import java.util.List;
JM 30 import java.util.concurrent.atomic.AtomicBoolean;
31
32 import org.eclipse.jgit.lib.ObjectId;
33 import org.eclipse.jgit.lib.Repository;
34 import org.eclipse.jgit.transport.ReceiveCommand;
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38
39 import com.gitblit.GitBlit;
40 import com.gitblit.GitBlitException;
41 import com.gitblit.models.RepositoryModel;
42 import com.gitblit.models.TeamModel;
43 import com.gitblit.models.UserModel;
44 import com.gitblit.utils.StringUtils;
45
46 /**
47  * Test class for Groovy scripts. Mostly this is to facilitate development.
48  * 
49  * @author James Moger
50  * 
51  */
52 public class GroovyScriptTest {
53
54     private static final AtomicBoolean started = new AtomicBoolean(false);
55
56     @BeforeClass
57     public static void startGitblit() throws Exception {
58         started.set(GitBlitSuite.startGitblit());
59     }
60
61     @AfterClass
62     public static void stopGitblit() throws Exception {
63         if (started.get()) {
64             GitBlitSuite.stopGitblit();
65         }
66     }
67
68     @Test
69     public void testSendMail() throws Exception {
70         MockGitblit gitblit = new MockGitblit();
71         MockLogger logger = new MockLogger();
72         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
73         commands.add(new ReceiveCommand(ObjectId
74                 .fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
75                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
5e8e7e 76         commands.add(new ReceiveCommand(ObjectId
JM 77                 .fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
78                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master2"));
357109 79
ff148d 80         RepositoryModel repository = GitBlit.self().getRepositoryModel("helloworld.git");
JM 81         repository.mailingLists.add("list@helloworld.git");
82
83         test("sendmail.groovy", gitblit, logger, commands, repository);
357109 84         assertEquals(1, logger.messages.size());
JM 85         assertEquals(1, gitblit.messages.size());
86         MockMail m = gitblit.messages.get(0);
87         assertEquals(5, m.toAddresses.size());
88         assertTrue(m.message.contains("BIT"));
89     }
ff148d 90     
JM 91     @Test
4e7813 92     public void testProtectRefsCreateBranch() throws Exception {
PLM 93         MockGitblit gitblit = new MockGitblit();
94         MockLogger logger = new MockLogger();
95         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
96         commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
97                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
98         
99         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
100
101         test("protect-refs.groovy", gitblit, logger, commands, repository);
102     }
103     
104     @Test
105     public void testProtectRefsCreateTag() throws Exception {
106         MockGitblit gitblit = new MockGitblit();
107         MockLogger logger = new MockLogger();
108         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
109         commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
110                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/tags/v1.0"));
111         
112         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
113
114         test("protect-refs.groovy", gitblit, logger, commands, repository);
115         assertEquals(0, logger.messages.size());
116     }
117     
118     @Test
119     public void testProtectRefsFastForward() throws Exception {
120         MockGitblit gitblit = new MockGitblit();
121         MockLogger logger = new MockLogger();
122         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
123         commands.add(new ReceiveCommand(ObjectId
124                 .fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
125                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
126         
127         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
128
129         test("protect-refs.groovy", gitblit, logger, commands, repository);
130         assertEquals(0, logger.messages.size());
131     }
132     
133     @Test
134     public void testProtectRefsDeleteMasterBranch() throws Exception {
135         MockGitblit gitblit = new MockGitblit();
136         MockLogger logger = new MockLogger();
137         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
138         ReceiveCommand command = new ReceiveCommand(ObjectId
139                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), ObjectId.zeroId(),
140                 "refs/heads/master");
141         commands.add(command);
142         
143         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
144
145         test("protect-refs.groovy", gitblit, logger, commands, repository);
146         assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
147         assertEquals(0, logger.messages.size());
148     }
149     
150     @Test
151     public void testProtectRefsDeleteOtherBranch() throws Exception {
152         MockGitblit gitblit = new MockGitblit();
153         MockLogger logger = new MockLogger();
154         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
155         commands.add(new ReceiveCommand(ObjectId
156                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), ObjectId.zeroId(),
157                 "refs/heads/other"));
158         
159         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
160
161         test("protect-refs.groovy", gitblit, logger, commands, repository);
162         assertEquals(0, logger.messages.size());
163     }
164     
165     @Test
166     public void testProtectRefsDeleteTag() throws Exception {
167         MockGitblit gitblit = new MockGitblit();
168         MockLogger logger = new MockLogger();
169         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
170         ReceiveCommand command = new ReceiveCommand(ObjectId
171                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), ObjectId.zeroId(),
172                 "refs/tags/v1.0");
173         commands.add(command);
174         
175         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
176
177         test("protect-refs.groovy", gitblit, logger, commands, repository);
178         assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
179         assertEquals(0, logger.messages.size());
180     }
181     
182     @Test
ff148d 183     public void testBlockPush() throws Exception {
JM 184         MockGitblit gitblit = new MockGitblit();
185         MockLogger logger = new MockLogger();
186         List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
187         commands.add(new ReceiveCommand(ObjectId
188                 .fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
189                 .fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
190         
191         RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());        
192
193         try {
194             test("blockpush.groovy", gitblit, logger, commands, repository);
195             assertTrue("blockpush should have failed!", false);
196         } catch (GitBlitException e) {
197             assertTrue(e.getMessage().contains("failed"));
198         }
199     }
357109 200
JM 201     private void test(String script, MockGitblit gitblit, MockLogger logger,
ff148d 202             List<ReceiveCommand> commands, RepositoryModel repository) throws Exception {
357109 203
JM 204         UserModel user = new UserModel("mock");
205
206         String gitblitUrl = GitBlitSuite.url;
207
208         File groovyDir = GitBlit.getGroovyScriptsFolder();
209         GroovyScriptEngine gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
210
211         Binding binding = new Binding();
212         binding.setVariable("gitblit", gitblit);
213         binding.setVariable("repository", repository);
214         binding.setVariable("user", user);
215         binding.setVariable("commands", commands);
216         binding.setVariable("url", gitblitUrl);
217         binding.setVariable("logger", logger);
218
219         Object result = gse.run(script, binding);
220         if (result instanceof Boolean) {
221             if (!((Boolean) result)) {
222                 throw new GitBlitException(MessageFormat.format(
223                         "Groovy script {0} has failed!  Hook scripts aborted.", script));
224             }
225         }
226     }
227
228     class MockGitblit {
229         List<MockMail> messages = new ArrayList<MockMail>();
230
231         public Repository getRepository(String name) throws Exception {
232             return GitBlitSuite.getHelloworldRepository();
233         }
234
235         public List<String> getStrings(String key) {
236             return Arrays.asList("alpha@aaa.com", "beta@bee.com", "gamma@see.com");
237         }
238
239         public List<String> getRepositoryTeams(RepositoryModel repository) {
240             return Arrays.asList("testteam");
241         }
242
243         public TeamModel getTeamModel(String name) {
244             TeamModel model = new TeamModel(name);
245             model.mailingLists.add("list@" + name + ".com");
246             return model;
247         }
248
249         public String getString(String key, String dv) {
250             return dv;
251         }
252
253         public boolean getBoolean(String key, boolean dv) {
254             return dv;
255         }
256
257         public void sendMail(String subject, String message, Collection<String> toAddresses) {
258             messages.add(new MockMail(subject, message, toAddresses));
259         }
260     }
261
262     class MockLogger {
263         List<String> messages = new ArrayList<String>();
264
265         public void info(String message) {
266             messages.add(message);
267         }
268     }
269
270     class MockMail {
271         final Collection<String> toAddresses;
272         final String subject;
273         final String message;
274
275         MockMail(String subject, String message, Collection<String> toAddresses) {
276             this.subject = subject;
277             this.message = message;
278             this.toAddresses = toAddresses;
279         }
280
281         @Override
282         public String toString() {
283             return StringUtils.flattenStrings(toAddresses, ", ") + "\n\n" + subject + "\n\n"
284                     + message;
285         }
286     }
287 }