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.test.AbstractServiceTestImpl;
45 import org.collectionspace.services.client.test.BaseServiceTest;
46 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
47 import org.collectionspace.services.collectionobject.TitleGroup;
48 import org.collectionspace.services.collectionobject.TitleGroupList;
49 import org.collectionspace.services.jaxb.AbstractCommonList;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * AuthenticationServiceTest uses CollectionObject service to test
58 * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
61 public class AuthenticationServiceTest extends AbstractServiceTestImpl {
63 /** The known resource id. */
64 private String knownResourceId = null;
65 private String barneyAccountId = null; //active
66 private String georgeAccountId = null; //inactive
68 private final String CLASS_NAME = AuthenticationServiceTest.class.getName();
69 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
72 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
75 protected String getServicePathComponent() {
76 // no need to return anything but null since no auth resources are
82 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
85 protected CollectionSpaceClient getClientInstance() {
86 return new AccountClient();
90 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
93 protected AbstractCommonList getAbstractCommonList(
94 ClientResponse<AbstractCommonList> response) {
95 throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
98 @Test(dataProvider = "testName")
100 public void readPaginatedList(String testName) throws Exception {
101 // Test not supported.
104 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
105 public void createActiveAccount(String testName) throws Exception {
107 if (logger.isDebugEnabled()) {
108 logger.debug(testBanner(testName, CLASS_NAME));
110 // Perform setup, such as initializing the type of service request
111 // (e.g. CREATE, DELETE), its valid and expected status codes, and
112 // its associated HTTP method name (e.g. POST, DELETE).
115 AccountClient accountClient = new AccountClient();
116 // This should not be needed - the auth is already set up
117 //accountClient.setAuth(true, "test", true, "test", true);
119 // Submit the request to the service and store the response.
120 AccountsCommon account =
121 createAccountInstance("barney", "barney08", "barney@dinoland.com",
122 accountClient.getTenantId(), false);
123 ClientResponse<Response> res = accountClient.create(account);
124 int statusCode = res.getStatus();
126 if (logger.isDebugEnabled()) {
127 logger.debug(testName + ": barney status = " + statusCode);
129 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
130 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
131 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
133 // Store the ID returned from this create operation
134 // for additional tests below.
135 barneyAccountId = extractId(res);
136 if (logger.isDebugEnabled()) {
137 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
139 res.releaseConnection();
143 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
144 public void createInactiveAccount(String testName) throws Exception {
146 if (logger.isDebugEnabled()) {
147 logger.debug(testBanner(testName, CLASS_NAME));
152 AccountClient accountClient = new AccountClient();
153 // This should not be needed - the auth is already set up
154 //accountClient.setAuth(true, "test", true, "test", true);
156 // Submit the request to the service and store the response.
157 AccountsCommon account =
158 createAccountInstance("george", "george08", "george@curiousland.com",
159 accountClient.getTenantId(), false);
160 ClientResponse<Response> res = accountClient.create(account);
161 int statusCode = res.getStatus();
163 if (logger.isDebugEnabled()) {
164 logger.debug(testName + ": george status = " + statusCode);
166 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
167 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
168 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
170 // Store the ID returned from this create operation
171 // for additional tests below.
172 georgeAccountId = extractId(res);
173 if (logger.isDebugEnabled()) {
174 logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
176 res.releaseConnection();
179 account.setStatus(Status.INACTIVE);
180 if (logger.isDebugEnabled()) {
181 logger.debug(testName + ":updated object");
182 logger.debug(objectAsXmlString(account,
183 AccountsCommon.class));
186 // Submit the request to the service and store the response.
187 ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
188 statusCode = res1.getStatus();
189 // Check the status code of the response: does it match the expected response(s)?
190 if (logger.isDebugEnabled()) {
191 logger.debug(testName + ": status = " + statusCode);
193 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
194 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
195 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
196 res1.releaseConnection();
201 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
203 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
204 dependsOnMethods = {"createActiveAccount"})
206 public void create(String testName) {
207 if (logger.isDebugEnabled()) {
208 logger.debug(testBanner(testName, CLASS_NAME));
211 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
212 collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
213 String identifier = BaseServiceTest.createIdentifier();
214 MultipartOutput multipart = createCollectionObjectInstance(
215 collectionObjectClient.getCommonPartName(), identifier);
216 ClientResponse<Response> res = collectionObjectClient.create(multipart);
217 if (logger.isDebugEnabled()) {
218 logger.debug("create: status = " + res.getStatus());
220 //so it does not have any permissions out-of-the-box to create a
222 Assert.assertEquals(res.getStatus(),
223 Response.Status.FORBIDDEN.getStatusCode(), "expected "
224 + Response.Status.FORBIDDEN.getStatusCode());
226 // Store the ID returned from this create operation for additional tests
228 res.releaseConnection();
232 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
233 dependsOnMethods = {"createActiveAccount"})
234 public void createWithoutAuthn(String testName) {
235 if (logger.isDebugEnabled()) {
236 logger.debug(testBanner(testName, CLASS_NAME));
239 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
240 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
241 String pass = collectionObjectClient.getProperty(collectionObjectClient.PASSWORD_PROPERTY);
242 collectionObjectClient.setAuth(false, user, true, pass, true);
243 String identifier = BaseServiceTest.createIdentifier();
244 MultipartOutput multipart = createCollectionObjectInstance(
245 collectionObjectClient.getCommonPartName(), identifier);
246 ClientResponse<Response> res = collectionObjectClient.create(multipart);
247 if (logger.isDebugEnabled()) {
248 logger.debug("create: status = " + res.getStatus());
250 Assert.assertEquals(res.getStatus(),
251 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
252 + Response.Status.UNAUTHORIZED.getStatusCode());
253 res.releaseConnection();
257 @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"})
258 public void createWithInactiveAccount(String testName) {
259 if (logger.isDebugEnabled()) {
260 logger.debug(testBanner(testName));
262 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
263 collectionObjectClient.setAuth(true, "george", true, "george08", true);
264 String identifier = BaseServiceTest.createIdentifier();
265 MultipartOutput multipart = createCollectionObjectInstance(
266 collectionObjectClient.getCommonPartName(), identifier);
268 ClientResponse<Response> res = collectionObjectClient.create(multipart);
269 if (logger.isDebugEnabled()) {
270 logger.debug(testName + ": status = " + res.getStatus());
272 Assert.assertEquals(res.getStatus(),
273 Response.Status.FORBIDDEN.getStatusCode(), "expected "
274 + Response.Status.FORBIDDEN.getStatusCode());
275 res.releaseConnection();
279 * Creates the collection object instance without password.
281 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
282 public void createWithoutPassword(String testName) {
283 if (logger.isDebugEnabled()) {
284 logger.debug(testBanner(testName));
286 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
287 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
288 collectionObjectClient.setAuth(true, user, true, "", false);
289 String identifier = BaseServiceTest.createIdentifier();
290 MultipartOutput multipart = createCollectionObjectInstance(
291 collectionObjectClient.getCommonPartName(), identifier);
292 ClientResponse<Response> res = collectionObjectClient.create(multipart);
293 if (logger.isDebugEnabled()) {
294 logger.debug(testName + ": status = " + res.getStatus());
296 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
297 res.releaseConnection();
301 * Creates the collection object with unknown user
303 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
304 public void createWithUnknownUser(String testName) {
305 if (logger.isDebugEnabled()) {
306 logger.debug(testBanner(testName));
308 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
309 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
310 String identifier = BaseServiceTest.createIdentifier();
311 MultipartOutput multipart = createCollectionObjectInstance(
312 collectionObjectClient.getCommonPartName(), identifier);
313 ClientResponse<Response> res = collectionObjectClient.create(multipart);
314 if (logger.isDebugEnabled()) {
315 logger.debug(testName + ": status = " + res.getStatus());
317 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
318 res.releaseConnection();
322 * Creates the collection object instance with incorrect password.
324 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
325 public void createWithIncorrectPassword(String testName) {
326 if (logger.isDebugEnabled()) {
327 logger.debug(testBanner(testName));
329 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
330 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
331 collectionObjectClient.setAuth(true, user, true, "bar", true);
332 String identifier = BaseServiceTest.createIdentifier();
333 MultipartOutput multipart = createCollectionObjectInstance(
334 collectionObjectClient.getCommonPartName(), identifier);
335 ClientResponse<Response> res = collectionObjectClient.create(multipart);
336 if (logger.isDebugEnabled()) {
337 logger.debug(testName + ": status = " + res.getStatus());
339 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
340 res.releaseConnection();
344 * Creates the collection object instance with incorrect user password.
346 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
347 public void createWithIncorrectUserPassword(String testName) {
348 if (logger.isDebugEnabled()) {
349 logger.debug(testBanner(testName));
351 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
352 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
353 String identifier = BaseServiceTest.createIdentifier();
354 MultipartOutput multipart = createCollectionObjectInstance(
355 collectionObjectClient.getCommonPartName(), identifier);
356 ClientResponse<Response> res = collectionObjectClient.create(multipart);
357 if (logger.isDebugEnabled()) {
358 logger.debug(testName + ": status = "
361 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
362 res.releaseConnection();
366 * Creates the collection object instance with incorrect user password.
368 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
369 public void createWithoutTenant(String testName) {
370 if (logger.isDebugEnabled()) {
371 logger.debug(testBanner(testName));
373 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
374 collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
375 String identifier = BaseServiceTest.createIdentifier();
376 MultipartOutput multipart = createCollectionObjectInstance(
377 collectionObjectClient.getCommonPartName(), identifier);
378 ClientResponse<Response> res = collectionObjectClient.create(multipart);
379 if (logger.isDebugEnabled()) {
380 logger.debug(testName + ": status = "
383 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
384 res.releaseConnection();
388 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
391 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
392 dependsOnMethods = {"create"})
393 public void delete(String testName) {
398 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
399 dependsOnMethods = {"create", "createWithInactiveAccount"})
400 public void deleteAccounts(String testName) throws Exception {
402 if (logger.isDebugEnabled()) {
403 logger.debug(testBanner(testName, CLASS_NAME));
407 AccountClient accountClient = new AccountClient();
408 // accountClient.setAuth(true, "test", true, "test", true);
409 // Submit the request to the service and store the response.
410 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
411 int statusCode = res.getStatus();
412 if (logger.isDebugEnabled()) {
413 logger.debug(testName + ": barney status = " + statusCode);
415 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
416 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
418 res = accountClient.delete(georgeAccountId);
419 statusCode = res.getStatus();
420 if (logger.isDebugEnabled()) {
421 logger.debug(testName + ": george status = " + statusCode);
423 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425 res.releaseConnection();
428 // ---------------------------------------------------------------
429 // Utility methods used by tests above
430 // ---------------------------------------------------------------
432 * Creates the collection object instance.
434 * @param commonPartName the common part name
435 * @param identifier the identifier
437 * @return the multipart output
439 private MultipartOutput createCollectionObjectInstance(
440 String commonPartName, String identifier) {
441 return createCollectionObjectInstance(commonPartName, "objectNumber-"
442 + identifier, "title-" + identifier);
446 * Creates the collection object instance.
448 * @param commonPartName the common part name
449 * @param objectNumber the object number
450 * @param title the object title
452 * @return the multipart output
454 private MultipartOutput createCollectionObjectInstance(
455 String commonPartName, String objectNumber, String title) {
456 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
458 collectionObject.setObjectNumber(objectNumber);
459 TitleGroupList titleGroupList = new TitleGroupList();
460 List<TitleGroup> titleGroups = titleGroupList.getTitleGroup();
461 TitleGroup titleGroup = new TitleGroup();
462 titleGroup.setTitle(title);
463 titleGroups.add(titleGroup);
464 collectionObject.setTitleGroupList(titleGroupList);
465 MultipartOutput multipart =
466 CollectionObjectFactory.createCollectionObjectInstance(
467 commonPartName, collectionObject, null, null);
469 if (logger.isDebugEnabled()) {
470 logger.debug("to be created, collectionobject common ",
471 collectionObject, CollectionobjectsCommon.class);
476 private AccountsCommon createAccountInstance(String screenName,
477 String passwd, String email, String tenantId, boolean invalidTenant) {
479 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
480 screenName, passwd, email, tenantId,
481 true, invalidTenant, true, true);
483 List<AccountTenant> atl = account.getTenants();
485 //disable 2nd tenant till tenant identification is in effect
486 //on the service side for 1-n user-tenants
487 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
488 // at2.setId(UUID.randomUUID().toString());
489 // at2.setName("collectionspace.org");
491 // account.setTenants(atl);
493 if (logger.isDebugEnabled()) {
494 logger.debug("to be created, account common");
495 logger.debug(objectAsXmlString(account,
496 AccountsCommon.class));
503 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
506 public void createList(String testName) throws Exception {
507 //FIXME: Should this test really be empty? If so, please comment accordingly.
511 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
514 public void createWithEmptyEntityBody(String testName) throws Exception {
515 //FIXME: Should this test really be empty? If so, please comment accordingly.
519 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
522 public void createWithMalformedXml(String testName) throws Exception {
523 //FIXME: Should this test really be empty? If so, please comment accordingly.
527 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
530 public void createWithWrongXmlSchema(String testName) throws Exception {
531 //FIXME: Should this test really be empty? If so, please comment accordingly.
535 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
538 public void read(String testName) throws Exception {
539 //FIXME: Should this test really be empty? If so, please comment accordingly.
543 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
546 public void readNonExistent(String testName) throws Exception {
547 //FIXME: Should this test really be empty? If so, please comment accordingly.
551 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
554 public void readList(String testName) throws Exception {
555 //FIXME: Should this test really be empty? If so, please comment accordingly.
559 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
562 public void update(String testName) throws Exception {
563 //FIXME: Should this test really be empty? If so, please comment accordingly.
567 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
570 public void updateWithEmptyEntityBody(String testName) throws Exception {
571 //FIXME: Should this test really be empty? If so, please comment accordingly.
575 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
578 public void updateWithMalformedXml(String testName) throws Exception {
579 //FIXME: Should this test really be empty? If so, please comment accordingly.
583 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
586 public void updateWithWrongXmlSchema(String testName) throws Exception {
587 //FIXME: Should this test really be empty? If so, please comment accordingly.
591 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
594 public void updateNonExistent(String testName) throws Exception {
595 //FIXME: Should this test really be empty? If so, please comment accordingly.
599 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
602 public void deleteNonExistent(String testName) throws Exception {
603 //FIXME: Should this test really be empty? If so, please comment accordingly.