From a502d96a860456ec5e8c96761db70f7cabb74751 Mon Sep 17 00:00:00 2001 From: Paul Martin <paul@paulsputer.com> Date: Sat, 30 Apr 2016 04:19:14 -0400 Subject: [PATCH] Merge pull request #1073 from gitblit/1062-DocEditorUpdates --- src/test/java/com/gitblit/tests/RpcTests.java | 69 +++++++++++++++++++++++++++++++++- 1 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/gitblit/tests/RpcTests.java b/src/test/java/com/gitblit/tests/RpcTests.java index 33e8505..51b4671 100644 --- a/src/test/java/com/gitblit/tests/RpcTests.java +++ b/src/test/java/com/gitblit/tests/RpcTests.java @@ -32,10 +32,9 @@ import com.gitblit.Constants.AuthorizationControl; import com.gitblit.Constants.PermissionType; import com.gitblit.Constants.RegistrantType; -import com.gitblit.GitBlitException.NotAllowedException; +import com.gitblit.GitBlitException.ForbiddenException; import com.gitblit.GitBlitException.UnauthorizedException; import com.gitblit.Keys; -import com.gitblit.RpcServlet; import com.gitblit.models.FederationModel; import com.gitblit.models.FederationProposal; import com.gitblit.models.FederationSet; @@ -45,6 +44,7 @@ import com.gitblit.models.ServerStatus; import com.gitblit.models.TeamModel; import com.gitblit.models.UserModel; +import com.gitblit.servlet.RpcServlet; import com.gitblit.utils.RpcUtils; /** @@ -104,7 +104,7 @@ UserModel user = null; try { user = RpcUtils.getUser("admin", url, null, null); - } catch (NotAllowedException e) { + } catch (ForbiddenException e) { } assertNull("Server allows anyone to get user!", user); @@ -397,4 +397,67 @@ assertNotNull(branches); assertTrue(branches.size() > 0); } + + @Test + public void testFork() throws Exception { + // test forking by an administrator + // admins are all-powerful and can fork the unforakable :) + testFork(account, password, true, true); + testFork(account, password, false, true); + + // test forking by a permitted normal user + UserModel forkUser = new UserModel("forkuser"); + forkUser.password = forkUser.username; + forkUser.canFork = true; + RpcUtils.deleteUser(forkUser, url, account, password.toCharArray()); + RpcUtils.createUser(forkUser, url, account, password.toCharArray()); + testFork(forkUser.username, forkUser.password, true, true); + testFork(forkUser.username, forkUser.password, false, false); + RpcUtils.deleteUser(forkUser, url, account, password.toCharArray()); + + // test forking by a non-permitted normal user + UserModel noForkUser = new UserModel("noforkuser"); + noForkUser.password = noForkUser.username; + noForkUser.canFork = false; + RpcUtils.deleteUser(noForkUser, url, account, password.toCharArray()); + RpcUtils.createUser(noForkUser, url, account, password.toCharArray()); + testFork(forkUser.username, forkUser.password, true, false); + testFork(forkUser.username, forkUser.password, false, false); + RpcUtils.deleteUser(noForkUser, url, account, password.toCharArray()); + } + + private void testFork(String forkAcct, String forkAcctPassword, boolean allowForks, boolean expectSuccess) throws Exception { + // test does not exist + RepositoryModel dne = new RepositoryModel(); + dne.name = "doesNotExist.git"; + assertFalse(String.format("Successfully forked %s!", dne.name), + RpcUtils.forkRepository(dne, url, forkAcct, forkAcctPassword.toCharArray())); + + // delete any previous fork + RepositoryModel fork = findRepository(String.format("~%s/helloworld.git", forkAcct)); + if (fork != null) { + RpcUtils.deleteRepository(fork, url, account, password.toCharArray()); + } + + // update the origin to allow forks or not + RepositoryModel origin = findRepository("helloworld.git"); + origin.allowForks = allowForks; + RpcUtils.updateRepository(origin.name, origin, url, account, password.toCharArray()); + + // fork the repository + if (expectSuccess) { + assertTrue(String.format("Failed to fork %s!", origin.name), + RpcUtils.forkRepository(origin, url, forkAcct, forkAcctPassword.toCharArray())); + } else { + assertFalse(String.format("Successfully forked %s!", origin.name), + RpcUtils.forkRepository(origin, url, forkAcct, forkAcctPassword.toCharArray())); + } + + // attempt another fork + assertFalse(String.format("Successfully forked %s!", origin.name), + RpcUtils.forkRepository(origin, url, forkAcct, forkAcctPassword.toCharArray())); + + // delete the fork repository + RpcUtils.deleteRepository(fork, url, account, password.toCharArray()); + } } -- Gitblit v1.9.1