James Moger
2011-12-07 7e8873a14ccc2cb25213489d7d7ba97f09673831
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;
c75304 24 import java.util.Collection;
97a20e 25 import java.util.HashMap;
31abc2 26 import java.util.List;
93f0b1 27 import java.util.Map;
7e8873 28 import java.util.concurrent.atomic.AtomicBoolean;
93f0b1 29
29692a 30 import org.junit.AfterClass;
JM 31 import org.junit.BeforeClass;
32 import org.junit.Test;
93f0b1 33
31abc2 34 import com.gitblit.Constants.AccessRestrictionType;
JM 35 import com.gitblit.GitBlitException.UnauthorizedException;
97a20e 36 import com.gitblit.Keys;
31abc2 37 import com.gitblit.models.FederationModel;
JM 38 import com.gitblit.models.FederationProposal;
39 import com.gitblit.models.FederationSet;
93f0b1 40 import com.gitblit.models.RepositoryModel;
84c1d5 41 import com.gitblit.models.ServerSettings;
b75734 42 import com.gitblit.models.ServerStatus;
31abc2 43 import com.gitblit.models.UserModel;
93f0b1 44 import com.gitblit.utils.RpcUtils;
JM 45
31abc2 46 /**
JM 47  * Tests all the rpc client utility methods, the rpc filter and rpc servlet.
48  * 
49  * @author James Moger
50  * 
51  */
7e8873 52 public class RpcTests {
JM 53
143fc9 54     String url = GitBlitSuite.url;
JM 55     String account = GitBlitSuite.account;
56     String password = GitBlitSuite.password;
7e8873 57
JM 58     private static final AtomicBoolean started = new AtomicBoolean(false);
31abc2 59
29692a 60     @BeforeClass
JM 61     public static void startGitblit() throws Exception {
7e8873 62         started.set(GitBlitSuite.startGitblit());
29692a 63     }
JM 64
65     @AfterClass
66     public static void stopGitblit() throws Exception {
7e8873 67         if (started.get()) {
JM 68             GitBlitSuite.stopGitblit();
69         }
29692a 70     }
JM 71
72     @Test
31abc2 73     public void testListRepositories() throws IOException {
JM 74         Map<String, RepositoryModel> map = RpcUtils.getRepositories(url, null, null);
7e8873 75         assertNotNull("Repository list is null!", map);
93f0b1 76         assertTrue("Repository list is empty!", map.size() > 0);
JM 77     }
31abc2 78
29692a 79     @Test
31abc2 80     public void testListUsers() throws IOException {
JM 81         List<UserModel> list = null;
82         try {
83             list = RpcUtils.getUsers(url, null, null);
84         } catch (UnauthorizedException e) {
85         }
7e8873 86         assertNull("Server allows anyone to admin!", list);
31abc2 87
JM 88         list = RpcUtils.getUsers(url, "admin", "admin".toCharArray());
89         assertTrue("User list is empty!", list.size() > 0);
90     }
91
29692a 92     @Test
31abc2 93     public void testUserAdministration() throws IOException {
JM 94         UserModel user = new UserModel("garbage");
95         user.canAdmin = true;
96         user.password = "whocares";
97
98         // create
99         assertTrue("Failed to create user!",
100                 RpcUtils.createUser(user, url, account, password.toCharArray()));
101
102         UserModel retrievedUser = findUser(user.username);
7e8873 103         assertNotNull("Failed to find " + user.username, retrievedUser);
31abc2 104         assertTrue("Retrieved user can not administer Gitblit", retrievedUser.canAdmin);
JM 105
106         // rename and toggle admin permission
107         String originalName = user.username;
108         user.username = "garbage2";
109         user.canAdmin = false;
110         assertTrue("Failed to update user!",
111                 RpcUtils.updateUser(originalName, user, url, account, password.toCharArray()));
112
113         retrievedUser = findUser(user.username);
7e8873 114         assertNotNull("Failed to find " + user.username, retrievedUser);
31abc2 115         assertTrue("Retrieved user did not update", !retrievedUser.canAdmin);
JM 116
117         // delete
118         assertTrue("Failed to delete " + user.username,
119                 RpcUtils.deleteUser(retrievedUser, url, account, password.toCharArray()));
120
121         retrievedUser = findUser(user.username);
7e8873 122         assertNull("Failed to delete " + user.username, retrievedUser);
31abc2 123     }
JM 124
125     private UserModel findUser(String name) throws IOException {
126         List<UserModel> users = RpcUtils.getUsers(url, account, password.toCharArray());
127         UserModel retrievedUser = null;
128         for (UserModel model : users) {
129             if (model.username.equalsIgnoreCase(name)) {
130                 retrievedUser = model;
131                 break;
132             }
133         }
134         return retrievedUser;
135     }
136
29692a 137     @Test
31abc2 138     public void testRepositoryAdministration() throws IOException {
JM 139         RepositoryModel model = new RepositoryModel();
140         model.name = "garbagerepo.git";
141         model.description = "created by RpcUtils";
142         model.owner = "garbage";
143         model.accessRestriction = AccessRestrictionType.VIEW;
144
145         // create
146         assertTrue("Failed to create repository!",
147                 RpcUtils.createRepository(model, url, account, password.toCharArray()));
148
149         RepositoryModel retrievedRepository = findRepository(model.name);
7e8873 150         assertNotNull("Failed to find " + model.name, retrievedRepository);
JM 151         assertEquals(AccessRestrictionType.VIEW, retrievedRepository.accessRestriction);
31abc2 152
JM 153         // rename and change access restriciton
154         String originalName = model.name;
155         model.name = "garbagerepo2.git";
156         model.accessRestriction = AccessRestrictionType.PUSH;
157         assertTrue("Failed to update repository!", RpcUtils.updateRepository(originalName, model,
158                 url, account, password.toCharArray()));
159
160         retrievedRepository = findRepository(model.name);
7e8873 161         assertNotNull("Failed to find " + model.name, retrievedRepository);
31abc2 162         assertTrue("Access retriction type is wrong",
JM 163                 AccessRestrictionType.PUSH.equals(retrievedRepository.accessRestriction));
164
165         // memberships
7e8873 166         UserModel testMember = new UserModel("justadded");
JM 167         assertTrue(RpcUtils.createUser(testMember, url, account, password.toCharArray()));
168
31abc2 169         List<String> members = RpcUtils.getRepositoryMembers(retrievedRepository, url, account,
JM 170                 password.toCharArray());
7e8873 171         assertEquals("Membership roster is not empty!", 0, members.size());
JM 172         members.add(testMember.username);
31abc2 173         assertTrue(
JM 174                 "Failed to set memberships!",
175                 RpcUtils.setRepositoryMembers(retrievedRepository, members, url, account,
176                         password.toCharArray()));
177         members = RpcUtils.getRepositoryMembers(retrievedRepository, url, account,
178                 password.toCharArray());
179         boolean foundMember = false;
180         for (String member : members) {
7e8873 181             if (member.equalsIgnoreCase(testMember.username)) {
31abc2 182                 foundMember = true;
JM 183                 break;
184             }
185         }
186         assertTrue("Failed to find member!", foundMember);
187
188         // delete
189         assertTrue("Failed to delete " + model.name, RpcUtils.deleteRepository(retrievedRepository,
190                 url, account, password.toCharArray()));
191
192         retrievedRepository = findRepository(model.name);
7e8873 193         assertNull("Failed to delete " + model.name, retrievedRepository);
5c1ae2 194
JM 195         for (UserModel u : RpcUtils.getUsers(url, account, password.toCharArray())) {
7e8873 196             if (u.username.equals(testMember.username)) {
JM 197                 assertTrue(RpcUtils.deleteUser(u, url, account, password.toCharArray()));
5c1ae2 198                 break;
JM 199             }
200         }
31abc2 201     }
JM 202
203     private RepositoryModel findRepository(String name) throws IOException {
204         Map<String, RepositoryModel> repositories = RpcUtils.getRepositories(url, account,
205                 password.toCharArray());
206         RepositoryModel retrievedRepository = null;
207         for (RepositoryModel model : repositories.values()) {
208             if (model.name.equalsIgnoreCase(name)) {
209                 retrievedRepository = model;
210                 break;
211             }
212         }
213         return retrievedRepository;
214     }
215
29692a 216     @Test
31abc2 217     public void testFederationRegistrations() throws Exception {
JM 218         List<FederationModel> registrations = RpcUtils.getFederationRegistrations(url, account,
219                 password.toCharArray());
29692a 220         assertTrue("No federation registrations were retrieved!", registrations.size() >= 0);
31abc2 221     }
JM 222
29692a 223     @Test
31abc2 224     public void testFederationResultRegistrations() throws Exception {
JM 225         List<FederationModel> registrations = RpcUtils.getFederationResultRegistrations(url,
226                 account, password.toCharArray());
29692a 227         assertTrue("No federation result registrations were retrieved!", registrations.size() >= 0);
31abc2 228     }
5c1ae2 229
29692a 230     @Test
31abc2 231     public void testFederationProposals() throws Exception {
5c1ae2 232         List<FederationProposal> proposals = RpcUtils.getFederationProposals(url, account,
JM 233                 password.toCharArray());
29692a 234         assertTrue("No federation proposals were retrieved!", proposals.size() >= 0);
31abc2 235     }
5c1ae2 236
29692a 237     @Test
31abc2 238     public void testFederationSets() throws Exception {
5c1ae2 239         List<FederationSet> sets = RpcUtils.getFederationSets(url, account, password.toCharArray());
29692a 240         assertTrue("No federation sets were retrieved!", sets.size() >= 0);
31abc2 241     }
da0269 242
29692a 243     @Test
da0269 244     public void testSettings() throws Exception {
84c1d5 245         ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
7e8873 246         assertNotNull("No settings were retrieved!", settings);
b75734 247     }
97a20e 248
29692a 249     @Test
b75734 250     public void testServerStatus() throws Exception {
JM 251         ServerStatus status = RpcUtils.getStatus(url, account, password.toCharArray());
7e8873 252         assertNotNull("No status was retrieved!", status);
da0269 253     }
97a20e 254
29692a 255     @Test
97a20e 256     public void testUpdateSettings() throws Exception {
JM 257         Map<String, String> updated = new HashMap<String, String>();
c75304 258
97a20e 259         // grab current setting
JM 260         ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
261         boolean showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true);
262         showSizes = !showSizes;
c75304 263
97a20e 264         // update setting
JM 265         updated.put(Keys.web.showRepositorySizes, String.valueOf(showSizes));
29692a 266         boolean success = RpcUtils.updateSettings(updated, url, account, password.toCharArray());
97a20e 267         assertTrue("Failed to update server settings", success);
c75304 268
97a20e 269         // confirm setting change
JM 270         settings = RpcUtils.getSettings(url, account, password.toCharArray());
271         boolean newValue = settings.get(Keys.web.showRepositorySizes).getBoolean(false);
272         assertEquals(newValue, showSizes);
c75304 273
97a20e 274         // restore setting
JM 275         newValue = !newValue;
276         updated.put(Keys.web.showRepositorySizes, String.valueOf(newValue));
277     }
c75304 278
29692a 279     @Test
c75304 280     public void testBranches() throws Exception {
17820f 281         Map<String, Collection<String>> branches = RpcUtils.getBranches(url, account,
c75304 282                 password.toCharArray());
7e8873 283         assertNotNull(branches);
c75304 284         assertTrue(branches.size() > 0);
JM 285     }
93f0b1 286 }