| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.service.full; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 4 | |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 5 | import java.io.IOException; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 6 | import java.util.List; |
| 7 | import java.util.UUID; |
| 8 | |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 9 | import org.junit.BeforeClass; |
| 10 | import org.junit.Test; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 11 | |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 12 | import com.sun.jersey.api.client.ClientResponse; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 13 | |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 14 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 15 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 16 | import de.ids_mannheim.korap.interfaces.db.PolicyHandlerIface; |
| 17 | import de.ids_mannheim.korap.interfaces.db.ResourceOperationIface; |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame^] | 18 | import de.ids_mannheim.korap.resources.Corpus; |
| 19 | import de.ids_mannheim.korap.resources.Foundry; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 20 | import de.ids_mannheim.korap.resources.KustvaktResource; |
| 21 | import de.ids_mannheim.korap.resources.Permissions; |
| 22 | import de.ids_mannheim.korap.resources.Permissions.Permission; |
| 23 | import de.ids_mannheim.korap.resources.VirtualCollection; |
| 24 | import de.ids_mannheim.korap.security.PolicyCondition; |
| 25 | import de.ids_mannheim.korap.security.SecurityPolicy; |
| 26 | import de.ids_mannheim.korap.security.auth.BasicHttpAuth; |
| 27 | import de.ids_mannheim.korap.user.User; |
| 28 | import de.ids_mannheim.korap.user.User.UserFactory; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 29 | import de.ids_mannheim.korap.web.service.FastJerseyTest; |
| 30 | |
| 31 | /** |
| 32 | * @author margaretha |
| 33 | */ |
| 34 | public class PolicyServiceTest extends FastJerseyTest { |
| 35 | |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame^] | 36 | private User user = UserFactory.getDemoUser(); |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 37 | |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame^] | 38 | |
| 39 | @BeforeClass |
| 40 | public static void configure () throws Exception { |
| 41 | FastJerseyTest.setPackages("de.ids_mannheim.korap.web.service.full", |
| 42 | "de.ids_mannheim.korap.web.filter", |
| 43 | "de.ids_mannheim.korap.web.utils"); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | @Test |
| 48 | public void testCreatePolicyForVirtualCollection () |
| 49 | throws IOException, KustvaktException { |
| 50 | String id = UUID.randomUUID().toString(); |
| 51 | ClientResponse response = resource().path(getAPIVersion()).path("admin") |
| 52 | .path("createPolicies").path(id) |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 53 | .queryParam("type", "virtualcollection") |
| 54 | .queryParam("name", "Goethe VC") |
| 55 | .queryParam("description", "Goethe corpus") |
| 56 | .queryParam("group", "public") |
| 57 | .queryParam("perm", Permission.READ.name()) |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 58 | .queryParam("expire", "") |
| 59 | .header(Attributes.AUTHORIZATION, |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame^] | 60 | BasicHttpAuth.encode("kustvakt", "kustvakt2015")) |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 61 | .post(ClientResponse.class); |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame^] | 62 | |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 63 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 64 | response.getStatus()); |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame^] | 65 | |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 66 | // Check the policies |
| 67 | PolicyHandlerIface dao = helper().getContext().getPolicyDbProvider(); |
| 68 | List<SecurityPolicy> policies = dao.getPolicies( |
| 69 | new PolicyCondition("public"), VirtualCollection.class, |
| 70 | Permissions.Permission.READ.toByte()); |
| 71 | assertEquals(2, policies.size()); |
| 72 | |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame^] | 73 | policies = dao.getPoliciesByPersistentId( |
| 74 | new PolicyCondition("public"), VirtualCollection.class, |
| 75 | Permissions.Permission.READ.toByte(),id); |
| 76 | assertEquals(1, policies.size()); |
| 77 | assertEquals(id, policies.get(0).getTarget()); |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 78 | |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame^] | 79 | // Check the resource |
| 80 | List<ResourceOperationIface> providers = (List<ResourceOperationIface>) helper() |
| 81 | .getContext().getResourceProviders(); |
| 82 | ResourceOperationIface resourceDao = providers.get(0); |
| 83 | |
| 84 | User user = UserFactory.getDemoUser(); |
| 85 | KustvaktResource resource = resourceDao.findbyId(id, user); |
| 86 | assertEquals("Goethe VC", resource.getName()); |
| 87 | |
| 88 | } |
| 89 | |
| 90 | |
| 91 | @Test |
| 92 | public void testCreatePolicyForFoundry () |
| 93 | throws IOException, KustvaktException { |
| 94 | String id = UUID.randomUUID().toString(); |
| 95 | ClientResponse response = resource().path(getAPIVersion()).path("admin") |
| 96 | .path("createPolicies").path(id).queryParam("type", "foundry") |
| 97 | .queryParam("name", "stanford") |
| 98 | .queryParam("description", "stanford parser") |
| 99 | .queryParam("group", "public") |
| 100 | .queryParam("perm", Permission.READ.name()) |
| 101 | .queryParam("loc", "255.255.255.0") |
| 102 | .queryParam("expire", "30D") |
| 103 | .header(Attributes.AUTHORIZATION, |
| 104 | BasicHttpAuth.encode("kustvakt", "kustvakt2015")) |
| 105 | .post(ClientResponse.class); |
| 106 | |
| 107 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 108 | response.getStatus()); |
| 109 | |
| 110 | // Check the resource store |
| 111 | List<ResourceOperationIface> providers = (List<ResourceOperationIface>) helper() |
| 112 | .getContext().getResourceProviders(); |
| 113 | ResourceOperationIface resourceDao = providers.get(0); |
| 114 | KustvaktResource resource = resourceDao.findbyId(id, user); |
| 115 | assertEquals("stanford", resource.getName()); |
| 116 | |
| 117 | // Check the policies |
| 118 | PolicyHandlerIface dao = helper().getContext().getPolicyDbProvider(); |
| 119 | List<SecurityPolicy> policies = dao.getPoliciesByPersistentId( |
| 120 | new PolicyCondition("public"), Foundry.class, |
| 121 | Permissions.Permission.READ.toByte(),id); |
| 122 | assertEquals(1, policies.size()); |
| 123 | assertEquals("255.255.255.0",policies.get(0).getContext().getIpmask()); |
| 124 | |
| 125 | } |
| 126 | |
| 127 | |
| 128 | @Test |
| 129 | public void testCreatePolicyForMultiplePermissions () |
| 130 | throws IOException, KustvaktException { |
| 131 | String id = UUID.randomUUID().toString(); |
| 132 | ClientResponse response = resource().path(getAPIVersion()).path("admin") |
| 133 | .path("createPolicies").path(id).queryParam("type", "corpus") |
| 134 | .queryParam("name", "Brown") |
| 135 | .queryParam("description", "Brown corpus") |
| 136 | .queryParam("group", "public") |
| 137 | .queryParam("perm", Permission.READ.name()) |
| 138 | .queryParam("perm", Permission.WRITE.name()) |
| 139 | .queryParam("perm", Permission.DELETE.name()) |
| 140 | .queryParam("expire", "30D") |
| 141 | .header(Attributes.AUTHORIZATION, |
| 142 | BasicHttpAuth.encode("kustvakt", "kustvakt2015")) |
| 143 | .post(ClientResponse.class); |
| 144 | |
| 145 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 146 | response.getStatus()); |
| 147 | |
| 148 | // Check resource store |
| 149 | List<ResourceOperationIface> providers = (List<ResourceOperationIface>) helper() |
| 150 | .getContext().getResourceProviders(); |
| 151 | ResourceOperationIface resourceDao = providers.get(0); |
| 152 | |
| 153 | KustvaktResource resource = resourceDao.findbyId(id, user); |
| 154 | assertEquals("Brown", resource.getName()); |
| 155 | |
| 156 | // Check the policies |
| 157 | PolicyHandlerIface dao = helper().getContext().getPolicyDbProvider(); |
| 158 | List<SecurityPolicy> policies = dao.getPoliciesByPersistentId( |
| 159 | new PolicyCondition("public"), Corpus.class, |
| 160 | Permissions.Permission.WRITE.toByte(),id); |
| 161 | assertEquals(1, policies.size()); |
| 162 | assertEquals(id, policies.get(0).getTarget()); |
| 163 | |
| 164 | policies = dao.getPoliciesByPersistentId( |
| 165 | new PolicyCondition("public"), Corpus.class, |
| 166 | Permissions.Permission.DELETE.toByte(),id); |
| 167 | assertEquals(1, policies.size()); |
| 168 | assertEquals(id, policies.get(0).getTarget()); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | @Override |
| 173 | public void initMethod () throws KustvaktException { |
| 174 | helper().runBootInterfaces(); |
| 175 | } |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 176 | } |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame^] | 177 | |