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 private final String CLASS_NAME = AuthenticationServiceTest.class.getName();
67 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
70 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
73 protected String getServicePathComponent() {
74 // no need to return anything but null since no auth resources are
80 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
83 protected CollectionSpaceClient getClientInstance() {
84 return new AccountClient();
88 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
91 protected AbstractCommonList getAbstractCommonList(
92 ClientResponse<AbstractCommonList> response) {
93 throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
96 @Test(dataProvider = "testName")
98 public void readPaginatedList(String testName) throws Exception {
99 // Test not supported.
102 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
103 public void createActiveAccount(String testName) throws Exception {
105 if (logger.isDebugEnabled()) {
106 logger.debug(testBanner(testName, CLASS_NAME));
108 // Perform setup, such as initializing the type of service request
109 // (e.g. CREATE, DELETE), its valid and expected status codes, and
110 // its associated HTTP method name (e.g. POST, DELETE).
113 AccountClient accountClient = new AccountClient();
114 accountClient.setAuth(true, "test", true, "test", true);
116 // Submit the request to the service and store the response.
117 AccountsCommon account =
118 createAccountInstance("barney", "barney08", "barney@dinoland.com",
119 accountClient.getTenantId(), 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",
155 accountClient.getTenantId(), false);
156 ClientResponse<Response> res = accountClient.create(account);
157 int statusCode = res.getStatus();
159 if (logger.isDebugEnabled()) {
160 logger.debug(testName + ": george status = " + statusCode);
162 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
163 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
164 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
166 // Store the ID returned from this create operation
167 // for additional tests below.
168 georgeAccountId = extractId(res);
169 if (logger.isDebugEnabled()) {
170 logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
172 res.releaseConnection();
175 account.setStatus(Status.INACTIVE);
176 if (logger.isDebugEnabled()) {
177 logger.debug(testName + ":updated object");
178 logger.debug(objectAsXmlString(account,
179 AccountsCommon.class));
182 // Submit the request to the service and store the response.
183 ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
184 statusCode = res1.getStatus();
185 // Check the status code of the response: does it match the expected response(s)?
186 if (logger.isDebugEnabled()) {
187 logger.debug(testName + ": status = " + statusCode);
189 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
190 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
191 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
192 res1.releaseConnection();
197 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
199 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
200 dependsOnMethods = {"createActiveAccount"})
202 public void create(String testName) {
203 if (logger.isDebugEnabled()) {
204 logger.debug(testBanner(testName, CLASS_NAME));
207 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
208 collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
209 String identifier = BaseServiceTest.createIdentifier();
210 MultipartOutput multipart = createCollectionObjectInstance(
211 collectionObjectClient.getCommonPartName(), identifier);
212 ClientResponse<Response> res = collectionObjectClient.create(multipart);
213 if (logger.isDebugEnabled()) {
214 logger.debug("create: status = " + res.getStatus());
216 //so it does not have any permissions out-of-the-box to create a
218 Assert.assertEquals(res.getStatus(),
219 Response.Status.FORBIDDEN.getStatusCode(), "expected "
220 + Response.Status.FORBIDDEN.getStatusCode());
222 // Store the ID returned from this create operation for additional tests
224 res.releaseConnection();
228 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
229 dependsOnMethods = {"createActiveAccount"})
230 public void createWithoutAuthn(String testName) {
231 if (logger.isDebugEnabled()) {
232 logger.debug(testBanner(testName, CLASS_NAME));
235 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
236 collectionObjectClient.setAuth(false, "test", true, "test", true);
237 String identifier = BaseServiceTest.createIdentifier();
238 MultipartOutput multipart = createCollectionObjectInstance(
239 collectionObjectClient.getCommonPartName(), identifier);
240 ClientResponse<Response> res = collectionObjectClient.create(multipart);
241 if (logger.isDebugEnabled()) {
242 logger.debug("create: status = " + res.getStatus());
244 Assert.assertEquals(res.getStatus(),
245 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
246 + Response.Status.UNAUTHORIZED.getStatusCode());
247 res.releaseConnection();
251 @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"})
252 public void createWithInactiveAccount(String testName) {
253 if (logger.isDebugEnabled()) {
254 logger.debug(testBanner(testName));
256 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
257 collectionObjectClient.setAuth(true, "george", true, "george08", true);
258 String identifier = BaseServiceTest.createIdentifier();
259 MultipartOutput multipart = createCollectionObjectInstance(
260 collectionObjectClient.getCommonPartName(), identifier);
262 ClientResponse<Response> res = collectionObjectClient.create(multipart);
263 if (logger.isDebugEnabled()) {
264 logger.debug(testName + ": status = " + res.getStatus());
266 Assert.assertEquals(res.getStatus(),
267 Response.Status.FORBIDDEN.getStatusCode(), "expected "
268 + Response.Status.FORBIDDEN.getStatusCode());
269 res.releaseConnection();
273 * Creates the collection object instance without password.
275 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
276 public void createWithoutPassword(String testName) {
277 if (logger.isDebugEnabled()) {
278 logger.debug(testBanner(testName));
280 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
281 collectionObjectClient.setAuth(true, "test", true, "", false);
282 String identifier = BaseServiceTest.createIdentifier();
283 MultipartOutput multipart = createCollectionObjectInstance(
284 collectionObjectClient.getCommonPartName(), identifier);
285 ClientResponse<Response> res = collectionObjectClient.create(multipart);
286 if (logger.isDebugEnabled()) {
287 logger.debug(testName + ": status = " + res.getStatus());
289 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
290 res.releaseConnection();
294 * Creates the collection object with unknown user
296 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
297 public void createWithUnknownUser(String testName) {
298 if (logger.isDebugEnabled()) {
299 logger.debug(testBanner(testName));
301 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
302 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
303 String identifier = BaseServiceTest.createIdentifier();
304 MultipartOutput multipart = createCollectionObjectInstance(
305 collectionObjectClient.getCommonPartName(), identifier);
306 ClientResponse<Response> res = collectionObjectClient.create(multipart);
307 if (logger.isDebugEnabled()) {
308 logger.debug(testName + ": status = " + res.getStatus());
310 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
311 res.releaseConnection();
315 * Creates the collection object instance with incorrect password.
317 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
318 public void createWithIncorrectPassword(String testName) {
319 if (logger.isDebugEnabled()) {
320 logger.debug(testBanner(testName));
322 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
323 collectionObjectClient.setAuth(true, "test", true, "bar", true);
324 String identifier = BaseServiceTest.createIdentifier();
325 MultipartOutput multipart = createCollectionObjectInstance(
326 collectionObjectClient.getCommonPartName(), identifier);
327 ClientResponse<Response> res = collectionObjectClient.create(multipart);
328 if (logger.isDebugEnabled()) {
329 logger.debug(testName + ": status = " + res.getStatus());
331 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
332 res.releaseConnection();
336 * Creates the collection object instance with incorrect user password.
338 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
339 public void createWithIncorrectUserPassword(String testName) {
340 if (logger.isDebugEnabled()) {
341 logger.debug(testBanner(testName));
343 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
344 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
345 String identifier = BaseServiceTest.createIdentifier();
346 MultipartOutput multipart = createCollectionObjectInstance(
347 collectionObjectClient.getCommonPartName(), identifier);
348 ClientResponse<Response> res = collectionObjectClient.create(multipart);
349 if (logger.isDebugEnabled()) {
350 logger.debug(testName + ": status = "
353 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
354 res.releaseConnection();
358 * Creates the collection object instance with incorrect user password.
360 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
361 public void createWithoutTenant(String testName) {
362 if (logger.isDebugEnabled()) {
363 logger.debug(testBanner(testName));
365 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
366 collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
367 String identifier = BaseServiceTest.createIdentifier();
368 MultipartOutput multipart = createCollectionObjectInstance(
369 collectionObjectClient.getCommonPartName(), identifier);
370 ClientResponse<Response> res = collectionObjectClient.create(multipart);
371 if (logger.isDebugEnabled()) {
372 logger.debug(testName + ": status = "
375 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
376 res.releaseConnection();
380 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
383 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
384 dependsOnMethods = {"create"})
385 public void delete(String testName) {
390 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
391 dependsOnMethods = {"create", "createWithInactiveAccount"})
392 public void deleteAccounts(String testName) throws Exception {
394 if (logger.isDebugEnabled()) {
395 logger.debug(testBanner(testName, CLASS_NAME));
399 AccountClient accountClient = new AccountClient();
400 accountClient.setAuth(true, "test", true, "test", true);
401 // Submit the request to the service and store the response.
402 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
403 int statusCode = res.getStatus();
404 if (logger.isDebugEnabled()) {
405 logger.debug(testName + ": barney status = " + statusCode);
407 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
408 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
410 res = accountClient.delete(georgeAccountId);
411 statusCode = res.getStatus();
412 if (logger.isDebugEnabled()) {
413 logger.debug(testName + ": george status = " + statusCode);
415 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
416 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
417 res.releaseConnection();
420 // ---------------------------------------------------------------
421 // Utility methods used by tests above
422 // ---------------------------------------------------------------
424 * Creates the collection object instance.
426 * @param commonPartName the common part name
427 * @param identifier the identifier
429 * @return the multipart output
431 private MultipartOutput createCollectionObjectInstance(
432 String commonPartName, String identifier) {
433 return createCollectionObjectInstance(commonPartName, "objectNumber-"
434 + identifier, "objectName-" + identifier);
438 * Creates the collection object instance.
440 * @param commonPartName the common part name
441 * @param objectNumber the object number
442 * @param objectName the object name
444 * @return the multipart output
446 private MultipartOutput createCollectionObjectInstance(
447 String commonPartName, String objectNumber, String objectName) {
448 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
450 collectionObject.setObjectNumber(objectNumber);
451 collectionObject.setObjectName(objectName);
452 MultipartOutput multipart =
453 CollectionObjectFactory.createCollectionObjectInstance(
454 commonPartName, collectionObject, null, null);
456 if (logger.isDebugEnabled()) {
457 logger.debug("to be created, collectionobject common ",
458 collectionObject, CollectionobjectsCommon.class);
463 private AccountsCommon createAccountInstance(String screenName,
464 String passwd, String email, String tenantId, boolean invalidTenant) {
466 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
467 screenName, passwd, email, tenantId,
468 true, invalidTenant, true, true);
470 List<AccountTenant> atl = account.getTenants();
472 //disable 2nd tenant till tenant identification is in effect
473 //on the service side for 1-n user-tenants
474 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
475 // at2.setId(UUID.randomUUID().toString());
476 // at2.setName("collectionspace.org");
478 // account.setTenants(atl);
480 if (logger.isDebugEnabled()) {
481 logger.debug("to be created, account common");
482 logger.debug(objectAsXmlString(account,
483 AccountsCommon.class));
490 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
493 public void createList(String testName) throws Exception {
494 //FIXME: Should this test really be empty? If so, please comment accordingly.
498 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
501 public void createWithEmptyEntityBody(String testName) throws Exception {
502 //FIXME: Should this test really be empty? If so, please comment accordingly.
506 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
509 public void createWithMalformedXml(String testName) throws Exception {
510 //FIXME: Should this test really be empty? If so, please comment accordingly.
514 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
517 public void createWithWrongXmlSchema(String testName) throws Exception {
518 //FIXME: Should this test really be empty? If so, please comment accordingly.
522 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
525 public void read(String testName) throws Exception {
526 //FIXME: Should this test really be empty? If so, please comment accordingly.
530 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
533 public void readNonExistent(String testName) throws Exception {
534 //FIXME: Should this test really be empty? If so, please comment accordingly.
538 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
541 public void readList(String testName) throws Exception {
542 //FIXME: Should this test really be empty? If so, please comment accordingly.
546 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
549 public void update(String testName) throws Exception {
550 //FIXME: Should this test really be empty? If so, please comment accordingly.
554 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
557 public void updateWithEmptyEntityBody(String testName) throws Exception {
558 //FIXME: Should this test really be empty? If so, please comment accordingly.
562 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
565 public void updateWithMalformedXml(String testName) throws Exception {
566 //FIXME: Should this test really be empty? If so, please comment accordingly.
570 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
573 public void updateWithWrongXmlSchema(String testName) throws Exception {
574 //FIXME: Should this test really be empty? If so, please comment accordingly.
578 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
581 public void updateNonExistent(String testName) throws Exception {
582 //FIXME: Should this test really be empty? If so, please comment accordingly.
586 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
589 public void deleteNonExistent(String testName) throws Exception {
590 //FIXME: Should this test really be empty? If so, please comment accordingly.