You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RpcTests.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. package com.gitblit.tests;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.concurrent.atomic.AtomicBoolean;
  28. import org.junit.AfterClass;
  29. import org.junit.BeforeClass;
  30. import org.junit.Test;
  31. import com.gitblit.Constants.AccessPermission;
  32. import com.gitblit.Constants.AccessRestrictionType;
  33. import com.gitblit.Constants.AuthorizationControl;
  34. import com.gitblit.Constants.PermissionType;
  35. import com.gitblit.Constants.RegistrantType;
  36. import com.gitblit.GitBlitException.UnauthorizedException;
  37. import com.gitblit.Keys;
  38. import com.gitblit.RpcServlet;
  39. import com.gitblit.models.RegistrantAccessPermission;
  40. import com.gitblit.models.FederationModel;
  41. import com.gitblit.models.FederationProposal;
  42. import com.gitblit.models.FederationSet;
  43. import com.gitblit.models.RepositoryModel;
  44. import com.gitblit.models.ServerSettings;
  45. import com.gitblit.models.ServerStatus;
  46. import com.gitblit.models.TeamModel;
  47. import com.gitblit.models.UserModel;
  48. import com.gitblit.utils.RpcUtils;
  49. /**
  50. * Tests all the rpc client utility methods, the rpc filter and rpc servlet.
  51. *
  52. * @author James Moger
  53. *
  54. */
  55. public class RpcTests {
  56. String url = GitBlitSuite.url;
  57. String account = GitBlitSuite.account;
  58. String password = GitBlitSuite.password;
  59. private static final AtomicBoolean started = new AtomicBoolean(false);
  60. @BeforeClass
  61. public static void startGitblit() throws Exception {
  62. started.set(GitBlitSuite.startGitblit());
  63. }
  64. @AfterClass
  65. public static void stopGitblit() throws Exception {
  66. if (started.get()) {
  67. GitBlitSuite.stopGitblit();
  68. }
  69. }
  70. @Test
  71. public void testGetProtocolVersion() throws IOException {
  72. int protocol = RpcUtils.getProtocolVersion(url, null, null);
  73. assertEquals(RpcServlet.PROTOCOL_VERSION, protocol);
  74. }
  75. @Test
  76. public void testListRepositories() throws IOException {
  77. Map<String, RepositoryModel> map = RpcUtils.getRepositories(url, null, null);
  78. assertNotNull("Repository list is null!", map);
  79. assertTrue("Repository list is empty!", map.size() > 0);
  80. }
  81. @Test
  82. public void testListUsers() throws IOException {
  83. List<UserModel> list = null;
  84. try {
  85. list = RpcUtils.getUsers(url, null, null);
  86. } catch (UnauthorizedException e) {
  87. }
  88. assertNull("Server allows anyone to admin!", list);
  89. list = RpcUtils.getUsers(url, "admin", "admin".toCharArray());
  90. assertTrue("User list is empty!", list.size() > 0);
  91. }
  92. @Test
  93. public void testListTeams() throws IOException {
  94. List<TeamModel> list = null;
  95. try {
  96. list = RpcUtils.getTeams(url, null, null);
  97. } catch (UnauthorizedException e) {
  98. }
  99. assertNull("Server allows anyone to admin!", list);
  100. list = RpcUtils.getTeams(url, "admin", "admin".toCharArray());
  101. assertTrue("Team list is empty!", list.size() > 0);
  102. assertEquals("admins", list.get(0).name);
  103. }
  104. @Test
  105. public void testUserAdministration() throws IOException {
  106. UserModel user = new UserModel("garbage");
  107. user.canAdmin = true;
  108. user.password = "whocares";
  109. // create
  110. assertTrue("Failed to create user!",
  111. RpcUtils.createUser(user, url, account, password.toCharArray()));
  112. UserModel retrievedUser = findUser(user.username);
  113. assertNotNull("Failed to find " + user.username, retrievedUser);
  114. assertTrue("Retrieved user can not administer Gitblit", retrievedUser.canAdmin);
  115. // rename and toggle admin permission
  116. String originalName = user.username;
  117. user.username = "garbage2";
  118. user.canAdmin = false;
  119. assertTrue("Failed to update user!",
  120. RpcUtils.updateUser(originalName, user, url, account, password.toCharArray()));
  121. retrievedUser = findUser(user.username);
  122. assertNotNull("Failed to find " + user.username, retrievedUser);
  123. assertTrue("Retrieved user did not update", !retrievedUser.canAdmin);
  124. // delete
  125. assertTrue("Failed to delete " + user.username,
  126. RpcUtils.deleteUser(retrievedUser, url, account, password.toCharArray()));
  127. retrievedUser = findUser(user.username);
  128. assertNull("Failed to delete " + user.username, retrievedUser);
  129. }
  130. private UserModel findUser(String name) throws IOException {
  131. List<UserModel> users = RpcUtils.getUsers(url, account, password.toCharArray());
  132. UserModel retrievedUser = null;
  133. for (UserModel model : users) {
  134. if (model.username.equalsIgnoreCase(name)) {
  135. retrievedUser = model;
  136. break;
  137. }
  138. }
  139. return retrievedUser;
  140. }
  141. @Test
  142. public void testRepositoryAdministration() throws IOException {
  143. RepositoryModel model = new RepositoryModel();
  144. model.name = "garbagerepo.git";
  145. model.description = "created by RpcUtils";
  146. model.owner = "garbage";
  147. model.accessRestriction = AccessRestrictionType.VIEW;
  148. model.authorizationControl = AuthorizationControl.AUTHENTICATED;
  149. // create
  150. assertTrue("Failed to create repository!",
  151. RpcUtils.createRepository(model, url, account, password.toCharArray()));
  152. RepositoryModel retrievedRepository = findRepository(model.name);
  153. assertNotNull("Failed to find " + model.name, retrievedRepository);
  154. assertEquals(AccessRestrictionType.VIEW, retrievedRepository.accessRestriction);
  155. assertEquals(AuthorizationControl.AUTHENTICATED, retrievedRepository.authorizationControl);
  156. // rename and change access restriciton
  157. String originalName = model.name;
  158. model.name = "garbagerepo2.git";
  159. model.accessRestriction = AccessRestrictionType.PUSH;
  160. model.authorizationControl = AuthorizationControl.NAMED;
  161. assertTrue("Failed to update repository!", RpcUtils.updateRepository(originalName, model,
  162. url, account, password.toCharArray()));
  163. retrievedRepository = findRepository(model.name);
  164. assertNotNull("Failed to find " + model.name, retrievedRepository);
  165. assertTrue("Access retriction type is wrong",
  166. AccessRestrictionType.PUSH.equals(retrievedRepository.accessRestriction));
  167. // memberships
  168. UserModel testMember = new UserModel("justadded");
  169. assertTrue(RpcUtils.createUser(testMember, url, account, password.toCharArray()));
  170. List<RegistrantAccessPermission> permissions = RpcUtils.getRepositoryMemberPermissions(retrievedRepository, url, account,
  171. password.toCharArray());
  172. assertEquals("Membership permissions is not empty!", 0, permissions.size());
  173. permissions.add(new RegistrantAccessPermission(testMember.username, AccessPermission.PUSH, PermissionType.EXPLICIT, RegistrantType.USER, null, true));
  174. assertTrue(
  175. "Failed to set member permissions!",
  176. RpcUtils.setRepositoryMemberPermissions(retrievedRepository, permissions, url, account,
  177. password.toCharArray()));
  178. permissions = RpcUtils.getRepositoryMemberPermissions(retrievedRepository, url, account,
  179. password.toCharArray());
  180. boolean foundMember = false;
  181. for (RegistrantAccessPermission permission : permissions) {
  182. if (permission.registrant.equalsIgnoreCase(testMember.username)) {
  183. foundMember = true;
  184. assertEquals(AccessPermission.PUSH, permission.permission);
  185. break;
  186. }
  187. }
  188. assertTrue("Failed to find member!", foundMember);
  189. // delete
  190. assertTrue("Failed to delete " + model.name, RpcUtils.deleteRepository(retrievedRepository,
  191. url, account, password.toCharArray()));
  192. retrievedRepository = findRepository(model.name);
  193. assertNull("Failed to delete " + model.name, retrievedRepository);
  194. for (UserModel u : RpcUtils.getUsers(url, account, password.toCharArray())) {
  195. if (u.username.equals(testMember.username)) {
  196. assertTrue(RpcUtils.deleteUser(u, url, account, password.toCharArray()));
  197. break;
  198. }
  199. }
  200. }
  201. private RepositoryModel findRepository(String name) throws IOException {
  202. Map<String, RepositoryModel> repositories = RpcUtils.getRepositories(url, account,
  203. password.toCharArray());
  204. RepositoryModel retrievedRepository = null;
  205. for (RepositoryModel model : repositories.values()) {
  206. if (model.name.equalsIgnoreCase(name)) {
  207. retrievedRepository = model;
  208. break;
  209. }
  210. }
  211. return retrievedRepository;
  212. }
  213. @Test
  214. public void testTeamAdministration() throws IOException {
  215. List<TeamModel> teams = RpcUtils.getTeams(url, account, password.toCharArray());
  216. assertEquals(1, teams.size());
  217. // Create the A-Team
  218. TeamModel aTeam = new TeamModel("A-Team");
  219. aTeam.users.add("admin");
  220. aTeam.addRepositoryPermission("helloworld.git");
  221. assertTrue(RpcUtils.createTeam(aTeam, url, account, password.toCharArray()));
  222. aTeam = null;
  223. teams = RpcUtils.getTeams(url, account, password.toCharArray());
  224. assertEquals(2, teams.size());
  225. for (TeamModel team : teams) {
  226. if (team.name.equals("A-Team")) {
  227. aTeam = team;
  228. break;
  229. }
  230. }
  231. assertNotNull(aTeam);
  232. assertTrue(aTeam.hasUser("admin"));
  233. assertTrue(aTeam.hasRepositoryPermission("helloworld.git"));
  234. RepositoryModel helloworld = null;
  235. Map<String, RepositoryModel> repositories = RpcUtils.getRepositories(url, account,
  236. password.toCharArray());
  237. for (RepositoryModel repository : repositories.values()) {
  238. if (repository.name.equals("helloworld.git")) {
  239. helloworld = repository;
  240. break;
  241. }
  242. }
  243. assertNotNull(helloworld);
  244. // Confirm that we have added the team
  245. List<String> helloworldTeams = RpcUtils.getRepositoryTeams(helloworld, url, account,
  246. password.toCharArray());
  247. assertEquals(1, helloworldTeams.size());
  248. assertTrue(helloworldTeams.contains(aTeam.name));
  249. // set no teams
  250. List<RegistrantAccessPermission> permissions = new ArrayList<RegistrantAccessPermission>();
  251. for (String team : helloworldTeams) {
  252. permissions.add(new RegistrantAccessPermission(team, AccessPermission.NONE, PermissionType.EXPLICIT, RegistrantType.TEAM, null, true));
  253. }
  254. assertTrue(RpcUtils.setRepositoryTeamPermissions(helloworld, permissions, url, account,
  255. password.toCharArray()));
  256. helloworldTeams = RpcUtils.getRepositoryTeams(helloworld, url, account,
  257. password.toCharArray());
  258. assertEquals(0, helloworldTeams.size());
  259. // delete the A-Team
  260. assertTrue(RpcUtils.deleteTeam(aTeam, url, account, password.toCharArray()));
  261. teams = RpcUtils.getTeams(url, account, password.toCharArray());
  262. assertEquals(1, teams.size());
  263. }
  264. @Test
  265. public void testFederationRegistrations() throws Exception {
  266. List<FederationModel> registrations = RpcUtils.getFederationRegistrations(url, account,
  267. password.toCharArray());
  268. assertTrue("No federation registrations were retrieved!", registrations.size() >= 0);
  269. }
  270. @Test
  271. public void testFederationResultRegistrations() throws Exception {
  272. List<FederationModel> registrations = RpcUtils.getFederationResultRegistrations(url,
  273. account, password.toCharArray());
  274. assertTrue("No federation result registrations were retrieved!", registrations.size() >= 0);
  275. }
  276. @Test
  277. public void testFederationProposals() throws Exception {
  278. List<FederationProposal> proposals = RpcUtils.getFederationProposals(url, account,
  279. password.toCharArray());
  280. assertTrue("No federation proposals were retrieved!", proposals.size() >= 0);
  281. }
  282. @Test
  283. public void testFederationSets() throws Exception {
  284. List<FederationSet> sets = RpcUtils.getFederationSets(url, account, password.toCharArray());
  285. assertTrue("No federation sets were retrieved!", sets.size() >= 0);
  286. }
  287. @Test
  288. public void testSettings() throws Exception {
  289. ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
  290. assertNotNull("No settings were retrieved!", settings);
  291. }
  292. @Test
  293. public void testServerStatus() throws Exception {
  294. ServerStatus status = RpcUtils.getStatus(url, account, password.toCharArray());
  295. assertNotNull("No status was retrieved!", status);
  296. }
  297. @Test
  298. public void testUpdateSettings() throws Exception {
  299. Map<String, String> updated = new HashMap<String, String>();
  300. // grab current setting
  301. ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
  302. boolean showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true);
  303. showSizes = !showSizes;
  304. // update setting
  305. updated.put(Keys.web.showRepositorySizes, String.valueOf(showSizes));
  306. boolean success = RpcUtils.updateSettings(updated, url, account, password.toCharArray());
  307. assertTrue("Failed to update server settings", success);
  308. // confirm setting change
  309. settings = RpcUtils.getSettings(url, account, password.toCharArray());
  310. boolean newValue = settings.get(Keys.web.showRepositorySizes).getBoolean(false);
  311. assertEquals(newValue, showSizes);
  312. // restore setting
  313. newValue = !newValue;
  314. updated.put(Keys.web.showRepositorySizes, String.valueOf(newValue));
  315. success = RpcUtils.updateSettings(updated, url, account, password.toCharArray());
  316. assertTrue("Failed to update server settings", success);
  317. settings = RpcUtils.getSettings(url, account, password.toCharArray());
  318. showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true);
  319. assertEquals(newValue, showSizes);
  320. }
  321. @Test
  322. public void testBranches() throws Exception {
  323. Map<String, Collection<String>> branches = RpcUtils.getBranches(url, account,
  324. password.toCharArray());
  325. assertNotNull(branches);
  326. assertTrue(branches.size() > 0);
  327. }
  328. }