lemval
2012-01-31 1c30dad2115fc513791d8a5b292ad0f7d7b85749
commit | author | age
375caa 1 package com.gitblit.tests;
JM 2
7e8873 3 import static org.junit.Assert.assertFalse;
JM 4 import static org.junit.Assert.assertTrue;
5
375caa 6 import java.io.BufferedWriter;
JM 7 import java.io.File;
8 import java.io.FileOutputStream;
9 import java.io.OutputStreamWriter;
10 import java.text.MessageFormat;
11 import java.util.Date;
7e8873 12 import java.util.concurrent.atomic.AtomicBoolean;
375caa 13
JM 14 import org.eclipse.jgit.api.CloneCommand;
15 import org.eclipse.jgit.api.Git;
d40adc 16 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
375caa 17 import org.eclipse.jgit.util.FileUtils;
29692a 18 import org.junit.AfterClass;
JM 19 import org.junit.BeforeClass;
20 import org.junit.Test;
375caa 21
7e8873 22 import com.gitblit.Constants.AccessRestrictionType;
JM 23 import com.gitblit.GitBlit;
24 import com.gitblit.models.RepositoryModel;
375caa 25
29692a 26 public class GitServletTest {
375caa 27
ee0b1f 28     static File ticgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit");
cb285c 29     
ee0b1f 30     static File ticgit2Folder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit2");
JM 31
32     static File jgitFolder = new File(GitBlitSuite.REPOSITORIES, "working/jgit");
375caa 33
7e8873 34     String url = GitBlitSuite.url;
JM 35     String account = GitBlitSuite.account;
36     String password = GitBlitSuite.password;
375caa 37
7e8873 38     private static final AtomicBoolean started = new AtomicBoolean(false);
375caa 39
29692a 40     @BeforeClass
JM 41     public static void startGitblit() throws Exception {
ee0b1f 42         deleteWorkingFolders();
7e8873 43         started.set(GitBlitSuite.startGitblit());
375caa 44     }
JM 45
29692a 46     @AfterClass
JM 47     public static void stopGitblit() throws Exception {
7e8873 48         if (started.get()) {
JM 49             GitBlitSuite.stopGitblit();
50         }
ee0b1f 51         deleteWorkingFolders();
JM 52     }
53     
54     private static void deleteWorkingFolders() throws Exception {
55         if (ticgitFolder.exists()) {
56             FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE);
57         }
58         if (ticgit2Folder.exists()) {
59             FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
60         }
61         if (jgitFolder.exists()) {
62             FileUtils.delete(jgitFolder, FileUtils.RECURSIVE);
63         }
375caa 64     }
JM 65
29692a 66     @Test
375caa 67     public void testClone() throws Exception {
JM 68         CloneCommand clone = Git.cloneRepository();
7e8873 69         clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
ee0b1f 70         clone.setDirectory(ticgitFolder);
375caa 71         clone.setBare(false);
JM 72         clone.setCloneAllBranches(true);
7e8873 73         clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
ee0b1f 74         close(clone.call());        
7e8873 75         assertTrue(true);
JM 76     }
77
78     @Test
79     public void testBogusLoginClone() throws Exception {
80         // restrict repository access
81         RepositoryModel model = GitBlit.self().getRepositoryModel("ticgit.git");
82         model.accessRestriction = AccessRestrictionType.CLONE;
83         GitBlit.self().updateRepositoryModel(model.name, model, false);
84
ee0b1f 85         // delete any existing working folder        
7e8873 86         boolean cloned = false;
JM 87         try {
88             CloneCommand clone = Git.cloneRepository();
89             clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
ee0b1f 90             clone.setDirectory(ticgit2Folder);
7e8873 91             clone.setBare(false);
JM 92             clone.setCloneAllBranches(true);
93             clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider("bogus", "bogus"));
ee0b1f 94             close(clone.call());
7e8873 95             cloned = true;
JM 96         } catch (Exception e) {
dcf575 97             // swallow the exception which we expect
7e8873 98         }
JM 99
100         // restore anonymous repository access
101         model.accessRestriction = AccessRestrictionType.NONE;
102         GitBlit.self().updateRepositoryModel(model.name, model, false);
103
104         assertFalse("Bogus login cloned a repository?!", cloned);
375caa 105     }
JM 106
29692a 107     @Test
cb285c 108     public void testAnonymousPush() throws Exception {
ee0b1f 109         Git git = Git.open(ticgitFolder);
JM 110         File file = new File(ticgitFolder, "TODO");
111         OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));
112         BufferedWriter w = new BufferedWriter(os);
113         w.write("// " + new Date().toString() + "\n");
114         w.close();
115         git.add().addFilepattern(file.getName()).call();
116         git.commit().setMessage("test commit").call();
375caa 117         git.push().setPushAll().call();
ee0b1f 118         close(git);
d40adc 119     }
ee0b1f 120
cb285c 121     @Test
JM 122     public void testSubfolderPush() throws Exception {
123         CloneCommand clone = Git.cloneRepository();
124         clone.setURI(MessageFormat.format("{0}/git/test/jgit.git", url));
125         clone.setDirectory(jgitFolder);
126         clone.setBare(false);
127         clone.setCloneAllBranches(true);
128         clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
ee0b1f 129         close(clone.call());
cb285c 130         assertTrue(true);
ee0b1f 131
cb285c 132         Git git = Git.open(jgitFolder);
JM 133         File file = new File(jgitFolder, "TODO");
ee0b1f 134         OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true));
JM 135         BufferedWriter w = new BufferedWriter(os);
136         w.write("// " + new Date().toString() + "\n");
137         w.close();
138         git.add().addFilepattern(file.getName()).call();
139         git.commit().setMessage("test commit").call();
cb285c 140         git.push().setPushAll().call();
ee0b1f 141         close(git);
JM 142     }
143     
144     private void close(Git git) {
145         // really close the repository
146         // decrement the use counter to 0
147         for (int i = 0; i < 2; i++) {
148             git.getRepository().close();
149         }
cb285c 150     }
375caa 151 }