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;
27 import javax.ws.rs.core.Response;
29 //import org.apache.commons.codec.binary.Base64;
30 import org.jboss.resteasy.client.ClientResponse;
31 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
33 import org.testng.Assert;
34 import org.testng.annotations.Test;
36 import org.collectionspace.services.account.AccountTenant;
37 import org.collectionspace.services.account.AccountsCommon;
38 import org.collectionspace.services.account.Status;
39 import org.collectionspace.services.client.AccountClient;
40 import org.collectionspace.services.client.AccountFactory;
41 import org.collectionspace.services.client.CollectionObjectClient;
42 import org.collectionspace.services.client.CollectionObjectFactory;
43 import org.collectionspace.services.client.CollectionSpaceClient;
44 import org.collectionspace.services.client.PoxPayloadOut;
45 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
46 import org.collectionspace.services.client.test.BaseServiceTest;
47 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
48 import org.collectionspace.services.collectionobject.TitleGroup;
49 import org.collectionspace.services.collectionobject.TitleGroupList;
50 import org.collectionspace.services.jaxb.AbstractCommonList;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
56 * AuthenticationServiceTest uses CollectionObject service to test
59 * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
62 public class AuthenticationServiceTest extends AbstractServiceTestImpl {
64 /** The known resource id. */
65 private String knownResourceId = null;
66 private String barneyAccountId = null; //active
67 private String georgeAccountId = null; //inactive
69 private final String CLASS_NAME = AuthenticationServiceTest.class.getName();
70 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
73 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
76 protected String getServicePathComponent() {
77 // no need to return anything but null since no auth resources are
79 throw new UnsupportedOperationException();
83 protected String getServiceName() {
84 // no need to return anything but null since no auth resources are
86 throw new UnsupportedOperationException();
90 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
93 protected CollectionSpaceClient getClientInstance() {
94 return new AccountClient();
98 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
101 protected AbstractCommonList getAbstractCommonList(
102 ClientResponse<AbstractCommonList> response) {
103 throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
106 @Test(dataProvider = "testName")
108 public void readPaginatedList(String testName) throws Exception {
109 // Test not supported.
112 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
113 public void createActiveAccount(String testName) throws Exception {
115 if (logger.isDebugEnabled()) {
116 logger.debug(testBanner(testName, CLASS_NAME));
118 // Perform setup, such as initializing the type of service request
119 // (e.g. CREATE, DELETE), its valid and expected status codes, and
120 // its associated HTTP method name (e.g. POST, DELETE).
123 AccountClient accountClient = new AccountClient();
124 // This should not be needed - the auth is already set up
125 //accountClient.setAuth(true, "test", true, "test", true);
127 // Submit the request to the service and store the response.
128 AccountsCommon account =
129 createAccountInstance("barney", "barney08", "barney@dinoland.com",
130 accountClient.getTenantId(), false);
131 ClientResponse<Response> res = accountClient.create(account);
132 int statusCode = res.getStatus();
134 if (logger.isDebugEnabled()) {
135 logger.debug(testName + ": barney status = " + statusCode);
137 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
138 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
139 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
141 // Store the ID returned from this create operation
142 // for additional tests below.
143 barneyAccountId = extractId(res);
144 if (logger.isDebugEnabled()) {
145 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
147 res.releaseConnection();
151 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
152 public void createInactiveAccount(String testName) throws Exception {
154 if (logger.isDebugEnabled()) {
155 logger.debug(testBanner(testName, CLASS_NAME));
160 AccountClient accountClient = new AccountClient();
161 // This should not be needed - the auth is already set up
162 //accountClient.setAuth(true, "test", true, "test", true);
164 // Submit the request to the service and store the response.
165 AccountsCommon account =
166 createAccountInstance("george", "george08", "george@curiousland.com",
167 accountClient.getTenantId(), false);
168 ClientResponse<Response> res = accountClient.create(account);
169 int statusCode = res.getStatus();
171 if (logger.isDebugEnabled()) {
172 logger.debug(testName + ": george status = " + statusCode);
174 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178 // Store the ID returned from this create operation
179 // for additional tests below.
180 georgeAccountId = extractId(res);
181 if (logger.isDebugEnabled()) {
182 logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
184 res.releaseConnection();
187 account.setStatus(Status.INACTIVE);
188 if (logger.isDebugEnabled()) {
189 logger.debug(testName + ":updated object");
190 logger.debug(objectAsXmlString(account,
191 AccountsCommon.class));
194 // Submit the request to the service and store the response.
195 ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
196 statusCode = res1.getStatus();
197 // Check the status code of the response: does it match the expected response(s)?
198 if (logger.isDebugEnabled()) {
199 logger.debug(testName + ": status = " + statusCode);
201 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
202 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
203 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
204 res1.releaseConnection();
209 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
211 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
212 dependsOnMethods = {"createActiveAccount"})
214 public void create(String testName) {
215 if (logger.isDebugEnabled()) {
216 logger.debug(testBanner(testName, CLASS_NAME));
219 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
220 collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
221 String identifier = BaseServiceTest.createIdentifier();
222 PoxPayloadOut multipart = createCollectionObjectInstance(
223 collectionObjectClient.getCommonPartName(), identifier);
224 ClientResponse<Response> res = collectionObjectClient.create(multipart);
225 if (logger.isDebugEnabled()) {
226 logger.debug("create: status = " + res.getStatus());
228 //so it does not have any permissions out-of-the-box to create a
230 Assert.assertEquals(res.getStatus(),
231 Response.Status.FORBIDDEN.getStatusCode(), "expected "
232 + Response.Status.FORBIDDEN.getStatusCode());
234 // Store the ID returned from this create operation for additional tests
236 res.releaseConnection();
240 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
241 dependsOnMethods = {"createActiveAccount"})
242 public void createWithoutAuthn(String testName) {
243 if (logger.isDebugEnabled()) {
244 logger.debug(testBanner(testName, CLASS_NAME));
247 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
248 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
249 String pass = collectionObjectClient.getProperty(collectionObjectClient.PASSWORD_PROPERTY);
250 collectionObjectClient.setAuth(false, user, true, pass, true);
251 String identifier = BaseServiceTest.createIdentifier();
252 PoxPayloadOut multipart = createCollectionObjectInstance(
253 collectionObjectClient.getCommonPartName(), identifier);
254 ClientResponse<Response> res = collectionObjectClient.create(multipart);
255 if (logger.isDebugEnabled()) {
256 logger.debug("create: status = " + res.getStatus());
258 Assert.assertEquals(res.getStatus(),
259 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
260 + Response.Status.UNAUTHORIZED.getStatusCode());
261 res.releaseConnection();
265 @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"})
266 public void createWithInactiveAccount(String testName) {
267 if (logger.isDebugEnabled()) {
268 logger.debug(testBanner(testName));
270 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
271 collectionObjectClient.setAuth(true, "george", true, "george08", true);
272 String identifier = BaseServiceTest.createIdentifier();
273 PoxPayloadOut multipart = createCollectionObjectInstance(
274 collectionObjectClient.getCommonPartName(), identifier);
276 ClientResponse<Response> res = collectionObjectClient.create(multipart);
277 if (logger.isDebugEnabled()) {
278 logger.debug(testName + ": status = " + res.getStatus());
280 Assert.assertEquals(res.getStatus(),
281 Response.Status.FORBIDDEN.getStatusCode(), "expected "
282 + Response.Status.FORBIDDEN.getStatusCode());
283 res.releaseConnection();
287 * Creates the collection object instance without password.
289 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
290 public void createWithoutPassword(String testName) {
291 if (logger.isDebugEnabled()) {
292 logger.debug(testBanner(testName));
294 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
295 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
296 collectionObjectClient.setAuth(true, user, true, "", false);
297 String identifier = BaseServiceTest.createIdentifier();
298 PoxPayloadOut multipart = createCollectionObjectInstance(
299 collectionObjectClient.getCommonPartName(), identifier);
300 ClientResponse<Response> res = collectionObjectClient.create(multipart);
301 if (logger.isDebugEnabled()) {
302 logger.debug(testName + ": status = " + res.getStatus());
304 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
305 res.releaseConnection();
309 * Creates the collection object with unknown user
311 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
312 public void createWithUnknownUser(String testName) {
313 if (logger.isDebugEnabled()) {
314 logger.debug(testBanner(testName));
316 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
317 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
318 String identifier = BaseServiceTest.createIdentifier();
319 PoxPayloadOut multipart = createCollectionObjectInstance(
320 collectionObjectClient.getCommonPartName(), identifier);
321 ClientResponse<Response> res = collectionObjectClient.create(multipart);
322 if (logger.isDebugEnabled()) {
323 logger.debug(testName + ": status = " + res.getStatus());
325 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
326 res.releaseConnection();
330 * Creates the collection object instance with incorrect password.
332 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
333 public void createWithIncorrectPassword(String testName) {
334 if (logger.isDebugEnabled()) {
335 logger.debug(testBanner(testName));
337 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
338 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
339 collectionObjectClient.setAuth(true, user, true, "bar", true);
340 String identifier = BaseServiceTest.createIdentifier();
341 PoxPayloadOut multipart = createCollectionObjectInstance(
342 collectionObjectClient.getCommonPartName(), identifier);
343 ClientResponse<Response> res = collectionObjectClient.create(multipart);
344 if (logger.isDebugEnabled()) {
345 logger.debug(testName + ": status = " + res.getStatus());
347 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
348 res.releaseConnection();
352 * Creates the collection object instance with incorrect user password.
354 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
355 public void createWithIncorrectUserPassword(String testName) {
356 if (logger.isDebugEnabled()) {
357 logger.debug(testBanner(testName));
359 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
360 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
361 String identifier = BaseServiceTest.createIdentifier();
362 PoxPayloadOut multipart = createCollectionObjectInstance(
363 collectionObjectClient.getCommonPartName(), identifier);
364 ClientResponse<Response> res = collectionObjectClient.create(multipart);
365 if (logger.isDebugEnabled()) {
366 logger.debug(testName + ": status = "
369 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
370 res.releaseConnection();
374 * Creates the collection object instance with incorrect user password.
376 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
377 public void createWithoutTenant(String testName) {
378 if (logger.isDebugEnabled()) {
379 logger.debug(testBanner(testName));
381 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
382 collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
383 String identifier = BaseServiceTest.createIdentifier();
384 PoxPayloadOut multipart = createCollectionObjectInstance(
385 collectionObjectClient.getCommonPartName(), identifier);
386 ClientResponse<Response> res = collectionObjectClient.create(multipart);
387 if (logger.isDebugEnabled()) {
388 logger.debug(testName + ": status = "
391 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
392 res.releaseConnection();
396 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
399 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
400 dependsOnMethods = {"create"})
401 public void delete(String testName) {
406 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
407 dependsOnMethods = {"create", "createWithInactiveAccount"})
408 public void deleteAccounts(String testName) throws Exception {
410 if (logger.isDebugEnabled()) {
411 logger.debug(testBanner(testName, CLASS_NAME));
415 AccountClient accountClient = new AccountClient();
416 // accountClient.setAuth(true, "test", true, "test", true);
417 // Submit the request to the service and store the response.
418 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
419 int statusCode = res.getStatus();
420 if (logger.isDebugEnabled()) {
421 logger.debug(testName + ": barney status = " + statusCode);
423 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
426 res = accountClient.delete(georgeAccountId);
427 statusCode = res.getStatus();
428 if (logger.isDebugEnabled()) {
429 logger.debug(testName + ": george status = " + statusCode);
431 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
432 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
433 res.releaseConnection();
436 // ---------------------------------------------------------------
438 // ---------------------------------------------------------------
441 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
442 public void searchWorkflowDeleted(String testName) throws Exception {
443 // Fixme: null test for now, overriding test in base class
446 // ---------------------------------------------------------------
447 // Utility methods used by tests above
448 // ---------------------------------------------------------------
450 * Creates the collection object instance.
452 * @param commonPartName the common part name
453 * @param identifier the identifier
455 * @return the multipart output
457 private PoxPayloadOut createCollectionObjectInstance(
458 String commonPartName, String identifier) {
459 return createCollectionObjectInstance(commonPartName, "objectNumber-"
460 + identifier, "title-" + identifier);
464 * Creates the collection object instance.
466 * @param commonPartName the common part name
467 * @param objectNumber the object number
468 * @param title the object title
470 * @return the multipart output
472 private PoxPayloadOut createCollectionObjectInstance(
473 String commonPartName, String objectNumber, String title) {
474 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
476 collectionObject.setObjectNumber(objectNumber);
477 TitleGroupList titleGroupList = new TitleGroupList();
478 List<TitleGroup> titleGroups = titleGroupList.getTitleGroup();
479 TitleGroup titleGroup = new TitleGroup();
480 titleGroup.setTitle(title);
481 titleGroups.add(titleGroup);
482 collectionObject.setTitleGroupList(titleGroupList);
483 PoxPayloadOut multipart =
484 CollectionObjectFactory.createCollectionObjectInstance(
485 commonPartName, collectionObject, null, null);
487 if (logger.isDebugEnabled()) {
488 logger.debug("to be created, collectionobject common ",
489 collectionObject, CollectionobjectsCommon.class);
494 private AccountsCommon createAccountInstance(String screenName,
495 String passwd, String email, String tenantId, boolean invalidTenant) {
497 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
498 screenName, passwd, email, tenantId,
499 true, invalidTenant, true, true);
501 List<AccountTenant> atl = account.getTenants();
503 //disable 2nd tenant till tenant identification is in effect
504 //on the service side for 1-n user-tenants
505 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
506 // at2.setId(UUID.randomUUID().toString());
507 // at2.setName("collectionspace.org");
509 // account.setTenants(atl);
511 if (logger.isDebugEnabled()) {
512 logger.debug("to be created, account common");
513 logger.debug(objectAsXmlString(account,
514 AccountsCommon.class));
521 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
524 public void createList(String testName) throws Exception {
525 //FIXME: Should this test really be empty? If so, please comment accordingly.
529 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
532 public void createWithEmptyEntityBody(String testName) throws Exception {
533 //FIXME: Should this test really be empty? If so, please comment accordingly.
537 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
540 public void createWithMalformedXml(String testName) throws Exception {
541 //FIXME: Should this test really be empty? If so, please comment accordingly.
545 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
548 public void createWithWrongXmlSchema(String testName) throws Exception {
549 //FIXME: Should this test really be empty? If so, please comment accordingly.
553 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
556 public void read(String testName) throws Exception {
557 //FIXME: Should this test really be empty? If so, please comment accordingly.
561 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
564 public void readNonExistent(String testName) throws Exception {
565 //FIXME: Should this test really be empty? If so, please comment accordingly.
569 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
572 public void readList(String testName) throws Exception {
573 //FIXME: Should this test really be empty? If so, please comment accordingly.
577 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
580 public void update(String testName) throws Exception {
581 //FIXME: Should this test really be empty? If so, please comment accordingly.
585 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
588 public void updateWithEmptyEntityBody(String testName) throws Exception {
589 //FIXME: Should this test really be empty? If so, please comment accordingly.
593 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
596 public void updateWithMalformedXml(String testName) throws Exception {
597 //FIXME: Should this test really be empty? If so, please comment accordingly.
601 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
604 public void updateWithWrongXmlSchema(String testName) throws Exception {
605 //FIXME: Should this test really be empty? If so, please comment accordingly.
609 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
612 public void updateNonExistent(String testName) throws Exception {
613 //FIXME: Should this test really be empty? If so, please comment accordingly.
617 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
620 public void deleteNonExistent(String testName) throws Exception {
621 //FIXME: Should this test really be empty? If so, please comment accordingly.