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