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 // This should not be needed - the auth is already set up
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",
120 accountClient.getTenantId(), false);
121 ClientResponse<Response> res = accountClient.create(account);
122 int statusCode = res.getStatus();
124 if (logger.isDebugEnabled()) {
125 logger.debug(testName + ": barney status = " + statusCode);
127 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
128 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
129 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
131 // Store the ID returned from this create operation
132 // for additional tests below.
133 barneyAccountId = extractId(res);
134 if (logger.isDebugEnabled()) {
135 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
137 res.releaseConnection();
141 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
142 public void createInactiveAccount(String testName) throws Exception {
144 if (logger.isDebugEnabled()) {
145 logger.debug(testBanner(testName, CLASS_NAME));
150 AccountClient accountClient = new AccountClient();
151 // This should not be needed - the auth is already set up
152 //accountClient.setAuth(true, "test", true, "test", true);
154 // Submit the request to the service and store the response.
155 AccountsCommon account =
156 createAccountInstance("george", "george08", "george@curiousland.com",
157 accountClient.getTenantId(), false);
158 ClientResponse<Response> res = accountClient.create(account);
159 int statusCode = res.getStatus();
161 if (logger.isDebugEnabled()) {
162 logger.debug(testName + ": george status = " + statusCode);
164 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
165 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
166 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
168 // Store the ID returned from this create operation
169 // for additional tests below.
170 georgeAccountId = extractId(res);
171 if (logger.isDebugEnabled()) {
172 logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
174 res.releaseConnection();
177 account.setStatus(Status.INACTIVE);
178 if (logger.isDebugEnabled()) {
179 logger.debug(testName + ":updated object");
180 logger.debug(objectAsXmlString(account,
181 AccountsCommon.class));
184 // Submit the request to the service and store the response.
185 ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
186 statusCode = res1.getStatus();
187 // Check the status code of the response: does it match the expected response(s)?
188 if (logger.isDebugEnabled()) {
189 logger.debug(testName + ": status = " + statusCode);
191 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
192 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
193 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
194 res1.releaseConnection();
199 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
201 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
202 dependsOnMethods = {"createActiveAccount"})
204 public void create(String testName) {
205 if (logger.isDebugEnabled()) {
206 logger.debug(testBanner(testName, CLASS_NAME));
209 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
210 collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
211 String identifier = BaseServiceTest.createIdentifier();
212 MultipartOutput multipart = createCollectionObjectInstance(
213 collectionObjectClient.getCommonPartName(), identifier);
214 ClientResponse<Response> res = collectionObjectClient.create(multipart);
215 if (logger.isDebugEnabled()) {
216 logger.debug("create: status = " + res.getStatus());
218 //so it does not have any permissions out-of-the-box to create a
220 Assert.assertEquals(res.getStatus(),
221 Response.Status.FORBIDDEN.getStatusCode(), "expected "
222 + Response.Status.FORBIDDEN.getStatusCode());
224 // Store the ID returned from this create operation for additional tests
226 res.releaseConnection();
230 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
231 dependsOnMethods = {"createActiveAccount"})
232 public void createWithoutAuthn(String testName) {
233 if (logger.isDebugEnabled()) {
234 logger.debug(testBanner(testName, CLASS_NAME));
237 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
238 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
239 String pass = collectionObjectClient.getProperty(collectionObjectClient.PASSWORD_PROPERTY);
240 collectionObjectClient.setAuth(false, user, true, pass, true);
241 String identifier = BaseServiceTest.createIdentifier();
242 MultipartOutput multipart = createCollectionObjectInstance(
243 collectionObjectClient.getCommonPartName(), identifier);
244 ClientResponse<Response> res = collectionObjectClient.create(multipart);
245 if (logger.isDebugEnabled()) {
246 logger.debug("create: status = " + res.getStatus());
248 Assert.assertEquals(res.getStatus(),
249 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
250 + Response.Status.UNAUTHORIZED.getStatusCode());
251 res.releaseConnection();
255 @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"})
256 public void createWithInactiveAccount(String testName) {
257 if (logger.isDebugEnabled()) {
258 logger.debug(testBanner(testName));
260 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
261 collectionObjectClient.setAuth(true, "george", true, "george08", true);
262 String identifier = BaseServiceTest.createIdentifier();
263 MultipartOutput multipart = createCollectionObjectInstance(
264 collectionObjectClient.getCommonPartName(), identifier);
266 ClientResponse<Response> res = collectionObjectClient.create(multipart);
267 if (logger.isDebugEnabled()) {
268 logger.debug(testName + ": status = " + res.getStatus());
270 Assert.assertEquals(res.getStatus(),
271 Response.Status.FORBIDDEN.getStatusCode(), "expected "
272 + Response.Status.FORBIDDEN.getStatusCode());
273 res.releaseConnection();
277 * Creates the collection object instance without password.
279 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
280 public void createWithoutPassword(String testName) {
281 if (logger.isDebugEnabled()) {
282 logger.debug(testBanner(testName));
284 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
285 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
286 collectionObjectClient.setAuth(true, user, true, "", false);
287 String identifier = BaseServiceTest.createIdentifier();
288 MultipartOutput multipart = createCollectionObjectInstance(
289 collectionObjectClient.getCommonPartName(), identifier);
290 ClientResponse<Response> res = collectionObjectClient.create(multipart);
291 if (logger.isDebugEnabled()) {
292 logger.debug(testName + ": status = " + res.getStatus());
294 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
295 res.releaseConnection();
299 * Creates the collection object with unknown user
301 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
302 public void createWithUnknownUser(String testName) {
303 if (logger.isDebugEnabled()) {
304 logger.debug(testBanner(testName));
306 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
307 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
308 String identifier = BaseServiceTest.createIdentifier();
309 MultipartOutput multipart = createCollectionObjectInstance(
310 collectionObjectClient.getCommonPartName(), identifier);
311 ClientResponse<Response> res = collectionObjectClient.create(multipart);
312 if (logger.isDebugEnabled()) {
313 logger.debug(testName + ": status = " + res.getStatus());
315 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
316 res.releaseConnection();
320 * Creates the collection object instance with incorrect password.
322 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
323 public void createWithIncorrectPassword(String testName) {
324 if (logger.isDebugEnabled()) {
325 logger.debug(testBanner(testName));
327 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
328 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
329 collectionObjectClient.setAuth(true, user, true, "bar", true);
330 String identifier = BaseServiceTest.createIdentifier();
331 MultipartOutput multipart = createCollectionObjectInstance(
332 collectionObjectClient.getCommonPartName(), identifier);
333 ClientResponse<Response> res = collectionObjectClient.create(multipart);
334 if (logger.isDebugEnabled()) {
335 logger.debug(testName + ": status = " + res.getStatus());
337 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
338 res.releaseConnection();
342 * Creates the collection object instance with incorrect user password.
344 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
345 public void createWithIncorrectUserPassword(String testName) {
346 if (logger.isDebugEnabled()) {
347 logger.debug(testBanner(testName));
349 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
350 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
351 String identifier = BaseServiceTest.createIdentifier();
352 MultipartOutput multipart = createCollectionObjectInstance(
353 collectionObjectClient.getCommonPartName(), identifier);
354 ClientResponse<Response> res = collectionObjectClient.create(multipart);
355 if (logger.isDebugEnabled()) {
356 logger.debug(testName + ": status = "
359 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
360 res.releaseConnection();
364 * Creates the collection object instance with incorrect user password.
366 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
367 public void createWithoutTenant(String testName) {
368 if (logger.isDebugEnabled()) {
369 logger.debug(testBanner(testName));
371 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
372 collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
373 String identifier = BaseServiceTest.createIdentifier();
374 MultipartOutput multipart = createCollectionObjectInstance(
375 collectionObjectClient.getCommonPartName(), identifier);
376 ClientResponse<Response> res = collectionObjectClient.create(multipart);
377 if (logger.isDebugEnabled()) {
378 logger.debug(testName + ": status = "
381 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
382 res.releaseConnection();
386 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
389 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
390 dependsOnMethods = {"create"})
391 public void delete(String testName) {
396 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
397 dependsOnMethods = {"create", "createWithInactiveAccount"})
398 public void deleteAccounts(String testName) throws Exception {
400 if (logger.isDebugEnabled()) {
401 logger.debug(testBanner(testName, CLASS_NAME));
405 AccountClient accountClient = new AccountClient();
406 // accountClient.setAuth(true, "test", true, "test", true);
407 // Submit the request to the service and store the response.
408 ClientResponse<Response> res = accountClient.delete(barneyAccountId);
409 int statusCode = res.getStatus();
410 if (logger.isDebugEnabled()) {
411 logger.debug(testName + ": barney status = " + statusCode);
413 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
414 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
416 res = accountClient.delete(georgeAccountId);
417 statusCode = res.getStatus();
418 if (logger.isDebugEnabled()) {
419 logger.debug(testName + ": george status = " + statusCode);
421 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
422 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
423 res.releaseConnection();
426 // ---------------------------------------------------------------
427 // Utility methods used by tests above
428 // ---------------------------------------------------------------
430 * Creates the collection object instance.
432 * @param commonPartName the common part name
433 * @param identifier the identifier
435 * @return the multipart output
437 private MultipartOutput createCollectionObjectInstance(
438 String commonPartName, String identifier) {
439 return createCollectionObjectInstance(commonPartName, "objectNumber-"
440 + identifier, "title-" + identifier);
444 * Creates the collection object instance.
446 * @param commonPartName the common part name
447 * @param objectNumber the object number
448 * @param title the object title
450 * @return the multipart output
452 private MultipartOutput createCollectionObjectInstance(
453 String commonPartName, String objectNumber, String title) {
454 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
456 collectionObject.setObjectNumber(objectNumber);
457 collectionObject.setTitle(title);
458 MultipartOutput multipart =
459 CollectionObjectFactory.createCollectionObjectInstance(
460 commonPartName, collectionObject, null, null);
462 if (logger.isDebugEnabled()) {
463 logger.debug("to be created, collectionobject common ",
464 collectionObject, CollectionobjectsCommon.class);
469 private AccountsCommon createAccountInstance(String screenName,
470 String passwd, String email, String tenantId, boolean invalidTenant) {
472 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
473 screenName, passwd, email, tenantId,
474 true, invalidTenant, true, true);
476 List<AccountTenant> atl = account.getTenants();
478 //disable 2nd tenant till tenant identification is in effect
479 //on the service side for 1-n user-tenants
480 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
481 // at2.setId(UUID.randomUUID().toString());
482 // at2.setName("collectionspace.org");
484 // account.setTenants(atl);
486 if (logger.isDebugEnabled()) {
487 logger.debug("to be created, account common");
488 logger.debug(objectAsXmlString(account,
489 AccountsCommon.class));
496 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
499 public void createList(String testName) throws Exception {
500 //FIXME: Should this test really be empty? If so, please comment accordingly.
504 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
507 public void createWithEmptyEntityBody(String testName) throws Exception {
508 //FIXME: Should this test really be empty? If so, please comment accordingly.
512 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
515 public void createWithMalformedXml(String testName) throws Exception {
516 //FIXME: Should this test really be empty? If so, please comment accordingly.
520 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
523 public void createWithWrongXmlSchema(String testName) throws Exception {
524 //FIXME: Should this test really be empty? If so, please comment accordingly.
528 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
531 public void read(String testName) throws Exception {
532 //FIXME: Should this test really be empty? If so, please comment accordingly.
536 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
539 public void readNonExistent(String testName) throws Exception {
540 //FIXME: Should this test really be empty? If so, please comment accordingly.
544 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
547 public void readList(String testName) throws Exception {
548 //FIXME: Should this test really be empty? If so, please comment accordingly.
552 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
555 public void update(String testName) throws Exception {
556 //FIXME: Should this test really be empty? If so, please comment accordingly.
560 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
563 public void updateWithEmptyEntityBody(String testName) throws Exception {
564 //FIXME: Should this test really be empty? If so, please comment accordingly.
568 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
571 public void updateWithMalformedXml(String testName) throws Exception {
572 //FIXME: Should this test really be empty? If so, please comment accordingly.
576 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
579 public void updateWithWrongXmlSchema(String testName) throws Exception {
580 //FIXME: Should this test really be empty? If so, please comment accordingly.
584 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
587 public void updateNonExistent(String testName) throws Exception {
588 //FIXME: Should this test really be empty? If so, please comment accordingly.
592 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
595 public void deleteNonExistent(String testName) throws Exception {
596 //FIXME: Should this test really be empty? If so, please comment accordingly.