James Moger
2012-11-30 e377dce8818ecd55bb599a8532376cf55e56381a
commit | author | age
93f0b1 1 /*
JM 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
7e8873 18 import static org.junit.Assert.assertEquals;
JM 19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22
93f0b1 23 import java.io.IOException;
f08aab 24 import java.util.ArrayList;
c75304 25 import java.util.Collection;
97a20e 26 import java.util.HashMap;
31abc2 27 import java.util.List;
93f0b1 28 import java.util.Map;
7e8873 29 import java.util.concurrent.atomic.AtomicBoolean;
93f0b1 30
29692a 31 import org.junit.AfterClass;
JM 32 import org.junit.BeforeClass;
33 import org.junit.Test;
93f0b1 34
822dfe 35 import com.gitblit.Constants.AccessPermission;
31abc2 36 import com.gitblit.Constants.AccessRestrictionType;
6adf56 37 import com.gitblit.Constants.AuthorizationControl;
092f0a 38 import com.gitblit.Constants.PermissionType;
822dfe 39 import com.gitblit.Constants.RegistrantType;
31abc2 40 import com.gitblit.GitBlitException.UnauthorizedException;
97a20e 41 import com.gitblit.Keys;
f08aab 42 import com.gitblit.RpcServlet;
822dfe 43 import com.gitblit.models.RegistrantAccessPermission;
31abc2 44 import com.gitblit.models.FederationModel;
JM 45 import com.gitblit.models.FederationProposal;
46 import com.gitblit.models.FederationSet;
93f0b1 47 import com.gitblit.models.RepositoryModel;
84c1d5 48 import com.gitblit.models.ServerSettings;
b75734 49 import com.gitblit.models.ServerStatus;
f08aab 50 import com.gitblit.models.TeamModel;
31abc2 51 import com.gitblit.models.UserModel;
93f0b1 52 import com.gitblit.utils.RpcUtils;
JM 53
31abc2 54 /**
JM 55  * Tests all the rpc client utility methods, the rpc filter and rpc servlet.
56  * 
57  * @author James Moger
58  * 
59  */
7e8873 60 public class RpcTests {
JM 61
143fc9 62     String url = GitBlitSuite.url;
JM 63     String account = GitBlitSuite.account;
64     String password = GitBlitSuite.password;
7e8873 65
JM 66     private static final AtomicBoolean started = new AtomicBoolean(false);
31abc2 67
29692a 68     @BeforeClass
JM 69     public static void startGitblit() throws Exception {
7e8873 70         started.set(GitBlitSuite.startGitblit());
29692a 71     }
JM 72
73     @AfterClass
74     public static void stopGitblit() throws Exception {
7e8873 75         if (started.get()) {
JM 76             GitBlitSuite.stopGitblit();
77         }
29692a 78     }
JM 79
80     @Test
f08aab 81     public void testGetProtocolVersion() throws IOException {
JM 82         int protocol = RpcUtils.getProtocolVersion(url, null, null);
83         assertEquals(RpcServlet.PROTOCOL_VERSION, protocol);
84     }
85
86     @Test
31abc2 87     public void testListRepositories() throws IOException {
JM 88         Map<String, RepositoryModel> map = RpcUtils.getRepositories(url, null, null);
7e8873 89         assertNotNull("Repository list is null!", map);
93f0b1 90         assertTrue("Repository list is empty!", map.size() > 0);
JM 91     }
31abc2 92
29692a 93     @Test
31abc2 94     public void testListUsers() throws IOException {
JM 95         List<UserModel> list = null;
96         try {
97             list = RpcUtils.getUsers(url, null, null);
98         } catch (UnauthorizedException e) {
99         }
7e8873 100         assertNull("Server allows anyone to admin!", list);
31abc2 101
JM 102         list = RpcUtils.getUsers(url, "admin", "admin".toCharArray());
103         assertTrue("User list is empty!", list.size() > 0);
f08aab 104     }
JM 105
106     @Test
107     public void testListTeams() throws IOException {
108         List<TeamModel> list = null;
109         try {
110             list = RpcUtils.getTeams(url, null, null);
111         } catch (UnauthorizedException e) {
112         }
113         assertNull("Server allows anyone to admin!", list);
114
115         list = RpcUtils.getTeams(url, "admin", "admin".toCharArray());
116         assertTrue("Team list is empty!", list.size() > 0);
117         assertEquals("admins", list.get(0).name);
31abc2 118     }
JM 119
29692a 120     @Test
31abc2 121     public void testUserAdministration() throws IOException {
JM 122         UserModel user = new UserModel("garbage");
123         user.canAdmin = true;
124         user.password = "whocares";
125
126         // create
127         assertTrue("Failed to create user!",
128                 RpcUtils.createUser(user, url, account, password.toCharArray()));
129
130         UserModel retrievedUser = findUser(user.username);
7e8873 131         assertNotNull("Failed to find " + user.username, retrievedUser);
31abc2 132         assertTrue("Retrieved user can not administer Gitblit", retrievedUser.canAdmin);
JM 133
134         // rename and toggle admin permission
135         String originalName = user.username;
136         user.username = "garbage2";
137         user.canAdmin = false;
138         assertTrue("Failed to update user!",
139                 RpcUtils.updateUser(originalName, user, url, account, password.toCharArray()));
140
141         retrievedUser = findUser(user.username);
7e8873 142         assertNotNull("Failed to find " + user.username, retrievedUser);
31abc2 143         assertTrue("Retrieved user did not update", !retrievedUser.canAdmin);
JM 144
145         // delete
146         assertTrue("Failed to delete " + user.username,
147                 RpcUtils.deleteUser(retrievedUser, url, account, password.toCharArray()));
148
149         retrievedUser = findUser(user.username);
7e8873 150         assertNull("Failed to delete " + user.username, retrievedUser);
31abc2 151     }
JM 152
153     private UserModel findUser(String name) throws IOException {
154         List<UserModel> users = RpcUtils.getUsers(url, account, password.toCharArray());
155         UserModel retrievedUser = null;
156         for (UserModel model : users) {
157             if (model.username.equalsIgnoreCase(name)) {
158                 retrievedUser = model;
159                 break;
160             }
161         }
162         return retrievedUser;
163     }
164
29692a 165     @Test
31abc2 166     public void testRepositoryAdministration() throws IOException {
JM 167         RepositoryModel model = new RepositoryModel();
168         model.name = "garbagerepo.git";
169         model.description = "created by RpcUtils";
170         model.owner = "garbage";
171         model.accessRestriction = AccessRestrictionType.VIEW;
6adf56 172         model.authorizationControl = AuthorizationControl.AUTHENTICATED;
31abc2 173
JM 174         // create
175         assertTrue("Failed to create repository!",
176                 RpcUtils.createRepository(model, url, account, password.toCharArray()));
177
178         RepositoryModel retrievedRepository = findRepository(model.name);
7e8873 179         assertNotNull("Failed to find " + model.name, retrievedRepository);
JM 180         assertEquals(AccessRestrictionType.VIEW, retrievedRepository.accessRestriction);
6adf56 181         assertEquals(AuthorizationControl.AUTHENTICATED, retrievedRepository.authorizationControl);
31abc2 182
JM 183         // rename and change access restriciton
184         String originalName = model.name;
185         model.name = "garbagerepo2.git";
186         model.accessRestriction = AccessRestrictionType.PUSH;
822dfe 187         model.authorizationControl = AuthorizationControl.NAMED;
31abc2 188         assertTrue("Failed to update repository!", RpcUtils.updateRepository(originalName, model,
JM 189                 url, account, password.toCharArray()));
190
191         retrievedRepository = findRepository(model.name);
7e8873 192         assertNotNull("Failed to find " + model.name, retrievedRepository);
31abc2 193         assertTrue("Access retriction type is wrong",
JM 194                 AccessRestrictionType.PUSH.equals(retrievedRepository.accessRestriction));
195
196         // memberships
7e8873 197         UserModel testMember = new UserModel("justadded");
JM 198         assertTrue(RpcUtils.createUser(testMember, url, account, password.toCharArray()));
199
822dfe 200         List<RegistrantAccessPermission> permissions = RpcUtils.getRepositoryMemberPermissions(retrievedRepository, url, account,
31abc2 201                 password.toCharArray());
822dfe 202         assertEquals("Membership permissions is not empty!", 0, permissions.size());
644bdd 203         permissions.add(new RegistrantAccessPermission(testMember.username, AccessPermission.PUSH, PermissionType.EXPLICIT, RegistrantType.USER, null, true));
31abc2 204         assertTrue(
822dfe 205                 "Failed to set member permissions!",
JM 206                 RpcUtils.setRepositoryMemberPermissions(retrievedRepository, permissions, url, account,
31abc2 207                         password.toCharArray()));
822dfe 208         permissions = RpcUtils.getRepositoryMemberPermissions(retrievedRepository, url, account,
31abc2 209                 password.toCharArray());
JM 210         boolean foundMember = false;
822dfe 211         for (RegistrantAccessPermission permission : permissions) {
JM 212             if (permission.registrant.equalsIgnoreCase(testMember.username)) {
31abc2 213                 foundMember = true;
822dfe 214                 assertEquals(AccessPermission.PUSH, permission.permission);
31abc2 215                 break;
JM 216             }
217         }
218         assertTrue("Failed to find member!", foundMember);
219
220         // delete
221         assertTrue("Failed to delete " + model.name, RpcUtils.deleteRepository(retrievedRepository,
222                 url, account, password.toCharArray()));
223
224         retrievedRepository = findRepository(model.name);
7e8873 225         assertNull("Failed to delete " + model.name, retrievedRepository);
5c1ae2 226
JM 227         for (UserModel u : RpcUtils.getUsers(url, account, password.toCharArray())) {
7e8873 228             if (u.username.equals(testMember.username)) {
JM 229                 assertTrue(RpcUtils.deleteUser(u, url, account, password.toCharArray()));
5c1ae2 230                 break;
JM 231             }
232         }
31abc2 233     }
JM 234
235     private RepositoryModel findRepository(String name) throws IOException {
236         Map<String, RepositoryModel> repositories = RpcUtils.getRepositories(url, account,
237                 password.toCharArray());
238         RepositoryModel retrievedRepository = null;
239         for (RepositoryModel model : repositories.values()) {
240             if (model.name.equalsIgnoreCase(name)) {
241                 retrievedRepository = model;
242                 break;
243             }
244         }
245         return retrievedRepository;
246     }
247
29692a 248     @Test
f08aab 249     public void testTeamAdministration() throws IOException {
JM 250         List<TeamModel> teams = RpcUtils.getTeams(url, account, password.toCharArray());
251         assertEquals(1, teams.size());
252         
253         // Create the A-Team
254         TeamModel aTeam = new TeamModel("A-Team");
255         aTeam.users.add("admin");
20714a 256         aTeam.addRepositoryPermission("helloworld.git");
f08aab 257         assertTrue(RpcUtils.createTeam(aTeam, url, account, password.toCharArray()));
JM 258
259         aTeam = null;
260         teams = RpcUtils.getTeams(url, account, password.toCharArray());
261         assertEquals(2, teams.size());
262         for (TeamModel team : teams) {
263             if (team.name.equals("A-Team")) {
264                 aTeam = team;
265                 break;
266             }
267         }
268         assertNotNull(aTeam);
269         assertTrue(aTeam.hasUser("admin"));
20714a 270         assertTrue(aTeam.hasRepositoryPermission("helloworld.git"));
f08aab 271
JM 272         RepositoryModel helloworld = null;
273         Map<String, RepositoryModel> repositories = RpcUtils.getRepositories(url, account,
274                 password.toCharArray());
275         for (RepositoryModel repository : repositories.values()) {
276             if (repository.name.equals("helloworld.git")) {
277                 helloworld = repository;
278                 break;
279             }
280         }
281         assertNotNull(helloworld);
282         
283         // Confirm that we have added the team
284         List<String> helloworldTeams = RpcUtils.getRepositoryTeams(helloworld, url, account,
285                 password.toCharArray());
286         assertEquals(1, helloworldTeams.size());
287         assertTrue(helloworldTeams.contains(aTeam.name));
288
289         // set no teams
822dfe 290         List<RegistrantAccessPermission> permissions = new ArrayList<RegistrantAccessPermission>();
JM 291         for (String team : helloworldTeams) {
644bdd 292             permissions.add(new RegistrantAccessPermission(team, AccessPermission.NONE, PermissionType.EXPLICIT, RegistrantType.TEAM, null, true));
822dfe 293         }
JM 294         assertTrue(RpcUtils.setRepositoryTeamPermissions(helloworld, permissions, url, account,
f08aab 295                 password.toCharArray()));
JM 296         helloworldTeams = RpcUtils.getRepositoryTeams(helloworld, url, account,
297                 password.toCharArray());
298         assertEquals(0, helloworldTeams.size());
299         
300         // delete the A-Team
301         assertTrue(RpcUtils.deleteTeam(aTeam, url, account, password.toCharArray()));
302
303         teams = RpcUtils.getTeams(url, account, password.toCharArray());
304         assertEquals(1, teams.size());
305     }
306
307     @Test
31abc2 308     public void testFederationRegistrations() throws Exception {
JM 309         List<FederationModel> registrations = RpcUtils.getFederationRegistrations(url, account,
310                 password.toCharArray());
29692a 311         assertTrue("No federation registrations were retrieved!", registrations.size() >= 0);
31abc2 312     }
JM 313
29692a 314     @Test
31abc2 315     public void testFederationResultRegistrations() throws Exception {
JM 316         List<FederationModel> registrations = RpcUtils.getFederationResultRegistrations(url,
317                 account, password.toCharArray());
29692a 318         assertTrue("No federation result registrations were retrieved!", registrations.size() >= 0);
31abc2 319     }
5c1ae2 320
29692a 321     @Test
31abc2 322     public void testFederationProposals() throws Exception {
5c1ae2 323         List<FederationProposal> proposals = RpcUtils.getFederationProposals(url, account,
JM 324                 password.toCharArray());
29692a 325         assertTrue("No federation proposals were retrieved!", proposals.size() >= 0);
31abc2 326     }
5c1ae2 327
29692a 328     @Test
31abc2 329     public void testFederationSets() throws Exception {
5c1ae2 330         List<FederationSet> sets = RpcUtils.getFederationSets(url, account, password.toCharArray());
29692a 331         assertTrue("No federation sets were retrieved!", sets.size() >= 0);
31abc2 332     }
da0269 333
29692a 334     @Test
da0269 335     public void testSettings() throws Exception {
84c1d5 336         ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
7e8873 337         assertNotNull("No settings were retrieved!", settings);
b75734 338     }
97a20e 339
29692a 340     @Test
b75734 341     public void testServerStatus() throws Exception {
JM 342         ServerStatus status = RpcUtils.getStatus(url, account, password.toCharArray());
7e8873 343         assertNotNull("No status was retrieved!", status);
da0269 344     }
97a20e 345
29692a 346     @Test
97a20e 347     public void testUpdateSettings() throws Exception {
JM 348         Map<String, String> updated = new HashMap<String, String>();
c75304 349
97a20e 350         // grab current setting
JM 351         ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
352         boolean showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true);
353         showSizes = !showSizes;
c75304 354
97a20e 355         // update setting
JM 356         updated.put(Keys.web.showRepositorySizes, String.valueOf(showSizes));
29692a 357         boolean success = RpcUtils.updateSettings(updated, url, account, password.toCharArray());
97a20e 358         assertTrue("Failed to update server settings", success);
c75304 359
97a20e 360         // confirm setting change
JM 361         settings = RpcUtils.getSettings(url, account, password.toCharArray());
362         boolean newValue = settings.get(Keys.web.showRepositorySizes).getBoolean(false);
363         assertEquals(newValue, showSizes);
c75304 364
97a20e 365         // restore setting
JM 366         newValue = !newValue;
367         updated.put(Keys.web.showRepositorySizes, String.valueOf(newValue));
dcf575 368         success = RpcUtils.updateSettings(updated, url, account, password.toCharArray());
JM 369         assertTrue("Failed to update server settings", success);
370         settings = RpcUtils.getSettings(url, account, password.toCharArray());
371         showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true);
372         assertEquals(newValue, showSizes);
97a20e 373     }
c75304 374
29692a 375     @Test
c75304 376     public void testBranches() throws Exception {
17820f 377         Map<String, Collection<String>> branches = RpcUtils.getBranches(url, account,
c75304 378                 password.toCharArray());
7e8873 379         assertNotNull(branches);
c75304 380         assertTrue(branches.size() > 0);
JM 381     }
93f0b1 382 }