James Moger
2011-11-02 29692a574b10a01e070c0454868a3c234039ab7b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright 2011 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.tests;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
 
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
 
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
 
import com.gitblit.Constants.AccessRestrictionType;
import com.gitblit.GitBlitException.UnauthorizedException;
import com.gitblit.GitBlitServer;
import com.gitblit.Keys;
import com.gitblit.models.FederationModel;
import com.gitblit.models.FederationProposal;
import com.gitblit.models.FederationSet;
import com.gitblit.models.RepositoryModel;
import com.gitblit.models.ServerSettings;
import com.gitblit.models.ServerStatus;
import com.gitblit.models.UserModel;
import com.gitblit.utils.RpcUtils;
 
/**
 * Tests all the rpc client utility methods, the rpc filter and rpc servlet.
 * 
 * @author James Moger
 * 
 */
public class RpcTests {
 
    static int port = 8180;
    static int shutdownPort = 8181;
 
    String url = "http://localhost:" + port;
    String account = "admin";
    String password = "admin";
 
    @BeforeClass
    public static void startGitblit() throws Exception {
        // Start a Gitblit instance
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            public void run() {
                GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
                        "" + shutdownPort, "--repositoriesFolder",
                        "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
                        "distrib/users.properties");
            }
        });
 
        // Wait a few seconds for it to be running
        Thread.sleep(2500);
    }
 
    @AfterClass
    public static void stopGitblit() throws Exception {
        // Stop Gitblit
        GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
 
        // Wait a few seconds for it to be running
        Thread.sleep(2500);
    }
 
    @Test
    public void testListRepositories() throws IOException {
        Map<String, RepositoryModel> map = RpcUtils.getRepositories(url, null, null);
        assertTrue("Repository list is null!", map != null);
        assertTrue("Repository list is empty!", map.size() > 0);
    }
 
    @Test
    public void testListUsers() throws IOException {
        List<UserModel> list = null;
        try {
            list = RpcUtils.getUsers(url, null, null);
        } catch (UnauthorizedException e) {
        }
        assertTrue("Server allows anyone to admin!", list == null);
 
        list = RpcUtils.getUsers(url, "admin", "admin".toCharArray());
        assertTrue("User list is empty!", list.size() > 0);
    }
 
    @Test
    public void testUserAdministration() throws IOException {
        UserModel user = new UserModel("garbage");
        user.canAdmin = true;
        user.password = "whocares";
 
        // create
        assertTrue("Failed to create user!",
                RpcUtils.createUser(user, url, account, password.toCharArray()));
 
        UserModel retrievedUser = findUser(user.username);
        assertTrue("Failed to find " + user.username, retrievedUser != null);
        assertTrue("Retrieved user can not administer Gitblit", retrievedUser.canAdmin);
 
        // rename and toggle admin permission
        String originalName = user.username;
        user.username = "garbage2";
        user.canAdmin = false;
        assertTrue("Failed to update user!",
                RpcUtils.updateUser(originalName, user, url, account, password.toCharArray()));
 
        retrievedUser = findUser(user.username);
        assertTrue("Failed to find " + user.username, retrievedUser != null);
        assertTrue("Retrieved user did not update", !retrievedUser.canAdmin);
 
        // delete
        assertTrue("Failed to delete " + user.username,
                RpcUtils.deleteUser(retrievedUser, url, account, password.toCharArray()));
 
        retrievedUser = findUser(user.username);
        assertTrue("Failed to delete " + user.username, retrievedUser == null);
    }
 
    private UserModel findUser(String name) throws IOException {
        List<UserModel> users = RpcUtils.getUsers(url, account, password.toCharArray());
        UserModel retrievedUser = null;
        for (UserModel model : users) {
            if (model.username.equalsIgnoreCase(name)) {
                retrievedUser = model;
                break;
            }
        }
        return retrievedUser;
    }
 
    @Test
    public void testRepositoryAdministration() throws IOException {
        RepositoryModel model = new RepositoryModel();
        model.name = "garbagerepo.git";
        model.description = "created by RpcUtils";
        model.owner = "garbage";
        model.accessRestriction = AccessRestrictionType.VIEW;
 
        // create
        assertTrue("Failed to create repository!",
                RpcUtils.createRepository(model, url, account, password.toCharArray()));
 
        RepositoryModel retrievedRepository = findRepository(model.name);
        assertTrue("Failed to find " + model.name, retrievedRepository != null);
        assertTrue("Access retriction type is wrong",
                AccessRestrictionType.VIEW.equals(retrievedRepository.accessRestriction));
 
        // rename and change access restriciton
        String originalName = model.name;
        model.name = "garbagerepo2.git";
        model.accessRestriction = AccessRestrictionType.PUSH;
        assertTrue("Failed to update repository!", RpcUtils.updateRepository(originalName, model,
                url, account, password.toCharArray()));
 
        retrievedRepository = findRepository(model.name);
        assertTrue("Failed to find " + model.name, retrievedRepository != null);
        assertTrue("Access retriction type is wrong",
                AccessRestrictionType.PUSH.equals(retrievedRepository.accessRestriction));
 
        // memberships
        String testMember = "justadded";
        List<String> members = RpcUtils.getRepositoryMembers(retrievedRepository, url, account,
                password.toCharArray());
        assertTrue("Membership roster is not empty!", members.size() == 0);
        members.add(testMember);
        assertTrue(
                "Failed to set memberships!",
                RpcUtils.setRepositoryMembers(retrievedRepository, members, url, account,
                        password.toCharArray()));
        members = RpcUtils.getRepositoryMembers(retrievedRepository, url, account,
                password.toCharArray());
        boolean foundMember = false;
        for (String member : members) {
            if (member.equalsIgnoreCase(testMember)) {
                foundMember = true;
                break;
            }
        }
        assertTrue("Failed to find member!", foundMember);
 
        // delete
        assertTrue("Failed to delete " + model.name, RpcUtils.deleteRepository(retrievedRepository,
                url, account, password.toCharArray()));
 
        retrievedRepository = findRepository(model.name);
        assertTrue("Failed to delete " + model.name, retrievedRepository == null);
 
        for (UserModel u : RpcUtils.getUsers(url, account, password.toCharArray())) {
            if (u.username.equals(testMember)) {
                RpcUtils.deleteUser(u, url, account, password.toCharArray());
                break;
            }
        }
    }
 
    private RepositoryModel findRepository(String name) throws IOException {
        Map<String, RepositoryModel> repositories = RpcUtils.getRepositories(url, account,
                password.toCharArray());
        RepositoryModel retrievedRepository = null;
        for (RepositoryModel model : repositories.values()) {
            if (model.name.equalsIgnoreCase(name)) {
                retrievedRepository = model;
                break;
            }
        }
        return retrievedRepository;
    }
 
    @Test
    public void testFederationRegistrations() throws Exception {
        List<FederationModel> registrations = RpcUtils.getFederationRegistrations(url, account,
                password.toCharArray());
        assertTrue("No federation registrations were retrieved!", registrations.size() >= 0);
    }
 
    @Test
    public void testFederationResultRegistrations() throws Exception {
        List<FederationModel> registrations = RpcUtils.getFederationResultRegistrations(url,
                account, password.toCharArray());
        assertTrue("No federation result registrations were retrieved!", registrations.size() >= 0);
    }
 
    @Test
    public void testFederationProposals() throws Exception {
        List<FederationProposal> proposals = RpcUtils.getFederationProposals(url, account,
                password.toCharArray());
        assertTrue("No federation proposals were retrieved!", proposals.size() >= 0);
    }
 
    @Test
    public void testFederationSets() throws Exception {
        List<FederationSet> sets = RpcUtils.getFederationSets(url, account, password.toCharArray());
        assertTrue("No federation sets were retrieved!", sets.size() >= 0);
    }
 
    @Test
    public void testSettings() throws Exception {
        ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
        assertTrue("No settings were retrieved!", settings != null);
    }
 
    @Test
    public void testServerStatus() throws Exception {
        ServerStatus status = RpcUtils.getStatus(url, account, password.toCharArray());
        assertTrue("No status was retrieved!", status != null);
    }
 
    @Test
    public void testUpdateSettings() throws Exception {
        Map<String, String> updated = new HashMap<String, String>();
 
        // grab current setting
        ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
        boolean showSizes = settings.get(Keys.web.showRepositorySizes).getBoolean(true);
        showSizes = !showSizes;
 
        // update setting
        updated.put(Keys.web.showRepositorySizes, String.valueOf(showSizes));
        boolean success = RpcUtils.updateSettings(updated, url, account, password.toCharArray());
        assertTrue("Failed to update server settings", success);
 
        // confirm setting change
        settings = RpcUtils.getSettings(url, account, password.toCharArray());
        boolean newValue = settings.get(Keys.web.showRepositorySizes).getBoolean(false);
        assertEquals(newValue, showSizes);
 
        // restore setting
        newValue = !newValue;
        updated.put(Keys.web.showRepositorySizes, String.valueOf(newValue));
    }
 
    @Test
    public void testBranches() throws Exception {
        Map<String, Collection<String>> branches = RpcUtils.getBranches(url, account,
                password.toCharArray());
        assertTrue(branches != null);
        assertTrue(branches.size() > 0);
    }
}