|
| 1 | +/* |
| 2 | + * Copyright 2026 Martynas Jusevičius <martynas@atomgraph.com>. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.atomgraph.linkeddatahub.server.filter.request; |
| 17 | + |
| 18 | +import com.atomgraph.linkeddatahub.vocabulary.ACL; |
| 19 | +import com.atomgraph.linkeddatahub.vocabulary.LACL; |
| 20 | +import jakarta.ws.rs.HttpMethod; |
| 21 | +import org.apache.jena.rdf.model.Model; |
| 22 | +import org.apache.jena.rdf.model.ModelFactory; |
| 23 | +import org.apache.jena.rdf.model.Resource; |
| 24 | +import org.apache.jena.rdf.model.ResourceFactory; |
| 25 | +import org.apache.jena.vocabulary.RDF; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | + |
| 32 | +/** |
| 33 | + * Unit tests for the pure authorization logic in {@link AuthorizationFilter}: |
| 34 | + * the HTTP-method-to-access-mode contract, mode lookup, and the owner grant. |
| 35 | + * These methods do not touch the injected collaborators, so the filter is exercised directly. |
| 36 | + * |
| 37 | + * @author Martynas Jusevičius {@literal <martynas@atomgraph.com>} |
| 38 | + */ |
| 39 | +public class AuthorizationFilterTest |
| 40 | +{ |
| 41 | + |
| 42 | + private static final Resource ACCESS_TO = ResourceFactory.createResource("https://localhost/doc/"); |
| 43 | + private static final Resource AGENT = ResourceFactory.createResource("https://localhost/acl/agents/me/#this"); |
| 44 | + |
| 45 | + // HTTP method -> ACL access mode: a security-critical contract (a regression here silently changes what each verb requires) |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testReadMethodsRequireRead() |
| 49 | + { |
| 50 | + assertEquals(ACL.Read, AuthorizationFilter.ACCESS_MODES.get(HttpMethod.GET)); |
| 51 | + assertEquals(ACL.Read, AuthorizationFilter.ACCESS_MODES.get(HttpMethod.HEAD)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testPostRequiresAppend() |
| 56 | + { |
| 57 | + assertEquals(ACL.Append, AuthorizationFilter.ACCESS_MODES.get(HttpMethod.POST)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testWriteMethodsRequireWrite() |
| 62 | + { |
| 63 | + assertEquals(ACL.Write, AuthorizationFilter.ACCESS_MODES.get(HttpMethod.PUT)); |
| 64 | + assertEquals(ACL.Write, AuthorizationFilter.ACCESS_MODES.get(HttpMethod.DELETE)); |
| 65 | + assertEquals(ACL.Write, AuthorizationFilter.ACCESS_MODES.get(HttpMethod.PATCH)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testUnknownMethodHasNoAccessMode() |
| 70 | + { |
| 71 | + assertNull(AuthorizationFilter.ACCESS_MODES.get(HttpMethod.OPTIONS)); |
| 72 | + } |
| 73 | + |
| 74 | + // getAuthorizationByMode: find an authorization in the model that grants the requested mode |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testFindsAuthorizationGrantingMode() |
| 78 | + { |
| 79 | + Model model = ModelFactory.createDefaultModel(); |
| 80 | + Resource auth = model.createResource("https://localhost/acl/authorizations/1/#this"). |
| 81 | + addProperty(RDF.type, ACL.Authorization). |
| 82 | + addProperty(ACL.mode, ACL.Read). |
| 83 | + addProperty(ACL.mode, ACL.Append); |
| 84 | + |
| 85 | + assertEquals(auth, new AuthorizationFilter().getAuthorizationByMode(model, ACL.Read)); |
| 86 | + assertEquals(auth, new AuthorizationFilter().getAuthorizationByMode(model, ACL.Append)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testReturnsNullWhenNoAuthorizationGrantsMode() |
| 91 | + { |
| 92 | + Model model = ModelFactory.createDefaultModel(); |
| 93 | + model.createResource("https://localhost/acl/authorizations/1/#this"). |
| 94 | + addProperty(RDF.type, ACL.Authorization). |
| 95 | + addProperty(ACL.mode, ACL.Read); |
| 96 | + |
| 97 | + assertNull(new AuthorizationFilter().getAuthorizationByMode(model, ACL.Write)); |
| 98 | + } |
| 99 | + |
| 100 | + // createOwnerAuthorization: owner is granted Read/Write/Append on the document |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testOwnerAuthorizationGrantsReadWriteAppend() |
| 104 | + { |
| 105 | + Model model = ModelFactory.createDefaultModel(); |
| 106 | + Resource auth = new AuthorizationFilter().createOwnerAuthorization(model, ACCESS_TO, AGENT); |
| 107 | + |
| 108 | + assertTrue(auth.hasProperty(RDF.type, ACL.Authorization)); |
| 109 | + assertTrue(auth.hasProperty(RDF.type, LACL.OwnerAuthorization)); |
| 110 | + assertTrue(auth.hasProperty(ACL.accessTo, ACCESS_TO)); |
| 111 | + assertTrue(auth.hasProperty(ACL.agent, AGENT)); |
| 112 | + assertTrue(auth.hasProperty(ACL.mode, ACL.Read)); |
| 113 | + assertTrue(auth.hasProperty(ACL.mode, ACL.Write)); |
| 114 | + assertTrue(auth.hasProperty(ACL.mode, ACL.Append)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testOwnerAuthorizationIsDiscoverableByMode() |
| 119 | + { |
| 120 | + Model model = ModelFactory.createDefaultModel(); |
| 121 | + Resource auth = new AuthorizationFilter().createOwnerAuthorization(model, ACCESS_TO, AGENT); |
| 122 | + |
| 123 | + assertEquals(auth, new AuthorizationFilter().getAuthorizationByMode(model, ACL.Write)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testCreateOwnerAuthorizationRejectsNullArguments() |
| 128 | + { |
| 129 | + Model model = ModelFactory.createDefaultModel(); |
| 130 | + assertThrows(IllegalArgumentException.class, () -> new AuthorizationFilter().createOwnerAuthorization(null, ACCESS_TO, AGENT)); |
| 131 | + assertThrows(IllegalArgumentException.class, () -> new AuthorizationFilter().createOwnerAuthorization(model, null, AGENT)); |
| 132 | + assertThrows(IllegalArgumentException.class, () -> new AuthorizationFilter().createOwnerAuthorization(model, ACCESS_TO, null)); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments