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 |
|
|
18 |
import java.io.IOException;
|
31abc2
|
19 |
import java.util.List;
|
93f0b1
|
20 |
import java.util.Map;
|
JM |
21 |
|
|
22 |
import junit.framework.TestCase;
|
|
23 |
|
31abc2
|
24 |
import com.gitblit.Constants.AccessRestrictionType;
|
JM |
25 |
import com.gitblit.GitBlitException.UnauthorizedException;
|
|
26 |
import com.gitblit.models.FederationModel;
|
|
27 |
import com.gitblit.models.FederationProposal;
|
|
28 |
import com.gitblit.models.FederationSet;
|
93f0b1
|
29 |
import com.gitblit.models.RepositoryModel;
|
b75734
|
30 |
import com.gitblit.models.ServerStatus;
|
JM |
31 |
import com.gitblit.models.SettingModel;
|
31abc2
|
32 |
import com.gitblit.models.UserModel;
|
93f0b1
|
33 |
import com.gitblit.utils.RpcUtils;
|
JM |
34 |
|
31abc2
|
35 |
/**
|
JM |
36 |
* Tests all the rpc client utility methods, the rpc filter and rpc servlet.
|
|
37 |
*
|
|
38 |
* @author James Moger
|
|
39 |
*
|
|
40 |
*/
|
93f0b1
|
41 |
public class RpcTests extends TestCase {
|
JM |
42 |
|
31abc2
|
43 |
String url = "https://localhost:8443";
|
JM |
44 |
String account = "admin";
|
|
45 |
String password = "admin";
|
|
46 |
|
|
47 |
public void testListRepositories() throws IOException {
|
|
48 |
Map<String, RepositoryModel> map = RpcUtils.getRepositories(url, null, null);
|
93f0b1
|
49 |
assertTrue("Repository list is null!", map != null);
|
JM |
50 |
assertTrue("Repository list is empty!", map.size() > 0);
|
|
51 |
}
|
31abc2
|
52 |
|
JM |
53 |
public void testListUsers() throws IOException {
|
|
54 |
List<UserModel> list = null;
|
|
55 |
try {
|
|
56 |
list = RpcUtils.getUsers(url, null, null);
|
|
57 |
} catch (UnauthorizedException e) {
|
|
58 |
}
|
|
59 |
assertTrue("Server allows anyone to admin!", list == null);
|
|
60 |
|
|
61 |
list = RpcUtils.getUsers(url, "admin", "admin".toCharArray());
|
|
62 |
assertTrue("User list is empty!", list.size() > 0);
|
|
63 |
}
|
|
64 |
|
|
65 |
public void testUserAdministration() throws IOException {
|
|
66 |
UserModel user = new UserModel("garbage");
|
|
67 |
user.canAdmin = true;
|
|
68 |
user.password = "whocares";
|
|
69 |
|
|
70 |
// create
|
|
71 |
assertTrue("Failed to create user!",
|
|
72 |
RpcUtils.createUser(user, url, account, password.toCharArray()));
|
|
73 |
|
|
74 |
UserModel retrievedUser = findUser(user.username);
|
|
75 |
assertTrue("Failed to find " + user.username, retrievedUser != null);
|
|
76 |
assertTrue("Retrieved user can not administer Gitblit", retrievedUser.canAdmin);
|
|
77 |
|
|
78 |
// rename and toggle admin permission
|
|
79 |
String originalName = user.username;
|
|
80 |
user.username = "garbage2";
|
|
81 |
user.canAdmin = false;
|
|
82 |
assertTrue("Failed to update user!",
|
|
83 |
RpcUtils.updateUser(originalName, user, url, account, password.toCharArray()));
|
|
84 |
|
|
85 |
retrievedUser = findUser(user.username);
|
|
86 |
assertTrue("Failed to find " + user.username, retrievedUser != null);
|
|
87 |
assertTrue("Retrieved user did not update", !retrievedUser.canAdmin);
|
|
88 |
|
|
89 |
// delete
|
|
90 |
assertTrue("Failed to delete " + user.username,
|
|
91 |
RpcUtils.deleteUser(retrievedUser, url, account, password.toCharArray()));
|
|
92 |
|
|
93 |
retrievedUser = findUser(user.username);
|
|
94 |
assertTrue("Failed to delete " + user.username, retrievedUser == null);
|
|
95 |
}
|
|
96 |
|
|
97 |
private UserModel findUser(String name) throws IOException {
|
|
98 |
List<UserModel> users = RpcUtils.getUsers(url, account, password.toCharArray());
|
|
99 |
UserModel retrievedUser = null;
|
|
100 |
for (UserModel model : users) {
|
|
101 |
if (model.username.equalsIgnoreCase(name)) {
|
|
102 |
retrievedUser = model;
|
|
103 |
break;
|
|
104 |
}
|
|
105 |
}
|
|
106 |
return retrievedUser;
|
|
107 |
}
|
|
108 |
|
|
109 |
public void testRepositoryAdministration() throws IOException {
|
|
110 |
RepositoryModel model = new RepositoryModel();
|
|
111 |
model.name = "garbagerepo.git";
|
|
112 |
model.description = "created by RpcUtils";
|
|
113 |
model.owner = "garbage";
|
|
114 |
model.accessRestriction = AccessRestrictionType.VIEW;
|
|
115 |
|
|
116 |
// create
|
|
117 |
assertTrue("Failed to create repository!",
|
|
118 |
RpcUtils.createRepository(model, url, account, password.toCharArray()));
|
|
119 |
|
|
120 |
RepositoryModel retrievedRepository = findRepository(model.name);
|
|
121 |
assertTrue("Failed to find " + model.name, retrievedRepository != null);
|
|
122 |
assertTrue("Access retriction type is wrong",
|
|
123 |
AccessRestrictionType.VIEW.equals(retrievedRepository.accessRestriction));
|
|
124 |
|
|
125 |
// rename and change access restriciton
|
|
126 |
String originalName = model.name;
|
|
127 |
model.name = "garbagerepo2.git";
|
|
128 |
model.accessRestriction = AccessRestrictionType.PUSH;
|
|
129 |
assertTrue("Failed to update repository!", RpcUtils.updateRepository(originalName, model,
|
|
130 |
url, account, password.toCharArray()));
|
|
131 |
|
|
132 |
retrievedRepository = findRepository(model.name);
|
|
133 |
assertTrue("Failed to find " + model.name, retrievedRepository != null);
|
|
134 |
assertTrue("Access retriction type is wrong",
|
|
135 |
AccessRestrictionType.PUSH.equals(retrievedRepository.accessRestriction));
|
|
136 |
|
|
137 |
// memberships
|
|
138 |
String testMember = "justadded";
|
|
139 |
List<String> members = RpcUtils.getRepositoryMembers(retrievedRepository, url, account,
|
|
140 |
password.toCharArray());
|
|
141 |
assertTrue("Membership roster is not empty!", members.size() == 0);
|
|
142 |
members.add(testMember);
|
|
143 |
assertTrue(
|
|
144 |
"Failed to set memberships!",
|
|
145 |
RpcUtils.setRepositoryMembers(retrievedRepository, members, url, account,
|
|
146 |
password.toCharArray()));
|
|
147 |
members = RpcUtils.getRepositoryMembers(retrievedRepository, url, account,
|
|
148 |
password.toCharArray());
|
|
149 |
boolean foundMember = false;
|
|
150 |
for (String member : members) {
|
|
151 |
if (member.equalsIgnoreCase(testMember)) {
|
|
152 |
foundMember = true;
|
|
153 |
break;
|
|
154 |
}
|
|
155 |
}
|
|
156 |
assertTrue("Failed to find member!", foundMember);
|
|
157 |
|
|
158 |
// delete
|
|
159 |
assertTrue("Failed to delete " + model.name, RpcUtils.deleteRepository(retrievedRepository,
|
|
160 |
url, account, password.toCharArray()));
|
|
161 |
|
|
162 |
retrievedRepository = findRepository(model.name);
|
|
163 |
assertTrue("Failed to delete " + model.name, retrievedRepository == null);
|
5c1ae2
|
164 |
|
JM |
165 |
for (UserModel u : RpcUtils.getUsers(url, account, password.toCharArray())) {
|
|
166 |
if (u.username.equals(testMember)) {
|
|
167 |
RpcUtils.deleteUser(u, url, account, password.toCharArray());
|
|
168 |
break;
|
|
169 |
}
|
|
170 |
}
|
31abc2
|
171 |
}
|
JM |
172 |
|
|
173 |
private RepositoryModel findRepository(String name) throws IOException {
|
|
174 |
Map<String, RepositoryModel> repositories = RpcUtils.getRepositories(url, account,
|
|
175 |
password.toCharArray());
|
|
176 |
RepositoryModel retrievedRepository = null;
|
|
177 |
for (RepositoryModel model : repositories.values()) {
|
|
178 |
if (model.name.equalsIgnoreCase(name)) {
|
|
179 |
retrievedRepository = model;
|
|
180 |
break;
|
|
181 |
}
|
|
182 |
}
|
|
183 |
return retrievedRepository;
|
|
184 |
}
|
|
185 |
|
|
186 |
public void testFederationRegistrations() throws Exception {
|
|
187 |
List<FederationModel> registrations = RpcUtils.getFederationRegistrations(url, account,
|
|
188 |
password.toCharArray());
|
|
189 |
assertTrue("No federation registrations wre retrieved!", registrations.size() > 0);
|
|
190 |
}
|
|
191 |
|
|
192 |
public void testFederationResultRegistrations() throws Exception {
|
|
193 |
List<FederationModel> registrations = RpcUtils.getFederationResultRegistrations(url,
|
|
194 |
account, password.toCharArray());
|
|
195 |
assertTrue("No federation result registrations were retrieved!", registrations.size() > 0);
|
|
196 |
}
|
5c1ae2
|
197 |
|
31abc2
|
198 |
public void testFederationProposals() throws Exception {
|
5c1ae2
|
199 |
List<FederationProposal> proposals = RpcUtils.getFederationProposals(url, account,
|
JM |
200 |
password.toCharArray());
|
31abc2
|
201 |
assertTrue("No federation proposals were retrieved!", proposals.size() > 0);
|
JM |
202 |
}
|
5c1ae2
|
203 |
|
31abc2
|
204 |
public void testFederationSets() throws Exception {
|
5c1ae2
|
205 |
List<FederationSet> sets = RpcUtils.getFederationSets(url, account, password.toCharArray());
|
31abc2
|
206 |
assertTrue("No federation sets were retrieved!", sets.size() > 0);
|
JM |
207 |
}
|
da0269
|
208 |
|
JM |
209 |
public void testSettings() throws Exception {
|
b75734
|
210 |
Map<String, SettingModel> settings = RpcUtils.getSettings(url, account, password.toCharArray());
|
JM |
211 |
assertTrue("No settings were retrieved!", settings != null);
|
|
212 |
}
|
|
213 |
|
|
214 |
public void testServerStatus() throws Exception {
|
|
215 |
ServerStatus status = RpcUtils.getStatus(url, account, password.toCharArray());
|
|
216 |
assertTrue("No status was retrieved!", status != null);
|
da0269
|
217 |
}
|
93f0b1
|
218 |
}
|