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;
26 import javax.ws.rs.core.Response;
27 import org.jboss.resteasy.client.ClientResponse;
29 import org.testng.Assert;
30 import org.testng.annotations.Test;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import org.collectionspace.services.account.AccountTenant;
35 import org.collectionspace.services.account.AccountsCommon;
36 import org.collectionspace.services.account.Status;
37 import org.collectionspace.services.client.AccountClient;
38 import org.collectionspace.services.client.AccountFactory;
39 import org.collectionspace.services.client.CollectionObjectClient;
40 import org.collectionspace.services.client.CollectionObjectFactory;
41 import org.collectionspace.services.client.CollectionSpaceClient;
42 import org.collectionspace.services.client.PoxPayloadOut;
43 import org.collectionspace.services.client.test.BaseServiceTest;
44 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
45 import org.collectionspace.services.collectionobject.TitleGroup;
46 import org.collectionspace.services.collectionobject.TitleGroupList;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
50 * AuthenticationServiceTest uses CollectionObject service to test
53 * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
56 public class AuthenticationServiceTest extends BaseServiceTest<AbstractCommonList> {
58 private final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
59 /** The known resource id. */
60 private String barneyAccountId = null; //active
61 private String georgeAccountId = null; //inactive
63 private final String CLASS_NAME = AuthenticationServiceTest.class.getName();
66 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
69 protected String getServicePathComponent() {
70 // no need to return anything but null since no auth resources are
72 throw new UnsupportedOperationException();
76 protected String getServiceName() {
77 // no need to return anything but null since no auth resources are
79 throw new UnsupportedOperationException();
83 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
86 protected CollectionSpaceClient getClientInstance() {
87 return new AccountClient();
91 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
94 protected AbstractCommonList getCommonList(Response response) {
95 throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
98 @Test(dataProvider = "testName")
99 public void createActiveAccount(String testName) throws Exception {
100 // Perform setup, such as initializing the type of service request
101 // (e.g. CREATE, DELETE), its valid and expected status codes, and
102 // its associated HTTP method name (e.g. POST, DELETE).
105 AccountClient accountClient = new AccountClient();
106 // This should not be needed - the auth is already set up
107 //accountClient.setAuth(true, "test", true, "test", true);
109 // Submit the request to the service and store the response.
110 AccountsCommon account =
111 createAccountInstance("barney", "barney08", "barney@dinoland.com",
112 accountClient.getTenantId(), false);
113 ClientResponse<Response> res = accountClient.create(account);
115 int statusCode = res.getStatus();
117 if (logger.isDebugEnabled()) {
118 logger.debug(testName + ": barney status = " + statusCode);
120 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
121 invalidStatusCodeMessage(testRequestType, statusCode));
122 Assert.assertEquals(statusCode, testExpectedStatusCode);
124 // Store the ID returned from this create operation
125 // for additional tests below.
126 barneyAccountId = extractId(res);
127 if (logger.isDebugEnabled()) {
128 logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
136 @Test(dataProvider = "testName")
137 public void createInactiveAccount(String testName) throws Exception {
141 AccountClient accountClient = new AccountClient();
142 // This should not be needed - the auth is already set up
143 //accountClient.setAuth(true, "test", true, "test", true);
145 // Submit the request to the service and store the response.
146 AccountsCommon account =
147 createAccountInstance("george", "george08", "george@curiousland.com",
148 accountClient.getTenantId(), false);
149 ClientResponse<Response> res = accountClient.create(account);
150 int statusCode = res.getStatus();
152 if (logger.isDebugEnabled()) {
153 logger.debug(testName + ": george status = " + statusCode);
155 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
156 invalidStatusCodeMessage(testRequestType, statusCode));
157 Assert.assertEquals(statusCode, testExpectedStatusCode);
159 // Store the ID returned from this create operation
160 // for additional tests below.
161 georgeAccountId = extractId(res);
162 if (logger.isDebugEnabled()) {
163 logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
165 res.releaseConnection();
168 account.setStatus(Status.INACTIVE);
169 if (logger.isDebugEnabled()) {
170 logger.debug(testName + ":updated object");
171 logger.debug(objectAsXmlString(account,
172 AccountsCommon.class));
175 // Submit the request to the service and store the response.
176 ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
177 statusCode = res1.getStatus();
178 // Check the status code of the response: does it match the expected response(s)?
179 if (logger.isDebugEnabled()) {
180 logger.debug(testName + ": status = " + statusCode);
182 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
183 invalidStatusCodeMessage(testRequestType, statusCode));
184 Assert.assertEquals(statusCode, testExpectedStatusCode);
185 res1.releaseConnection();
190 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
192 @Test(dataProvider = "testName",
193 dependsOnMethods = {"createActiveAccount"})
194 public void create(String testName) {
197 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
198 collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
199 String identifier = createIdentifier();
200 PoxPayloadOut multipart = createCollectionObjectInstance(
201 collectionObjectClient.getCommonPartName(), identifier);
202 Response res = collectionObjectClient.create(multipart);
204 if (logger.isDebugEnabled()) {
205 logger.debug("create: status = " + res.getStatus());
207 //so it does not have any permissions out-of-the-box to create a
209 Assert.assertEquals(res.getStatus(),
210 Response.Status.FORBIDDEN.getStatusCode(), "expected "
211 + Response.Status.FORBIDDEN.getStatusCode());
213 // Store the ID returned from this create operation for additional tests
221 @Test(dataProvider = "testName",
222 dependsOnMethods = {"createActiveAccount"})
223 public void createWithoutAuthn(String testName) {
226 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
227 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
228 String pass = collectionObjectClient.getProperty(collectionObjectClient.PASSWORD_PROPERTY);
229 collectionObjectClient.setAuth(false, user, true, pass, true);
230 String identifier = createIdentifier();
231 PoxPayloadOut multipart = createCollectionObjectInstance(
232 collectionObjectClient.getCommonPartName(), identifier);
233 Response res = collectionObjectClient.create(multipart);
235 if (logger.isDebugEnabled()) {
236 logger.debug("create: status = " + res.getStatus());
238 Assert.assertEquals(res.getStatus(),
239 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
240 + Response.Status.UNAUTHORIZED.getStatusCode());
246 @Test(dataProvider = "testName",
247 dependsOnMethods = {"createInactiveAccount"})
248 public void createWithInactiveAccount(String testName) {
249 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
250 collectionObjectClient.setAuth(true, "george", true, "george08", true);
251 String identifier = createIdentifier();
252 PoxPayloadOut multipart = createCollectionObjectInstance(
253 collectionObjectClient.getCommonPartName(), identifier);
255 Response res = collectionObjectClient.create(multipart);
257 if (logger.isDebugEnabled()) {
258 logger.debug(testName + ": status = " + res.getStatus());
260 Assert.assertEquals(res.getStatus(),
261 Response.Status.FORBIDDEN.getStatusCode(), "expected "
262 + Response.Status.FORBIDDEN.getStatusCode());
269 * Creates the collection object instance without password.
271 @Test(dataProvider = "testName",
272 dependsOnMethods = {"createActiveAccount"})
273 public void createWithoutPassword(String testName) {
274 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
275 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
276 collectionObjectClient.setAuth(true, user, true, "", false);
277 String identifier = createIdentifier();
278 PoxPayloadOut multipart = createCollectionObjectInstance(
279 collectionObjectClient.getCommonPartName(), identifier);
280 Response res = collectionObjectClient.create(multipart);
282 if (logger.isDebugEnabled()) {
283 logger.debug(testName + ": status = " + res.getStatus());
285 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
292 * Creates the collection object with unknown user
294 @Test(dataProvider = "testName",
295 dependsOnMethods = {"createActiveAccount"})
296 public void createWithUnknownUser(String testName) {
297 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
298 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
299 String identifier = createIdentifier();
300 PoxPayloadOut multipart = createCollectionObjectInstance(
301 collectionObjectClient.getCommonPartName(), identifier);
302 Response res = collectionObjectClient.create(multipart);
304 if (logger.isDebugEnabled()) {
305 logger.debug(testName + ": status = " + res.getStatus());
307 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
314 * Creates the collection object instance with incorrect password.
316 @Test(dataProvider = "testName",
317 dependsOnMethods = {"createActiveAccount"})
318 public void createWithIncorrectPassword(String testName) {
319 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
320 String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
321 collectionObjectClient.setAuth(true, user, true, "bar", true);
322 String identifier = createIdentifier();
323 PoxPayloadOut multipart = createCollectionObjectInstance(
324 collectionObjectClient.getCommonPartName(), identifier);
325 Response res = collectionObjectClient.create(multipart);
327 if (logger.isDebugEnabled()) {
328 logger.debug(testName + ": status = " + res.getStatus());
330 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
337 * Creates the collection object instance with incorrect user password.
339 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
340 public void createWithIncorrectUserPassword(String testName) {
341 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
342 collectionObjectClient.setAuth(true, "foo", true, "bar", true);
343 String identifier = createIdentifier();
344 PoxPayloadOut multipart = createCollectionObjectInstance(
345 collectionObjectClient.getCommonPartName(), identifier);
346 Response res = collectionObjectClient.create(multipart);
348 if (logger.isDebugEnabled()) {
349 logger.debug(testName + ": status = " + res.getStatus());
351 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
358 * Creates the collection object instance with incorrect user password.
360 @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
361 public void createWithoutTenant(String testName) {
362 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
363 collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
364 String identifier = createIdentifier();
365 PoxPayloadOut multipart = createCollectionObjectInstance(
366 collectionObjectClient.getCommonPartName(), identifier);
367 Response res = collectionObjectClient.create(multipart);
369 if (logger.isDebugEnabled()) {
370 logger.debug(testName + ": status = " + res.getStatus());
372 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
378 @Test(dataProvider = "testName",
379 dependsOnMethods = {"create", "createWithInactiveAccount"})
380 public void deleteAccounts(String testName) throws Exception {
383 AccountClient accountClient = new AccountClient();
384 // accountClient.setAuth(true, "test", true, "test", true);
385 // Submit the request to the service and store the response.
387 Response res = accountClient.delete(barneyAccountId);
390 statusCode = res.getStatus();
391 if (logger.isDebugEnabled()) {
392 logger.debug(testName + ": barney status = " + statusCode);
394 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
395 invalidStatusCodeMessage(testRequestType, statusCode));
400 res = accountClient.delete(georgeAccountId);
402 statusCode = res.getStatus();
403 if (logger.isDebugEnabled()) {
404 logger.debug(testName + ": george status = " + statusCode);
406 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
407 invalidStatusCodeMessage(testRequestType, statusCode));
413 // ---------------------------------------------------------------
414 // Utility methods used by tests above
415 // ---------------------------------------------------------------
417 * Creates the collection object instance.
419 * @param commonPartName the common part name
420 * @param identifier the identifier
422 * @return the multipart output
424 private PoxPayloadOut createCollectionObjectInstance(
425 String commonPartName, String identifier) {
426 return createCollectionObjectInstance(commonPartName, "objectNumber-"
427 + identifier, "title-" + identifier);
431 * Creates the collection object instance.
433 * @param commonPartName the common part name
434 * @param objectNumber the object number
435 * @param title the object title
437 * @return the multipart output
439 private PoxPayloadOut createCollectionObjectInstance(
440 String commonPartName, String objectNumber, String title) {
441 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
443 collectionObject.setObjectNumber(objectNumber);
444 TitleGroupList titleGroupList = new TitleGroupList();
445 List<TitleGroup> titleGroups = titleGroupList.getTitleGroup();
446 TitleGroup titleGroup = new TitleGroup();
447 titleGroup.setTitle(title);
448 titleGroups.add(titleGroup);
449 collectionObject.setTitleGroupList(titleGroupList);
450 PoxPayloadOut multipart =
451 CollectionObjectFactory.createCollectionObjectInstance(
452 commonPartName, collectionObject, null, null);
454 if (logger.isDebugEnabled()) {
455 logger.debug("to be created, collectionobject common ",
456 collectionObject, CollectionobjectsCommon.class);
461 private AccountsCommon createAccountInstance(String screenName,
462 String passwd, String email, String tenantId, boolean invalidTenant) {
464 AccountsCommon account = AccountFactory.createAccountInstance(screenName,
465 screenName, passwd, email, tenantId,
466 true, invalidTenant, true, true);
468 List<AccountTenant> atl = account.getTenants();
470 //disable 2nd tenant till tenant identification is in effect
471 //on the service side for 1-n user-tenants
472 // AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
473 // at2.setId(UUID.randomUUID().toString());
474 // at2.setName("collectionspace.org");
476 // account.setTenants(atl);
478 if (logger.isDebugEnabled()) {
479 logger.debug("to be created, account common");
480 logger.debug(objectAsXmlString(account,
481 AccountsCommon.class));
488 protected Class<AbstractCommonList> getCommonListType() {
489 // TODO Auto-generated method stub