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.

UserServiceTest.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.File;
  18. import java.io.IOException;
  19. import org.junit.Test;
  20. import com.gitblit.ConfigUserService;
  21. import com.gitblit.Constants.AccessRestrictionType;
  22. import com.gitblit.IUserService;
  23. import com.gitblit.models.RepositoryModel;
  24. import com.gitblit.models.TeamModel;
  25. import com.gitblit.models.UserModel;
  26. public class UserServiceTest extends GitblitUnitTest {
  27. @Test
  28. public void testConfigUserService() throws IOException {
  29. File file = new File("us-test.conf");
  30. file.delete();
  31. IUserService service = new ConfigUserService(file);
  32. testUsers(service);
  33. testTeams(service);
  34. file.delete();
  35. }
  36. protected void testUsers(IUserService service) {
  37. UserModel admin = service.getUserModel("admin");
  38. assertTrue(admin == null);
  39. // add admin and admins team
  40. TeamModel admins = new TeamModel("admins");
  41. admins.mailingLists.add("admins@localhost.com");
  42. admin = new UserModel("admin");
  43. admin.password = "password";
  44. admin.canAdmin = true;
  45. admin.excludeFromFederation = true;
  46. admin.teams.add(admins);
  47. service.updateUserModel(admin);
  48. admin = null;
  49. admins = null;
  50. // add new user
  51. UserModel newUser = new UserModel("test");
  52. newUser.password = "testPassword";
  53. newUser.addRepositoryPermission("repo1");
  54. newUser.addRepositoryPermission("repo2");
  55. newUser.addRepositoryPermission("sub/repo3");
  56. service.updateUserModel(newUser);
  57. // add one more new user and then test reload of first new user
  58. newUser = new UserModel("GARBAGE");
  59. newUser.password = "garbage";
  60. service.updateUserModel(newUser);
  61. // confirm all added users
  62. assertEquals(3, service.getAllUsernames().size());
  63. assertTrue(service.getUserModel("garbage") != null);
  64. assertTrue(service.getUserModel("GaRbAgE") != null);
  65. assertTrue(service.getUserModel("GARBAGE") != null);
  66. // confirm reloaded test user
  67. newUser = service.getUserModel("test");
  68. assertEquals("testPassword", newUser.password);
  69. assertEquals(3, newUser.permissions.size());
  70. assertTrue(newUser.hasRepositoryPermission("repo1"));
  71. assertTrue(newUser.hasRepositoryPermission("repo2"));
  72. assertTrue(newUser.hasRepositoryPermission("sub/repo3"));
  73. // delete a repository role and confirm role removal from test user
  74. service.deleteRepositoryRole("repo2");
  75. UserModel testUser = service.getUserModel("test");
  76. assertEquals(2, testUser.permissions.size());
  77. // delete garbage user and confirm user count
  78. service.deleteUser("garbage");
  79. assertEquals(2, service.getAllUsernames().size());
  80. // rename repository and confirm role change for test user
  81. service.renameRepositoryRole("repo1", "newrepo1");
  82. testUser = service.getUserModel("test");
  83. assertTrue(testUser.hasRepositoryPermission("newrepo1"));
  84. }
  85. protected void testTeams(IUserService service) {
  86. // confirm we have 1 team (admins)
  87. assertEquals(1, service.getAllTeamNames().size());
  88. assertEquals("admins", service.getAllTeamNames().get(0));
  89. RepositoryModel newrepo1 = new RepositoryModel("newrepo1", null, null, null);
  90. newrepo1.accessRestriction = AccessRestrictionType.VIEW;
  91. RepositoryModel NEWREPO1 = new RepositoryModel("NEWREPO1", null, null, null);
  92. NEWREPO1.accessRestriction = AccessRestrictionType.VIEW;
  93. // remove newrepo1 from test user
  94. // now test user has no repositories
  95. UserModel user = service.getUserModel("test");
  96. user.permissions.clear();
  97. service.updateUserModel(user);
  98. user = service.getUserModel("test");
  99. assertEquals(0, user.permissions.size());
  100. assertFalse(user.canView(newrepo1));
  101. assertFalse(user.canView(NEWREPO1));
  102. // create test team and add test user and newrepo1
  103. TeamModel team = new TeamModel("testteam");
  104. team.addUser("test");
  105. team.addRepositoryPermission(newrepo1.name);
  106. service.updateTeamModel(team);
  107. // confirm 1 user and 1 repo
  108. team = service.getTeamModel("testteam");
  109. assertEquals(1, team.permissions.size());
  110. assertEquals(1, team.users.size());
  111. // confirm team membership
  112. user = service.getUserModel("test");
  113. assertEquals(0, user.permissions.size());
  114. assertEquals(1, user.teams.size());
  115. // confirm team access
  116. assertTrue(team.hasRepositoryPermission(newrepo1.name));
  117. assertTrue(user.canView(newrepo1));
  118. assertTrue(team.hasRepositoryPermission(NEWREPO1.name));
  119. assertTrue(user.canView(NEWREPO1));
  120. // rename the team and add new repository
  121. RepositoryModel newrepo2 = new RepositoryModel("newrepo2", null, null, null);
  122. newrepo2.accessRestriction = AccessRestrictionType.VIEW;
  123. RepositoryModel NEWREPO2 = new RepositoryModel("NEWREPO2", null, null, null);
  124. NEWREPO2.accessRestriction = AccessRestrictionType.VIEW;
  125. team.addRepositoryPermission(newrepo2.name);
  126. team.name = "testteam2";
  127. service.updateTeamModel("testteam", team);
  128. team = service.getTeamModel("testteam2");
  129. user = service.getUserModel("test");
  130. // confirm user and team can access newrepo2
  131. assertEquals(2, team.permissions.size());
  132. assertTrue(team.hasRepositoryPermission(newrepo2.name));
  133. assertTrue(user.canView(newrepo2));
  134. assertTrue(team.hasRepositoryPermission(NEWREPO2.name));
  135. assertTrue(user.canView(NEWREPO2));
  136. // delete testteam2
  137. service.deleteTeam("testteam2");
  138. team = service.getTeamModel("testteam2");
  139. user = service.getUserModel("test");
  140. // confirm team does not exist and user can not access newrepo1 and 2
  141. assertEquals(null, team);
  142. assertFalse(user.canView(newrepo1));
  143. assertFalse(user.canView(newrepo2));
  144. // create new team and add it to user
  145. // this tests the inverse team creation/team addition
  146. team = new TeamModel("testteam");
  147. team.addRepositoryPermission(NEWREPO1.name);
  148. team.addRepositoryPermission(NEWREPO2.name);
  149. user.teams.add(team);
  150. service.updateUserModel(user);
  151. // confirm the inverted team addition
  152. user = service.getUserModel("test");
  153. team = service.getTeamModel("testteam");
  154. assertTrue(user.canView(newrepo1));
  155. assertTrue(user.canView(newrepo2));
  156. assertTrue(team.hasUser("test"));
  157. // drop testteam from user and add nextteam to user
  158. team = new TeamModel("nextteam");
  159. team.addRepositoryPermission(NEWREPO1.name);
  160. team.addRepositoryPermission(NEWREPO2.name);
  161. user.teams.clear();
  162. user.teams.add(team);
  163. service.updateUserModel(user);
  164. // confirm implicit drop
  165. user = service.getUserModel("test");
  166. team = service.getTeamModel("testteam");
  167. assertTrue(user.canView(newrepo1));
  168. assertTrue(user.canView(newrepo2));
  169. assertFalse(team.hasUser("test"));
  170. team = service.getTeamModel("nextteam");
  171. assertTrue(team.hasUser("test"));
  172. // delete the user and confirm team no longer has user
  173. service.deleteUser("test");
  174. team = service.getTeamModel("testteam");
  175. assertFalse(team.hasUser("test"));
  176. // delete both teams
  177. service.deleteTeam("testteam");
  178. service.deleteTeam("nextteam");
  179. // assert we still have the admins team
  180. assertEquals(1, service.getAllTeamNames().size());
  181. assertEquals("admins", service.getAllTeamNames().get(0));
  182. team = service.getTeamModel("admins");
  183. assertEquals(1, team.mailingLists.size());
  184. assertTrue(team.mailingLists.contains("admins@localhost.com"));
  185. }
  186. }