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 */ 016 017package org.fcrepo.client.integration; 018 019import static javax.ws.rs.core.Response.Status.CREATED; 020import static javax.ws.rs.core.Response.Status.FORBIDDEN; 021import static javax.ws.rs.core.Response.Status.NO_CONTENT; 022import static javax.ws.rs.core.Response.Status.OK; 023import static org.fcrepo.client.TestUtils.TEXT_TURTLE; 024import static org.fcrepo.client.TestUtils.rdfTtl; 025import static org.fcrepo.client.TestUtils.sparqlUpdate; 026import static org.junit.Assert.assertEquals; 027 028import java.io.ByteArrayInputStream; 029import java.io.InputStream; 030import java.net.URI; 031 032import org.apache.commons.io.IOUtils; 033import org.fcrepo.client.FcrepoClient; 034import org.fcrepo.client.FcrepoResponse; 035import org.junit.Test; 036 037/** 038 * @author mohideen 039 */ 040public class FcrepoAuthenticationIT extends AbstractResourceIT { 041 042 protected static FcrepoClient authClient; 043 044 public FcrepoAuthenticationIT() throws Exception { 045 super(); 046 047 client = FcrepoClient.client().build(); 048 authClient = FcrepoClient.client() 049 .credentials("fedoraAdmin", "password") 050 .authScope("localhost") 051 .build(); 052 } 053 054 @Test 055 public void testAuthUserCanPut() throws Exception { 056 057 final InputStream body = new ByteArrayInputStream(rdfTtl.getBytes()); 058 final FcrepoResponse response = authClient.put(new URI(serverAddress + "testobj1")) 059 .body(body, TEXT_TURTLE) 060 .perform(); 061 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 062 final int status = response.getStatusCode(); 063 assertEquals("Didn't get a CREATED response! Got content:\n" + content, 064 CREATED.getStatusCode(), status); 065 } 066 067 @Test 068 public void testUnAuthUserCannotPut() throws Exception { 069 final InputStream body = new ByteArrayInputStream(rdfTtl.getBytes()); 070 final FcrepoResponse response = client.put(new URI(serverAddress + "testobj2")) 071 .body(body, TEXT_TURTLE) 072 .perform(); 073 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 074 final int status = response.getStatusCode(); 075 assertEquals("Unauthenticated user should be forbidden! Got content:\n" + content, 076 FORBIDDEN.getStatusCode(), status); 077 } 078 079 @Test 080 public void testAuthUserCanPatch() throws Exception { 081 final InputStream body = new ByteArrayInputStream(sparqlUpdate.getBytes()); 082 final FcrepoResponse response = authClient.patch(new URI(serverAddress + "testobj1")) 083 .body(body) 084 .perform(); 085 final int status = response.getStatusCode(); 086 assertEquals("Didn't get a successful PATCH response! Got content:\n", 087 NO_CONTENT.getStatusCode(), status); 088 } 089 090 @Test 091 public void testUnAuthUserCannotPatch() throws Exception { 092 final InputStream body = new ByteArrayInputStream(sparqlUpdate.getBytes()); 093 final FcrepoResponse response = client.patch(new URI(serverAddress + "testobj1")) 094 .body(body) 095 .perform(); 096 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 097 final int status = response.getStatusCode(); 098 assertEquals("Unauthenticated user should be forbidden! Got content:\n" + content, 099 FORBIDDEN.getStatusCode(), status); 100 } 101 102 @Test 103 public void testAuthUserCanPost() throws Exception { 104 final InputStream body = new ByteArrayInputStream(rdfTtl.getBytes()); 105 final FcrepoResponse response = authClient.post(new URI(serverAddress)) 106 .body(body, TEXT_TURTLE) 107 .perform(); 108 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 109 final int status = response.getStatusCode(); 110 assertEquals("Didn't get a CREATED response! Got content:\n" + content, 111 CREATED.getStatusCode(), status); 112 } 113 114 @Test 115 public void testUnAuthUserCannotPost() throws Exception { 116 final InputStream body = new ByteArrayInputStream(rdfTtl.getBytes()); 117 final FcrepoResponse response = client.post(new URI(serverAddress)) 118 .body(body, TEXT_TURTLE) 119 .perform(); 120 final String content = IOUtils.toString(response.getBody(), "UTF-8"); 121 final int status = response.getStatusCode(); 122 assertEquals("Unauthenticated user should be forbidden! Got content:\n" + content, 123 FORBIDDEN.getStatusCode(), status); 124 } 125 126 @Test 127 public void testAuthUserCanGet() 128 throws Exception { 129 final FcrepoResponse response = authClient.get(new URI(serverAddress)).perform(); 130 final int status = response.getStatusCode(); 131 assertEquals("Authenticated user can not read root!", OK 132 .getStatusCode(), status); 133 } 134 135 @Test 136 public void testUnAuthUserCannotGet() 137 throws Exception { 138 final FcrepoResponse response = client.get(new URI(serverAddress)).perform(); 139 final int status = response.getStatusCode(); 140 assertEquals("Unauthenticated user should be forbidden!", FORBIDDEN 141 .getStatusCode(), status); 142 } 143}