001/** 002 * Copyright 2015 DuraSpace, Inc. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.fcrepo.client.integration; 017 018import static javax.ws.rs.core.Response.Status.CREATED; 019import static javax.ws.rs.core.Response.Status.FORBIDDEN; 020import static javax.ws.rs.core.Response.Status.NO_CONTENT; 021import static javax.ws.rs.core.Response.Status.OK; 022import static org.fcrepo.client.TestUtils.TEXT_TURTLE; 023import static org.fcrepo.client.TestUtils.rdfTtl; 024import static org.fcrepo.client.TestUtils.sparqlUpdate; 025import static org.junit.Assert.assertEquals; 026import static org.slf4j.LoggerFactory.getLogger; 027 028import java.io.ByteArrayInputStream; 029import java.io.InputStream; 030import java.net.URI; 031import java.util.concurrent.TimeUnit; 032 033import org.fcrepo.client.FcrepoClient; 034import org.fcrepo.client.FcrepoResponse; 035 036import org.apache.commons.io.IOUtils; 037import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; 038import org.junit.Test; 039import org.junit.runner.RunWith; 040import org.slf4j.Logger; 041import org.springframework.test.context.ContextConfiguration; 042import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 043 044/** 045 * @author mohideen 046 */ 047@RunWith(SpringJUnit4ClassRunner.class) 048@ContextConfiguration("/spring-test/test-container.xml") 049public class FcrepoAuthenticationIT { 050 051 private static Logger logger = getLogger(FcrepoAuthenticationIT.class); 052 053 protected static final int SERVER_PORT = Integer.parseInt(System 054 .getProperty("fcrepo.dynamic.test.port", "8080")); 055 056 protected static final String HOSTNAME = "localhost"; 057 058 protected static final String serverAddress = "http://" + HOSTNAME + ":" + 059 SERVER_PORT + "/rest/"; 060 061 protected final PoolingHttpClientConnectionManager connectionManager = 062 new PoolingHttpClientConnectionManager(); 063 064 protected static FcrepoClient client; 065 066 protected static FcrepoClient authClient; 067 068 public FcrepoAuthenticationIT() throws Exception { 069 connectionManager.setMaxTotal(Integer.MAX_VALUE); 070 connectionManager.setDefaultMaxPerRoute(20); 071 connectionManager.closeIdleConnections(3, TimeUnit.SECONDS); 072 client = new FcrepoClient(null, null, null, false); 073 authClient = new FcrepoClient("fedoraAdmin", "password", "localhost", false); 074 } 075 076 @Test 077 public void testAuthUserCanPut() throws Exception { 078 079 final InputStream body = new ByteArrayInputStream(rdfTtl.getBytes()); 080 final FcrepoResponse response = authClient.put(new URI(serverAddress + "testobj1"), body, TEXT_TURTLE); 081 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 082 final int status = response.getStatusCode(); 083 assertEquals("Didn't get a CREATED response! Got content:\n" + content, 084 CREATED.getStatusCode(), status); 085 } 086 087 @Test 088 public void testUnAuthUserCannotPut() throws Exception { 089 final InputStream body = new ByteArrayInputStream(rdfTtl.getBytes()); 090 final FcrepoResponse response = client.put(new URI(serverAddress + "testobj2"), body, TEXT_TURTLE); 091 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 092 final int status = response.getStatusCode(); 093 assertEquals("Unauthenticated user should be forbidden! Got content:\n" + content, 094 FORBIDDEN.getStatusCode(), status); 095 } 096 097 @Test 098 public void testAuthUserCanPatch() throws Exception { 099 final InputStream body = new ByteArrayInputStream(sparqlUpdate.getBytes()); 100 final FcrepoResponse response = authClient.patch(new URI(serverAddress + "testobj1"), body); 101 final int status = response.getStatusCode(); 102 assertEquals("Didn't get a successful PATCH response! Got content:\n", 103 NO_CONTENT.getStatusCode(), status); 104 } 105 106 @Test 107 public void testUnAuthUserCannotPatch() throws Exception { 108 final InputStream body = new ByteArrayInputStream(sparqlUpdate.getBytes()); 109 final FcrepoResponse response = client.patch(new URI(serverAddress + "testobj1"), body); 110 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 111 final int status = response.getStatusCode(); 112 assertEquals("Unauthenticated user should be forbidden! Got content:\n" + content, 113 FORBIDDEN.getStatusCode(), status); 114 } 115 116 @Test 117 public void testAuthUserCanPost() throws Exception { 118 final InputStream body = new ByteArrayInputStream(rdfTtl.getBytes()); 119 final FcrepoResponse response = authClient.post(new URI(serverAddress), body, TEXT_TURTLE); 120 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 121 final int status = response.getStatusCode(); 122 assertEquals("Didn't get a CREATED response! Got content:\n" + content, 123 CREATED.getStatusCode(), status); 124 } 125 126 @Test 127 public void testUnAuthUserCannotPost() throws Exception { 128 final InputStream body = new ByteArrayInputStream(rdfTtl.getBytes()); 129 final FcrepoResponse response = client.post(new URI(serverAddress), body, TEXT_TURTLE); 130 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 131 final int status = response.getStatusCode(); 132 assertEquals("Unauthenticated user should be forbidden! Got content:\n" + content, 133 FORBIDDEN.getStatusCode(), status); 134 } 135 136 @Test 137 public void testAuthUserCanGet() 138 throws Exception { 139 final FcrepoResponse response = authClient.get(new URI(serverAddress), null, null); 140 final int status = response.getStatusCode(); 141 assertEquals("Authenticated user can not read root!", OK 142 .getStatusCode(), status); 143 } 144 145 @Test 146 public void testUnAuthUserCannotGet() 147 throws Exception { 148 final FcrepoResponse response = client.get(new URI(serverAddress), null, null); 149 final int status = response.getStatusCode(); 150 assertEquals("Unauthenticated user should be forbidden!", FORBIDDEN 151 .getStatusCode(), status); 152 } 153}