From 73c76b8f7ecaa43c7b5d60ade1c2273af525e8f1 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 20 Jun 2013 17:31:41 -0400
Subject: [PATCH] Added WindowsUserService using Waffle

---
 src/test/java/com/gitblit/tests/GitServletTest.java |   90 +++++++++++++++++++++++++++-----------------
 1 files changed, 55 insertions(+), 35 deletions(-)

diff --git a/src/test/java/com/gitblit/tests/GitServletTest.java b/src/test/java/com/gitblit/tests/GitServletTest.java
index a05b365..7607fbf 100644
--- a/src/test/java/com/gitblit/tests/GitServletTest.java
+++ b/src/test/java/com/gitblit/tests/GitServletTest.java
@@ -19,8 +19,9 @@
 import org.eclipse.jgit.api.ResetCommand.ResetType;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.storage.file.FileRepository;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
 import org.eclipse.jgit.transport.CredentialsProvider;
 import org.eclipse.jgit.transport.PushResult;
 import org.eclipse.jgit.transport.RefSpec;
@@ -37,12 +38,12 @@
 import com.gitblit.Constants.AuthorizationControl;
 import com.gitblit.GitBlit;
 import com.gitblit.Keys;
-import com.gitblit.models.PushLogEntry;
+import com.gitblit.models.RefLogEntry;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.UserModel;
 import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.JGitUtils;
-import com.gitblit.utils.PushLogUtils;
+import com.gitblit.utils.RefLogUtils;
 
 public class GitServletTest {
 
@@ -54,7 +55,7 @@
 	
 	static File jgit2Folder = new File(GitBlitSuite.REPOSITORIES, "working/jgit2");
 
-	String url = GitBlitSuite.url;
+	String url = GitBlitSuite.gitServletUrl;
 	String account = GitBlitSuite.account;
 	String password = GitBlitSuite.password;
 
@@ -100,7 +101,7 @@
 		}
 		
 		CloneCommand clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
+		clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
 		clone.setDirectory(ticgitFolder);
 		clone.setBare(false);
 		clone.setCloneAllBranches(true);
@@ -120,7 +121,7 @@
 		boolean cloned = false;
 		try {
 			CloneCommand clone = Git.cloneRepository();
-			clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
+			clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
 			clone.setDirectory(ticgit2Folder);
 			clone.setBare(false);
 			clone.setCloneAllBranches(true);
@@ -155,7 +156,7 @@
 		boolean cloned = false;
 		try {
 			CloneCommand clone = Git.cloneRepository();
-			clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
+			clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
 			clone.setDirectory(ticgit2Folder);
 			clone.setBare(false);
 			clone.setCloneAllBranches(true);
@@ -177,7 +178,7 @@
 		// try clone again
 		cloned = false;
 		CloneCommand clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
+		clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
 		clone.setDirectory(ticgit2Folder);
 		clone.setBare(false);
 		clone.setCloneAllBranches(true);
@@ -203,8 +204,12 @@
 			FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE | FileUtils.RETRY);
 		}
 
+		RepositoryModel model = GitBlit.self().getRepositoryModel("ticgit.git");
+		model.accessRestriction = AccessRestrictionType.NONE;
+		GitBlit.self().updateRepositoryModel(model.name, model, false);
+
 		CloneCommand clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
+		clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
 		clone.setDirectory(ticgitFolder);
 		clone.setBare(false);
 		clone.setCloneAllBranches(true);
@@ -220,8 +225,13 @@
 		w.close();
 		git.add().addFilepattern(file.getName()).call();
 		git.commit().setMessage("test commit").call();
-		git.push().setPushAll().call();
+		Iterable<PushResult> results = git.push().setPushAll().call();
 		GitBlitSuite.close(git);
+		for (PushResult result : results) {
+			for (RemoteRefUpdate update : result.getRemoteUpdates()) {
+				assertEquals(Status.OK, update.getStatus());
+			}
+		}
 	}
 
 	@Test
@@ -232,7 +242,7 @@
 		}
 		
 		CloneCommand clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/test/jgit.git", url));
+		clone.setURI(MessageFormat.format("{0}/test/jgit.git", url));
 		clone.setDirectory(jgitFolder);
 		clone.setBare(false);
 		clone.setCloneAllBranches(true);
@@ -248,8 +258,13 @@
 		w.close();
 		git.add().addFilepattern(file.getName()).call();
 		git.commit().setMessage("test commit").call();
-		git.push().setPushAll().call();
+		Iterable<PushResult> results = git.push().setPushAll().setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password)).call();
 		GitBlitSuite.close(git);
+		for (PushResult result : results) {
+			for (RemoteRefUpdate update : result.getRemoteUpdates()) {
+				assertEquals(Status.OK, update.getStatus());
+			}
+		}
 	}
 	
 	@Test
@@ -260,7 +275,7 @@
 		}
 		
 		CloneCommand clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/test/jgit.git", url));
+		clone.setURI(MessageFormat.format("{0}/test/jgit.git", url));
 		clone.setDirectory(jgitFolder);
 		clone.setBare(false);
 		clone.setCloneAllBranches(true);
@@ -282,25 +297,30 @@
 		git.add().addFilepattern(file.getName()).call();
 		git.commit().setMessage("test commit").call();
 		
-		try {
-			git.push().setPushAll().call();
-			assertTrue(false);
-		} catch (Exception e) {
-			assertTrue(e.getCause().getMessage().contains("access forbidden"));
+		Iterable<PushResult> results = git.push().setPushAll().setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password)).call();
+		for (PushResult result : results) {
+			for (RemoteRefUpdate update : result.getRemoteUpdates()) {
+				assertEquals(Status.REJECTED_OTHER_REASON, update.getStatus());
+			}
 		}
 		
 		// unfreeze repo
 		model.isFrozen = false;
 		GitBlit.self().updateRepositoryModel(model.name, model, false);
 
-		git.push().setPushAll().call();
+		results = git.push().setPushAll().setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password)).call();
 		GitBlitSuite.close(git);
+		for (PushResult result : results) {
+			for (RemoteRefUpdate update : result.getRemoteUpdates()) {
+				assertEquals(Status.OK, update.getStatus());
+			}
+		}
 	}
 	
 	@Test
 	public void testPushToNonBareRepository() throws Exception {
 		CloneCommand clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/working/jgit", url));
+		clone.setURI(MessageFormat.format("{0}/working/jgit", url));
 		clone.setDirectory(jgit2Folder);
 		clone.setBare(false);
 		clone.setCloneAllBranches(true);
@@ -316,13 +336,13 @@
 		w.close();
 		git.add().addFilepattern(file.getName()).call();
 		git.commit().setMessage("test commit followed by push to non-bare repository").call();
-		try {
-			git.push().setPushAll().call();
-			assertTrue(false);
-		} catch (Exception e) {
-			assertTrue(e.getCause().getMessage().contains("git-receive-pack not permitted"));
-		}
+		Iterable<PushResult> results = git.push().setPushAll().setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password)).call();
 		GitBlitSuite.close(git);
+		for (PushResult result : results) {
+			for (RemoteRefUpdate update : result.getRemoteUpdates()) {
+				assertEquals(Status.REJECTED_OTHER_REASON, update.getStatus());
+			}
+		}
 	}
 
 	@Test
@@ -367,7 +387,7 @@
 			FileUtils.delete(verification, FileUtils.RECURSIVE);
 		}
 		CloneCommand clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
+		clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
 		clone.setDirectory(verification);
 		clone.setBare(true);
 		clone.setCloneAllBranches(true);
@@ -392,7 +412,7 @@
 			FileUtils.delete(local, FileUtils.RECURSIVE);
 		}
 		clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/{1}", url, model.name));
+		clone.setURI(MessageFormat.format("{0}/{1}", url, model.name));
 		clone.setDirectory(local);
 		clone.setBare(false);
 		clone.setCloneAllBranches(true);
@@ -478,7 +498,7 @@
 			FileUtils.delete(refChecks, FileUtils.RECURSIVE);
 		}
 		CloneCommand clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
+		clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
 		clone.setDirectory(refChecks);
 		clone.setBare(true);
 		clone.setCloneAllBranches(true);
@@ -511,7 +531,7 @@
 			FileUtils.delete(local, FileUtils.RECURSIVE);
 		}
 		clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/{1}", url, model.name));
+		clone.setURI(MessageFormat.format("{0}/{1}", url, model.name));
 		clone.setDirectory(local);
 		clone.setBare(false);
 		clone.setCloneAllBranches(true);
@@ -698,7 +718,7 @@
 		}
 
 		CloneCommand clone = Git.cloneRepository();
-		clone.setURI(MessageFormat.format("{0}/git/ticgit.git", url));
+		clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
 		clone.setDirectory(createCheck);
 		clone.setBare(true);
 		clone.setCloneAllBranches(true);
@@ -708,8 +728,8 @@
 		GitBlitSuite.close(personalRepo);
 		
 		// add a personal repository remote and a project remote
-		git.getRepository().getConfig().setString("remote", "user", "url", MessageFormat.format("{0}/git/~{1}/ticgit.git", url, user.username));
-		git.getRepository().getConfig().setString("remote", "project", "url", MessageFormat.format("{0}/git/project/ticgit.git", url));
+		git.getRepository().getConfig().setString("remote", "user", "url", MessageFormat.format("{0}/~{1}/ticgit.git", url, user.username));
+		git.getRepository().getConfig().setString("remote", "project", "url", MessageFormat.format("{0}/project/ticgit.git", url));
 		git.getRepository().getConfig().save();
 
 		// push to non-existent user repository
@@ -767,8 +787,8 @@
 	public void testPushLog() throws IOException {
 		String name = "refchecks/ticgit.git";
 		File refChecks = new File(GitBlitSuite.REPOSITORIES, name);
-		FileRepository repository = new FileRepository(refChecks);
-		List<PushLogEntry> pushes = PushLogUtils.getPushLog(name, repository);
+		Repository repository = new FileRepositoryBuilder().setGitDir(refChecks).build();
+		List<RefLogEntry> pushes = RefLogUtils.getRefLog(name, repository);
 		GitBlitSuite.close(repository);
 		assertTrue("Repository has an empty push log!", pushes.size() > 0);
 	}

--
Gitblit v1.9.1