]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9e3d0b1bfd9bafecbb38a44ff58e0d52f6d1e83a
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c)) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.security.client.test;
24
25 import java.util.List;
26
27 import javax.ws.rs.core.Response;
28
29 //import org.apache.commons.codec.binary.Base64;
30 import org.jboss.resteasy.client.ClientResponse;
31 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
32
33 import org.testng.Assert;
34 import org.testng.annotations.Test;
35
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;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * AuthenticationServiceTest uses CollectionObject service to test
54  * authentication
55  * 
56  * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
57  * 28 Jul 2009) $
58  */
59 public class AuthenticationServiceTest extends AbstractServiceTestImpl {
60
61     /** The known resource id. */
62     private String knownResourceId = null;
63     private String barneyAccountId = null; //active
64     private String georgeAccountId = null; //inactive
65     
66     /** The logger. */
67     private final String CLASS_NAME = AuthenticationServiceTest.class.getName();
68     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
69     
70     /* (non-Javadoc)
71      * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
72      */
73     @Override
74     protected String getServicePathComponent() {
75         // no need to return anything but null since no auth resources are
76         // accessed
77         return null;
78     }
79
80     /* (non-Javadoc)
81      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
82      */
83     @Override
84     protected CollectionSpaceClient getClientInstance() {
85         return new AccountClient();
86     }
87
88     /* (non-Javadoc)
89      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
90      */
91     @Override
92     protected AbstractCommonList getAbstractCommonList(
93             ClientResponse<AbstractCommonList> response) {
94         throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
95     }
96
97     @Test(dataProvider = "testName")
98     @Override
99     public void readPaginatedList(String testName) throws Exception {
100         // Test not supported.
101     }
102
103     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
104     public void createActiveAccount(String testName) throws Exception {
105         
106         if (logger.isDebugEnabled()) {
107             logger.debug(testBanner(testName, CLASS_NAME));
108         }
109         // Perform setup, such as initializing the type of service request
110         // (e.g. CREATE, DELETE), its valid and expected status codes, and
111         // its associated HTTP method name (e.g. POST, DELETE).
112         setupCreate();
113         
114         AccountClient accountClient = new AccountClient();
115         accountClient.setAuth(true, "test", true, "test", true);
116
117         // Submit the request to the service and store the response.
118         AccountsCommon account =
119                 createAccountInstance("barney", "barney08", "barney@dinoland.com", false);
120         ClientResponse<Response> res = accountClient.create(account);
121         int statusCode = res.getStatus();
122
123         if (logger.isDebugEnabled()) {
124             logger.debug(testName + ": barney status = " + statusCode);
125         }
126         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
127                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
129
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);
135         }
136         res.releaseConnection();
137
138     }
139
140     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
141     public void createInactiveAccount(String testName) throws Exception {
142         
143         if (logger.isDebugEnabled()) {
144             logger.debug(testBanner(testName, CLASS_NAME));
145         }
146         // Perform setup.
147         setupCreate();
148         
149         AccountClient accountClient = new AccountClient();
150         accountClient.setAuth(true, "test", true, "test", true);
151
152         // Submit the request to the service and store the response.
153         AccountsCommon account =
154                 createAccountInstance("george", "george08", "george@curiousland.com", false);
155         ClientResponse<Response> res = accountClient.create(account);
156         int statusCode = res.getStatus();
157
158         if (logger.isDebugEnabled()) {
159             logger.debug(testName + ": george status = " + statusCode);
160         }
161         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
162                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
163         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
164
165         // Store the ID returned from this create operation
166         // for additional tests below.
167         georgeAccountId = extractId(res);
168         if (logger.isDebugEnabled()) {
169             logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
170         }
171         res.releaseConnection();
172         //deactivate
173         setupUpdate();
174         account.setStatus(Status.INACTIVE);
175         if (logger.isDebugEnabled()) {
176             logger.debug(testName + ":updated object");
177             logger.debug(objectAsXmlString(account,
178                     AccountsCommon.class));
179         }
180
181         // Submit the request to the service and store the response.
182         ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
183         statusCode = res1.getStatus();
184         // Check the status code of the response: does it match the expected response(s)?
185         if (logger.isDebugEnabled()) {
186             logger.debug(testName + ": status = " + statusCode);
187         }
188         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
189                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
190         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
191         res1.releaseConnection();
192     }
193
194
195     /* (non-Javadoc)
196      * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
197      */
198     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
199     dependsOnMethods = {"createActiveAccount"})
200     @Override
201     public void create(String testName) {
202         if (logger.isDebugEnabled()) {
203             logger.debug(testBanner(testName, CLASS_NAME));
204         }
205         setupCreate();
206         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
207         collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
208         String identifier = BaseServiceTest.createIdentifier();
209         MultipartOutput multipart = createCollectionObjectInstance(
210                 collectionObjectClient.getCommonPartName(), identifier);
211         ClientResponse<Response> res = collectionObjectClient.create(multipart);
212         if (logger.isDebugEnabled()) {
213             logger.debug("create: status = " + res.getStatus());
214         }
215         //so it does not have any permissions out-of-the-box to create a
216         //collection object
217         Assert.assertEquals(res.getStatus(),
218                 Response.Status.FORBIDDEN.getStatusCode(), "expected "
219                 + Response.Status.FORBIDDEN.getStatusCode());
220
221         // Store the ID returned from this create operation for additional tests
222         // below.
223         res.releaseConnection();
224
225     }
226
227     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
228     dependsOnMethods = {"createActiveAccount"})
229     public void createWithoutAuthn(String testName) {
230         if (logger.isDebugEnabled()) {
231             logger.debug(testBanner(testName, CLASS_NAME));
232         }
233         setupCreate();
234         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
235         collectionObjectClient.setAuth(false, "test", true, "test", true);
236         String identifier = BaseServiceTest.createIdentifier();
237         MultipartOutput multipart = createCollectionObjectInstance(
238                 collectionObjectClient.getCommonPartName(), identifier);
239         ClientResponse<Response> res = collectionObjectClient.create(multipart);
240         if (logger.isDebugEnabled()) {
241             logger.debug("create: status = " + res.getStatus());
242         }
243         Assert.assertEquals(res.getStatus(),
244                 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
245                 + Response.Status.UNAUTHORIZED.getStatusCode());
246         res.releaseConnection();
247
248     }
249
250     @Test(dataProvider = "testName", dependsOnMethods = {"createInactiveAccount"})
251     public void createWithInactiveAccount(String testName) {
252         if (logger.isDebugEnabled()) {
253             logger.debug(testBanner(testName));
254         }
255         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
256         collectionObjectClient.setAuth(true, "george", true, "george08", true);
257         String identifier = BaseServiceTest.createIdentifier();
258         MultipartOutput multipart = createCollectionObjectInstance(
259                 collectionObjectClient.getCommonPartName(), identifier);
260
261         ClientResponse<Response> res = collectionObjectClient.create(multipart);
262         if (logger.isDebugEnabled()) {
263             logger.debug(testName + ": status = " + res.getStatus());
264         }
265         Assert.assertEquals(res.getStatus(),
266                 Response.Status.FORBIDDEN.getStatusCode(), "expected "
267                 + Response.Status.FORBIDDEN.getStatusCode());
268         res.releaseConnection();
269     }
270
271     /**
272      * Creates the collection object instance without password.
273      */
274     @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
275     public void createWithoutPassword(String testName) {
276         if (logger.isDebugEnabled()) {
277             logger.debug(testBanner(testName));
278         }
279         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
280         collectionObjectClient.setAuth(true, "test", true, "", false);
281         String identifier = BaseServiceTest.createIdentifier();
282         MultipartOutput multipart = createCollectionObjectInstance(
283                 collectionObjectClient.getCommonPartName(), identifier);
284         ClientResponse<Response> res = collectionObjectClient.create(multipart);
285         if (logger.isDebugEnabled()) {
286             logger.debug(testName + ": status = " + res.getStatus());
287         }
288         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
289         res.releaseConnection();
290     }
291
292     /**
293      * Creates the collection object with unknown user
294      */
295     @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
296     public void createWithUnknownUser(String testName) {
297         if (logger.isDebugEnabled()) {
298             logger.debug(testBanner(testName));
299         }
300         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
301         collectionObjectClient.setAuth(true, "foo", true, "bar", true);
302         String identifier = BaseServiceTest.createIdentifier();
303         MultipartOutput multipart = createCollectionObjectInstance(
304                 collectionObjectClient.getCommonPartName(), identifier);
305         ClientResponse<Response> res = collectionObjectClient.create(multipart);
306         if (logger.isDebugEnabled()) {
307             logger.debug(testName + ": status = " + res.getStatus());
308         }
309         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
310         res.releaseConnection();
311     }
312
313     /**
314      * Creates the collection object instance with incorrect password.
315      */
316     @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
317     public void createWithIncorrectPassword(String testName) {
318         if (logger.isDebugEnabled()) {
319             logger.debug(testBanner(testName));
320         }
321         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
322         collectionObjectClient.setAuth(true, "test", true, "bar", true);
323         String identifier = BaseServiceTest.createIdentifier();
324         MultipartOutput multipart = createCollectionObjectInstance(
325                 collectionObjectClient.getCommonPartName(), identifier);
326         ClientResponse<Response> res = collectionObjectClient.create(multipart);
327         if (logger.isDebugEnabled()) {
328             logger.debug(testName + ": status = " + res.getStatus());
329         }
330         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
331         res.releaseConnection();
332     }
333
334     /**
335      * Creates the collection object instance with incorrect user password.
336      */
337     @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
338     public void createWithIncorrectUserPassword(String testName) {
339         if (logger.isDebugEnabled()) {
340             logger.debug(testBanner(testName));
341         }
342         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
343         collectionObjectClient.setAuth(true, "foo", true, "bar", true);
344         String identifier = BaseServiceTest.createIdentifier();
345         MultipartOutput multipart = createCollectionObjectInstance(
346                 collectionObjectClient.getCommonPartName(), identifier);
347         ClientResponse<Response> res = collectionObjectClient.create(multipart);
348         if (logger.isDebugEnabled()) {
349             logger.debug(testName + ": status = "
350                     + res.getStatus());
351         }
352         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
353         res.releaseConnection();
354     }
355
356     /**
357      * Creates the collection object instance with incorrect user password.
358      */
359     @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
360     public void createWithoutTenant(String testName) {
361         if (logger.isDebugEnabled()) {
362             logger.debug(testBanner(testName));
363         }
364         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
365         collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
366         String identifier = BaseServiceTest.createIdentifier();
367         MultipartOutput multipart = createCollectionObjectInstance(
368                 collectionObjectClient.getCommonPartName(), identifier);
369         ClientResponse<Response> res = collectionObjectClient.create(multipart);
370         if (logger.isDebugEnabled()) {
371             logger.debug(testName + ": status = "
372                     + res.getStatus());
373         }
374         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
375         res.releaseConnection();
376     }
377
378     /* (non-Javadoc)
379      * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
380      */
381     @Override
382     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
383     dependsOnMethods = {"create"})
384     public void delete(String testName) {
385         setupDelete();
386
387     }
388
389     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
390     dependsOnMethods = {"create", "createWithInactiveAccount"})
391     public void deleteAccounts(String testName) throws Exception {
392
393         if (logger.isDebugEnabled()) {
394             logger.debug(testBanner(testName, CLASS_NAME));
395         }
396         // Perform setup.
397         setupDelete();
398         AccountClient accountClient = new AccountClient();
399         accountClient.setAuth(true, "test", true, "test", true);
400         // Submit the request to the service and store the response.
401         ClientResponse<Response> res = accountClient.delete(barneyAccountId);
402         int statusCode = res.getStatus();
403         if (logger.isDebugEnabled()) {
404             logger.debug(testName + ": barney status = " + statusCode);
405         }
406         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
407                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
408
409         res = accountClient.delete(georgeAccountId);
410         statusCode = res.getStatus();
411         if (logger.isDebugEnabled()) {
412             logger.debug(testName + ": george status = " + statusCode);
413         }
414         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
415                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
416         res.releaseConnection();
417     }
418
419     // ---------------------------------------------------------------
420     // Utility methods used by tests above
421     // ---------------------------------------------------------------
422     /**
423      * Creates the collection object instance.
424      *
425      * @param commonPartName the common part name
426      * @param identifier the identifier
427      *
428      * @return the multipart output
429      */
430     private MultipartOutput createCollectionObjectInstance(
431             String commonPartName, String identifier) {
432         return createCollectionObjectInstance(commonPartName, "objectNumber-"
433                 + identifier, "objectName-" + identifier);
434     }
435
436     /**
437      * Creates the collection object instance.
438      *
439      * @param commonPartName the common part name
440      * @param objectNumber the object number
441      * @param objectName the object name
442      *
443      * @return the multipart output
444      */
445     private MultipartOutput createCollectionObjectInstance(
446             String commonPartName, String objectNumber, String objectName) {
447         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
448
449         collectionObject.setObjectNumber(objectNumber);
450         collectionObject.setObjectName(objectName);
451         MultipartOutput multipart =
452                 CollectionObjectFactory.createCollectionObjectInstance(
453                 commonPartName, collectionObject, null, null);
454
455         if (logger.isDebugEnabled()) {
456             logger.debug("to be created, collectionobject common ",
457                     collectionObject, CollectionobjectsCommon.class);
458         }
459         return multipart;
460     }
461
462     private AccountsCommon createAccountInstance(String screenName,
463             String passwd, String email, boolean invalidTenant) {
464
465         AccountsCommon account = AccountFactory.createAccountInstance(screenName,
466                 screenName, passwd, email,
467                 true, true, invalidTenant, true, true);
468
469         List<AccountTenant> atl = account.getTenants();
470
471         //disable 2nd tenant till tenant identification is in effect
472         //on the service side for 1-n user-tenants
473 //        AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
474 //        at2.setId(UUID.randomUUID().toString());
475 //        at2.setName("collectionspace.org");
476 //        atl.add(at2);
477 //        account.setTenants(atl);
478
479         if (logger.isDebugEnabled()) {
480             logger.debug("to be created, account common");
481             logger.debug(objectAsXmlString(account,
482                     AccountsCommon.class));
483         }
484         return account;
485
486     }
487
488     /* (non-Javadoc)
489      * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
490      */
491     @Override
492     public void createList(String testName) throws Exception {
493         //FIXME: Should this test really be empty?  If so, please comment accordingly.
494     }
495
496     /* (non-Javadoc)
497      * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
498      */
499     @Override
500     public void createWithEmptyEntityBody(String testName) throws Exception {
501         //FIXME: Should this test really be empty?  If so, please comment accordingly.
502     }
503
504     /* (non-Javadoc)
505      * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
506      */
507     @Override
508     public void createWithMalformedXml(String testName) throws Exception {
509         //FIXME: Should this test really be empty?  If so, please comment accordingly.
510     }
511
512     /* (non-Javadoc)
513      * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
514      */
515     @Override
516     public void createWithWrongXmlSchema(String testName) throws Exception {
517         //FIXME: Should this test really be empty?  If so, please comment accordingly.
518     }
519
520     /* (non-Javadoc)
521      * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
522      */
523     @Override
524     public void read(String testName) throws Exception {
525         //FIXME: Should this test really be empty?  If so, please comment accordingly.
526     }
527
528     /* (non-Javadoc)
529      * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
530      */
531     @Override
532     public void readNonExistent(String testName) throws Exception {
533         //FIXME: Should this test really be empty?  If so, please comment accordingly.
534     }
535
536     /* (non-Javadoc)
537      * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
538      */
539     @Override
540     public void readList(String testName) throws Exception {
541         //FIXME: Should this test really be empty?  If so, please comment accordingly.
542     }
543
544     /* (non-Javadoc)
545      * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
546      */
547     @Override
548     public void update(String testName) throws Exception {
549         //FIXME: Should this test really be empty?  If so, please comment accordingly.
550     }
551
552     /* (non-Javadoc)
553      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
554      */
555     @Override
556     public void updateWithEmptyEntityBody(String testName) throws Exception {
557         //FIXME: Should this test really be empty?  If so, please comment accordingly.
558     }
559
560     /* (non-Javadoc)
561      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
562      */
563     @Override
564     public void updateWithMalformedXml(String testName) throws Exception {
565         //FIXME: Should this test really be empty?  If so, please comment accordingly.
566     }
567
568     /* (non-Javadoc)
569      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
570      */
571     @Override
572     public void updateWithWrongXmlSchema(String testName) throws Exception {
573         //FIXME: Should this test really be empty?  If so, please comment accordingly.
574     }
575
576     /* (non-Javadoc)
577      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
578      */
579     @Override
580     public void updateNonExistent(String testName) throws Exception {
581         //FIXME: Should this test really be empty?  If so, please comment accordingly.
582     }
583
584     /* (non-Javadoc)
585      * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
586      */
587     @Override
588     public void deleteNonExistent(String testName) throws Exception {
589         //FIXME: Should this test really be empty?  If so, please comment accordingly.
590     }
591 }