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
|
|
92 |
public void testBlockPush() throws Exception {
|
|
93 |
MockGitblit gitblit = new MockGitblit();
|
|
94 |
MockLogger logger = new MockLogger();
|
|
95 |
List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
|
|
96 |
commands.add(new ReceiveCommand(ObjectId
|
|
97 |
.fromString("c18877690322dfc6ae3e37bb7f7085a24e94e887"), ObjectId
|
|
98 |
.fromString("3fa7c46d11b11d61f1cbadc6888be5d0eae21969"), "refs/heads/master"));
|
|
99 |
|
|
100 |
RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
|
|
101 |
|
|
102 |
try {
|
|
103 |
test("blockpush.groovy", gitblit, logger, commands, repository);
|
|
104 |
assertTrue("blockpush should have failed!", false);
|
|
105 |
} catch (GitBlitException e) {
|
|
106 |
assertTrue(e.getMessage().contains("failed"));
|
|
107 |
}
|
|
108 |
}
|
357109
|
109 |
|
JM |
110 |
private void test(String script, MockGitblit gitblit, MockLogger logger,
|
ff148d
|
111 |
List<ReceiveCommand> commands, RepositoryModel repository) throws Exception {
|
357109
|
112 |
|
JM |
113 |
UserModel user = new UserModel("mock");
|
|
114 |
|
|
115 |
String gitblitUrl = GitBlitSuite.url;
|
|
116 |
|
|
117 |
File groovyDir = GitBlit.getGroovyScriptsFolder();
|
|
118 |
GroovyScriptEngine gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
|
|
119 |
|
|
120 |
Binding binding = new Binding();
|
|
121 |
binding.setVariable("gitblit", gitblit);
|
|
122 |
binding.setVariable("repository", repository);
|
|
123 |
binding.setVariable("user", user);
|
|
124 |
binding.setVariable("commands", commands);
|
|
125 |
binding.setVariable("url", gitblitUrl);
|
|
126 |
binding.setVariable("logger", logger);
|
|
127 |
|
|
128 |
Object result = gse.run(script, binding);
|
|
129 |
if (result instanceof Boolean) {
|
|
130 |
if (!((Boolean) result)) {
|
|
131 |
throw new GitBlitException(MessageFormat.format(
|
|
132 |
"Groovy script {0} has failed! Hook scripts aborted.", script));
|
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
class MockGitblit {
|
|
138 |
List<MockMail> messages = new ArrayList<MockMail>();
|
|
139 |
|
|
140 |
public Repository getRepository(String name) throws Exception {
|
|
141 |
return GitBlitSuite.getHelloworldRepository();
|
|
142 |
}
|
|
143 |
|
|
144 |
public List<String> getStrings(String key) {
|
|
145 |
return Arrays.asList("alpha@aaa.com", "beta@bee.com", "gamma@see.com");
|
|
146 |
}
|
|
147 |
|
|
148 |
public List<String> getRepositoryTeams(RepositoryModel repository) {
|
|
149 |
return Arrays.asList("testteam");
|
|
150 |
}
|
|
151 |
|
|
152 |
public TeamModel getTeamModel(String name) {
|
|
153 |
TeamModel model = new TeamModel(name);
|
|
154 |
model.mailingLists.add("list@" + name + ".com");
|
|
155 |
return model;
|
|
156 |
}
|
|
157 |
|
|
158 |
public String getString(String key, String dv) {
|
|
159 |
return dv;
|
|
160 |
}
|
|
161 |
|
|
162 |
public boolean getBoolean(String key, boolean dv) {
|
|
163 |
return dv;
|
|
164 |
}
|
|
165 |
|
|
166 |
public void sendMail(String subject, String message, Collection<String> toAddresses) {
|
|
167 |
messages.add(new MockMail(subject, message, toAddresses));
|
|
168 |
}
|
|
169 |
}
|
|
170 |
|
|
171 |
class MockLogger {
|
|
172 |
List<String> messages = new ArrayList<String>();
|
|
173 |
|
|
174 |
public void info(String message) {
|
|
175 |
messages.add(message);
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
class MockMail {
|
|
180 |
final Collection<String> toAddresses;
|
|
181 |
final String subject;
|
|
182 |
final String message;
|
|
183 |
|
|
184 |
MockMail(String subject, String message, Collection<String> toAddresses) {
|
|
185 |
this.subject = subject;
|
|
186 |
this.message = message;
|
|
187 |
this.toAddresses = toAddresses;
|
|
188 |
}
|
|
189 |
|
|
190 |
@Override
|
|
191 |
public String toString() {
|
|
192 |
return StringUtils.flattenStrings(toAddresses, ", ") + "\n\n" + subject + "\n\n"
|
|
193 |
+ message;
|
|
194 |
}
|
|
195 |
}
|
|
196 |
} |