2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c)) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 package org.collectionspace.services.security.client.test;
25 import java.util.List;
26 import javax.ws.rs.core.Response;
27 import org.jboss.resteasy.client.ClientResponse;
29 import org.testng.Assert;
30 import org.testng.annotations.Test;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import org.collectionspace.services.account.AccountTenant;
35 import org.collectionspace.services.account.AccountsCommon;
36 import org.collectionspace.services.account.Status;
37 import org.collectionspace.services.client.AccountClient;
38 import org.collectionspace.services.client.AccountFactory;
39 import org.collectionspace.services.client.CollectionObjectClient;
40 import org.collectionspace.services.client.CollectionObjectFactory;
41 import org.collectionspace.services.client.CollectionSpaceClient;
42 import org.collectionspace.services.client.PoxPayloadOut;
43 import org.collectionspace.services.client.test.BaseServiceTest;
44 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
45 import org.collectionspace.services.collectionobject.TitleGroup;
46 import org.collectionspace.services.collectionobject.TitleGroupList;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
50 * AuthenticationServiceTest uses CollectionObject service to test
53 * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
56 public class AuthenticationServiceTest extends BaseServiceTest<AbstractCommonList> {
58 private final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
59 /** The known resource id. */
60 private String barneyAccountId = null; //active
61 private String georgeAccountId = null; //inactive
63 private final String CLASS_NAME = AuthenticationServiceTest.class.getName();
66 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
69 protected String getServicePathComponent() {
70 // no need to return anything but null since no auth resources are
72 throw new UnsupportedOperationException();
76 protected String getServiceName() {
77 // no need to return anything but null since no auth resources are
79 throw new UnsupportedOperationException();
83 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
86 protected CollectionSpaceClient getClientInstance() {
87 return new AccountClient();
91 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
94 protected AbstractCommonList getCommonList(
95 ClientResponse<AbstractCommonList> response) {
96 throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
99 @Test(dataProvider = "testName")
100 public void createActiveAccount(String testName) throws Exception {
101 // Perform setup, such as initializing the type of service request
102 // (e.g. CREATE, DELETE), its valid and expected status codes, and
103 // its associated HTTP method name (e.g. POST, DELETE).
106 AccountClient accountClient = new AccountClient();
107 // This should not be needed - the auth is already set up
108 //accountClient.setAuth(true, "test", true, "test", true);
110 // Submit the request to the service and store the response.
111 AccountsCommon account =
112 createAccountInstance("barney", "barney08", "barney@dinoland.com",
113 accountClient.getTenantId(), false);
114 ClientResponse<Response> res = accountClient.create(account);
115 int statusCode = res.getStatus();
117 if (logger.isDebugEnabled()) {
118 logger.debug(testName + ": barney status = " + statusCode);
120 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
121 invalidStatusCodeMessage(testRequestType, statusCode));
122 Assert.assertEquals(statusCode, testExpectedStatusCode);
124 // Store the ID returned from this create operation
125 // for additional tests below.
126 barneyAccountId = extractId(res);
127 if (logger.isDebugEnabled()) {
128 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
130 res.releaseConnection();
134 @Test(dataProvider = "testName")
135 public void createInactiveAccount(String testName) throws Exception {
139 AccountClient accountClient = new AccountClient();
140 // This should not be needed - the auth is already set up
141 //accountClient.setAuth(true, "test", true, "test", true);
143 // Submit the request to the service and store the response.
144 AccountsCommon account =
145 createAccountInstance("george", "george08", "george@curiousland.com",
146 accountClient.getTenantId(), false);
147 ClientResponse<Response> res = accountClient.create(account);
148 int statusCode = res.getStatus();
150 if (logger.isDebugEnabled()) {
151 logger.debug(testName + ": george status = " + statusCode);
153 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
154 invalidStatusCodeMessage(testRequestType, statusCode));
155 Assert.assertEquals(statusCode, testExpectedStatusCode);
157 // Store the ID returned from this create operation
158 // for additional tests below.
159 georgeAccountId = extractId(res);
160 if (logger.isDebugEnabled()) {
161 logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
163 res.releaseConnection();
166 account.setStatus(Status.INACTIVE);
167 if (logger.isDebugEnabled()) {
168 logger.debug(testName + ":updated object");
169 logger.debug(objectAsXmlString(account,
170 AccountsCommon.class));
173 // Submit the request to the service and store the response.
174 ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
175 statusCode = res1.getStatus();
176 // Check the status code of the response: does it match the expected response(s)?
177 if (logger.isDebugEnabled()) {
178 logger.debug(testName + ": status = " + statusCode);
180 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
181 invalidStatusCodeMessage(testRequestType, statusCode));
182 Assert.assertEquals(statusCode, testExpectedStatusCode);
183 res1.releaseConnection();
188 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
190 @Test(dataProvider = "testName",
191 dependsOnMethods = {"createActiveAccount"})
192 public void create(String testName) {
195 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
196 collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
197 String identifier = createIdentifier();
198 PoxPayloadOut multipart = createCollectionObjectInstance(
199 collectionObjectClient.getCommonPartName(), identifier);
200 ClientResponse<Response> res = collectionObjectClient.create(multipart);
201 if (logger.isDebugEnabled()) {
202 logger.debug("create: status = " + res.getStatus());
204 //so it does not have any permissions out-of-the-box to create a
206 Assert.assertEquals(res.getStatus(),
207 Response.Status.FORBIDDEN.getStatusCode(), "expected "
208 + Response.Status.FORBIDDEN.getStatusCode());
210 // Store the ID returned from this create operation for additional tests
212 res.releaseConnection();
216 @Test(dataProvider = "testName",
217 dependsOnMethods = {"createActiveAccount"})
218 public void createWithoutAuthn(String testName) {
221 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
222 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
223 String pass = collectionObjectClient.getProperty(collectionObjectClient.PASSWORD_PROPERTY);
224 collectionObjectClient.setAuth(false, user, true, pass, true);
225 String identifier = createIdentifier();
226 PoxPayloadOut multipart = createCollectionObjectInstance(
227 collectionObjectClient.getCommonPartName(), identifier);
228 ClientResponse<Response> res = collectionObjectClient.create(multipart);
229 if (logger.isDebugEnabled()) {
230 logger.debug("create: status = " + res.getStatus());
232 Assert.assertEquals(res.getStatus(),
233 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
234 + Response.Status.UNAUTHORIZED.getStatusCode());
235 res.releaseConnection();
239 @Test(dataProvider = "testName",
240 dependsOnMethods = {"createInactiveAccount"})
241 public void createWithInactiveAccount(String testName) {
242 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
243 collectionObjectClient.setAuth(true, "george", true, "george08", true);
244 String identifier = createIdentifier();
245 PoxPayloadOut multipart = createCollectionObjectInstance(
246 collectionObjectClient.getCommonPartName(), identifier);
248 ClientResponse<Response> res = collectionObjectClient.create(multipart);
249 if (logger.isDebugEnabled()) {
250 logger.debug(testName + ": status = " + res.getStatus());
252 Assert.assertEquals(res.getStatus(),
253 Response.Status.FORBIDDEN.getStatusCode(), "expected "
254 + Response.Status.FORBIDDEN.getStatusCode());
255 res.releaseConnection();
259 * Creates the collection object instance without password.
261 @Test(dataProvider = "testName",
262 dependsOnMethods = {"createActiveAccount"})
263 public void createWithoutPassword(String testName) {
264 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
265 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
266 collectionObjectClient.setAuth(true, user, true, "", false);
267 String identifier = createIdentifier();
268 PoxPayloadOut multipart = createCollectionObjectInstance(
269 collectionObjectClient.getCommonPartName(), identifier);
270 ClientResponse<Response> res = collectionObjectClient.create(multipart);
271 if (logger.isDebugEnabled()) {
272 logger.debug(testName + ": status = " + res.getStatus());
274 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
275 res.releaseConnection();
279 * Creates the collection object with unknown user
281 @Test(dataProvider = "testName",
282 dependsOnMethods = {"createActiveAccount"})
283 public void createWithUnknownUser(String testName) {
284 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
285 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
286 String identifier = createIdentifier();
287 PoxPayloadOut multipart = createCollectionObjectInstance(
288 collectionObjectClient.getCommonPartName(), identifier);
289 ClientResponse<Response> res = collectionObjectClient.create(multipart);
290 if (logger.isDebugEnabled()) {
291 logger.debug(testName + ": status = " + res.getStatus());
293 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
294 res.releaseConnection();
298 * Creates the collection object instance with incorrect password.
300 @Test(dataProvider = "testName",
301 dependsOnMethods = {"createActiveAccount"})
302 public void createWithIncorrectPassword(String testName) {
303 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
304 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
305 collectionObjectClient.setAuth(true, user, true, "bar", true);
306 String identifier = createIdentifier();
307 PoxPayloadOut multipart = createCollectionObjectInstance(
308 collectionObjectClient.getCommonPartName(), identifier);
309 ClientResponse<Response> res = collectionObjectClient.create(multipart);
310 if (logger.isDebugEnabled()) {
311 logger.debug(testName + ": status = " + res.getStatus());
313 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
314 res.releaseConnection();
318 * Creates the collection object instance with incorrect user password.
320 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
321 public void createWithIncorrectUserPassword(String testName) {
322 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
323 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
324 String identifier = createIdentifier();
325 PoxPayloadOut multipart = createCollectionObjectInstance(
326 collectionObjectClient.getCommonPartName(), identifier);
327 ClientResponse<Response> res = collectionObjectClient.create(multipart);
328 if (logger.isDebugEnabled()) {
329 logger.debug(testName + ": status = "
332 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
333 res.releaseConnection();
337 * Creates the collection object instance with incorrect user password.
339 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
340 public void createWithoutTenant(String testName) {
341 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
342 collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
343 String identifier = createIdentifier();
344 PoxPayloadOut multipart = createCollectionObjectInstance(
345 collectionObjectClient.getCommonPartName(), identifier);
346 ClientResponse<Response> res = collectionObjectClient.create(multipart);
347 if (logger.isDebugEnabled()) {
348 logger.debug(testName + ": status = "
351 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
352 res.releaseConnection();
355 @Test(dataProvider = "testName",
356 dependsOnMethods = {"create", "createWithInactiveAccount"})
357 public void deleteAccounts(String testName) throws Exception {
360 AccountClient accountClient = new AccountClient();
361 // accountClient.setAuth(true, "test", true, "test", true);
362 // Submit the request to the service and store the response.
363 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
364 int statusCode = res.getStatus();
365 if (logger.isDebugEnabled()) {
366 logger.debug(testName + ": barney status = " + statusCode);
368 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
369 invalidStatusCodeMessage(testRequestType, statusCode));
371 res = accountClient.delete(georgeAccountId);
372 statusCode = res.getStatus();
373 if (logger.isDebugEnabled()) {
374 logger.debug(testName + ": george status = " + statusCode);
376 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
377 invalidStatusCodeMessage(testRequestType, statusCode));
378 res.releaseConnection();
381 // ---------------------------------------------------------------
382 // Utility methods used by tests above
383 // ---------------------------------------------------------------
385 * Creates the collection object instance.
387 * @param commonPartName the common part name
388 * @param identifier the identifier
390 * @return the multipart output
392 private PoxPayloadOut createCollectionObjectInstance(
393 String commonPartName, String identifier) {
394 return createCollectionObjectInstance(commonPartName, "objectNumber-"
395 + identifier, "title-" + identifier);
399 * Creates the collection object instance.
401 * @param commonPartName the common part name
402 * @param objectNumber the object number
403 * @param title the object title
405 * @return the multipart output
407 private PoxPayloadOut createCollectionObjectInstance(
408 String commonPartName, String objectNumber, String title) {
409 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
411 collectionObject.setObjectNumber(objectNumber);
412 TitleGroupList titleGroupList = new TitleGroupList();
413 List<TitleGroup> titleGroups = titleGroupList.getTitleGroup();
414 TitleGroup titleGroup = new TitleGroup();
415 titleGroup.setTitle(title);
416 titleGroups.add(titleGroup);
417 collectionObject.setTitleGroupList(titleGroupList);
418 PoxPayloadOut multipart =
419 CollectionObjectFactory.createCollectionObjectInstance(
420 commonPartName, collectionObject, null, null);
422 if (logger.isDebugEnabled()) {
423 logger.debug("to be created, collectionobject common ",
424 collectionObject, CollectionobjectsCommon.class);
429 private AccountsCommon createAccountInstance(String screenName,
430 String passwd, String email, String tenantId, boolean invalidTenant) {
432 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
433 screenName, passwd, email, tenantId,
434 true, invalidTenant, true, true);
436 List<AccountTenant> atl = account.getTenants();
438 //disable 2nd tenant till tenant identification is in effect
439 //on the service side for 1-n user-tenants
440 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
441 // at2.setId(UUID.randomUUID().toString());
442 // at2.setName("collectionspace.org");
444 // account.setTenants(atl);
446 if (logger.isDebugEnabled()) {
447 logger.debug("to be created, account common");
448 logger.debug(objectAsXmlString(account,
449 AccountsCommon.class));
456 protected Class<AbstractCommonList> getCommonListType() {
457 // TODO Auto-generated method stub