]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fc3341251a3d340d5b51caf070c117477fc2c1bf
[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 import javax.ws.rs.core.Response;
27 import org.jboss.resteasy.client.ClientResponse;
28
29 import org.testng.Assert;
30 import org.testng.annotations.Test;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
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;
48
49 /**
50  * AuthenticationServiceTest uses CollectionObject service to test
51  * authentication
52  * 
53  * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
54  * 28 Jul 2009) $
55  */
56 public class AuthenticationServiceTest extends BaseServiceTest<AbstractCommonList> {
57
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
62     /** The logger. */
63     private final String CLASS_NAME = AuthenticationServiceTest.class.getName();
64
65     /* (non-Javadoc)
66      * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
67      */
68     @Override
69     protected String getServicePathComponent() {
70         // no need to return anything but null since no auth resources are
71         // accessed
72         throw new UnsupportedOperationException();
73     }
74
75         @Override
76         protected String getServiceName() {
77         // no need to return anything but null since no auth resources are
78         // accessed
79         throw new UnsupportedOperationException();
80         }
81         
82     /* (non-Javadoc)
83      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
84      */
85     @Override
86     protected CollectionSpaceClient getClientInstance() {
87         return new AccountClient();
88     }
89
90     /* (non-Javadoc)
91      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
92      */
93     @Override
94     protected AbstractCommonList getCommonList(
95             ClientResponse<AbstractCommonList> response) {
96         throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
97     }
98
99     @Test(dataProvider = "testName")
100     public void createActiveAccount(String testName) throws Exception {
101         // Perform setup, such as initializing the type of service request
102         // (e.g. CREATE, DELETE), its valid and expected status codes, and
103         // its associated HTTP method name (e.g. POST, DELETE).
104         setupCreate();
105
106         AccountClient accountClient = new AccountClient();
107         // This should not be needed - the auth is already set up
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",
113                 accountClient.getTenantId(), false);
114         ClientResponse<Response> res = accountClient.create(account);
115         int statusCode = res.getStatus();
116
117         if (logger.isDebugEnabled()) {
118             logger.debug(testName + ": barney status = " + statusCode);
119         }
120         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
121                 invalidStatusCodeMessage(testRequestType, statusCode));
122         Assert.assertEquals(statusCode, testExpectedStatusCode);
123
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);
129         }
130         res.releaseConnection();
131
132     }
133
134     @Test(dataProvider = "testName")
135     public void createInactiveAccount(String testName) throws Exception {
136         // Perform setup.
137         setupCreate();
138
139         AccountClient accountClient = new AccountClient();
140         // This should not be needed - the auth is already set up
141         //accountClient.setAuth(true, "test", true, "test", true);
142
143         // Submit the request to the service and store the response.
144         AccountsCommon account =
145                 createAccountInstance("george", "george08", "george@curiousland.com",
146                 accountClient.getTenantId(), false);
147         ClientResponse<Response> res = accountClient.create(account);
148         int statusCode = res.getStatus();
149
150         if (logger.isDebugEnabled()) {
151             logger.debug(testName + ": george status = " + statusCode);
152         }
153         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
154                 invalidStatusCodeMessage(testRequestType, statusCode));
155         Assert.assertEquals(statusCode, testExpectedStatusCode);
156
157         // Store the ID returned from this create operation
158         // for additional tests below.
159         georgeAccountId = extractId(res);
160         if (logger.isDebugEnabled()) {
161             logger.debug(testName + ": georgeAccountId=" + georgeAccountId);
162         }
163         res.releaseConnection();
164         //deactivate
165         setupUpdate();
166         account.setStatus(Status.INACTIVE);
167         if (logger.isDebugEnabled()) {
168             logger.debug(testName + ":updated object");
169             logger.debug(objectAsXmlString(account,
170                     AccountsCommon.class));
171         }
172
173         // Submit the request to the service and store the response.
174         ClientResponse<AccountsCommon> res1 = accountClient.update(georgeAccountId, account);
175         statusCode = res1.getStatus();
176         // Check the status code of the response: does it match the expected response(s)?
177         if (logger.isDebugEnabled()) {
178             logger.debug(testName + ": status = " + statusCode);
179         }
180         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
181                 invalidStatusCodeMessage(testRequestType, statusCode));
182         Assert.assertEquals(statusCode, testExpectedStatusCode);
183         res1.releaseConnection();
184     }
185
186
187     /* (non-Javadoc)
188      * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
189      */
190     @Test(dataProvider = "testName",
191                 dependsOnMethods = {"createActiveAccount"})
192     public void create(String testName) {
193         setupCreate();
194
195         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
196         collectionObjectClient.setAuth(true, "barney", true, "barney08", true);
197         String identifier = createIdentifier();
198         PoxPayloadOut multipart = createCollectionObjectInstance(
199                 collectionObjectClient.getCommonPartName(), identifier);
200         ClientResponse<Response> res = collectionObjectClient.create(multipart);
201         if (logger.isDebugEnabled()) {
202             logger.debug("create: status = " + res.getStatus());
203         }
204         //so it does not have any permissions out-of-the-box to create a
205         //collection object
206         Assert.assertEquals(res.getStatus(),
207                 Response.Status.FORBIDDEN.getStatusCode(), "expected "
208                 + Response.Status.FORBIDDEN.getStatusCode());
209
210         // Store the ID returned from this create operation for additional tests
211         // below.
212         res.releaseConnection();
213
214     }
215
216     @Test(dataProvider = "testName",
217                 dependsOnMethods = {"createActiveAccount"})
218     public void createWithoutAuthn(String testName) {
219         setupCreate();
220         
221         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
222         String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
223         String pass = collectionObjectClient.getProperty(collectionObjectClient.PASSWORD_PROPERTY);
224         collectionObjectClient.setAuth(false, user, true, pass, true);
225         String identifier = createIdentifier();
226         PoxPayloadOut multipart = createCollectionObjectInstance(
227                 collectionObjectClient.getCommonPartName(), identifier);
228         ClientResponse<Response> res = collectionObjectClient.create(multipart);
229         if (logger.isDebugEnabled()) {
230             logger.debug("create: status = " + res.getStatus());
231         }
232         Assert.assertEquals(res.getStatus(),
233                 Response.Status.UNAUTHORIZED.getStatusCode(), "expected "
234                 + Response.Status.UNAUTHORIZED.getStatusCode());
235         res.releaseConnection();
236
237     }
238
239     @Test(dataProvider = "testName",
240                 dependsOnMethods = {"createInactiveAccount"})
241     public void createWithInactiveAccount(String testName) {
242         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
243         collectionObjectClient.setAuth(true, "george", true, "george08", true);
244         String identifier = createIdentifier();
245         PoxPayloadOut multipart = createCollectionObjectInstance(
246                 collectionObjectClient.getCommonPartName(), identifier);
247
248         ClientResponse<Response> res = collectionObjectClient.create(multipart);
249         if (logger.isDebugEnabled()) {
250             logger.debug(testName + ": status = " + res.getStatus());
251         }
252         Assert.assertEquals(res.getStatus(),
253                 Response.Status.FORBIDDEN.getStatusCode(), "expected "
254                 + Response.Status.FORBIDDEN.getStatusCode());
255         res.releaseConnection();
256     }
257
258     /**
259      * Creates the collection object instance without password.
260      */
261     @Test(dataProvider = "testName",
262                 dependsOnMethods = {"createActiveAccount"})
263     public void createWithoutPassword(String testName) {
264         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
265         String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
266         collectionObjectClient.setAuth(true, user, true, "", false);
267         String identifier = createIdentifier();
268         PoxPayloadOut multipart = createCollectionObjectInstance(
269                 collectionObjectClient.getCommonPartName(), identifier);
270         ClientResponse<Response> res = collectionObjectClient.create(multipart);
271         if (logger.isDebugEnabled()) {
272             logger.debug(testName + ": status = " + res.getStatus());
273         }
274         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
275         res.releaseConnection();
276     }
277
278     /**
279      * Creates the collection object with unknown user
280      */
281     @Test(dataProvider = "testName",
282                 dependsOnMethods = {"createActiveAccount"})
283     public void createWithUnknownUser(String testName) {
284         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
285         collectionObjectClient.setAuth(true, "foo", true, "bar", true);
286         String identifier = createIdentifier();
287         PoxPayloadOut multipart = createCollectionObjectInstance(
288                 collectionObjectClient.getCommonPartName(), identifier);
289         ClientResponse<Response> res = collectionObjectClient.create(multipart);
290         if (logger.isDebugEnabled()) {
291             logger.debug(testName + ": status = " + res.getStatus());
292         }
293         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
294         res.releaseConnection();
295     }
296
297     /**
298      * Creates the collection object instance with incorrect password.
299      */
300     @Test(dataProvider = "testName",
301                 dependsOnMethods = {"createActiveAccount"})
302     public void createWithIncorrectPassword(String testName) {
303         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
304         String user = collectionObjectClient.getProperty(collectionObjectClient.USER_PROPERTY);
305         collectionObjectClient.setAuth(true, user, true, "bar", true);
306         String identifier = createIdentifier();
307         PoxPayloadOut multipart = createCollectionObjectInstance(
308                 collectionObjectClient.getCommonPartName(), identifier);
309         ClientResponse<Response> res = collectionObjectClient.create(multipart);
310         if (logger.isDebugEnabled()) {
311             logger.debug(testName + ": status = " + res.getStatus());
312         }
313         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
314         res.releaseConnection();
315     }
316
317     /**
318      * Creates the collection object instance with incorrect user password.
319      */
320     @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
321     public void createWithIncorrectUserPassword(String testName) {
322         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
323         collectionObjectClient.setAuth(true, "foo", true, "bar", true);
324         String identifier = createIdentifier();
325         PoxPayloadOut multipart = createCollectionObjectInstance(
326                 collectionObjectClient.getCommonPartName(), identifier);
327         ClientResponse<Response> res = collectionObjectClient.create(multipart);
328         if (logger.isDebugEnabled()) {
329             logger.debug(testName + ": status = "
330                     + res.getStatus());
331         }
332         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
333         res.releaseConnection();
334     }
335
336     /**
337      * Creates the collection object instance with incorrect user password.
338      */
339     @Test(dataProvider = "testName", dependsOnMethods = {"createActiveAccount"})
340     public void createWithoutTenant(String testName) {
341         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
342         collectionObjectClient.setAuth(true, "babybop", true, "babybop09", true);
343         String identifier = createIdentifier();
344         PoxPayloadOut multipart = createCollectionObjectInstance(
345                 collectionObjectClient.getCommonPartName(), identifier);
346         ClientResponse<Response> res = collectionObjectClient.create(multipart);
347         if (logger.isDebugEnabled()) {
348             logger.debug(testName + ": status = "
349                     + res.getStatus());
350         }
351         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
352         res.releaseConnection();
353     }
354
355     @Test(dataProvider = "testName",
356                 dependsOnMethods = {"create", "createWithInactiveAccount"})
357     public void deleteAccounts(String testName) throws Exception {
358         // Perform setup.
359         setupDelete();
360         AccountClient accountClient = new AccountClient();
361         // accountClient.setAuth(true, "test", true, "test", true);
362         // Submit the request to the service and store the response.
363         ClientResponse<Response> res = accountClient.delete(barneyAccountId);
364         int statusCode = res.getStatus();
365         if (logger.isDebugEnabled()) {
366             logger.debug(testName + ": barney status = " + statusCode);
367         }
368         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
369                 invalidStatusCodeMessage(testRequestType, statusCode));
370
371         res = accountClient.delete(georgeAccountId);
372         statusCode = res.getStatus();
373         if (logger.isDebugEnabled()) {
374             logger.debug(testName + ": george status = " + statusCode);
375         }
376         Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
377                 invalidStatusCodeMessage(testRequestType, statusCode));
378         res.releaseConnection();
379     }
380     
381     // ---------------------------------------------------------------
382     // Utility methods used by tests above
383     // ---------------------------------------------------------------
384     /**
385      * Creates the collection object instance.
386      *
387      * @param commonPartName the common part name
388      * @param identifier the identifier
389      *
390      * @return the multipart output
391      */
392     private PoxPayloadOut createCollectionObjectInstance(
393             String commonPartName, String identifier) {
394         return createCollectionObjectInstance(commonPartName, "objectNumber-"
395                 + identifier, "title-" + identifier);
396     }
397
398     /**
399      * Creates the collection object instance.
400      *
401      * @param commonPartName the common part name
402      * @param objectNumber the object number
403      * @param title the object title
404      *
405      * @return the multipart output
406      */
407     private PoxPayloadOut createCollectionObjectInstance(
408             String commonPartName, String objectNumber, String title) {
409         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
410
411         collectionObject.setObjectNumber(objectNumber);
412         TitleGroupList titleGroupList = new TitleGroupList();
413         List<TitleGroup> titleGroups = titleGroupList.getTitleGroup();
414         TitleGroup titleGroup = new TitleGroup();
415         titleGroup.setTitle(title);
416         titleGroups.add(titleGroup);
417         collectionObject.setTitleGroupList(titleGroupList);
418         PoxPayloadOut multipart =
419                 CollectionObjectFactory.createCollectionObjectInstance(
420                 commonPartName, collectionObject, null, null);
421
422         if (logger.isDebugEnabled()) {
423             logger.debug("to be created, collectionobject common ",
424                     collectionObject, CollectionobjectsCommon.class);
425         }
426         return multipart;
427     }
428
429     private AccountsCommon createAccountInstance(String screenName,
430             String passwd, String email, String tenantId, boolean invalidTenant) {
431
432         AccountsCommon account = AccountFactory.createAccountInstance(screenName,
433                 screenName, passwd, email, tenantId,
434                 true, invalidTenant, true, true);
435
436         List<AccountTenant> atl = account.getTenants();
437
438         //disable 2nd tenant till tenant identification is in effect
439         //on the service side for 1-n user-tenants
440 //        AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
441 //        at2.setId(UUID.randomUUID().toString());
442 //        at2.setName("collectionspace.org");
443 //        atl.add(at2);
444 //        account.setTenants(atl);
445
446         if (logger.isDebugEnabled()) {
447             logger.debug("to be created, account common");
448             logger.debug(objectAsXmlString(account,
449                     AccountsCommon.class));
450         }
451         return account;
452
453     }
454
455         @Override
456         protected Class<AbstractCommonList> getCommonListType() {
457                 // TODO Auto-generated method stub
458                 return null;
459         }       
460 }