Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RpcTests.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.concurrent.atomic.AtomicBoolean;
  24. import org.junit.AfterClass;
  25. import org.junit.BeforeClass;
  26. import org.junit.Test;
  27. import com.gitblit.Constants.AccessPermission;
  28. import com.gitblit.Constants.AccessRestrictionType;
  29. import com.gitblit.Constants.AuthorizationControl;
  30. import com.gitblit.Constants.PermissionType;
  31. import com.gitblit.Constants.RegistrantType;
  32. import com.gitblit.GitBlitException.ForbiddenException;
  33. import com.gitblit.GitBlitException.UnauthorizedException;
  34. import com.gitblit.Keys;
  35. import com.gitblit.models.FederationModel;
  36. import com.gitblit.models.FederationProposal;
  37. import com.gitblit.models.FederationSet;
  38. import com.gitblit.models.RegistrantAccessPermission;
  39. import com.gitblit.models.RepositoryModel;
  40. import com.gitblit.models.ServerSettings;
  41. import com.gitblit.models.ServerStatus;
  42. import com.gitblit.models.TeamModel;
  43. import com.gitblit.models.UserModel;
  44. import com.gitblit.servlet.RpcServlet;
  45. import com.gitblit.utils.RpcUtils;
  46. /**
  47. * Tests all the rpc client utility methods, the rpc filter and rpc servlet.
  48. *
  49. * @author James Moger
  50. *
  51. */
  52. public class RpcTests extends GitblitUnitTest {
  53. String url = GitBlitSuite.url;
  54. String account = GitBlitSuite.account;
  55. String password = GitBlitSuite.password;
  56. private static final AtomicBoolean started = new AtomicBoolean(false);
  57. @BeforeClass
  58. public static void startGitblit() throws Exception {
  59. started.set(GitBlitSuite.startGitblit());
  60. }
  61. @AfterClass
  62. public static void stopGitblit() throws Exception {
  63. //clean up the "A-Team" if left over
  64. TeamModel aTeam = new TeamModel("A-Team");
  65. aTeam.addRepositoryPermission("helloworld.git");
  66. RpcUtils.deleteTeam(aTeam, GitBlitSuite.url, GitBlitSuite.account, GitBlitSuite.password.toCharArray());
  67. if (started.get()) {
  68. GitBlitSuite.stopGitblit();
  69. }
  70. }
  71. @Test
  72. public void testGetProtocolVersion() throws IOException {
  73. int protocol = RpcUtils.getProtocolVersion(url, null, null);
  74. assertEquals(RpcServlet.PROTOCOL_VERSION, protocol);
  75. }
  76. @Test
  77. public void testListRepositories() throws IOException {
  78. Map<String, RepositoryModel> map = RpcUtils.getRepositories(url, null, null);
  79. assertNotNull("Repository list is null!", map);
  80. assertTrue("Repository list is empty!", map.size() > 0);
  81. }
  82. @Test
  83. public void testListUsers() throws IOException {
  84. List<UserModel> list = null;
  85. try {
  86. list = RpcUtils.getUsers(url, null, null);
  87. } catch (UnauthorizedException e) {
  88. }
  89. assertNull("Server allows anyone to admin!", list);
  90. list = RpcUtils.getUsers(url, "admin", "admin".toCharArray());
  91. assertTrue("User list is empty!", list.size() > 0);
  92. }
  93. @Test
  94. public void testGetUser() throws IOException {
  95. UserModel user = null;
  96. try {
  97. user = RpcUtils.getUser("admin", url, null, null);
  98. } catch (ForbiddenException e) {
  99. }
  100. assertNull("Server allows anyone to get user!", user);
  101. user = RpcUtils.getUser("admin", url, "admin", "admin".toCharArray());
  102. assertEquals("User is not the admin!", "admin", user.username);
  103. assertTrue("User is not an administrator!", user.canAdmin());
  104. }
  105. @Test
  106. public void testListTeams() throws IOException {
  107. List<TeamModel> list = null;
  108. try {
  109. list = RpcUtils.getTeams(url, null, null);
  110. } catch (UnauthorizedException e) {
  111. }
  112. assertNull("Server allows anyone to admin!", list);
  113. list = RpcUtils.getTeams(url, "admin", "admin".toCharArray());
  114. assertTrue("Team list is empty!", list.size() > 0);
  115. assertEquals("admins", list.get(0).name);
  116. }
  117. @Test
  118. public void testUserAdministration() throws IOException {
  119. UserModel user = new UserModel("garbage");
  120. user.canAdmin = true;
  121. user.password = "whocares";
  122. // create
  123. assertTrue("Failed to create user!",
  124. RpcUtils.createUser(user, url, account, password.toCharArray()));
  125. UserModel retrievedUser = findUser(user.username);
  126. assertNotNull("Failed to find " + user.username, retrievedUser);
  127. assertTrue("Retrieved user can not administer Gitblit", retrievedUser.canAdmin);
  128. // rename and toggle admin permission
  129. String originalName = user.username;
  130. user.username = "garbage2";
  131. user.canAdmin = false;
  132. assertTrue("Failed to update user!",
  133. RpcUtils.updateUser(originalName, user, url, account, password.toCharArray()));
  134. retrievedUser = findUser(user.username);
  135. assertNotNull("Failed to find " + user.username, retrievedUser);
  136. assertTrue("Retrieved user did not update", !retrievedUser.canAdmin);
  137. // delete
  138. assertTrue("Failed to delete " + user.username,
  139. RpcUtils.deleteUser(retrievedUser, url, account, password.toCharArray()));
  140. retrievedUser = findUser(user.username);
  141. assertNull("Failed to delete " + user.username, retrievedUser);
  142. }
  143. private UserModel findUser(String name) throws IOException {
  144. List<UserModel> users = RpcUtils.getUsers(url, account, password.toCharArray());
  145. UserModel retrievedUser = null;
  146. for (UserModel model : users) {
  147. if (model.username.equalsIgnoreCase(name)) {
  148. retrievedUser = model;
  149. break;
  150. }
  151. }
  152. return retrievedUser;
  153. }
  154. @Test
  155. public void testRepositoryAdministration() throws IOException {
  156. RepositoryModel model = new RepositoryModel();
  157. model.name = "garbagerepo.git";
  158. model.description = "created by RpcUtils";
  159. model.addOwner("garbage");
  160. model.accessRestriction = AccessRestrictionType.VIEW;
  161. model.authorizationControl = AuthorizationControl.AUTHENTICATED;
  162. // create
  163. RpcUtils.deleteRepository(model, url, account, password.toCharArray());
  164. assertTrue("Failed to create repository!",
  165. RpcUtils.createRepository(model, url, account, password.toCharArray()));
  166. RepositoryModel retrievedRepository = findRepository(model.name);
  167. assertNotNull("Failed to find " + model.name, retrievedRepository);
  168. assertEquals(AccessRestrictionType.VIEW, retrievedRepository.accessRestriction);
  169. assertEquals(AuthorizationControl.AUTHENTICATED, retrievedRepository.authorizationControl);
  170. // rename and change access restriciton
  171. String originalName = model.name;
  172. model.name = "garbagerepo2.git";
  173. model.accessRestriction = AccessRestrictionType.CLONE;
  174. model.authorizationControl = AuthorizationControl.NAMED;
  175. RpcUtils.deleteRepository(model, url, account, password.toCharArray());
  176. assertTrue("Failed to update repository!", RpcUtils.updateRepository(originalName, model,
  177. url, account, password.toCharArray()));
  178. retrievedRepository = findRepository(model.name);
  179. assertNotNull("Failed to find " + model.name, retrievedRepository);
  180. assertTrue("Access retriction type is wrong",
  181. AccessRestrictionType.CLONE.equals(retrievedRepository.accessRestriction));
  182. // restore VIEW restriction
  183. retrievedRepository.accessRestriction = AccessRestrictionType.VIEW;
  184. assertTrue("Failed to update repository!", RpcUtils.updateRepository(retrievedRepository.name, retrievedRepository,
  185. url, account, password.toCharArray()));
  186. retrievedRepository = findRepository(retrievedRepository.name);
  187. // memberships
  188. UserModel testMember = new UserModel("justadded");
  189. assertTrue(RpcUtils.createUser(testMember, url, account, password.toCharArray()));
  190. List<RegistrantAccessPermission> permissions = RpcUtils.getRepositoryMemberPermissions(retrievedRepository, url, account,
  191. password.toCharArray());
  192. assertEquals("Unexpected permissions! " + permissions.toString(), 1, permissions.size());
  193. permissions.add(new RegistrantAccessPermission(testMember.username, AccessPermission.VIEW, PermissionType.EXPLICIT, RegistrantType.USER, null, true));
  194. assertTrue(
  195. "Failed to set member permissions!",
  196. RpcUtils.setRepositoryMemberPermissions(retrievedRepository, permissions, url, account,
  197. password.toCharArray()));
  198. permissions = RpcUtils.getRepositoryMemberPermissions(retrievedRepository, url, account,
  199. password.toCharArray());
  200. boolean foundMember = false;
  201. for (RegistrantAccessPermission permission : permissions) {
  202. if (permission.registrant.equalsIgnoreCase(testMember.username)) {
  203. foundMember = true;
  204. assertEquals(AccessPermission.VIEW, permission.permission);
  205. break;
  206. }
  207. }
  208. assertTrue("Failed to find member!", foundMember);
  209. // delete
  210. assertTrue("Failed to delete " + model.name, RpcUtils.deleteRepository(retrievedRepository,
  211. url, account, password.toCharArray()));
  212. retrievedRepository = findRepository(model.name);
  213. assertNull("Failed to delete " + model.name, retrievedRepository);
  214. for (UserModel u : RpcUtils.getUsers(url, account, password.toCharArray())) {
  215. if (u.username.equals(testMember.username)) {
  216. assertTrue(RpcUtils.deleteUser(u, url, account, password.toCharArray()));
  217. break;
  218. }
  219. }
  220. }
  221. private RepositoryModel findRepository(String name) throws IOException {
  222. Map<String, RepositoryModel> repositories = RpcUtils.getRepositories(url, account,
  223. password.toCharArray());
  224. RepositoryModel retrievedRepository = null;
  225. for (RepositoryModel model : repositories.values()) {
  226. if (model.name.equalsIgnoreCase(name)) {
  227. retrievedRepository = model;
  228. break;
  229. }
  230. }
  231. return retrievedRepository;
  232. }
  233. @Test
  234. public void testTeamAdministration() throws IOException {
  235. //clean up the "A-Team" left over from previous run, if any
  236. TeamModel aTeam = new TeamModel("A-Team");
  237. aTeam.addRepositoryPermission("helloworld.git");
  238. RpcUtils.deleteTeam(aTeam, url, account, password.toCharArray());
  239. List<TeamModel> teams = RpcUtils.getTeams(url, account, password.toCharArray());
  240. //should be just the admins team
  241. assertEquals("In addition to 'admins', too many left-over team(s) in Gitblit server: " + teams, 1, teams.size());
  242. // Create the A-Team
  243. aTeam = new TeamModel("A-Team");
  244. aTeam.users.add("admin");
  245. aTeam.addRepositoryPermission("helloworld.git");
  246. assertTrue(RpcUtils.createTeam(aTeam, url, account, password.toCharArray()));
  247. aTeam = null;
  248. teams = RpcUtils.getTeams(url, account, password.toCharArray());
  249. assertEquals(2, teams.size());
  250. for (TeamModel team : teams) {
  251. if (team.name.equals("A-Team")) {
  252. aTeam = team;
  253. break;
  254. }
  255. }
  256. assertNotNull(aTeam);
  257. assertTrue(aTeam.hasUser("admin"));
  258. assertTrue(aTeam.hasRepositoryPermission("helloworld.git"));
  259. RepositoryModel helloworld = null;
  260. Map<String, RepositoryModel> repositories = RpcUtils.getRepositories(url, account,
  261. password.toCharArray());
  262. for (RepositoryModel repository : repositories.values()) {
  263. if (repository.name.equals("helloworld.git")) {
  264. helloworld = repository;
  265. break;
  266. }
  267. }
  268. assertNotNull(helloworld);
  269. // Confirm that we have added the team
  270. List<String> helloworldTeams = RpcUtils.getRepositoryTeams(helloworld, url, account,
  271. password.toCharArray());
  272. assertEquals(1, helloworldTeams.size());
  273. assertTrue(helloworldTeams.contains(aTeam.name));
  274. // set no teams
  275. List<RegistrantAccessPermission> permissions = new ArrayList<RegistrantAccessPermission>();
  276. for (String team : helloworldTeams) {
  277. permissions.add(new RegistrantAccessPermission(team, AccessPermission.NONE, PermissionType.EXPLICIT, RegistrantType.TEAM, null, true));
  278. }
  279. assertTrue(RpcUtils.setRepositoryTeamPermissions(helloworld, permissions, url, account,
  280. password.toCharArray()));
  281. helloworldTeams = RpcUtils.getRepositoryTeams(helloworld, url, account,
  282. password.toCharArray());
  283. assertEquals(0, helloworldTeams.size());
  284. // delete the A-Team
  285. assertTrue(RpcUtils.deleteTeam(aTeam, url, account, password.toCharArray()));
  286. teams = RpcUtils.getTeams(url, account, password.toCharArray());
  287. assertEquals(1, teams.size());
  288. }
  289. @Test
  290. public void testFederationRegistrations() throws Exception {
  291. List<FederationModel> registrations = RpcUtils.getFederationRegistrations(url, account,
  292. password.toCharArray());
  293. assertTrue("No federation registrations were retrieved!", registrations.size() >= 0);
  294. }
  295. @Test
  296. public void testFederationResultRegistrations() throws Exception {
  297. List<FederationModel> registrations = RpcUtils.getFederationResultRegistrations(url,
  298. account, password.toCharArray());
  299. assertTrue("No federation result registrations were retrieved!", registrations.size() >= 0);
  300. }
  301. @Test
  302. public void testFederationProposals() throws Exception {
  303. List<FederationProposal> proposals = RpcUtils.getFederationProposals(url, account,
  304. password.toCharArray());
  305. assertTrue("No federation proposals were retrieved!", proposals.size() >= 0);
  306. }
  307. @Test
  308. public void testFederationSets() throws Exception {
  309. List<FederationSet> sets = RpcUtils.getFederationSets(url, account, password.toCharArray());
  310. assertTrue("No federation sets were retrieved!", sets.size() >= 0);
  311. }
  312. @Test
  313. public void testSettings() throws Exception {
  314. ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
  315. assertNotNull("No settings were retrieved!", settings);
  316. }
  317. @Test
  318. public void testServerStatus() throws Exception {
  319. ServerStatus status = RpcUtils.getStatus(url, account, password.toCharArray());
  320. assertNotNull("No status was retrieved!", status);
  321. }
  322. @Test
  323. public void testUpdateSettings() throws Exception {
  324. Map<String, String> updated = new HashMap<String, String>();
  325. // grab current setting
  326. ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
  327. boolean showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true);
  328. showSizes = !showSizes;
  329. // update setting
  330. updated.put(Keys.web.showRepositorySizes, String.valueOf(showSizes));
  331. boolean success = RpcUtils.updateSettings(updated, url, account, password.toCharArray());
  332. assertTrue("Failed to update server settings", success);
  333. // confirm setting change
  334. settings = RpcUtils.getSettings(url, account, password.toCharArray());
  335. boolean newValue = settings.get(Keys.web.showRepositorySizes).getBoolean(false);
  336. assertEquals(newValue, showSizes);
  337. // restore setting
  338. newValue = !newValue;
  339. updated.put(Keys.web.showRepositorySizes, String.valueOf(newValue));
  340. success = RpcUtils.updateSettings(updated, url, account, password.toCharArray());
  341. assertTrue("Failed to update server settings", success);
  342. settings = RpcUtils.getSettings(url, account, password.toCharArray());
  343. showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true);
  344. assertEquals(newValue, showSizes);
  345. }
  346. @Test
  347. public void testBranches() throws Exception {
  348. Map<String, Collection<String>> branches = RpcUtils.getBranches(url, account,
  349. password.toCharArray());
  350. assertNotNull(branches);
  351. assertTrue(branches.size() > 0);
  352. }
  353. @Test
  354. public void testFork() throws Exception {
  355. // test forking by an administrator
  356. // admins are all-powerful and can fork the unforakable :)
  357. testFork(account, password, true, true);
  358. testFork(account, password, false, true);
  359. // test forking by a permitted normal user
  360. UserModel forkUser = new UserModel("forkuser");
  361. forkUser.password = forkUser.username;
  362. forkUser.canFork = true;
  363. RpcUtils.deleteUser(forkUser, url, account, password.toCharArray());
  364. RpcUtils.createUser(forkUser, url, account, password.toCharArray());
  365. testFork(forkUser.username, forkUser.password, true, true);
  366. testFork(forkUser.username, forkUser.password, false, false);
  367. RpcUtils.deleteUser(forkUser, url, account, password.toCharArray());
  368. // test forking by a non-permitted normal user
  369. UserModel noForkUser = new UserModel("noforkuser");
  370. noForkUser.password = noForkUser.username;
  371. noForkUser.canFork = false;
  372. RpcUtils.deleteUser(noForkUser, url, account, password.toCharArray());
  373. RpcUtils.createUser(noForkUser, url, account, password.toCharArray());
  374. testFork(forkUser.username, forkUser.password, true, false);
  375. testFork(forkUser.username, forkUser.password, false, false);
  376. RpcUtils.deleteUser(noForkUser, url, account, password.toCharArray());
  377. }
  378. private void testFork(String forkAcct, String forkAcctPassword, boolean allowForks, boolean expectSuccess) throws Exception {
  379. // test does not exist
  380. RepositoryModel dne = new RepositoryModel();
  381. dne.name = "doesNotExist.git";
  382. assertFalse(String.format("Successfully forked %s!", dne.name),
  383. RpcUtils.forkRepository(dne, url, forkAcct, forkAcctPassword.toCharArray()));
  384. // delete any previous fork
  385. RepositoryModel fork = findRepository(String.format("~%s/helloworld.git", forkAcct));
  386. if (fork != null) {
  387. RpcUtils.deleteRepository(fork, url, account, password.toCharArray());
  388. }
  389. // update the origin to allow forks or not
  390. RepositoryModel origin = findRepository("helloworld.git");
  391. origin.allowForks = allowForks;
  392. RpcUtils.updateRepository(origin.name, origin, url, account, password.toCharArray());
  393. // fork the repository
  394. if (expectSuccess) {
  395. assertTrue(String.format("Failed to fork %s!", origin.name),
  396. RpcUtils.forkRepository(origin, url, forkAcct, forkAcctPassword.toCharArray()));
  397. } else {
  398. assertFalse(String.format("Successfully forked %s!", origin.name),
  399. RpcUtils.forkRepository(origin, url, forkAcct, forkAcctPassword.toCharArray()));
  400. }
  401. // attempt another fork
  402. assertFalse(String.format("Successfully forked %s!", origin.name),
  403. RpcUtils.forkRepository(origin, url, forkAcct, forkAcctPassword.toCharArray()));
  404. // delete the fork repository
  405. RpcUtils.deleteRepository(fork, url, account, password.toCharArray());
  406. }
  407. }