2 * Copyright 2000-2012 JetBrains s.r.o.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package jetbrains.buildServer.buildTriggers.vcs.git.tests;
19 import jetbrains.buildServer.BaseTestCase;
20 import jetbrains.buildServer.TempFiles;
21 import jetbrains.buildServer.buildTriggers.vcs.git.*;
22 import jetbrains.buildServer.serverSide.ServerPaths;
23 import jetbrains.buildServer.util.StringUtil;
24 import jetbrains.buildServer.vcs.Credentials;
25 import jetbrains.buildServer.vcs.MavenVcsUrl;
26 import jetbrains.buildServer.vcs.VcsException;
27 import jetbrains.buildServer.vcs.VcsUrl;
28 import jetbrains.buildServer.vcs.impl.VcsRootImpl;
29 import org.eclipse.jgit.transport.URIish;
30 import org.jetbrains.annotations.NotNull;
31 import org.testng.annotations.AfterMethod;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.Test;
35 import java.io.IOException;
36 import java.net.MalformedURLException;
37 import java.net.URISyntaxException;
38 import java.util.Arrays;
39 import java.util.List;
42 import static jetbrains.buildServer.buildTriggers.vcs.git.tests.GitSupportBuilder.gitSupport;
45 * @author dmitry.neverov
47 public class GitUrlSupportTest extends BaseTestCase {
49 private TempFiles myTempFiles = new TempFiles();
50 private GitUrlSupport myUrlSupport;
51 private MirrorManager myMirrorManager;
54 public void setUp() throws IOException {
55 ServerPaths paths = new ServerPaths(myTempFiles.createTempDir().getAbsolutePath());
56 PluginConfig config = new PluginConfigBuilder(paths).build();
57 myMirrorManager = new MirrorManagerImpl(config, new HashCalculatorImpl());
59 GitVcsSupport vcsSupport = gitSupport().withServerPaths(paths).build();
60 myUrlSupport = new GitUrlSupport(vcsSupport);
64 public void tearDown() {
65 myTempFiles.cleanup();
69 public void test_convert() throws MalformedURLException, VcsException, URISyntaxException {
70 List<String> urls = Arrays.asList("scm:git:git://github.com/path_to_repository",
71 "scm:git:http://github.com/path_to_repository",
72 "scm:git:https://github.com/path_to_repository",
73 "scm:git:ssh://github.com/path_to_repository",
74 "scm:git:file://localhost/path_to_repository",
75 "scm:git:ssh://github.com/nd/regex.git/pom.xml");
77 for (String url : urls) {
78 MavenVcsUrl vcsUrl = new MavenVcsUrl(url);
79 GitVcsRoot root = toGitRoot(vcsUrl);
80 assertEquals(new URIish(vcsUrl.getProviderSpecificPart()), root.getRepositoryFetchURL());
81 checkAuthMethod(vcsUrl, root);
86 for (String url : urls) {
87 MavenVcsUrl vcsUrl = new MavenVcsUrl(url, new Credentials(user, pass));
88 GitVcsRoot s = toGitRoot(vcsUrl);
89 checkAuthMethod(vcsUrl, s);
95 public void should_not_throw_exception_when_url_is_from_other_provider() throws MalformedURLException, VcsException {
96 VcsUrl url = new VcsUrl("scm:svn:ssh://svn.repo.com/path_to_repository");
97 assertNull(myUrlSupport.convertToVcsRootProperties(url));
99 url = new VcsUrl("svn://svn.repo.com/path_to_repository");
100 assertNull(myUrlSupport.convertToVcsRootProperties(url));
105 public void convert_scp_like_syntax() throws Exception {
106 MavenVcsUrl url = new MavenVcsUrl("scm:git:git@github.com:user/repo.git");
107 GitVcsRoot root = toGitRoot(url);
108 assertEquals(new URIish(url.getProviderSpecificPart()), root.getRepositoryFetchURL());
109 assertEquals(AuthenticationMethod.PRIVATE_KEY_DEFAULT, root.getAuthSettings().getAuthMethod());
110 assertEquals("git", root.getAuthSettings().toMap().get(Constants.USERNAME));
115 public void convert_scp_like_syntax_with_credentials() throws Exception {
116 VcsUrl url = new VcsUrl("scm:git:git@github.com:user/repo.git", new Credentials("user", "pass"));
117 GitVcsRoot root = toGitRoot(url);
118 assertEquals(new URIish("user@github.com:user/repo.git"), root.getRepositoryFetchURL());
119 assertEquals(AuthenticationMethod.PRIVATE_KEY_DEFAULT, root.getAuthSettings().getAuthMethod());
120 assertEquals("user", root.getAuthSettings().toMap().get(Constants.USERNAME));
121 assertNull(root.getAuthSettings().toMap().get(Constants.PASSWORD));
123 assertEquals(root.getProperties(),
124 myUrlSupport.convertToVcsRootProperties(new VcsUrl("git@github.com:user/repo.git", new Credentials("user", "pass"))));
128 public void http_protocol() throws Exception {
129 VcsUrl url = new VcsUrl("https://github.com/JetBrains/kotlin.git");
130 GitVcsRoot root = toGitRoot(url);
132 assertEquals("https://github.com/JetBrains/kotlin.git", root.getProperty(Constants.FETCH_URL));
133 assertEquals("refs/heads/master", root.getProperty(Constants.BRANCH_NAME));
134 assertEquals(AuthenticationMethod.ANONYMOUS.name(), root.getProperty(Constants.AUTH_METHOD));
138 public void ssh_protocol() throws Exception {
139 VcsUrl url = new VcsUrl("ssh://git@github.com:JetBrains/kotlin.git");
140 GitVcsRoot root = toGitRoot(url);
142 assertEquals("ssh://git@github.com:JetBrains/kotlin.git", root.getProperty(Constants.FETCH_URL));
143 assertEquals("refs/heads/master", root.getProperty(Constants.BRANCH_NAME));
144 assertEquals(AuthenticationMethod.PRIVATE_KEY_DEFAULT.name(), root.getProperty(Constants.AUTH_METHOD));
148 public void http_protocol_with_credentials() throws Exception {
149 VcsUrl url = new VcsUrl("https://github.com/JetBrains/kotlin.git", new Credentials("user1", "pwd1"));
150 GitVcsRoot root = toGitRoot(url);
152 assertEquals("https://github.com/JetBrains/kotlin.git", root.getProperty(Constants.FETCH_URL));
153 assertEquals("refs/heads/master", root.getProperty(Constants.BRANCH_NAME));
154 assertEquals(AuthenticationMethod.PASSWORD.name(), root.getProperty(Constants.AUTH_METHOD));
155 assertEquals("user1", root.getProperty(Constants.USERNAME));
156 assertEquals("pwd1", root.getProperty(Constants.PASSWORD));
160 public void url_without_git_suffix() throws Exception {
161 VcsUrl url = new VcsUrl("https://github.com/hhariri/wasabi");
162 GitVcsRoot root = toGitRoot(url);
164 assertEquals("https://github.com/hhariri/wasabi.git", root.getProperty(Constants.FETCH_URL));
168 public void http_protocol_svn_repo() throws Exception {
169 VcsUrl url = new VcsUrl("http://svn.jetbrains.org/teamcity/plugins/xml-tests-reporting/trunk/");
170 assertNull(myUrlSupport.convertToVcsRootProperties(url));
174 public void should_not_use_private_key_for_local_repository() throws VcsException {
175 VcsUrl url = new VcsUrl("/home/user/repository.git");
176 GitVcsRoot root = toGitRoot(url);
177 assertEquals(AuthenticationMethod.ANONYMOUS.name(), root.getProperty(Constants.AUTH_METHOD));
182 public void should_use_username_from_url() throws Exception {
183 VcsUrl url = new VcsUrl("scm:git:http://teamcity@acme.com/repository.git");
184 GitVcsRoot root = toGitRoot(url);
185 assertEquals("teamcity", root.getProperty(Constants.USERNAME));
189 public void vault_url() throws Exception {
190 VcsUrl url = new VcsUrl("http://some.host.com/VaultService/VaultWeb/Default.aspx?repid=1709&path=$/");
191 assertNull(myUrlSupport.convertToVcsRootProperties(url));
195 private void checkAuthMethod(MavenVcsUrl url, GitVcsRoot root) {
196 if (url.getProviderSpecificPart().startsWith("ssh")) {
197 assertEquals(AuthenticationMethod.PRIVATE_KEY_DEFAULT, root.getAuthSettings().getAuthMethod());
198 assertTrue(root.getAuthSettings().isIgnoreKnownHosts());
199 if (url.getCredentials() != null) {
200 assertEquals(url.getCredentials().getUsername(), root.getAuthSettings().toMap().get(Constants.USERNAME));
202 assertNull(root.getAuthSettings().toMap().get(Constants.PASSWORD));
204 if (url.getCredentials() != null &&
205 !StringUtil.isEmptyOrSpaces(url.getCredentials().getUsername()) &&
206 !StringUtil.isEmptyOrSpaces(url.getCredentials().getPassword())) {
207 assertEquals(AuthenticationMethod.PASSWORD, root.getAuthSettings().getAuthMethod());
208 assertEquals(url.getCredentials().getUsername(), root.getAuthSettings().toMap().get(Constants.USERNAME));
209 assertEquals(url.getCredentials().getPassword(), root.getAuthSettings().toMap().get(Constants.PASSWORD));
211 assertEquals(AuthenticationMethod.ANONYMOUS, root.getAuthSettings().getAuthMethod());
212 assertNull(root.getAuthSettings().toMap().get(Constants.USERNAME));
213 assertNull(root.getAuthSettings().toMap().get(Constants.PASSWORD));
218 private GitVcsRoot toGitRoot(@NotNull VcsUrl url) throws VcsException {
219 Map<String, String> properties = myUrlSupport.convertToVcsRootProperties(url);
220 assertNotNull(properties);
221 VcsRootImpl myRoot = new VcsRootImpl(1, properties);
222 return new GitVcsRoot(myMirrorManager, myRoot);