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.setAuth(true, "test", true, "test", true);
110 // Submit the request to the service and store the response.
111 AccountsCommon account =
112 createAccountInstance("barney", "barney08", "barney@dinoland.com", false);
113 ClientResponse<Response> res = accountClient.create(account);
114 int statusCode = res.getStatus();
116 if (logger.isDebugEnabled()) {
117 logger.debug(testName + ": barney status = " + statusCode);
119 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
120 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
121 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
123 // Store the ID returned from this create operation
124 // for additional tests below.
125 barneyAccountId = extractId(res);
126 if (logger.isDebugEnabled()) {
127 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
129 res.releaseConnection();
133 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
134 public void createInactiveAccount(String testName) throws Exception {
135 // Perform setup, such as initializing the type of service request
136 // (e.g. CREATE, DELETE), its valid and expected status codes, and
137 // its associated HTTP method name (e.g. POST, DELETE).
138 setupCreate(testName);
139 AccountClient accountClient = new AccountClient();
140 accountClient.setAuth(true, "test", true, "test", true);
142 // Submit the request to the service and store the response.
143 AccountsCommon account =
144 createAccountInstance("george", "george08", "george@curiousland.com", false);
145 ClientResponse<Response> res = accountClient.create(account);
146 int statusCode = res.getStatus();
148 if (logger.isDebugEnabled()) {
149 logger.debug(testName + ": george status = " + statusCode);
151 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
155 // Store the ID returned from this create operation
156 // for additional tests below.
157 georgeAccountId = extractId(res);
158 if (logger.isDebugEnabled()) {
159 logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
161 res.releaseConnection();
163 setupUpdate(testName);
164 account.setStatus(Status.INACTIVE);
165 if (logger.isDebugEnabled()) {
166 logger.debug(testName + ":updated object");
167 logger.debug(objectAsXmlString(account,
168 AccountsCommon.class));
171 // Submit the request to the service and store the response.
172 ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
173 statusCode = res1.getStatus();
174 // Check the status code of the response: does it match the expected response(s)?
175 if (logger.isDebugEnabled()) {
176 logger.debug(testName + ": status = " + statusCode);
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181 res1.releaseConnection();
186 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
188 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
189 dependsOnMethods = {"createActiveAccount"})
191 public void create(String testName) {
192 setupCreate(testName);
193 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
194 collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
195 String identifier = BaseServiceTest.createIdentifier();
196 MultipartOutput multipart = createCollectionObjectInstance(
197 collectionObjectClient.getCommonPartName(), identifier);
198 ClientResponse<Response> res = collectionObjectClient.create(multipart);
199 if (logger.isDebugEnabled()) {
200 logger.debug("create: status = " + res.getStatus());
202 Assert.assertEquals(res.getStatus(),
203 Response.Status.CREATED.getStatusCode(), "expected "
204 + Response.Status.CREATED.getStatusCode());
206 // Store the ID returned from this create operation for additional tests
208 knownResourceId = extractId(res);
209 res.releaseConnection();
213 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
214 dependsOnMethods = {"createActiveAccount"})
215 public void createWithoutAuthn(String testName) {
216 setupCreate(testName);
217 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
218 collectionObjectClient.setAuth(false, "test", true, "test", true);
219 String identifier = BaseServiceTest.createIdentifier();
220 MultipartOutput multipart = createCollectionObjectInstance(
221 collectionObjectClient.getCommonPartName(), identifier);
222 ClientResponse<Response> res = collectionObjectClient.create(multipart);
223 if (logger.isDebugEnabled()) {
224 logger.debug("create: status = " + res.getStatus());
226 Assert.assertEquals(res.getStatus(),
227 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
228 + Response.Status.UNAUTHORIZED.getStatusCode());
229 res.releaseConnection();
233 @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"})
234 public void createWithInactiveAccount(String testName) {
236 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
237 collectionObjectClient.setAuth(true, "george", true, "george08", true);
238 String identifier = BaseServiceTest.createIdentifier();
239 MultipartOutput multipart = createCollectionObjectInstance(
240 collectionObjectClient.getCommonPartName(), identifier);
242 ClientResponse<Response> res = collectionObjectClient.create(multipart);
243 if (logger.isDebugEnabled()) {
244 logger.debug(testName + ": status = " + res.getStatus());
246 Assert.assertEquals(res.getStatus(),
247 Response.Status.FORBIDDEN.getStatusCode(), "expected "
248 + Response.Status.FORBIDDEN.getStatusCode());
249 res.releaseConnection();
253 * Creates the collection object instance without password.
255 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
256 public void createWithoutPassword(String testName) {
258 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
259 collectionObjectClient.setAuth(true, "test", true, "", false);
260 String identifier = BaseServiceTest.createIdentifier();
261 MultipartOutput multipart = createCollectionObjectInstance(
262 collectionObjectClient.getCommonPartName(), identifier);
263 ClientResponse<Response> res = collectionObjectClient.create(multipart);
264 if (logger.isDebugEnabled()) {
265 logger.debug(testName + ": status = " + res.getStatus());
267 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
268 res.releaseConnection();
272 * Creates the collection object with unknown user
274 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
275 public void createWithUnknownUser(String testName) {
277 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
278 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
279 String identifier = BaseServiceTest.createIdentifier();
280 MultipartOutput multipart = createCollectionObjectInstance(
281 collectionObjectClient.getCommonPartName(), identifier);
282 ClientResponse<Response> res = collectionObjectClient.create(multipart);
283 if (logger.isDebugEnabled()) {
284 logger.debug(testName + ": status = " + res.getStatus());
286 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
287 res.releaseConnection();
291 * Creates the collection object instance with incorrect password.
293 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
294 public void createWithIncorrectPassword(String testName) {
296 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
297 collectionObjectClient.setAuth(true, "test", true, "bar", true);
298 String identifier = BaseServiceTest.createIdentifier();
299 MultipartOutput multipart = createCollectionObjectInstance(
300 collectionObjectClient.getCommonPartName(), identifier);
301 ClientResponse<Response> res = collectionObjectClient.create(multipart);
302 if (logger.isDebugEnabled()) {
303 logger.debug(testName + ": status = " + res.getStatus());
305 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
306 res.releaseConnection();
310 * Creates the collection object instance with incorrect user password.
312 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
313 public void createWithIncorrectUserPassword(String testName) {
315 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
316 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
317 String identifier = BaseServiceTest.createIdentifier();
318 MultipartOutput multipart = createCollectionObjectInstance(
319 collectionObjectClient.getCommonPartName(), identifier);
320 ClientResponse<Response> res = collectionObjectClient.create(multipart);
321 if (logger.isDebugEnabled()) {
322 logger.debug(testName + ": status = "
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 user password.
332 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
333 public void createWithoutTenant(String testName) {
335 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
336 collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
337 String identifier = BaseServiceTest.createIdentifier();
338 MultipartOutput multipart = createCollectionObjectInstance(
339 collectionObjectClient.getCommonPartName(), identifier);
340 ClientResponse<Response> res = collectionObjectClient.create(multipart);
341 if (logger.isDebugEnabled()) {
342 logger.debug(testName + ": status = "
345 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
346 res.releaseConnection();
350 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
353 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
354 dependsOnMethods = {"create"})
355 public void delete(String testName) {
356 setupDelete(testName);
357 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
358 collectionObjectClient = new CollectionObjectClient();
359 collectionObjectClient.setAuth(true, "test", true, "test", true);
360 if (logger.isDebugEnabled()) {
361 logger.debug("Calling deleteCollectionObject:" + knownResourceId);
363 ClientResponse<Response> res = collectionObjectClient.delete(knownResourceId);
364 if (logger.isDebugEnabled()) {
365 logger.debug("deleteCollectionObject: status = " + res.getStatus());
367 Assert.assertEquals(res.getStatus(),
368 Response.Status.OK.getStatusCode(), "expected " + Response.Status.OK.getStatusCode());
369 res.releaseConnection();
372 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
373 dependsOnMethods = {"create", "createWithInactiveAccount"})
374 public void deleteAccounts(String testName) throws Exception {
377 setupDelete(testName);
378 AccountClient accountClient = new AccountClient();
379 accountClient.setAuth(true, "test", true, "test", true);
380 // Submit the request to the service and store the response.
381 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
382 int statusCode = res.getStatus();
383 if (logger.isDebugEnabled()) {
384 logger.debug(testName + ": barney status = " + statusCode);
386 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
387 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
389 res = accountClient.delete(georgeAccountId);
390 statusCode = res.getStatus();
391 if (logger.isDebugEnabled()) {
392 logger.debug(testName + ": george status = " + statusCode);
394 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
395 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
396 res.releaseConnection();
399 // ---------------------------------------------------------------
400 // Utility methods used by tests above
401 // ---------------------------------------------------------------
403 * Creates the collection object instance.
405 * @param commonPartName the common part name
406 * @param identifier the identifier
408 * @return the multipart output
410 private MultipartOutput createCollectionObjectInstance(
411 String commonPartName, String identifier) {
412 return createCollectionObjectInstance(commonPartName, "objectNumber-"
413 + identifier, "objectName-" + identifier);
417 * Creates the collection object instance.
419 * @param commonPartName the common part name
420 * @param objectNumber the object number
421 * @param objectName the object name
423 * @return the multipart output
425 private MultipartOutput createCollectionObjectInstance(
426 String commonPartName, String objectNumber, String objectName) {
427 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
429 collectionObject.setObjectNumber(objectNumber);
430 collectionObject.setObjectName(objectName);
431 MultipartOutput multipart =
432 CollectionObjectFactory.createCollectionObjectInstance(
433 commonPartName, collectionObject, null, null);
435 if (logger.isDebugEnabled()) {
436 logger.debug("to be created, collectionobject common ",
437 collectionObject, CollectionobjectsCommon.class);
442 private AccountsCommon createAccountInstance(String screenName,
443 String passwd, String email, boolean invalidTenant) {
445 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
446 screenName, passwd, email,
447 true, true, invalidTenant, true, true);
449 List<AccountTenant> atl = account.getTenants();
451 //disable 2nd tenant till tenant identification is in effect
452 //on the service side for 1-n user-tenants
453 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
454 // at2.setId(UUID.randomUUID().toString());
455 // at2.setName("collectionspace.org");
457 // account.setTenants(atl);
459 if (logger.isDebugEnabled()) {
460 logger.debug("to be created, account common");
461 logger.debug(objectAsXmlString(account,
462 AccountsCommon.class));
469 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
472 public void createList(String testName) throws Exception {
473 //FIXME: Should this test really be empty? If so, please comment accordingly.
477 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
480 public void createWithEmptyEntityBody(String testName) throws Exception {
481 //FIXME: Should this test really be empty? If so, please comment accordingly.
485 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
488 public void createWithMalformedXml(String testName) throws Exception {
489 //FIXME: Should this test really be empty? If so, please comment accordingly.
493 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
496 public void createWithWrongXmlSchema(String testName) throws Exception {
497 //FIXME: Should this test really be empty? If so, please comment accordingly.
501 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
504 public void read(String testName) throws Exception {
505 //FIXME: Should this test really be empty? If so, please comment accordingly.
509 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
512 public void readNonExistent(String testName) throws Exception {
513 //FIXME: Should this test really be empty? If so, please comment accordingly.
517 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
520 public void readList(String testName) throws Exception {
521 //FIXME: Should this test really be empty? If so, please comment accordingly.
525 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
528 public void update(String testName) throws Exception {
529 //FIXME: Should this test really be empty? If so, please comment accordingly.
533 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
536 public void updateWithEmptyEntityBody(String testName) throws Exception {
537 //FIXME: Should this test really be empty? If so, please comment accordingly.
541 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
544 public void updateWithMalformedXml(String testName) throws Exception {
545 //FIXME: Should this test really be empty? If so, please comment accordingly.
549 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
552 public void updateWithWrongXmlSchema(String testName) throws Exception {
553 //FIXME: Should this test really be empty? If so, please comment accordingly.
557 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
560 public void updateNonExistent(String testName) throws Exception {
561 //FIXME: Should this test really be empty? If so, please comment accordingly.
565 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
568 public void deleteNonExistent(String testName) throws Exception {
569 //FIXME: Should this test really be empty? If so, please comment accordingly.