blob: c48f40c6ee1a4d727f697fb6eee5f19849b3a129 [file] [log] [blame]
margarethad8437f12017-03-14 17:09:02 +01001package de.ids_mannheim.korap.web.service.full;
2
3import static org.junit.Assert.assertEquals;
margarethad8437f12017-03-14 17:09:02 +01004
margarethad8437f12017-03-14 17:09:02 +01005import java.io.IOException;
margarethad8437f12017-03-14 17:09:02 +01006import java.util.List;
7import java.util.UUID;
8
margarethad8437f12017-03-14 17:09:02 +01009import org.junit.BeforeClass;
10import org.junit.Test;
margarethad8437f12017-03-14 17:09:02 +010011
margarethad8437f12017-03-14 17:09:02 +010012import com.sun.jersey.api.client.ClientResponse;
margarethad8437f12017-03-14 17:09:02 +010013
margarethad8437f12017-03-14 17:09:02 +010014import de.ids_mannheim.korap.config.Attributes;
margarethad8437f12017-03-14 17:09:02 +010015import de.ids_mannheim.korap.exceptions.KustvaktException;
margarethad8437f12017-03-14 17:09:02 +010016import de.ids_mannheim.korap.interfaces.db.PolicyHandlerIface;
17import de.ids_mannheim.korap.interfaces.db.ResourceOperationIface;
margarethafc2040a2017-04-18 12:07:23 +020018import de.ids_mannheim.korap.resources.Corpus;
19import de.ids_mannheim.korap.resources.Foundry;
margarethad8437f12017-03-14 17:09:02 +010020import de.ids_mannheim.korap.resources.KustvaktResource;
21import de.ids_mannheim.korap.resources.Permissions;
22import de.ids_mannheim.korap.resources.Permissions.Permission;
23import de.ids_mannheim.korap.resources.VirtualCollection;
24import de.ids_mannheim.korap.security.PolicyCondition;
25import de.ids_mannheim.korap.security.SecurityPolicy;
26import de.ids_mannheim.korap.security.auth.BasicHttpAuth;
27import de.ids_mannheim.korap.user.User;
28import de.ids_mannheim.korap.user.User.UserFactory;
margarethad8437f12017-03-14 17:09:02 +010029import de.ids_mannheim.korap.web.service.FastJerseyTest;
30
31/**
32 * @author margaretha
33 */
34public class PolicyServiceTest extends FastJerseyTest {
35
margarethafc2040a2017-04-18 12:07:23 +020036 private User user = UserFactory.getDemoUser();
margarethad8437f12017-03-14 17:09:02 +010037
margarethafc2040a2017-04-18 12:07:23 +020038
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)
margarethad8437f12017-03-14 17:09:02 +010053 .queryParam("type", "virtualcollection")
54 .queryParam("name", "Goethe VC")
55 .queryParam("description", "Goethe corpus")
56 .queryParam("group", "public")
57 .queryParam("perm", Permission.READ.name())
margarethad8437f12017-03-14 17:09:02 +010058 .queryParam("expire", "")
59 .header(Attributes.AUTHORIZATION,
margarethafc2040a2017-04-18 12:07:23 +020060 BasicHttpAuth.encode("kustvakt", "kustvakt2015"))
margarethad8437f12017-03-14 17:09:02 +010061 .post(ClientResponse.class);
margarethafc2040a2017-04-18 12:07:23 +020062
margarethad8437f12017-03-14 17:09:02 +010063 assertEquals(ClientResponse.Status.OK.getStatusCode(),
64 response.getStatus());
margarethafc2040a2017-04-18 12:07:23 +020065
margarethad8437f12017-03-14 17:09:02 +010066 // 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
margarethafc2040a2017-04-18 12:07:23 +020073 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());
margarethad8437f12017-03-14 17:09:02 +010078
margarethafc2040a2017-04-18 12:07:23 +020079 // 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 }
margarethad8437f12017-03-14 17:09:02 +0100176}
margarethafc2040a2017-04-18 12:07:23 +0200177