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