You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GroovyScriptTest.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  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. import groovy.lang.Binding;
  18. import groovy.util.GroovyScriptEngine;
  19. import java.io.BufferedWriter;
  20. import java.io.File;
  21. import java.io.FileWriter;
  22. import java.io.PrintWriter;
  23. import java.io.StringWriter;
  24. import java.text.MessageFormat;
  25. import java.util.ArrayList;
  26. import java.util.Arrays;
  27. import java.util.Collection;
  28. import java.util.Date;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.concurrent.atomic.AtomicBoolean;
  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. import com.gitblit.GitBlitException;
  39. import com.gitblit.models.RepositoryModel;
  40. import com.gitblit.models.TeamModel;
  41. import com.gitblit.models.UserModel;
  42. import com.gitblit.utils.StringUtils;
  43. /**
  44. * Test class for Groovy scripts. Mostly this is to facilitate development.
  45. *
  46. * @author James Moger
  47. *
  48. */
  49. public class GroovyScriptTest extends GitblitUnitTest {
  50. private static final AtomicBoolean started = new AtomicBoolean(false);
  51. @BeforeClass
  52. public static void startGitblit() throws Exception {
  53. started.set(GitBlitSuite.startGitblit());
  54. }
  55. @AfterClass
  56. public static void stopGitblit() throws Exception {
  57. if (started.get()) {
  58. GitBlitSuite.stopGitblit();
  59. }
  60. }
  61. @Test
  62. public void testFogbugz() throws Exception {
  63. MockGitblit gitblit = new MockGitblit();
  64. MockLogger logger = new MockLogger();
  65. MockClientLogger clientLogger = new MockClientLogger();
  66. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  67. commands.add(new ReceiveCommand(ObjectId
  68. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
  69. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
  70. commands.add(new ReceiveCommand(ObjectId
  71. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
  72. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master2"));
  73. RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
  74. repository.customFields = new HashMap<String,String>();
  75. repository.customFields.put( "fogbugzUrl", "http://bugs.test.com" );
  76. repository.customFields.put( "fogbugzRepositoryId", "1" );
  77. repository.customFields.put( "fogbugzCommitMessageRegex", "\\s*[Bb][Uu][Gg][(Zz)(Ss)]*\\s*[(IDs)]*\\s*[#:; ]+((\\d+[ ,:;#]*)+)" );
  78. test("fogbugz.groovy", gitblit, logger, clientLogger, commands, repository);
  79. }
  80. @Test
  81. public void testSendHtmlMail() throws Exception {
  82. MockGitblit gitblit = new MockGitblit();
  83. MockLogger logger = new MockLogger();
  84. MockClientLogger clientLogger = new MockClientLogger();
  85. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  86. commands.add(new ReceiveCommand(ObjectId
  87. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
  88. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
  89. commands.add(new ReceiveCommand(ObjectId
  90. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
  91. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master2"));
  92. RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
  93. repository.mailingLists.add("list@helloworld.git");
  94. test("sendmail-html.groovy", gitblit, logger, clientLogger, commands, repository);
  95. assertEquals(1, logger.messages.size());
  96. assertEquals(1, gitblit.messages.size());
  97. MockMail m = gitblit.messages.get(0);
  98. assertEquals(5, m.toAddresses.size());
  99. assertTrue(m.message.contains("BIT"));
  100. assertTrue(m.message.contains("<html>"));
  101. }
  102. @Test
  103. public void testSendMail() throws Exception {
  104. MockGitblit gitblit = new MockGitblit();
  105. MockLogger logger = new MockLogger();
  106. MockClientLogger clientLogger = new MockClientLogger();
  107. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  108. commands.add(new ReceiveCommand(ObjectId
  109. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
  110. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
  111. commands.add(new ReceiveCommand(ObjectId
  112. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
  113. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master2"));
  114. RepositoryModel repository = repositories().getRepositoryModel("helloworld.git");
  115. repository.mailingLists.add("list@helloworld.git");
  116. test("sendmail.groovy", gitblit, logger, clientLogger, commands, repository);
  117. assertEquals(1, logger.messages.size());
  118. assertEquals(1, gitblit.messages.size());
  119. MockMail m = gitblit.messages.get(0);
  120. assertEquals(5, m.toAddresses.size());
  121. assertTrue(m.message.contains("BIT"));
  122. }
  123. @Test
  124. public void testProtectRefsCreateBranch() throws Exception {
  125. MockGitblit gitblit = new MockGitblit();
  126. MockLogger logger = new MockLogger();
  127. MockClientLogger clientLogger = new MockClientLogger();
  128. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  129. commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
  130. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
  131. RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
  132. test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
  133. }
  134. @Test
  135. public void testProtectRefsCreateTag() throws Exception {
  136. MockGitblit gitblit = new MockGitblit();
  137. MockLogger logger = new MockLogger();
  138. MockClientLogger clientLogger = new MockClientLogger();
  139. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  140. commands.add(new ReceiveCommand(ObjectId.zeroId(), ObjectId
  141. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/tags/v1.0"));
  142. RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
  143. test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
  144. assertEquals(0, logger.messages.size());
  145. }
  146. @Test
  147. public void testProtectRefsFastForward() throws Exception {
  148. MockGitblit gitblit = new MockGitblit();
  149. MockLogger logger = new MockLogger();
  150. MockClientLogger clientLogger = new MockClientLogger();
  151. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  152. commands.add(new ReceiveCommand(ObjectId
  153. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
  154. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
  155. RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
  156. test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
  157. assertEquals(0, logger.messages.size());
  158. }
  159. @Test
  160. public void testProtectRefsDeleteMasterBranch() throws Exception {
  161. MockGitblit gitblit = new MockGitblit();
  162. MockLogger logger = new MockLogger();
  163. MockClientLogger clientLogger = new MockClientLogger();
  164. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  165. ReceiveCommand command = new ReceiveCommand(ObjectId
  166. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), ObjectId.zeroId(),
  167. "refs/heads/master");
  168. commands.add(command);
  169. RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
  170. test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
  171. assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
  172. assertEquals(0, logger.messages.size());
  173. }
  174. @Test
  175. public void testProtectRefsDeleteOtherBranch() throws Exception {
  176. MockGitblit gitblit = new MockGitblit();
  177. MockLogger logger = new MockLogger();
  178. MockClientLogger clientLogger = new MockClientLogger();
  179. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  180. commands.add(new ReceiveCommand(ObjectId
  181. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), ObjectId.zeroId(),
  182. "refs/heads/other"));
  183. RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
  184. test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
  185. assertEquals(0, logger.messages.size());
  186. }
  187. @Test
  188. public void testProtectRefsDeleteTag() throws Exception {
  189. MockGitblit gitblit = new MockGitblit();
  190. MockLogger logger = new MockLogger();
  191. MockClientLogger clientLogger = new MockClientLogger();
  192. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  193. ReceiveCommand command = new ReceiveCommand(ObjectId
  194. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), ObjectId.zeroId(),
  195. "refs/tags/v1.0");
  196. commands.add(command);
  197. RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
  198. test("protect-refs.groovy", gitblit, logger, clientLogger, commands, repository);
  199. assertEquals(ReceiveCommand.Result.REJECTED_NODELETE, command.getResult());
  200. assertEquals(0, logger.messages.size());
  201. }
  202. @Test
  203. public void testBlockPush() throws Exception {
  204. MockGitblit gitblit = new MockGitblit();
  205. MockLogger logger = new MockLogger();
  206. MockClientLogger clientLogger = new MockClientLogger();
  207. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  208. commands.add(new ReceiveCommand(ObjectId
  209. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
  210. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
  211. RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
  212. try {
  213. test("blockpush.groovy", gitblit, logger, clientLogger, commands, repository);
  214. assertTrue("blockpush should have failed!", false);
  215. } catch (GitBlitException e) {
  216. assertTrue(e.getMessage().contains("failed"));
  217. }
  218. }
  219. @Test
  220. public void testClientLogging() throws Exception {
  221. MockGitblit gitblit = new MockGitblit();
  222. MockLogger logger = new MockLogger();
  223. MockClientLogger clientLogger = new MockClientLogger();
  224. List<ReceiveCommand> commands = new ArrayList<ReceiveCommand>();
  225. commands.add(new ReceiveCommand(ObjectId
  226. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.fifth)), ObjectId
  227. .fromString(GitBlitSuite.helloworldSettings.getRequiredString(HelloworldKeys.commit.deleted)), "refs/heads/master"));
  228. RepositoryModel repository = new RepositoryModel("ex@mple.git", "", "admin", new Date());
  229. File groovyDir = repositories().getHooksFolder();
  230. File tempScript = File.createTempFile("testClientLogging", "groovy", groovyDir);
  231. tempScript.deleteOnExit();
  232. BufferedWriter writer = new BufferedWriter(new FileWriter(tempScript));
  233. writer.write("clientLogger.info('this is a test message')\n");
  234. writer.flush();
  235. writer.close();
  236. test(tempScript.getName(), gitblit, logger, clientLogger, commands, repository);
  237. assertTrue("Message Missing", clientLogger.messages.contains("this is a test message"));
  238. }
  239. private void test(String script, MockGitblit gitblit, MockLogger logger, MockClientLogger clientLogger,
  240. List<ReceiveCommand> commands, RepositoryModel repository) throws Exception {
  241. UserModel user = new UserModel("mock");
  242. String gitblitUrl = GitBlitSuite.url;
  243. File groovyDir = repositories().getHooksFolder();
  244. GroovyScriptEngine gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
  245. Binding binding = new Binding();
  246. binding.setVariable("gitblit", gitblit);
  247. binding.setVariable("repository", repository);
  248. binding.setVariable("user", user);
  249. binding.setVariable("commands", commands);
  250. binding.setVariable("url", gitblitUrl);
  251. binding.setVariable("logger", logger);
  252. binding.setVariable("clientLogger", clientLogger);
  253. Object result = gse.run(script, binding);
  254. if (result instanceof Boolean) {
  255. if (!((Boolean) result)) {
  256. throw new GitBlitException(MessageFormat.format(
  257. "Groovy script {0} has failed! Hook scripts aborted.", script));
  258. }
  259. }
  260. }
  261. class MockGitblit {
  262. List<MockMail> messages = new ArrayList<MockMail>();
  263. public Repository getRepository(String name) throws Exception {
  264. return GitBlitSuite.getHelloworldRepository();
  265. }
  266. public List<String> getStrings(String key) {
  267. return Arrays.asList("alpha@aaa.com", "beta@bee.com", "gamma@see.com");
  268. }
  269. public List<String> getRepositoryTeams(RepositoryModel repository) {
  270. return Arrays.asList("testteam");
  271. }
  272. public TeamModel getTeamModel(String name) {
  273. TeamModel model = new TeamModel(name);
  274. model.mailingLists.add("list@" + name + ".com");
  275. return model;
  276. }
  277. public String getString(String key, String dv) {
  278. return dv;
  279. }
  280. public boolean getBoolean(String key, boolean dv) {
  281. return dv;
  282. }
  283. public int getInteger(String key, int defaultValue) {
  284. return defaultValue;
  285. }
  286. public void sendMail(String subject, String message, Collection<String> toAddresses) {
  287. messages.add(new MockMail(subject, message, toAddresses));
  288. }
  289. public void sendHtmlMail(String subject, String message, Collection<String> toAddresses) {
  290. messages.add(new MockMail(subject, message, toAddresses));
  291. }
  292. }
  293. class MockLogger {
  294. List<String> messages = new ArrayList<String>();
  295. public void info(String message) {
  296. messages.add(message);
  297. }
  298. }
  299. class MockClientLogger {
  300. List<String> messages = new ArrayList<String>();
  301. public void info(String message) {
  302. messages.add(message);
  303. }
  304. public void error(String message) {
  305. messages.add(message);
  306. }
  307. public void error(String message, Throwable t) {
  308. PrintWriter writer = new PrintWriter(new StringWriter());
  309. if (!StringUtils.isEmpty(message)) {
  310. writer.append(message);
  311. writer.append('\n');
  312. }
  313. t.printStackTrace(writer);
  314. messages.add(writer.toString());
  315. }
  316. }
  317. class MockMail {
  318. final Collection<String> toAddresses;
  319. final String subject;
  320. final String message;
  321. MockMail(String subject, String message, Collection<String> toAddresses) {
  322. this.subject = subject;
  323. this.message = message;
  324. this.toAddresses = toAddresses;
  325. }
  326. @Override
  327. public String toString() {
  328. return StringUtils.flattenStrings(toAddresses, ", ") + "\n\n" + subject + "\n\n"
  329. + message;
  330. }
  331. }
  332. }