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