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 //so it does not have any permissions out-of-the-box to create a
204 Assert.assertEquals(res.getStatus(),
205 Response.Status.FORBIDDEN.getStatusCode(), "expected "
206 + Response.Status.FORBIDDEN.getStatusCode());
208 // Store the ID returned from this create operation for additional tests
210 res.releaseConnection();
214 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
215 dependsOnMethods = {"createActiveAccount"})
216 public void createWithoutAuthn(String testName) {
217 setupCreate(testName);
218 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
219 collectionObjectClient.setAuth(false, "test", true, "test", true);
220 String identifier = BaseServiceTest.createIdentifier();
221 MultipartOutput multipart = createCollectionObjectInstance(
222 collectionObjectClient.getCommonPartName(), identifier);
223 ClientResponse<Response> res = collectionObjectClient.create(multipart);
224 if (logger.isDebugEnabled()) {
225 logger.debug("create: status = " + res.getStatus());
227 Assert.assertEquals(res.getStatus(),
228 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
229 + Response.Status.UNAUTHORIZED.getStatusCode());
230 res.releaseConnection();
234 @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"})
235 public void createWithInactiveAccount(String testName) {
237 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
238 collectionObjectClient.setAuth(true, "george", true, "george08", true);
239 String identifier = BaseServiceTest.createIdentifier();
240 MultipartOutput multipart = createCollectionObjectInstance(
241 collectionObjectClient.getCommonPartName(), identifier);
243 ClientResponse<Response> res = collectionObjectClient.create(multipart);
244 if (logger.isDebugEnabled()) {
245 logger.debug(testName + ": status = " + res.getStatus());
247 Assert.assertEquals(res.getStatus(),
248 Response.Status.FORBIDDEN.getStatusCode(), "expected "
249 + Response.Status.FORBIDDEN.getStatusCode());
250 res.releaseConnection();
254 * Creates the collection object instance without password.
256 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
257 public void createWithoutPassword(String testName) {
259 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
260 collectionObjectClient.setAuth(true, "test", true, "", false);
261 String identifier = BaseServiceTest.createIdentifier();
262 MultipartOutput multipart = createCollectionObjectInstance(
263 collectionObjectClient.getCommonPartName(), identifier);
264 ClientResponse<Response> res = collectionObjectClient.create(multipart);
265 if (logger.isDebugEnabled()) {
266 logger.debug(testName + ": status = " + res.getStatus());
268 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
269 res.releaseConnection();
273 * Creates the collection object with unknown user
275 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
276 public void createWithUnknownUser(String testName) {
278 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
279 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
280 String identifier = BaseServiceTest.createIdentifier();
281 MultipartOutput multipart = createCollectionObjectInstance(
282 collectionObjectClient.getCommonPartName(), identifier);
283 ClientResponse<Response> res = collectionObjectClient.create(multipart);
284 if (logger.isDebugEnabled()) {
285 logger.debug(testName + ": status = " + res.getStatus());
287 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
288 res.releaseConnection();
292 * Creates the collection object instance with incorrect password.
294 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
295 public void createWithIncorrectPassword(String testName) {
297 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
298 collectionObjectClient.setAuth(true, "test", true, "bar", true);
299 String identifier = BaseServiceTest.createIdentifier();
300 MultipartOutput multipart = createCollectionObjectInstance(
301 collectionObjectClient.getCommonPartName(), identifier);
302 ClientResponse<Response> res = collectionObjectClient.create(multipart);
303 if (logger.isDebugEnabled()) {
304 logger.debug(testName + ": status = " + res.getStatus());
306 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
307 res.releaseConnection();
311 * Creates the collection object instance with incorrect user password.
313 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
314 public void createWithIncorrectUserPassword(String testName) {
316 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
317 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
318 String identifier = BaseServiceTest.createIdentifier();
319 MultipartOutput multipart = createCollectionObjectInstance(
320 collectionObjectClient.getCommonPartName(), identifier);
321 ClientResponse<Response> res = collectionObjectClient.create(multipart);
322 if (logger.isDebugEnabled()) {
323 logger.debug(testName + ": status = "
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 user password.
333 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
334 public void createWithoutTenant(String testName) {
336 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
337 collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
338 String identifier = BaseServiceTest.createIdentifier();
339 MultipartOutput multipart = createCollectionObjectInstance(
340 collectionObjectClient.getCommonPartName(), identifier);
341 ClientResponse<Response> res = collectionObjectClient.create(multipart);
342 if (logger.isDebugEnabled()) {
343 logger.debug(testName + ": status = "
346 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
347 res.releaseConnection();
351 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
354 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
355 dependsOnMethods = {"create"})
356 public void delete(String testName) {
357 setupDelete(testName);
361 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
362 dependsOnMethods = {"create", "createWithInactiveAccount"})
363 public void deleteAccounts(String testName) throws Exception {
366 setupDelete(testName);
367 AccountClient accountClient = new AccountClient();
368 accountClient.setAuth(true, "test", true, "test", true);
369 // Submit the request to the service and store the response.
370 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
371 int statusCode = res.getStatus();
372 if (logger.isDebugEnabled()) {
373 logger.debug(testName + ": barney status = " + statusCode);
375 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
376 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
378 res = accountClient.delete(georgeAccountId);
379 statusCode = res.getStatus();
380 if (logger.isDebugEnabled()) {
381 logger.debug(testName + ": george status = " + statusCode);
383 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
384 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
385 res.releaseConnection();
388 // ---------------------------------------------------------------
389 // Utility methods used by tests above
390 // ---------------------------------------------------------------
392 * Creates the collection object instance.
394 * @param commonPartName the common part name
395 * @param identifier the identifier
397 * @return the multipart output
399 private MultipartOutput createCollectionObjectInstance(
400 String commonPartName, String identifier) {
401 return createCollectionObjectInstance(commonPartName, "objectNumber-"
402 + identifier, "objectName-" + identifier);
406 * Creates the collection object instance.
408 * @param commonPartName the common part name
409 * @param objectNumber the object number
410 * @param objectName the object name
412 * @return the multipart output
414 private MultipartOutput createCollectionObjectInstance(
415 String commonPartName, String objectNumber, String objectName) {
416 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
418 collectionObject.setObjectNumber(objectNumber);
419 collectionObject.setObjectName(objectName);
420 MultipartOutput multipart =
421 CollectionObjectFactory.createCollectionObjectInstance(
422 commonPartName, collectionObject, null, null);
424 if (logger.isDebugEnabled()) {
425 logger.debug("to be created, collectionobject common ",
426 collectionObject, CollectionobjectsCommon.class);
431 private AccountsCommon createAccountInstance(String screenName,
432 String passwd, String email, boolean invalidTenant) {
434 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
435 screenName, passwd, email,
436 true, true, invalidTenant, true, true);
438 List<AccountTenant> atl = account.getTenants();
440 //disable 2nd tenant till tenant identification is in effect
441 //on the service side for 1-n user-tenants
442 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
443 // at2.setId(UUID.randomUUID().toString());
444 // at2.setName("collectionspace.org");
446 // account.setTenants(atl);
448 if (logger.isDebugEnabled()) {
449 logger.debug("to be created, account common");
450 logger.debug(objectAsXmlString(account,
451 AccountsCommon.class));
458 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
461 public void createList(String testName) throws Exception {
462 //FIXME: Should this test really be empty? If so, please comment accordingly.
466 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
469 public void createWithEmptyEntityBody(String testName) throws Exception {
470 //FIXME: Should this test really be empty? If so, please comment accordingly.
474 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
477 public void createWithMalformedXml(String testName) throws Exception {
478 //FIXME: Should this test really be empty? If so, please comment accordingly.
482 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
485 public void createWithWrongXmlSchema(String testName) throws Exception {
486 //FIXME: Should this test really be empty? If so, please comment accordingly.
490 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
493 public void read(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#readNonExistent()
501 public void readNonExistent(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#readList()
509 public void readList(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#update()
517 public void update(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#updateWithEmptyEntityBody()
525 public void updateWithEmptyEntityBody(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#updateWithMalformedXml()
533 public void updateWithMalformedXml(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#updateWithWrongXmlSchema()
541 public void updateWithWrongXmlSchema(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#updateNonExistent()
549 public void updateNonExistent(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#deleteNonExistent()
557 public void deleteNonExistent(String testName) throws Exception {
558 //FIXME: Should this test really be empty? If so, please comment accordingly.