| | |
| | | */
|
| | | package com.gitblit.tests;
|
| | |
|
| | | import static org.junit.Assert.assertEquals;
|
| | | import static org.junit.Assert.assertNotNull;
|
| | | import static org.junit.Assert.assertNull;
|
| | | import static org.junit.Assert.assertTrue;
|
| | |
|
| | | import java.io.IOException;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Collection;
|
| | |
| | | import com.gitblit.Constants.AuthorizationControl;
|
| | | import com.gitblit.Constants.PermissionType;
|
| | | import com.gitblit.Constants.RegistrantType;
|
| | | import com.gitblit.GitBlitException.ForbiddenException;
|
| | | import com.gitblit.GitBlitException.UnauthorizedException;
|
| | | import com.gitblit.Keys;
|
| | | import com.gitblit.RpcServlet;
|
| | | import com.gitblit.models.RegistrantAccessPermission;
|
| | | import com.gitblit.models.FederationModel;
|
| | | import com.gitblit.models.FederationProposal;
|
| | | import com.gitblit.models.FederationSet;
|
| | | import com.gitblit.models.RegistrantAccessPermission;
|
| | | import com.gitblit.models.RepositoryModel;
|
| | | import com.gitblit.models.ServerSettings;
|
| | | 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;
|
| | |
|
| | | /**
|
| | | * Tests all the rpc client utility methods, the rpc filter and rpc servlet.
|
| | | * |
| | | *
|
| | | * @author James Moger
|
| | | * |
| | | *
|
| | | */
|
| | | public class RpcTests {
|
| | | public class RpcTests extends GitblitUnitTest {
|
| | |
|
| | | String url = GitBlitSuite.url;
|
| | | String account = GitBlitSuite.account;
|
| | |
| | |
|
| | | list = RpcUtils.getUsers(url, "admin", "admin".toCharArray());
|
| | | assertTrue("User list is empty!", list.size() > 0);
|
| | | }
|
| | |
|
| | | @Test
|
| | | public void testGetUser() throws IOException {
|
| | | UserModel user = null;
|
| | | try {
|
| | | user = RpcUtils.getUser("admin", url, null, null);
|
| | | } catch (ForbiddenException e) {
|
| | | }
|
| | | assertNull("Server allows anyone to get user!", user);
|
| | |
|
| | | user = RpcUtils.getUser("admin", url, "admin", "admin".toCharArray());
|
| | | assertEquals("User is not the admin!", "admin", user.username);
|
| | | assertTrue("User is not an administrator!", user.canAdmin());
|
| | | }
|
| | |
|
| | | @Test
|
| | |
| | | model.authorizationControl = AuthorizationControl.AUTHENTICATED;
|
| | |
|
| | | // create
|
| | | RpcUtils.deleteRepository(model, url, account, password.toCharArray());
|
| | | assertTrue("Failed to create repository!",
|
| | | RpcUtils.createRepository(model, url, account, password.toCharArray()));
|
| | |
|
| | |
| | | // rename and change access restriciton
|
| | | String originalName = model.name;
|
| | | model.name = "garbagerepo2.git";
|
| | | model.accessRestriction = AccessRestrictionType.PUSH;
|
| | | model.accessRestriction = AccessRestrictionType.CLONE;
|
| | | model.authorizationControl = AuthorizationControl.NAMED;
|
| | | RpcUtils.deleteRepository(model, url, account, password.toCharArray());
|
| | | assertTrue("Failed to update repository!", RpcUtils.updateRepository(originalName, model,
|
| | | url, account, password.toCharArray()));
|
| | |
|
| | | retrievedRepository = findRepository(model.name);
|
| | | assertNotNull("Failed to find " + model.name, retrievedRepository);
|
| | | assertTrue("Access retriction type is wrong",
|
| | | AccessRestrictionType.PUSH.equals(retrievedRepository.accessRestriction));
|
| | | AccessRestrictionType.CLONE.equals(retrievedRepository.accessRestriction));
|
| | |
|
| | | // restore VIEW restriction
|
| | | retrievedRepository.accessRestriction = AccessRestrictionType.VIEW;
|
| | | assertTrue("Failed to update repository!", RpcUtils.updateRepository(retrievedRepository.name, retrievedRepository,
|
| | | url, account, password.toCharArray()));
|
| | | retrievedRepository = findRepository(retrievedRepository.name);
|
| | |
|
| | | // memberships
|
| | | UserModel testMember = new UserModel("justadded");
|
| | |
| | |
|
| | | List<RegistrantAccessPermission> permissions = RpcUtils.getRepositoryMemberPermissions(retrievedRepository, url, account,
|
| | | password.toCharArray());
|
| | | assertEquals("Membership permissions is not empty!", 0, permissions.size());
|
| | | permissions.add(new RegistrantAccessPermission(testMember.username, AccessPermission.PUSH, PermissionType.EXPLICIT, RegistrantType.USER, null, true));
|
| | | assertEquals("Unexpected permissions! " + permissions.toString(), 1, permissions.size());
|
| | | permissions.add(new RegistrantAccessPermission(testMember.username, AccessPermission.VIEW, PermissionType.EXPLICIT, RegistrantType.USER, null, true));
|
| | | assertTrue(
|
| | | "Failed to set member permissions!",
|
| | | RpcUtils.setRepositoryMemberPermissions(retrievedRepository, permissions, url, account,
|
| | |
| | | for (RegistrantAccessPermission permission : permissions) {
|
| | | if (permission.registrant.equalsIgnoreCase(testMember.username)) {
|
| | | foundMember = true;
|
| | | assertEquals(AccessPermission.PUSH, permission.permission);
|
| | | assertEquals(AccessPermission.VIEW, permission.permission);
|
| | | break;
|
| | | }
|
| | | }
|
| | |
| | | public void testTeamAdministration() throws IOException {
|
| | | List<TeamModel> teams = RpcUtils.getTeams(url, account, password.toCharArray());
|
| | | assertEquals(1, teams.size());
|
| | | |
| | |
|
| | | // Create the A-Team
|
| | | TeamModel aTeam = new TeamModel("A-Team");
|
| | | aTeam.users.add("admin");
|
| | |
| | | }
|
| | | }
|
| | | assertNotNull(helloworld);
|
| | | |
| | |
|
| | | // Confirm that we have added the team
|
| | | List<String> helloworldTeams = RpcUtils.getRepositoryTeams(helloworld, url, account,
|
| | | password.toCharArray());
|
| | |
| | | helloworldTeams = RpcUtils.getRepositoryTeams(helloworld, url, account,
|
| | | password.toCharArray());
|
| | | assertEquals(0, helloworldTeams.size());
|
| | | |
| | |
|
| | | // delete the A-Team
|
| | | assertTrue(RpcUtils.deleteTeam(aTeam, url, account, password.toCharArray()));
|
| | |
|
| | |
| | | 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());
|
| | | }
|
| | | }
|