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.

FederationTests.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.assertTrue;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.concurrent.atomic.AtomicBoolean;
  25. import org.junit.AfterClass;
  26. import org.junit.BeforeClass;
  27. import org.junit.Test;
  28. import com.gitblit.Constants.AccessRestrictionType;
  29. import com.gitblit.Constants.FederationProposalResult;
  30. import com.gitblit.Constants.FederationRequest;
  31. import com.gitblit.Constants.FederationToken;
  32. import com.gitblit.models.FederationModel;
  33. import com.gitblit.models.FederationProposal;
  34. import com.gitblit.models.RepositoryModel;
  35. import com.gitblit.models.TeamModel;
  36. import com.gitblit.models.UserModel;
  37. import com.gitblit.utils.FederationUtils;
  38. import com.gitblit.utils.JsonUtils;
  39. import com.gitblit.utils.RpcUtils;
  40. public class FederationTests {
  41. String url = GitBlitSuite.url;
  42. String account = GitBlitSuite.account;
  43. String password = GitBlitSuite.password;
  44. String token = "d7cc58921a80b37e0329a4dae2f9af38bf61ef5c";
  45. private static final AtomicBoolean started = new AtomicBoolean(false);
  46. @BeforeClass
  47. public static void startGitblit() throws Exception {
  48. started.set(GitBlitSuite.startGitblit());
  49. }
  50. @AfterClass
  51. public static void stopGitblit() throws Exception {
  52. if (started.get()) {
  53. GitBlitSuite.stopGitblit();
  54. }
  55. }
  56. @Test
  57. public void testProposal() throws Exception {
  58. // create dummy repository data
  59. Map<String, RepositoryModel> repositories = new HashMap<String, RepositoryModel>();
  60. for (int i = 0; i < 5; i++) {
  61. RepositoryModel model = new RepositoryModel();
  62. model.accessRestriction = AccessRestrictionType.VIEW;
  63. model.description = "cloneable repository " + i;
  64. model.lastChange = new Date();
  65. model.owner = "adminuser";
  66. model.name = "repo" + i + ".git";
  67. model.size = "5 MB";
  68. model.hasCommits = true;
  69. repositories.put(model.name, model);
  70. }
  71. FederationProposal proposal = new FederationProposal("http://testurl", FederationToken.ALL,
  72. "testtoken", repositories);
  73. // propose federation
  74. assertEquals("proposal refused", FederationUtils.propose(url, proposal),
  75. FederationProposalResult.NO_PROPOSALS);
  76. }
  77. @Test
  78. public void testJsonRepositories() throws Exception {
  79. String requrl = FederationUtils.asLink(url, token, FederationRequest.PULL_REPOSITORIES);
  80. String json = JsonUtils.retrieveJsonString(requrl, null, null);
  81. assertNotNull(json);
  82. }
  83. @Test
  84. public void testJsonUsers() throws Exception {
  85. String requrl = FederationUtils.asLink(url, token, FederationRequest.PULL_USERS);
  86. String json = JsonUtils.retrieveJsonString(requrl, null, null);
  87. assertNotNull(json);
  88. }
  89. @Test
  90. public void testJsonTeams() throws Exception {
  91. String requrl = FederationUtils.asLink(url, token, FederationRequest.PULL_TEAMS);
  92. String json = JsonUtils.retrieveJsonString(requrl, null, null);
  93. assertNotNull(json);
  94. }
  95. private FederationModel getRegistration() {
  96. FederationModel model = new FederationModel("localhost");
  97. model.url = this.url;
  98. model.token = this.token;
  99. return model;
  100. }
  101. @Test
  102. public void testPullRepositories() throws Exception {
  103. Map<String, RepositoryModel> repos = FederationUtils.getRepositories(getRegistration(),
  104. false);
  105. assertNotNull(repos);
  106. assertTrue(repos.size() > 0);
  107. }
  108. @Test
  109. public void testPullUsers() throws Exception {
  110. List<UserModel> users = FederationUtils.getUsers(getRegistration());
  111. assertNotNull(users);
  112. // admin is excluded
  113. assertEquals(0, users.size());
  114. UserModel newUser = new UserModel("test");
  115. newUser.password = "whocares";
  116. assertTrue(RpcUtils.createUser(newUser, url, account, password.toCharArray()));
  117. TeamModel team = new TeamModel("testteam");
  118. team.addUser("test");
  119. team.addRepositoryPermission("helloworld.git");
  120. assertTrue(RpcUtils.createTeam(team, url, account, password.toCharArray()));
  121. users = FederationUtils.getUsers(getRegistration());
  122. assertNotNull(users);
  123. assertEquals(1, users.size());
  124. newUser = users.get(0);
  125. assertTrue(newUser.isTeamMember("testteam"));
  126. assertTrue(RpcUtils.deleteUser(newUser, url, account, password.toCharArray()));
  127. assertTrue(RpcUtils.deleteTeam(team, url, account, password.toCharArray()));
  128. }
  129. @Test
  130. public void testPullTeams() throws Exception {
  131. List<TeamModel> teams = FederationUtils.getTeams(getRegistration());
  132. assertNotNull(teams);
  133. assertTrue(teams.size() > 0);
  134. }
  135. @Test
  136. public void testPullScripts() throws Exception {
  137. Map<String, String> scripts = FederationUtils.getScripts(getRegistration());
  138. assertNotNull(scripts);
  139. assertTrue(scripts.keySet().contains("sendmail"));
  140. }
  141. }