/src/test/java/com/atlassian/bamboo/plugins/git/GitRepositoryValidationTest.java

https://bitbucket.org/andsliz/bamboo-git-plugin · Java · 236 lines · 214 code · 20 blank · 2 comment · 3 complexity · 270c97663d4384c8a3f4d6f8737b068e MD5 · raw file

  1. package com.atlassian.bamboo.plugins.git;
  2. import com.atlassian.bamboo.FeatureManager;
  3. import com.atlassian.bamboo.core.TransportProtocol;
  4. import com.atlassian.bamboo.utils.error.ErrorCollection;
  5. import com.atlassian.bamboo.variable.CustomVariableContext;
  6. import com.atlassian.bamboo.ww2.actions.build.admin.create.BuildConfiguration;
  7. import com.atlassian.sal.api.message.I18nResolver;
  8. import org.mockito.Matchers;
  9. import org.mockito.Mockito;
  10. import org.mockito.internal.stubbing.defaultanswers.ReturnsMocks;
  11. import org.testng.Assert;
  12. import org.testng.annotations.DataProvider;
  13. import org.testng.annotations.Test;
  14. import java.util.List;
  15. import static com.atlassian.bamboo.plugins.git.GitAuthenticationType.NONE;
  16. import static com.atlassian.bamboo.plugins.git.GitAuthenticationType.PASSWORD;
  17. import static com.atlassian.bamboo.plugins.git.GitAuthenticationType.SSH_KEYPAIR;
  18. import static com.google.common.collect.Lists.newArrayList;
  19. import static org.mockito.Mockito.mock;
  20. import static org.mockito.Mockito.when;
  21. public class GitRepositoryValidationTest
  22. {
  23. @DataProvider
  24. Object[][] invalidMavenPaths()
  25. {
  26. return new String[][] {
  27. {".."},
  28. {"../relative/pom.xml"},
  29. {"path/../path/pom.xml"},
  30. };
  31. }
  32. @Test(dataProvider = "invalidMavenPaths")
  33. public void testValidateDots(String mavenPath) throws Exception
  34. {
  35. BuildConfiguration conf = new BuildConfiguration();
  36. conf.setProperty("repository.git.maven.path", mavenPath);
  37. GitRepository repo = createRepository();
  38. ErrorCollection errorCollection = repo.validate(conf);
  39. Assert.assertNotNull(errorCollection.getErrors().get("repository.git.maven.path"));
  40. }
  41. @DataProvider(parallel = true)
  42. Object[][] urlUsernamePasswordCombinations()
  43. {
  44. return new Object [][] {
  45. { "http://host/repo", null, null, NONE, null},
  46. { "http://host/repo", null, null, PASSWORD, null}, // let's accept empty username and passwords so far
  47. { "http://host/repo", null, null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  48. { "http://host/repo", "username", null, NONE, null}, // set but ignored - no reason to complain
  49. { "http://host/repo", "username", null, PASSWORD, null},
  50. { "http://host/repo", "username", null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  51. { "http://host/repo", null, "password", NONE, null}, // set but ignored - no reason to complain
  52. { "http://host/repo", null, "password", PASSWORD, null},
  53. { "http://host/repo", null, "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  54. { "http://host/repo", "username", "password", NONE, null}, // set but ignored - no reason to complain
  55. { "http://host/repo", "username", "password", PASSWORD, null},
  56. { "http://host/repo", "username", "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  57. { "http://username@host/repo", null, null, NONE, null},
  58. { "http://username@host/repo", null, null, PASSWORD, null},
  59. { "http://username@host/repo", null, null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  60. { "http://username@host/repo", "username", null, NONE, null}, // duplicate username, but ignored - no reason to complain
  61. { "http://username@host/repo", "username", null, PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username
  62. { "http://username@host/repo", "username", null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  63. { "http://username@host/repo", null, "password", NONE, null}, // password set but ignored - no reason to complain
  64. { "http://username@host/repo", null, "password", PASSWORD, null},
  65. { "http://username@host/repo", null, "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  66. { "http://username@host/repo", "username", "password", NONE, null}, // duplicate username but ignored - no reason to complain
  67. { "http://username@host/repo", "username", "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username},
  68. { "http://username@host/repo", "username", "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  69. { "http://username:password@host/repo", null, null, NONE, null},
  70. { "http://username:password@host/repo", null, null, PASSWORD, null},
  71. { "http://username:password@host/repo", null, null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  72. { "http://username:password@host/repo", "username", null, NONE, null}, // duplicate username, but ignored - no reason to complain
  73. { "http://username:password@host/repo", "username", null, PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username
  74. { "http://username:password@host/repo", "username", null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  75. { "http://username:password@host/repo", null, "password", NONE, null}, // duplicate password but ignored - no reason to complain
  76. { "http://username:password@host/repo", null, "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "temporary.git.password.change")}, //duplicate password
  77. { "http://username:password@host/repo", null, "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  78. { "http://username:password@host/repo", "username", "password", NONE, null}, // duplicate username and password but ignored - no reason to complain
  79. { "http://username:password@host/repo", "username", "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username", "temporary.git.password.change")}, //duplicate username and password
  80. { "http://username:password@host/repo", "username", "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  81. // same as above but https
  82. { "https://host/repo", null, null, NONE, null},
  83. { "https://host/repo", null, null, PASSWORD, null}, // let's accept empty username and passwords so far
  84. { "https://host/repo", null, null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  85. { "https://host/repo", "username", null, NONE, null}, // set but ignored - no reason to complain
  86. { "https://host/repo", "username", null, PASSWORD, null},
  87. { "https://host/repo", "username", null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  88. { "https://host/repo", null, "password", NONE, null}, // set but ignored - no reason to complain
  89. { "https://host/repo", null, "password", PASSWORD, null},
  90. { "https://host/repo", null, "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  91. { "https://host/repo", "username", "password", NONE, null}, // set but ignored - no reason to complain
  92. { "https://host/repo", "username", "password", PASSWORD, null},
  93. { "https://host/repo", "username", "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  94. { "https://username@host/repo", null, null, NONE, null},
  95. { "https://username@host/repo", null, null, PASSWORD, null},
  96. { "https://username@host/repo", null, null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  97. { "https://username@host/repo", "username", null, NONE, null}, // duplicate username, but ignored - no reason to complain
  98. { "https://username@host/repo", "username", null, PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username
  99. { "https://username@host/repo", "username", null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  100. { "https://username@host/repo", null, "password", NONE, null}, // password set but ignored - no reason to complain
  101. { "https://username@host/repo", null, "password", PASSWORD, null},
  102. { "https://username@host/repo", null, "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  103. { "https://username@host/repo", "username", "password", NONE, null}, // duplicate username but ignored - no reason to complain
  104. { "https://username@host/repo", "username", "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username},
  105. { "https://username@host/repo", "username", "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  106. { "https://username:password@host/repo", null, null, NONE, null},
  107. { "https://username:password@host/repo", null, null, PASSWORD, null},
  108. { "https://username:password@host/repo", null, null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  109. { "https://username:password@host/repo", "username", null, NONE, null}, // duplicate username, but ignored - no reason to complain
  110. { "https://username:password@host/repo", "username", null, PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username
  111. { "https://username:password@host/repo", "username", null, SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  112. { "https://username:password@host/repo", null, "password", NONE, null}, // duplicate password but ignored - no reason to complain
  113. { "https://username:password@host/repo", null, "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "temporary.git.password.change")}, //duplicate password
  114. { "https://username:password@host/repo", null, "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  115. { "https://username:password@host/repo", "username", "password", NONE, null}, // duplicate username and password but ignored - no reason to complain
  116. { "https://username:password@host/repo", "username", "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username", "temporary.git.password.change")}, //duplicate username and password
  117. { "https://username:password@host/repo", "username", "password", SSH_KEYPAIR, newArrayList("repository.git.authenticationType")}, // invalid - you can't use http with keyfile
  118. { "ssh://host/repo", null, null, NONE, null},
  119. { "ssh://host/repo", null, null, PASSWORD, null}, // let's accept empty username and passwords so far
  120. { "ssh://host/repo", null, null, SSH_KEYPAIR, null},
  121. { "ssh://host/repo", "username", null, NONE, null}, // set but ignored - no reason to complain
  122. { "ssh://host/repo", "username", null, PASSWORD, null},
  123. { "ssh://host/repo", "username", null, SSH_KEYPAIR, null}, // set but ignored - no reason to complain
  124. { "ssh://host/repo", null, "password", NONE, null}, // set but ignored - no reason to complain
  125. { "ssh://host/repo", null, "password", PASSWORD, null},
  126. { "ssh://host/repo", null, "password", SSH_KEYPAIR, null}, // set but ignored - no reason to complain
  127. { "ssh://host/repo", "username", "password", NONE, null}, // set but ignored - no reason to complain
  128. { "ssh://host/repo", "username", "password", PASSWORD, null},
  129. { "ssh://host/repo", "username", "password", SSH_KEYPAIR, null}, // set but ignored - no reason to complain
  130. { "ssh://username@host/repo", null, null, NONE, null},
  131. { "ssh://username@host/repo", null, null, PASSWORD, null},
  132. { "ssh://username@host/repo", null, null, SSH_KEYPAIR, null},
  133. { "ssh://username@host/repo", "username", null, NONE, null}, // duplicate username, but ignored - no reason to complain
  134. { "ssh://username@host/repo", "username", null, PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username
  135. { "ssh://username@host/repo", "username", null, SSH_KEYPAIR, null}, // duplicate username but ignored - no reason to complain
  136. { "ssh://username@host/repo", null, "password", NONE, null}, // password set but ignored - no reason to complain
  137. { "ssh://username@host/repo", null, "password", PASSWORD, null},
  138. { "ssh://username@host/repo", null, "password", SSH_KEYPAIR, null},
  139. { "ssh://username@host/repo", "username", "password", NONE, null}, // duplicate username but ignored - no reason to complain
  140. { "ssh://username@host/repo", "username", "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username},
  141. { "ssh://username@host/repo", "username", "password", SSH_KEYPAIR, null}, // duplicate username but ignored - no reason to complain
  142. { "ssh://username:password@host/repo", null, null, NONE, null},
  143. { "ssh://username:password@host/repo", null, null, PASSWORD, null},
  144. { "ssh://username:password@host/repo", null, null, SSH_KEYPAIR, null},
  145. { "ssh://username:password@host/repo", "username", null, NONE, null}, // duplicate username, but ignored - no reason to complain
  146. { "ssh://username:password@host/repo", "username", null, PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username
  147. { "ssh://username:password@host/repo", "username", null, SSH_KEYPAIR, null}, // duplicate username, but ignored - no reason to complain
  148. { "ssh://username:password@host/repo", null, "password", NONE, null}, // duplicate password but ignored - no reason to complain
  149. { "ssh://username:password@host/repo", null, "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "temporary.git.password.change")}, //duplicate password
  150. { "ssh://username:password@host/repo", null, "password", SSH_KEYPAIR, null}, // duplicate password but ignored - no reason to complain
  151. { "ssh://username:password@host/repo", "username", "password", NONE, null}, // duplicate username and password but ignored - no reason to complain
  152. { "ssh://username:password@host/repo", "username", "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username", "temporary.git.password.change")}, //duplicate username and password
  153. { "ssh://username:password@host/repo", "username", "password", SSH_KEYPAIR, null}, // duplicate username and password but ignored - no reason to complain
  154. { "username@host:repo", null, null, NONE, null},
  155. { "username@host:repo", null, null, PASSWORD, null},
  156. { "username@host:repo", null, null, SSH_KEYPAIR, null},
  157. { "username@host:repo", "username", null, NONE, null}, // duplicate username but ignored - no reason to complain
  158. { "username@host:repo", "username", null, PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username
  159. { "username@host:repo", "username", null, SSH_KEYPAIR, null}, // duplicate username but ignored - no reason to complain
  160. { "username@host:repo", null, "password", NONE, null}, // password set but ignored - no reason to complain
  161. { "username@host:repo", null, "password", PASSWORD, null},
  162. { "username@host:repo", null, "password", SSH_KEYPAIR, null}, // password set but ignored - no reason to complain
  163. { "username@host:repo", "username", "password", NONE, null}, // duplicate username but ignored - no reason to complain
  164. { "username@host:repo", "username", "password", PASSWORD, newArrayList("repository.git.repositoryUrl", "repository.git.username")}, //duplicate username
  165. { "username@host:repo", "username", "password", SSH_KEYPAIR, null}, // duplicate username but ignored - no reason to complain
  166. // invalid ones:
  167. { "host/repo", "username", null, PASSWORD, newArrayList("repository.git.username")},
  168. { "/host/repo", "username", null, PASSWORD, newArrayList("repository.git.username")},
  169. };
  170. }
  171. @Test(dataProvider = "urlUsernamePasswordCombinations")
  172. public void testUrlUsernamePasswordCombinations(String url, String username, String password, GitAuthenticationType authenticationType, List<String> expectedErrorFields) throws Exception
  173. {
  174. ErrorCollection errorCollection = doValidateConfiguration(url, username, password, authenticationType);
  175. if (expectedErrorFields != null)
  176. {
  177. Assert.assertTrue(errorCollection.hasAnyErrors());
  178. Assert.assertEquals(errorCollection.getTotalErrors(), expectedErrorFields.size());
  179. for (String field : expectedErrorFields)
  180. {
  181. Assert.assertNotNull(errorCollection.getFieldErrors().get(field), errorCollection.toString());
  182. }
  183. }
  184. else
  185. {
  186. Assert.assertFalse(errorCollection.hasAnyErrors());
  187. }
  188. }
  189. @Test
  190. public void testInvalidAuthForHttp() throws Exception
  191. {
  192. ErrorCollection errorCollection = doValidateConfiguration("http://host/repo", null, null, SSH_KEYPAIR);
  193. Assert.assertTrue(errorCollection.hasAnyErrors());
  194. Assert.assertNotNull(errorCollection.getFieldErrors().get("repository.git.authenticationType"));
  195. }
  196. private ErrorCollection doValidateConfiguration(String url, String username, String password, GitAuthenticationType authenticationType)
  197. {
  198. GitRepository repository = createRepository();
  199. BuildConfiguration buildConfiguration = new BuildConfiguration();
  200. buildConfiguration.setProperty("repository.git.repositoryUrl", url);
  201. buildConfiguration.setProperty("repository.git.username", username);
  202. buildConfiguration.setProperty("repository.git.password", password);
  203. buildConfiguration.setProperty("repository.git.authenticationType", authenticationType.name());
  204. return repository.validate(buildConfiguration);
  205. }
  206. private static GitRepository createRepository()
  207. {
  208. GitRepository repo = new GitRepository();
  209. repo.setCustomVariableContext(Mockito.mock(CustomVariableContext.class));
  210. repo.setI18nResolver(Mockito.mock(I18nResolver.class, new ReturnsMocks()));
  211. FeatureManager featureManager = mock(FeatureManager.class);
  212. when(featureManager.isTransportSupported(Matchers.<TransportProtocol>any())).thenReturn(true);
  213. repo.setFeatureManager(featureManager);
  214. return repo;
  215. }
  216. }