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 javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import org.jboss.resteasy.client.ClientResponse;
28 import org.testng.Assert;
29 import org.testng.annotations.Test;
31 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
32 import org.collectionspace.services.client.CollectionObjectClient;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.test.AbstractServiceTest;
35 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
36 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
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 extends AbstractServiceTest {
48 final String SERVICE_PATH_COMPONENT = "collectionobjects";
49 private String knownResourceId = null;
50 final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
53 public String getServicePathComponent() {
54 // @TODO Determine if it is possible to obtain this
55 // value programmatically.
57 // We set this in an annotation in the CollectionObjectProxy
58 // interface, for instance. We also set service-specific
59 // constants in each service module, which might also
61 return SERVICE_PATH_COMPONENT;
66 public void create() {
67 String identifier = this.createIdentifier();
68 MultipartOutput multipart = createCollectionObjectInstance(identifier);
69 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
70 if(!collectionObjectClient.isServerSecure()){
71 logger.warn("set -Dcspace.server.secure=true to run security tests");
74 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
75 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
76 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
78 collectionObjectClient.setupHttpClient();
79 collectionObjectClient.setProxy();
81 logger.error("create: caught " + e.getMessage());
84 ClientResponse<Response> res = collectionObjectClient.create(multipart);
85 verbose("create: status = " + res.getStatus());
86 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode(),
87 "expected " + Response.Status.CREATED.getStatusCode());
89 // Store the ID returned from this create operation for additional tests below.
90 knownResourceId = extractId(res);
93 @Test(dependsOnMethods = {"create"})
94 public void createWithoutUser() {
95 String identifier = this.createIdentifier();
96 MultipartOutput multipart = createCollectionObjectInstance(identifier);
97 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
98 if(!collectionObjectClient.isServerSecure()){
99 logger.warn("set -Dcspace.server.secure=true to run security tests");
102 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
103 collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
104 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
106 collectionObjectClient.setupHttpClient();
107 collectionObjectClient.setProxy();
109 logger.error("createWithoutUser: caught " + e.getMessage());
112 ClientResponse<Response> res = collectionObjectClient.create(multipart);
113 verbose("createWithoutUser: status = " + res.getStatus());
114 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
115 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
118 @Test(dependsOnMethods = {"createWithoutUser"})
119 public void createWithoutPassword() {
120 String identifier = this.createIdentifier();
121 MultipartOutput multipart = createCollectionObjectInstance(identifier);
122 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
123 if(!collectionObjectClient.isServerSecure()){
124 logger.warn("set -Dcspace.server.secure=true to run security tests");
127 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
128 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
129 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
131 collectionObjectClient.setupHttpClient();
132 collectionObjectClient.setProxy();
134 logger.error("createWithoutPassword: caught " + e.getMessage());
137 ClientResponse<Response> res = collectionObjectClient.create(multipart);
138 verbose("createWithoutPassword: status = " + res.getStatus());
139 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
140 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
143 @Test(dependsOnMethods = {"createWithoutPassword"})
144 public void createWithIncorrectPassword() {
145 String identifier = this.createIdentifier();
146 MultipartOutput multipart = createCollectionObjectInstance(identifier);
147 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
148 if(!collectionObjectClient.isServerSecure()){
149 logger.warn("set -Dcspace.server.secure=true to run security tests");
152 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
153 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
154 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
156 collectionObjectClient.setupHttpClient();
157 collectionObjectClient.setProxy();
159 logger.error("createWithIncorrectPassword: caught " + e.getMessage());
162 ClientResponse<Response> res = collectionObjectClient.create(multipart);
163 verbose("createWithIncorrectPassword: status = " + res.getStatus());
164 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
165 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
168 @Test(dependsOnMethods = {"createWithoutPassword"})
169 public void createWithoutUserPassword() {
170 String identifier = this.createIdentifier();
171 MultipartOutput multipart = createCollectionObjectInstance(identifier);
172 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
173 if(!collectionObjectClient.isServerSecure()){
174 logger.warn("set -Dcspace.server.secure=true to run security tests");
177 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
178 collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
179 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
181 collectionObjectClient.setupHttpClient();
182 collectionObjectClient.setProxy();
184 logger.error("createWithoutUserPassword: caught " + e.getMessage());
187 ClientResponse<Response> res = collectionObjectClient.create(multipart);
188 verbose("createWithoutUserPassword: status = " + res.getStatus());
189 Assert.assertEquals(res.getStatus(), Response.Status.FORBIDDEN.getStatusCode(),
190 "expected " + Response.Status.FORBIDDEN.getStatusCode());
193 @Test(dependsOnMethods = {"createWithoutPassword"})
194 public void createWithIncorrectUserPassword() {
195 String identifier = this.createIdentifier();
196 MultipartOutput multipart = createCollectionObjectInstance(identifier);
197 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
198 if(!collectionObjectClient.isServerSecure()){
199 logger.warn("set -Dcspace.server.secure=true to run security tests");
202 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
203 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "foo");
204 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
206 collectionObjectClient.setupHttpClient();
207 collectionObjectClient.setProxy();
209 logger.error("createWithIncorrectUserPassword: caught " + e.getMessage());
212 ClientResponse<Response> res = collectionObjectClient.create(multipart);
213 verbose("createWithIncorrectUserPassword: status = " + res.getStatus());
214 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(),
215 "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
219 @Test(dependsOnMethods = {"createWithIncorrectUserPassword"})
220 public void delete() {
221 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
222 collectionObjectClient = new CollectionObjectClient();
223 if(!collectionObjectClient.isServerSecure()){
224 logger.warn("set -Dcspace.server.secure=true to run security tests");
227 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY, "true");
228 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY, "test");
229 collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY, "test");
231 collectionObjectClient.setupHttpClient();
232 collectionObjectClient.setProxy();
234 logger.error("deleteCollectionObject: caught " + e.getMessage());
237 verbose("Calling deleteCollectionObject:" + knownResourceId);
238 ClientResponse<Response> res = collectionObjectClient.delete(knownResourceId);
239 verbose("deleteCollectionObject: status = " + res.getStatus());
240 Assert.assertEquals(res.getStatus(), Response.Status.OK.getStatusCode(),
241 "expected " + Response.Status.OK.getStatusCode());
244 // ---------------------------------------------------------------
245 // Utility methods used by tests above
246 // ---------------------------------------------------------------
247 private MultipartOutput createCollectionObjectInstance(String identifier) {
248 return createCollectionObjectInstance("objectNumber-" + identifier,
249 "objectName-" + identifier);
252 private MultipartOutput createCollectionObjectInstance(String objectNumber, String objectName) {
253 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
255 collectionObject.setObjectNumber(objectNumber);
256 collectionObject.setObjectName(objectName);
257 MultipartOutput multipart = new MultipartOutput();
258 OutputPart commonPart = multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
259 commonPart.getHeaders().add("label", getCommonPartName());
261 verbose("to be created, collectionobject common ", collectionObject, CollectionobjectsCommon.class);
266 public void createList() {
270 public void createWithEmptyEntityBody() {
274 public void createWithMalformedXml() {
278 public void createWithWrongXmlSchema() {
286 public void readNonExistent() {
290 public void readList() {
294 public void update() {
298 public void updateWithEmptyEntityBody() {
302 public void updateWithMalformedXml() {
306 public void updateWithWrongXmlSchema() {
310 public void updateNonExistent() {
314 public void deleteNonExistent() {