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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // confirm authentication of test user
  74. UserModel testUser = service.authenticate("test", "testPassword".toCharArray());
  75. assertEquals("test", testUser.username);
  76. assertEquals("testPassword", testUser.password);
  77. // delete a repository role and confirm role removal from test user
  78. service.deleteRepositoryRole("repo2");
  79. testUser = service.getUserModel("test");
  80. assertEquals(2, testUser.permissions.size());
  81. // delete garbage user and confirm user count
  82. service.deleteUser("garbage");
  83. assertEquals(2, service.getAllUsernames().size());
  84. // rename repository and confirm role change for test user
  85. service.renameRepositoryRole("repo1", "newrepo1");
  86. testUser = service.getUserModel("test");
  87. assertTrue(testUser.hasRepositoryPermission("newrepo1"));
  88. }
  89. protected void testTeams(IUserService service) {
  90. // confirm we have 1 team (admins)
  91. assertEquals(1, service.getAllTeamNames().size());
  92. assertEquals("admins", service.getAllTeamNames().get(0));
  93. RepositoryModel newrepo1 = new RepositoryModel("newrepo1", null, null, null);
  94. newrepo1.accessRestriction = AccessRestrictionType.VIEW;
  95. RepositoryModel NEWREPO1 = new RepositoryModel("NEWREPO1", null, null, null);
  96. NEWREPO1.accessRestriction = AccessRestrictionType.VIEW;
  97. // remove newrepo1 from test user
  98. // now test user has no repositories
  99. UserModel user = service.getUserModel("test");
  100. user.permissions.clear();
  101. service.updateUserModel(user);
  102. user = service.getUserModel("test");
  103. assertEquals(0, user.permissions.size());
  104. assertFalse(user.canView(newrepo1));
  105. assertFalse(user.canView(NEWREPO1));
  106. // create test team and add test user and newrepo1
  107. TeamModel team = new TeamModel("testteam");
  108. team.addUser("test");
  109. team.addRepositoryPermission(newrepo1.name);
  110. service.updateTeamModel(team);
  111. // confirm 1 user and 1 repo
  112. team = service.getTeamModel("testteam");
  113. assertEquals(1, team.permissions.size());
  114. assertEquals(1, team.users.size());
  115. // confirm team membership
  116. user = service.getUserModel("test");
  117. assertEquals(0, user.permissions.size());
  118. assertEquals(1, user.teams.size());
  119. // confirm team access
  120. assertTrue(team.hasRepositoryPermission(newrepo1.name));
  121. assertTrue(user.canView(newrepo1));
  122. assertTrue(team.hasRepositoryPermission(NEWREPO1.name));
  123. assertTrue(user.canView(NEWREPO1));
  124. // rename the team and add new repository
  125. RepositoryModel newrepo2 = new RepositoryModel("newrepo2", null, null, null);
  126. newrepo2.accessRestriction = AccessRestrictionType.VIEW;
  127. RepositoryModel NEWREPO2 = new RepositoryModel("NEWREPO2", null, null, null);
  128. NEWREPO2.accessRestriction = AccessRestrictionType.VIEW;
  129. team.addRepositoryPermission(newrepo2.name);
  130. team.name = "testteam2";
  131. service.updateTeamModel("testteam", team);
  132. team = service.getTeamModel("testteam2");
  133. user = service.getUserModel("test");
  134. // confirm user and team can access newrepo2
  135. assertEquals(2, team.permissions.size());
  136. assertTrue(team.hasRepositoryPermission(newrepo2.name));
  137. assertTrue(user.canView(newrepo2));
  138. assertTrue(team.hasRepositoryPermission(NEWREPO2.name));
  139. assertTrue(user.canView(NEWREPO2));
  140. // delete testteam2
  141. service.deleteTeam("testteam2");
  142. team = service.getTeamModel("testteam2");
  143. user = service.getUserModel("test");
  144. // confirm team does not exist and user can not access newrepo1 and 2
  145. assertEquals(null, team);
  146. assertFalse(user.canView(newrepo1));
  147. assertFalse(user.canView(newrepo2));
  148. // create new team and add it to user
  149. // this tests the inverse team creation/team addition
  150. team = new TeamModel("testteam");
  151. team.addRepositoryPermission(NEWREPO1.name);
  152. team.addRepositoryPermission(NEWREPO2.name);
  153. user.teams.add(team);
  154. service.updateUserModel(user);
  155. // confirm the inverted team addition
  156. user = service.getUserModel("test");
  157. team = service.getTeamModel("testteam");
  158. assertTrue(user.canView(newrepo1));
  159. assertTrue(user.canView(newrepo2));
  160. assertTrue(team.hasUser("test"));
  161. // drop testteam from user and add nextteam to user
  162. team = new TeamModel("nextteam");
  163. team.addRepositoryPermission(NEWREPO1.name);
  164. team.addRepositoryPermission(NEWREPO2.name);
  165. user.teams.clear();
  166. user.teams.add(team);
  167. service.updateUserModel(user);
  168. // confirm implicit drop
  169. user = service.getUserModel("test");
  170. team = service.getTeamModel("testteam");
  171. assertTrue(user.canView(newrepo1));
  172. assertTrue(user.canView(newrepo2));
  173. assertFalse(team.hasUser("test"));
  174. team = service.getTeamModel("nextteam");
  175. assertTrue(team.hasUser("test"));
  176. // delete the user and confirm team no longer has user
  177. service.deleteUser("test");
  178. team = service.getTeamModel("testteam");
  179. assertFalse(team.hasUser("test"));
  180. // delete both teams
  181. service.deleteTeam("testteam");
  182. service.deleteTeam("nextteam");
  183. // assert we still have the admins team
  184. assertEquals(1, service.getAllTeamNames().size());
  185. assertEquals("admins", service.getAllTeamNames().get(0));
  186. team = service.getTeamModel("admins");
  187. assertEquals(1, team.mailingLists.size());
  188. assertTrue(team.mailingLists.contains("admins@localhost.com"));
  189. }
  190. }