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