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 String identifier = this.createIdentifier();
54 CollectionObject collectionObject = createCollectionObject(identifier);
55 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
56 if(!collectionObjectClient.isServerSecure()){
57 logger.warn("set -Dcspace.server.secure=true to run security tests");
60 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
61 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
62 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
64 collectionObjectClient.setupHttpClient();
65 collectionObjectClient.setProxy();
67 logger.error("auth_createCollectionObject: caught " + e.getMessage());
70 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
71 verbose("auth_createCollectionObject: status = " + res.getStatus());
72 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode(),
73 "expected " + Response.Status.CREATED.getStatusCode());
75 // Store the ID returned from this create operation for additional tests below.
76 knownCollectionObjectId = extractId(res);
79 @Test(dependsOnMethods = {"auth_createCollectionObject"})
80 public void auth_createCollectionObjectWithoutUser() {
81 String identifier = this.createIdentifier();
82 CollectionObject collectionObject = createCollectionObject(identifier);
83 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
84 if(!collectionObjectClient.isServerSecure()){
85 logger.warn("set -Dcspace.server.secure=true to run security tests");
88 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
89 collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
90 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
92 collectionObjectClient.setupHttpClient();
93 collectionObjectClient.setProxy();
95 logger.error("auth_createCollectionObjectWithoutUser: caught " + e.getMessage());
98 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
99 verbose("auth_createCollectionObjectWithoutUser: status = " + res.getStatus());
100 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
101 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
104 @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutUser"})
105 public void auth_createCollectionObjectWithoutPassword() {
106 String identifier = this.createIdentifier();
107 CollectionObject collectionObject = createCollectionObject(identifier);
108 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
109 if(!collectionObjectClient.isServerSecure()){
110 logger.warn("set -Dcspace.server.secure=true to run security tests");
113 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
114 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
115 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
117 collectionObjectClient.setupHttpClient();
118 collectionObjectClient.setProxy();
120 logger.error("auth_createCollectionObjectWithoutPassword: caught " + e.getMessage());
123 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
124 verbose("auth_createCollectionObjectWithoutPassword: status = " + res.getStatus());
125 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
126 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
129 @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"})
130 public void auth_createCollectionObjectWithIncorrectPassword() {
131 String identifier = this.createIdentifier();
132 CollectionObject collectionObject = createCollectionObject(identifier);
133 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
134 if(!collectionObjectClient.isServerSecure()){
135 logger.warn("set -Dcspace.server.secure=true to run security tests");
138 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
139 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
140 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
142 collectionObjectClient.setupHttpClient();
143 collectionObjectClient.setProxy();
145 logger.error("auth_createCollectionObjectWithIncorrectPassword: caught " + e.getMessage());
148 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
149 verbose("auth_createCollectionObjectWithIncorrectPassword: status = " + res.getStatus());
150 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
151 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
154 @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"})
155 public void auth_createCollectionObjectWithoutUserPassword() {
156 String identifier = this.createIdentifier();
157 CollectionObject collectionObject = createCollectionObject(identifier);
158 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
159 if(!collectionObjectClient.isServerSecure()){
160 logger.warn("set -Dcspace.server.secure=true to run security tests");
163 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
164 collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
165 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
167 collectionObjectClient.setupHttpClient();
168 collectionObjectClient.setProxy();
170 logger.error("auth_createCollectionObjectWithoutUserPassword: caught " + e.getMessage());
173 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
174 verbose("auth_createCollectionObjectWithoutUserPassword: status = " + res.getStatus());
175 Assert.assertEquals(res.getStatus(), Response.Status.FORBIDDEN.getStatusCode(),
176 "expected " + Response.Status.FORBIDDEN.getStatusCode());
179 @Test(dependsOnMethods = {"auth_createCollectionObjectWithoutPassword"})
180 public void auth_createCollectionObjectWithIncorrectUserPassword() {
181 String identifier = this.createIdentifier();
182 CollectionObject collectionObject = createCollectionObject(identifier);
183 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
184 if(!collectionObjectClient.isServerSecure()){
185 logger.warn("set -Dcspace.server.secure=true to run security tests");
188 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
189 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "foo");
190 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
192 collectionObjectClient.setupHttpClient();
193 collectionObjectClient.setProxy();
195 logger.error("auth_createCollectionObjectWithIncorrectUserPassword: caught " + e.getMessage());
198 ClientResponse<Response> res = collectionObjectClient.createCollectionObject(collectionObject);
199 verbose("auth_createCollectionObjectWithIncorrectUserPassword: status = " + res.getStatus());
200 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
201 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
204 @Test(dependsOnMethods = {"auth_createCollectionObjectWithIncorrectUserPassword"})
205 public void auth_deleteCollectionObject() {
206 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
207 collectionObjectClient = new CollectionObjectClient();
208 if(!collectionObjectClient.isServerSecure()){
209 logger.warn("set -Dcspace.server.secure=true to run security tests");
212 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
213 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
214 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
216 collectionObjectClient.setupHttpClient();
217 collectionObjectClient.setProxy();
219 logger.error("auth_deleteCollectionObject: caught " + e.getMessage());
222 verbose("Calling deleteCollectionObject:" + knownCollectionObjectId);
223 ClientResponse<Response> res = collectionObjectClient.deleteCollectionObject(knownCollectionObjectId);
224 verbose("auth_deleteCollectionObject: status = " + res.getStatus());
225 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode(),
226 "expected " + Response.Status.OK.getStatusCode());
229 // ---------------------------------------------------------------
230 // Utility methods used by tests above
231 // ---------------------------------------------------------------
232 private CollectionObject createCollectionObject(String identifier) {
233 CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
234 "objectName-" + identifier);
236 return collectionObject;
239 private CollectionObject createCollectionObject(String objectNumber, String objectName) {
240 CollectionObject collectionObject = new CollectionObject();
242 collectionObject.setObjectNumber(objectNumber);
243 collectionObject.setObjectName(objectName);
245 return collectionObject;
248 private String extractId(ClientResponse<Response> res) {
249 MultivaluedMap mvm = res.getMetadata();
250 String uri = (String) ((ArrayList) mvm.get("Location")).get(0);
251 verbose("extractId:uri=" + uri);
252 String[] segments = uri.split("/");
253 String id = segments[segments.length - 1];
258 private void verbose(String msg) {
259 if(logger.isInfoEnabled()){
264 private void verbose(String msg, Object o, Class clazz) {
267 JAXBContext jc = JAXBContext.newInstance(clazz);
268 Marshaller m = jc.createMarshaller();
269 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
271 m.marshal(o, System.out);
277 private String createIdentifier() {
278 long identifier = System.currentTimeMillis();
279 return Long.toString(identifier);