2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 package org.collectionspace.services.authentication.client;
25 import java.util.ArrayList;
26 import javax.ws.rs.core.MultivaluedMap;
27 import javax.ws.rs.core.Response;
28 import javax.xml.bind.JAXBContext;
29 import javax.xml.bind.Marshaller;
30 import org.jboss.resteasy.client.ClientResponse;
31 import org.testng.Assert;
32 import org.testng.annotations.Test;
34 import org.collectionspace.services.collectionobject.CollectionObject;
35 import org.collectionspace.services.client.CollectionObjectClient;
36 import org.collectionspace.services.client.CollectionSpaceClient;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * AuthenticationServiceTest uses CollectionObject service to test authentication
43 * $LastChangedRevision: 434 $
44 * $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue, 28 Jul 2009) $
46 public class AuthenticationServiceTest {
48 private String knownCollectionObjectId = null;
49 final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
52 public void auth_createCollectionObject() {
53 if(!isServerSecure()){
56 String identifier = this.createIdentifier();
57 CollectionObject collectionObject = createCollectionObject(identifier);
58 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
59 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
60 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
61 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
63 collectionObjectClient.setupHttpClient();
64 collectionObjectClient.setProxy();
66 logger.error("auth_createCollectionObject: caught " + e.getMessage());
69 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
70 verbose("auth_createCollectionObject: status = " + res.getStatus());
71 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode(),
72 "expected " + Response.Status.CREATED.getStatusCode());
74 // Store the ID returned from this create operation for additional tests below.
75 knownCollectionObjectId = extractId(res);
78 @Test(dependsOnMethods = {"auth_createCollectionObject"})
79 public void auth_createCollectionObjectWithoutUser() {
80 if(!isServerSecure()){
83 String identifier = this.createIdentifier();
84 CollectionObject collectionObject = createCollectionObject(identifier);
85 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
86 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
87 collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
88 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
90 collectionObjectClient.setupHttpClient();
91 collectionObjectClient.setProxy();
93 logger.error("auth_createCollectionObjectWithoutUser: caught " + e.getMessage());
96 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
97 verbose("auth_createCollectionObjectWithoutUser: status = " + res.getStatus());
98 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
99 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
102 @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutUser"})
103 public void auth_createCollectionObjectWithoutPassword() {
104 if(!isServerSecure()){
105 logger.warn("set -Dcspace.server.secure=true to run security tests");
108 String identifier = this.createIdentifier();
109 CollectionObject collectionObject = createCollectionObject(identifier);
110 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
111 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
112 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
113 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
115 collectionObjectClient.setupHttpClient();
116 collectionObjectClient.setProxy();
118 logger.error("auth_createCollectionObjectWithoutPassword: caught " + e.getMessage());
121 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
122 verbose("auth_createCollectionObjectWithoutPassword: status = " + res.getStatus());
123 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
124 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
127 @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"})
128 public void auth_createCollectionObjectWithIncorrectPassword() {
129 if(!isServerSecure()){
130 logger.warn("set -Dcspace.server.secure=true to run security tests");
133 String identifier = this.createIdentifier();
134 CollectionObject collectionObject = createCollectionObject(identifier);
135 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
136 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
137 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
138 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
140 collectionObjectClient.setupHttpClient();
141 collectionObjectClient.setProxy();
143 logger.error("auth_createCollectionObjectWithIncorrectPassword: caught " + e.getMessage());
146 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
147 verbose("auth_createCollectionObjectWithIncorrectPassword: status = " + res.getStatus());
148 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
149 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
152 @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"})
153 public void auth_createCollectionObjectWithoutUserPassword() {
154 if(!isServerSecure()){
155 logger.warn("set -Dcspace.server.secure=true to run security tests");
158 String identifier = this.createIdentifier();
159 CollectionObject collectionObject = createCollectionObject(identifier);
160 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
161 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
162 collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
163 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
165 collectionObjectClient.setupHttpClient();
166 collectionObjectClient.setProxy();
168 logger.error("auth_createCollectionObjectWithoutUserPassword: caught " + e.getMessage());
171 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
172 verbose("auth_createCollectionObjectWithoutUserPassword: status = " + res.getStatus());
173 Assert.assertEquals(res.getStatus(), Response.Status.FORBIDDEN.getStatusCode(),
174 "expected " + Response.Status.FORBIDDEN.getStatusCode());
177 @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"})
178 public void auth_createCollectionObjectWithIncorrectUserPassword() {
179 if(!isServerSecure()){
180 logger.warn("set -Dcspace.server.secure=true to run security tests");
183 String identifier = this.createIdentifier();
184 CollectionObject collectionObject = createCollectionObject(identifier);
185 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
186 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
187 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "foo");
188 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
190 collectionObjectClient.setupHttpClient();
191 collectionObjectClient.setProxy();
193 logger.error("auth_createCollectionObjectWithIncorrectUserPassword: caught " + e.getMessage());
196 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
197 verbose("auth_createCollectionObjectWithIncorrectUserPassword: status = " + res.getStatus());
198 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
199 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
203 @Test(dependsOnMethods = {"auth_createCollectionObjectWithIncorrectUserPassword"})
204 public void auth_deleteCollectionObject() {
205 if(!isServerSecure()){
206 logger.warn("set -Dcspace.server.secure=true to run security tests");
209 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
210 collectionObjectClient = new CollectionObjectClient();
211 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
212 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
213 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
215 collectionObjectClient.setupHttpClient();
216 collectionObjectClient.setProxy();
218 logger.error("auth_deleteCollectionObject: caught " + e.getMessage());
221 verbose("Calling deleteCollectionObject:" + knownCollectionObjectId);
222 ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(knownCollectionObjectId);
223 verbose("auth_deleteCollectionObject: status = " + res.getStatus());
224 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode(),
225 "expected " + Response.Status.OK.getStatusCode());
228 // ---------------------------------------------------------------
229 // Utility methods used by tests above
230 // ---------------------------------------------------------------
231 private CollectionObject createCollectionObject(String identifier) {
232 CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
233 "objectName-" + identifier);
235 return collectionObject;
238 private CollectionObject createCollectionObject(String objectNumber, String objectName) {
239 CollectionObject collectionObject = new CollectionObject();
241 collectionObject.setObjectNumber(objectNumber);
242 collectionObject.setObjectName(objectName);
244 return collectionObject;
247 private String extractId(ClientResponse<Response> res) {
248 MultivaluedMap mvm = res.getMetadata();
249 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
250 verbose("extractId:uri=" + uri);
251 String[] segments = uri.split("/");
252 String id = segments[segments.length - 1];
257 private void verbose(String msg) {
258 if(logger.isInfoEnabled()){
263 private void verbose(String msg, Object o, Class clazz) {
266 JAXBContext jc = JAXBContext.newInstance(clazz);
267 Marshaller m = jc.createMarshaller();
268 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
270 m.marshal(o, System.out);
276 private String createIdentifier() {
277 long identifier = System.currentTimeMillis();
278 return Long.toString(identifier);
281 private boolean isServerSecure() {
282 return Boolean.getBoolean("cspace.server.secure");