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