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 8.0KB

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