Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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