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.auth.webac; 019 020import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_CONTROL_VALUE; 021import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_READ_VALUE; 022import static org.fcrepo.auth.webac.URIConstants.WEBAC_MODE_WRITE_VALUE; 023import static org.junit.Assert.assertFalse; 024import static org.junit.Assert.assertTrue; 025import static org.modeshape.jcr.ModeShapePermissions.ADD_NODE; 026import static org.modeshape.jcr.ModeShapePermissions.MODIFY_ACCESS_CONTROL; 027import static org.modeshape.jcr.ModeShapePermissions.READ; 028import static org.modeshape.jcr.ModeShapePermissions.READ_ACCESS_CONTROL; 029import static org.modeshape.jcr.ModeShapePermissions.REGISTER_NAMESPACE; 030import static org.modeshape.jcr.ModeShapePermissions.REMOVE; 031import static org.modeshape.jcr.ModeShapePermissions.REMOVE_CHILD_NODES; 032import static org.modeshape.jcr.ModeShapePermissions.SET_PROPERTY; 033 034import java.util.HashSet; 035import java.util.Set; 036 037import org.junit.Before; 038import org.junit.Test; 039import org.junit.runner.RunWith; 040import org.mockito.Mock; 041import org.mockito.runners.MockitoJUnitRunner; 042import org.modeshape.jcr.api.Session; 043 044/** 045 * Unit test for the WebAC Authorization Delegate. 046 * 047 * @author Peter Eichman 048 * @since Aug 24, 2015 049 */ 050@RunWith(MockitoJUnitRunner.class) 051public class WebACAuthorizationDelegateTest { 052 053 private WebACAuthorizationDelegate webacAD; 054 055 @Mock 056 private Session mockSession; 057 058 @Before 059 public void setUp() { 060 webacAD = new WebACAuthorizationDelegate(); 061 } 062 063 @Test 064 public void testCanRead1() { 065 final String[] actions = {READ}; 066 final Set<String> roles = new HashSet<>(); 067 roles.add(WEBAC_MODE_READ_VALUE); 068 069 assertTrue(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 070 } 071 072 @Test 073 public void testCanRead2() { 074 final String[] actions = {READ}; 075 final Set<String> roles = new HashSet<>(); 076 roles.add(WEBAC_MODE_READ_VALUE); 077 roles.add(WEBAC_MODE_WRITE_VALUE); 078 079 assertTrue(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 080 } 081 082 @Test 083 public void testCannotRead1() { 084 final String[] actions = {READ, SET_PROPERTY}; 085 final Set<String> roles = new HashSet<>(); 086 roles.add(WEBAC_MODE_WRITE_VALUE); 087 088 assertFalse(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 089 } 090 091 @Test 092 public void testCannotRead2() { 093 final String[] actions = {READ}; 094 final Set<String> roles = new HashSet<>(); 095 096 assertFalse(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 097 } 098 099 @Test 100 public void testCanWrite1() { 101 final String[] actions = {ADD_NODE}; 102 final Set<String> roles = new HashSet<>(); 103 roles.add(WEBAC_MODE_WRITE_VALUE); 104 105 assertTrue(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 106 } 107 108 @Test 109 public void testCanWrite3() { 110 final String[] actions = {REMOVE}; 111 final Set<String> roles = new HashSet<>(); 112 roles.add(WEBAC_MODE_WRITE_VALUE); 113 114 assertTrue(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 115 } 116 117 @Test 118 public void testCanWrite4() { 119 final String[] actions = {REMOVE_CHILD_NODES}; 120 final Set<String> roles = new HashSet<>(); 121 roles.add(WEBAC_MODE_WRITE_VALUE); 122 123 assertTrue(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 124 } 125 126 @Test 127 public void testCanWrite5() { 128 final String[] actions = {SET_PROPERTY}; 129 final Set<String> roles = new HashSet<>(); 130 roles.add(WEBAC_MODE_WRITE_VALUE); 131 132 assertTrue(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 133 } 134 135 @Test 136 public void testCanWrite6() { 137 final String[] actions = {SET_PROPERTY, ADD_NODE, REMOVE, REMOVE_CHILD_NODES}; 138 final Set<String> roles = new HashSet<>(); 139 roles.add(WEBAC_MODE_WRITE_VALUE); 140 141 assertTrue(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 142 } 143 144 @Test 145 public void testCannotWrite1() { 146 final String[] actions = {READ, SET_PROPERTY, ADD_NODE, REGISTER_NAMESPACE, REMOVE, REMOVE_CHILD_NODES}; 147 final Set<String> roles = new HashSet<>(); 148 roles.add(WEBAC_MODE_WRITE_VALUE); 149 150 assertFalse(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 151 } 152 153 @Test 154 public void testCannotWrite2() { 155 final String[] actions = {SET_PROPERTY}; 156 final Set<String> roles = new HashSet<>(); 157 158 assertFalse(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 159 } 160 161 @Test 162 public void testCanReadAcl1() { 163 final String[] actions = {READ_ACCESS_CONTROL}; 164 final Set<String> roles = new HashSet<>(); 165 roles.add(WEBAC_MODE_CONTROL_VALUE); 166 167 assertTrue(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 168 } 169 170 @Test 171 public void testCannotReadAcl1() { 172 final String[] actions = {READ_ACCESS_CONTROL}; 173 final Set<String> roles = new HashSet<>(); 174 roles.add(WEBAC_MODE_READ_VALUE); 175 176 assertFalse(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 177 } 178 179 @Test 180 public void testCanWriteAcl1() { 181 final String[] actions = {MODIFY_ACCESS_CONTROL}; 182 final Set<String> roles = new HashSet<>(); 183 roles.add(WEBAC_MODE_CONTROL_VALUE); 184 185 assertTrue(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 186 } 187 188 @Test 189 public void testCannotWriteAcl1() { 190 final String[] actions = {MODIFY_ACCESS_CONTROL}; 191 final Set<String> roles = new HashSet<>(); 192 roles.add(WEBAC_MODE_WRITE_VALUE); 193 194 assertFalse(webacAD.rolesHavePermission(mockSession, "/fake/path", actions, roles)); 195 } 196}