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.client.AccountClient;
38 import org.collectionspace.services.account.AccountsCommon;
39 import org.collectionspace.services.account.Status;
40 import org.collectionspace.services.client.AccountFactory;
41 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
42 import org.collectionspace.services.client.CollectionObjectClient;
43 import org.collectionspace.services.client.CollectionObjectFactory;
44 import org.collectionspace.services.client.CollectionSpaceClient;
45 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
46 import org.collectionspace.services.client.test.BaseServiceTest;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * AuthenticationServiceTest uses CollectionObject service to test
56 * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
59 public class AuthenticationServiceTest extends AbstractServiceTestImpl {
61 /** The known resource id. */
62 private String knownResourceId = null;
63 private String barneyAccountId = null; //active
64 private String georgeAccountId = null; //inactive
66 final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
69 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
72 protected String getServicePathComponent() {
73 // no need to return anything but null since no auth resources are
79 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
82 protected CollectionSpaceClient getClientInstance() {
83 return new AccountClient();
87 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
90 protected AbstractCommonList getAbstractCommonList(
91 ClientResponse<AbstractCommonList> response) {
92 throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
95 @Test(dataProvider = "testName")
97 public void readPaginatedList(String testName) throws Exception {
98 // Test not supported.
101 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
102 public void createActiveAccount(String testName) throws Exception {
103 // Perform setup, such as initializing the type of service request
104 // (e.g. CREATE, DELETE), its valid and expected status codes, and
105 // its associated HTTP method name (e.g. POST, DELETE).
106 setupCreate(testName);
107 AccountClient accountClient = new AccountClient();
108 accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
110 accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
112 accountClient.setProperty(
113 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
115 // Submit the request to the service and store the response.
116 AccountsCommon account =
117 createAccountInstance("barney", "barney08", "barney@dinoland.com", false);
118 ClientResponse<Response> res = accountClient.create(account);
119 int statusCode = res.getStatus();
121 if (logger.isDebugEnabled()) {
122 logger.debug(testName + ": barney status = " + statusCode);
124 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
125 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
126 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
128 // Store the ID returned from this create operation
129 // for additional tests below.
130 barneyAccountId = extractId(res);
131 if (logger.isDebugEnabled()) {
132 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
134 res.releaseConnection();
138 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
139 public void createInactiveAccount(String testName) throws Exception {
140 // Perform setup, such as initializing the type of service request
141 // (e.g. CREATE, DELETE), its valid and expected status codes, and
142 // its associated HTTP method name (e.g. POST, DELETE).
143 setupCreate(testName);
144 AccountClient accountClient = new AccountClient();
145 accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
147 accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
149 accountClient.setProperty(
150 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
152 // Submit the request to the service and store the response.
153 AccountsCommon account =
154 createAccountInstance("george", "george08", "george@curiousland.com", false);
155 ClientResponse<Response> res = accountClient.create(account);
156 int statusCode = res.getStatus();
158 if (logger.isDebugEnabled()) {
159 logger.debug(testName + ": george status = " + statusCode);
161 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
162 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
163 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
165 // Store the ID returned from this create operation
166 // for additional tests below.
167 georgeAccountId = extractId(res);
168 if (logger.isDebugEnabled()) {
169 logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
171 res.releaseConnection();
173 setupUpdate(testName);
174 account.setStatus(Status.INACTIVE);
175 if (logger.isDebugEnabled()) {
176 logger.debug(testName + ":updated object");
177 logger.debug(objectAsXmlString(account,
178 AccountsCommon.class));
181 // Submit the request to the service and store the response.
182 ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
183 statusCode = res1.getStatus();
184 // Check the status code of the response: does it match the expected response(s)?
185 if (logger.isDebugEnabled()) {
186 logger.debug(testName + ": status = " + statusCode);
188 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
189 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
190 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
191 res1.releaseConnection();
196 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
198 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
199 dependsOnMethods = {"createActiveAccount"})
201 public void create(String testName) {
202 setupCreate(testName);
203 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
204 String identifier = BaseServiceTest.createIdentifier();
205 MultipartOutput multipart = createCollectionObjectInstance(
206 collectionObjectClient.getCommonPartName(), identifier);
208 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
210 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
212 collectionObjectClient.setProperty(
213 CollectionSpaceClient.PASSWORD_PROPERTY, "barney08");
215 collectionObjectClient.setupHttpClient();
216 collectionObjectClient.setProxy();
217 } catch (Exception e) {
218 logger.error("create: caught " + e.getMessage());
221 ClientResponse<Response> res = collectionObjectClient.create(multipart);
222 if (logger.isDebugEnabled()) {
223 logger.debug("create: status = " + res.getStatus());
225 Assert.assertEquals(res.getStatus(),
226 Response.Status.CREATED.getStatusCode(), "expected "
227 + Response.Status.CREATED.getStatusCode());
229 // Store the ID returned from this create operation for additional tests
231 knownResourceId = extractId(res);
232 res.releaseConnection();
236 @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"})
237 public void createWithInactiveAccount(String testName) {
239 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
240 String identifier = BaseServiceTest.createIdentifier();
241 MultipartOutput multipart = createCollectionObjectInstance(
242 collectionObjectClient.getCommonPartName(), identifier);
244 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
246 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
248 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY,
251 collectionObjectClient.setupHttpClient();
252 collectionObjectClient.setProxy();
253 } catch (Exception e) {
254 logger.error(testName + ": caught " + e.getMessage());
257 ClientResponse<Response> res = collectionObjectClient.create(multipart);
258 if (logger.isDebugEnabled()) {
259 logger.debug(testName + ": status = " + res.getStatus());
261 Assert.assertEquals(res.getStatus(),
262 Response.Status.FORBIDDEN.getStatusCode(), "expected "
263 + Response.Status.FORBIDDEN.getStatusCode());
264 res.releaseConnection();
268 * Creates the collection object instance without password.
270 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
271 public void createWithoutPassword(String testName) {
273 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
274 String identifier = BaseServiceTest.createIdentifier();
275 MultipartOutput multipart = createCollectionObjectInstance(
276 collectionObjectClient.getCommonPartName(), identifier);
278 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
280 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
282 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
284 collectionObjectClient.setupHttpClient();
285 collectionObjectClient.setProxy();
286 } catch (Exception e) {
287 logger.error(testName + ": caught " + e.getMessage());
290 ClientResponse<Response> res = collectionObjectClient.create(multipart);
291 if (logger.isDebugEnabled()) {
292 logger.debug(testName + ": status = " + res.getStatus());
294 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
295 res.releaseConnection();
299 * Creates the collection object with unknown user
301 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
302 public void createWithUnknownUser(String testName) {
304 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
305 String identifier = BaseServiceTest.createIdentifier();
306 MultipartOutput multipart = createCollectionObjectInstance(
307 collectionObjectClient.getCommonPartName(), identifier);
309 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
311 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
313 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY,
316 collectionObjectClient.setupHttpClient();
317 collectionObjectClient.setProxy();
318 } catch (Exception e) {
319 logger.error(testName + ": caught " + e.getMessage());
322 ClientResponse<Response> res = collectionObjectClient.create(multipart);
323 if (logger.isDebugEnabled()) {
324 logger.debug(testName + ": status = " + res.getStatus());
326 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
327 res.releaseConnection();
331 * Creates the collection object instance with incorrect password.
333 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
334 public void createWithIncorrectPassword(String testName) {
336 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
337 String identifier = BaseServiceTest.createIdentifier();
338 MultipartOutput multipart = createCollectionObjectInstance(
339 collectionObjectClient.getCommonPartName(), identifier);
341 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
343 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
345 collectionObjectClient.setProperty(
346 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
348 collectionObjectClient.setupHttpClient();
349 collectionObjectClient.setProxy();
350 } catch (Exception e) {
351 logger.error(testName + ": caught " + e.getMessage());
354 ClientResponse<Response> res = collectionObjectClient.create(multipart);
355 if (logger.isDebugEnabled()) {
356 logger.debug(testName + ": status = " + res.getStatus());
358 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
359 res.releaseConnection();
363 * Creates the collection object instance with incorrect user password.
365 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
366 public void createWithIncorrectUserPassword(String testName) {
368 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
369 String identifier = BaseServiceTest.createIdentifier();
370 MultipartOutput multipart = createCollectionObjectInstance(
371 collectionObjectClient.getCommonPartName(), identifier);
373 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
375 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
377 collectionObjectClient.setProperty(
378 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
380 collectionObjectClient.setupHttpClient();
381 collectionObjectClient.setProxy();
382 } catch (Exception e) {
383 logger.error(testName + ": caught " + e.getMessage());
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 * Creates the collection object instance with incorrect user password.
398 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
399 public void createWithoutTenant(String testName) {
401 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
402 String identifier = BaseServiceTest.createIdentifier();
403 MultipartOutput multipart = createCollectionObjectInstance(
404 collectionObjectClient.getCommonPartName(), identifier);
406 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
408 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
410 collectionObjectClient.setProperty(
411 CollectionSpaceClient.PASSWORD_PROPERTY, "babybop09");
413 collectionObjectClient.setupHttpClient();
414 collectionObjectClient.setProxy();
415 } catch (Exception e) {
416 logger.error(testName + ": caught " + e.getMessage());
419 ClientResponse<Response> res = collectionObjectClient.create(multipart);
420 if (logger.isDebugEnabled()) {
421 logger.debug(testName + ": status = "
424 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
425 res.releaseConnection();
429 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
432 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
433 dependsOnMethods = {"create"})
434 public void delete(String testName) {
435 setupDelete(testName);
436 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
437 collectionObjectClient = new CollectionObjectClient();
439 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
441 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
443 collectionObjectClient.setProperty(
444 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
446 collectionObjectClient.setupHttpClient();
447 collectionObjectClient.setProxy();
448 } catch (Exception e) {
449 logger.error("deleteCollectionObject: caught " + e.getMessage());
452 if (logger.isDebugEnabled()) {
453 logger.debug("Calling deleteCollectionObject:" + knownResourceId);
455 ClientResponse<Response> res = collectionObjectClient.delete(knownResourceId);
456 if (logger.isDebugEnabled()) {
457 logger.debug("deleteCollectionObject: status = " + res.getStatus());
459 Assert.assertEquals(res.getStatus(),
460 Response.Status.OK.getStatusCode(), "expected " + Response.Status.OK.getStatusCode());
461 res.releaseConnection();
464 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
465 dependsOnMethods = {"create", "createWithInactiveAccount"})
466 public void deleteAccounts(String testName) throws Exception {
469 setupDelete(testName);
470 AccountClient accountClient = new AccountClient();
472 accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
474 accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
476 accountClient.setProperty(
477 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
478 // Submit the request to the service and store the response.
479 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
480 int statusCode = res.getStatus();
481 if (logger.isDebugEnabled()) {
482 logger.debug(testName + ": barney status = " + statusCode);
484 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
485 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
487 res = accountClient.delete(georgeAccountId);
488 statusCode = res.getStatus();
489 if (logger.isDebugEnabled()) {
490 logger.debug(testName + ": george status = " + statusCode);
492 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
493 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
494 res.releaseConnection();
497 // ---------------------------------------------------------------
498 // Utility methods used by tests above
499 // ---------------------------------------------------------------
501 * Creates the collection object instance.
503 * @param commonPartName the common part name
504 * @param identifier the identifier
506 * @return the multipart output
508 private MultipartOutput createCollectionObjectInstance(
509 String commonPartName, String identifier) {
510 return createCollectionObjectInstance(commonPartName, "objectNumber-" + identifier, "objectName-" + identifier);
514 * Creates the collection object instance.
516 * @param commonPartName the common part name
517 * @param objectNumber the object number
518 * @param objectName the object name
520 * @return the multipart output
522 private MultipartOutput createCollectionObjectInstance(
523 String commonPartName, String objectNumber, String objectName) {
524 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
526 collectionObject.setObjectNumber(objectNumber);
527 collectionObject.setObjectName(objectName);
528 MultipartOutput multipart =
529 CollectionObjectFactory.createCollectionObjectInstance(
530 commonPartName, collectionObject, null, null);
532 if (logger.isDebugEnabled()) {
533 logger.debug("to be created, collectionobject common ",
534 collectionObject, CollectionobjectsCommon.class);
539 private AccountsCommon createAccountInstance(String screenName,
540 String passwd, String email, boolean invalidTenant) {
542 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
543 screenName, passwd, email,
544 true, true, invalidTenant, true, true);
546 List<AccountTenant> atl = account.getTenants();
548 //disable 2nd tenant till tenant identification is in effect
549 //on the service side for 1-n user-tenants
550 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
551 // at2.setId(UUID.randomUUID().toString());
552 // at2.setName("collectionspace.org");
554 // account.setTenants(atl);
556 if (logger.isDebugEnabled()) {
557 logger.debug("to be created, account common");
558 logger.debug(objectAsXmlString(account,
559 AccountsCommon.class));
566 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
569 public void createList(String testName) throws Exception {
570 //FIXME: Should this test really be empty? If so, please comment accordingly.
574 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
577 public void createWithEmptyEntityBody(String testName) throws Exception {
578 //FIXME: Should this test really be empty? If so, please comment accordingly.
582 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
585 public void createWithMalformedXml(String testName) throws Exception {
586 //FIXME: Should this test really be empty? If so, please comment accordingly.
590 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
593 public void createWithWrongXmlSchema(String testName) throws Exception {
594 //FIXME: Should this test really be empty? If so, please comment accordingly.
598 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
601 public void read(String testName) throws Exception {
602 //FIXME: Should this test really be empty? If so, please comment accordingly.
606 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
609 public void readNonExistent(String testName) throws Exception {
610 //FIXME: Should this test really be empty? If so, please comment accordingly.
614 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
617 public void readList(String testName) throws Exception {
618 //FIXME: Should this test really be empty? If so, please comment accordingly.
622 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
625 public void update(String testName) throws Exception {
626 //FIXME: Should this test really be empty? If so, please comment accordingly.
630 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
633 public void updateWithEmptyEntityBody(String testName) throws Exception {
634 //FIXME: Should this test really be empty? If so, please comment accordingly.
638 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
641 public void updateWithMalformedXml(String testName) throws Exception {
642 //FIXME: Should this test really be empty? If so, please comment accordingly.
646 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
649 public void updateWithWrongXmlSchema(String testName) throws Exception {
650 //FIXME: Should this test really be empty? If so, please comment accordingly.
654 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
657 public void updateNonExistent(String testName) throws Exception {
658 //FIXME: Should this test really be empty? If so, please comment accordingly.
662 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
665 public void deleteNonExistent(String testName) throws Exception {
666 //FIXME: Should this test really be empty? If so, please comment accordingly.