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
67 private final String CLASS_NAME = AuthenticationServiceTest.class.getName();
68 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
71 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
74 protected String getServicePathComponent() {
75 // no need to return anything but null since no auth resources are
81 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
84 protected CollectionSpaceClient getClientInstance() {
85 return new AccountClient();
89 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
92 protected AbstractCommonList getAbstractCommonList(
93 ClientResponse<AbstractCommonList> response) {
94 throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
97 @Test(dataProvider = "testName")
99 public void readPaginatedList(String testName) throws Exception {
100 // Test not supported.
103 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
104 public void createActiveAccount(String testName) throws Exception {
106 if (logger.isDebugEnabled()) {
107 logger.debug(testBanner(testName, CLASS_NAME));
109 // Perform setup, such as initializing the type of service request
110 // (e.g. CREATE, DELETE), its valid and expected status codes, and
111 // its associated HTTP method name (e.g. POST, DELETE).
114 AccountClient accountClient = new AccountClient();
115 accountClient.setAuth(true, "test", true, "test", true);
117 // Submit the request to the service and store the response.
118 AccountsCommon account =
119 createAccountInstance("barney", "barney08", "barney@dinoland.com", false);
120 ClientResponse<Response> res = accountClient.create(account);
121 int statusCode = res.getStatus();
123 if (logger.isDebugEnabled()) {
124 logger.debug(testName + ": barney status = " + statusCode);
126 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
127 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
130 // Store the ID returned from this create operation
131 // for additional tests below.
132 barneyAccountId = extractId(res);
133 if (logger.isDebugEnabled()) {
134 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
136 res.releaseConnection();
140 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
141 public void createInactiveAccount(String testName) throws Exception {
143 if (logger.isDebugEnabled()) {
144 logger.debug(testBanner(testName, CLASS_NAME));
149 AccountClient accountClient = new AccountClient();
150 accountClient.setAuth(true, "test", true, "test", true);
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();
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 if (logger.isDebugEnabled()) {
203 logger.debug(testBanner(testName, CLASS_NAME));
206 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
207 collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
208 String identifier = BaseServiceTest.createIdentifier();
209 MultipartOutput multipart = createCollectionObjectInstance(
210 collectionObjectClient.getCommonPartName(), identifier);
211 ClientResponse<Response> res = collectionObjectClient.create(multipart);
212 if (logger.isDebugEnabled()) {
213 logger.debug("create: status = " + res.getStatus());
215 //so it does not have any permissions out-of-the-box to create a
217 Assert.assertEquals(res.getStatus(),
218 Response.Status.FORBIDDEN.getStatusCode(), "expected "
219 + Response.Status.FORBIDDEN.getStatusCode());
221 // Store the ID returned from this create operation for additional tests
223 res.releaseConnection();
227 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
228 dependsOnMethods = {"createActiveAccount"})
229 public void createWithoutAuthn(String testName) {
230 if (logger.isDebugEnabled()) {
231 logger.debug(testBanner(testName, CLASS_NAME));
234 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
235 collectionObjectClient.setAuth(false, "test", true, "test", true);
236 String identifier = BaseServiceTest.createIdentifier();
237 MultipartOutput multipart = createCollectionObjectInstance(
238 collectionObjectClient.getCommonPartName(), identifier);
239 ClientResponse<Response> res = collectionObjectClient.create(multipart);
240 if (logger.isDebugEnabled()) {
241 logger.debug("create: status = " + res.getStatus());
243 Assert.assertEquals(res.getStatus(),
244 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
245 + Response.Status.UNAUTHORIZED.getStatusCode());
246 res.releaseConnection();
250 @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"})
251 public void createWithInactiveAccount(String testName) {
252 if (logger.isDebugEnabled()) {
253 logger.debug(testBanner(testName));
255 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
256 collectionObjectClient.setAuth(true, "george", true, "george08", true);
257 String identifier = BaseServiceTest.createIdentifier();
258 MultipartOutput multipart = createCollectionObjectInstance(
259 collectionObjectClient.getCommonPartName(), identifier);
261 ClientResponse<Response> res = collectionObjectClient.create(multipart);
262 if (logger.isDebugEnabled()) {
263 logger.debug(testName + ": status = " + res.getStatus());
265 Assert.assertEquals(res.getStatus(),
266 Response.Status.FORBIDDEN.getStatusCode(), "expected "
267 + Response.Status.FORBIDDEN.getStatusCode());
268 res.releaseConnection();
272 * Creates the collection object instance without password.
274 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
275 public void createWithoutPassword(String testName) {
276 if (logger.isDebugEnabled()) {
277 logger.debug(testBanner(testName));
279 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
280 collectionObjectClient.setAuth(true, "test", true, "", false);
281 String identifier = BaseServiceTest.createIdentifier();
282 MultipartOutput multipart = createCollectionObjectInstance(
283 collectionObjectClient.getCommonPartName(), identifier);
284 ClientResponse<Response> res = collectionObjectClient.create(multipart);
285 if (logger.isDebugEnabled()) {
286 logger.debug(testName + ": status = " + res.getStatus());
288 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
289 res.releaseConnection();
293 * Creates the collection object with unknown user
295 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
296 public void createWithUnknownUser(String testName) {
297 if (logger.isDebugEnabled()) {
298 logger.debug(testBanner(testName));
300 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
301 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
302 String identifier = BaseServiceTest.createIdentifier();
303 MultipartOutput multipart = createCollectionObjectInstance(
304 collectionObjectClient.getCommonPartName(), identifier);
305 ClientResponse<Response> res = collectionObjectClient.create(multipart);
306 if (logger.isDebugEnabled()) {
307 logger.debug(testName + ": status = " + res.getStatus());
309 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
310 res.releaseConnection();
314 * Creates the collection object instance with incorrect password.
316 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
317 public void createWithIncorrectPassword(String testName) {
318 if (logger.isDebugEnabled()) {
319 logger.debug(testBanner(testName));
321 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
322 collectionObjectClient.setAuth(true, "test", true, "bar", true);
323 String identifier = BaseServiceTest.createIdentifier();
324 MultipartOutput multipart = createCollectionObjectInstance(
325 collectionObjectClient.getCommonPartName(), identifier);
326 ClientResponse<Response> res = collectionObjectClient.create(multipart);
327 if (logger.isDebugEnabled()) {
328 logger.debug(testName + ": status = " + res.getStatus());
330 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
331 res.releaseConnection();
335 * Creates the collection object instance with incorrect user password.
337 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
338 public void createWithIncorrectUserPassword(String testName) {
339 if (logger.isDebugEnabled()) {
340 logger.debug(testBanner(testName));
342 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
343 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
344 String identifier = BaseServiceTest.createIdentifier();
345 MultipartOutput multipart = createCollectionObjectInstance(
346 collectionObjectClient.getCommonPartName(), identifier);
347 ClientResponse<Response> res = collectionObjectClient.create(multipart);
348 if (logger.isDebugEnabled()) {
349 logger.debug(testName + ": status = "
352 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
353 res.releaseConnection();
357 * Creates the collection object instance with incorrect user password.
359 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
360 public void createWithoutTenant(String testName) {
361 if (logger.isDebugEnabled()) {
362 logger.debug(testBanner(testName));
364 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
365 collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
366 String identifier = BaseServiceTest.createIdentifier();
367 MultipartOutput multipart = createCollectionObjectInstance(
368 collectionObjectClient.getCommonPartName(), identifier);
369 ClientResponse<Response> res = collectionObjectClient.create(multipart);
370 if (logger.isDebugEnabled()) {
371 logger.debug(testName + ": status = "
374 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
375 res.releaseConnection();
379 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
382 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
383 dependsOnMethods = {"create"})
384 public void delete(String testName) {
389 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
390 dependsOnMethods = {"create", "createWithInactiveAccount"})
391 public void deleteAccounts(String testName) throws Exception {
393 if (logger.isDebugEnabled()) {
394 logger.debug(testBanner(testName, CLASS_NAME));
398 AccountClient accountClient = new AccountClient();
399 accountClient.setAuth(true, "test", true, "test", true);
400 // Submit the request to the service and store the response.
401 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
402 int statusCode = res.getStatus();
403 if (logger.isDebugEnabled()) {
404 logger.debug(testName + ": barney status = " + statusCode);
406 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
407 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
409 res = accountClient.delete(georgeAccountId);
410 statusCode = res.getStatus();
411 if (logger.isDebugEnabled()) {
412 logger.debug(testName + ": george status = " + statusCode);
414 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
415 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
416 res.releaseConnection();
419 // ---------------------------------------------------------------
420 // Utility methods used by tests above
421 // ---------------------------------------------------------------
423 * Creates the collection object instance.
425 * @param commonPartName the common part name
426 * @param identifier the identifier
428 * @return the multipart output
430 private MultipartOutput createCollectionObjectInstance(
431 String commonPartName, String identifier) {
432 return createCollectionObjectInstance(commonPartName, "objectNumber-"
433 + identifier, "objectName-" + identifier);
437 * Creates the collection object instance.
439 * @param commonPartName the common part name
440 * @param objectNumber the object number
441 * @param objectName the object name
443 * @return the multipart output
445 private MultipartOutput createCollectionObjectInstance(
446 String commonPartName, String objectNumber, String objectName) {
447 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
449 collectionObject.setObjectNumber(objectNumber);
450 collectionObject.setObjectName(objectName);
451 MultipartOutput multipart =
452 CollectionObjectFactory.createCollectionObjectInstance(
453 commonPartName, collectionObject, null, null);
455 if (logger.isDebugEnabled()) {
456 logger.debug("to be created, collectionobject common ",
457 collectionObject, CollectionobjectsCommon.class);
462 private AccountsCommon createAccountInstance(String screenName,
463 String passwd, String email, boolean invalidTenant) {
465 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
466 screenName, passwd, email,
467 true, true, invalidTenant, true, true);
469 List<AccountTenant> atl = account.getTenants();
471 //disable 2nd tenant till tenant identification is in effect
472 //on the service side for 1-n user-tenants
473 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
474 // at2.setId(UUID.randomUUID().toString());
475 // at2.setName("collectionspace.org");
477 // account.setTenants(atl);
479 if (logger.isDebugEnabled()) {
480 logger.debug("to be created, account common");
481 logger.debug(objectAsXmlString(account,
482 AccountsCommon.class));
489 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
492 public void createList(String testName) throws Exception {
493 //FIXME: Should this test really be empty? If so, please comment accordingly.
497 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
500 public void createWithEmptyEntityBody(String testName) throws Exception {
501 //FIXME: Should this test really be empty? If so, please comment accordingly.
505 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
508 public void createWithMalformedXml(String testName) throws Exception {
509 //FIXME: Should this test really be empty? If so, please comment accordingly.
513 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
516 public void createWithWrongXmlSchema(String testName) throws Exception {
517 //FIXME: Should this test really be empty? If so, please comment accordingly.
521 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
524 public void read(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#readNonExistent()
532 public void readNonExistent(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#readList()
540 public void readList(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#update()
548 public void update(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#updateWithEmptyEntityBody()
556 public void updateWithEmptyEntityBody(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#updateWithMalformedXml()
564 public void updateWithMalformedXml(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#updateWithWrongXmlSchema()
572 public void updateWithWrongXmlSchema(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#updateNonExistent()
580 public void updateNonExistent(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#deleteNonExistent()
588 public void deleteNonExistent(String testName) throws Exception {
589 //FIXME: Should this test really be empty? If so, please comment accordingly.