| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 5 | import javax.ws.rs.core.MediaType; |
| margaretha | 18533fd | 2018-03-28 16:01:06 +0200 | [diff] [blame] | 6 | import javax.ws.rs.core.MultivaluedHashMap; |
| 7 | import javax.ws.rs.core.MultivaluedMap; |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 8 | |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 9 | import org.junit.Test; |
| 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | |
| 12 | import com.fasterxml.jackson.databind.JsonNode; |
| 13 | import com.google.common.net.HttpHeaders; |
| 14 | import com.sun.jersey.api.client.ClientHandlerException; |
| 15 | import com.sun.jersey.api.client.ClientResponse; |
| 16 | import com.sun.jersey.api.client.ClientResponse.Status; |
| 17 | import com.sun.jersey.api.client.UniformInterfaceException; |
| margaretha | b1081b1 | 2018-07-03 23:35:01 +0200 | [diff] [blame] | 18 | import com.sun.jersey.core.util.MultivaluedMapImpl; |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 19 | |
| 20 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 21 | import de.ids_mannheim.korap.config.Attributes; |
| 22 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 23 | import de.ids_mannheim.korap.constant.GroupMemberStatus; |
| 24 | import de.ids_mannheim.korap.constant.PredefinedRole; |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 25 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 26 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 27 | import de.ids_mannheim.korap.web.input.UserGroupJson; |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 28 | |
| margaretha | a048627 | 2018-04-12 19:59:31 +0200 | [diff] [blame] | 29 | /** |
| 30 | * @author margaretha |
| 31 | * |
| 32 | */ |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 33 | public class UserGroupControllerAdminTest extends SpringJerseyTest { |
| 34 | @Autowired |
| 35 | private HttpAuthorizationHandler handler; |
| 36 | |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 37 | private String adminUsername = "admin"; |
| 38 | private String testUsername = "UserGroupControllerAdminTest"; |
| 39 | |
| 40 | private JsonNode listGroup (String username) |
| 41 | throws UniformInterfaceException, ClientHandlerException, |
| 42 | KustvaktException { |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 43 | ClientResponse response = resource().path("group").path("list") |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 44 | .queryParam("username", username) |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 45 | .header(Attributes.AUTHORIZATION, |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 46 | handler.createBasicAuthorizationHeaderValue( |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 47 | testUsername, "pass")) |
| 48 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 49 | .get(ClientResponse.class); |
| 50 | |
| 51 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 52 | String entity = response.getEntity(String.class); |
| 53 | JsonNode node = JsonUtils.readTree(entity); |
| 54 | return node; |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testListDoryGroups () throws KustvaktException { |
| 59 | ClientResponse response = resource().path("group").path("list") |
| 60 | .path("system-admin").queryParam("username", "dory") |
| 61 | .header(Attributes.AUTHORIZATION, |
| 62 | handler.createBasicAuthorizationHeaderValue( |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 63 | adminUsername, "pass")) |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 64 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 65 | .get(ClientResponse.class); |
| 66 | |
| 67 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 68 | String entity = response.getEntity(String.class); |
| margaretha | b1081b1 | 2018-07-03 23:35:01 +0200 | [diff] [blame] | 69 | // System.out.println(entity); |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 70 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 71 | assertEquals(3, node.size()); |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Test |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 75 | public void testListDoryActiveGroups () throws KustvaktException { |
| 76 | ClientResponse response = resource().path("group").path("list") |
| 77 | .path("system-admin").queryParam("username", "dory") |
| 78 | .queryParam("status", "ACTIVE") |
| 79 | .header(Attributes.AUTHORIZATION, |
| 80 | handler.createBasicAuthorizationHeaderValue( |
| 81 | adminUsername, "pass")) |
| 82 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 83 | .get(ClientResponse.class); |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 84 | |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 85 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 86 | String entity = response.getEntity(String.class); |
| margaretha | b1081b1 | 2018-07-03 23:35:01 +0200 | [diff] [blame] | 87 | // System.out.println(entity); |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 88 | JsonNode node = JsonUtils.readTree(entity); |
| 89 | assertEquals(2, node.size()); |
| 90 | } |
| margaretha | 18533fd | 2018-03-28 16:01:06 +0200 | [diff] [blame] | 91 | |
| 92 | |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 93 | // same as list user-groups of the admin |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 94 | @Test |
| 95 | public void testListWithoutUsername () throws UniformInterfaceException, |
| 96 | ClientHandlerException, KustvaktException { |
| 97 | ClientResponse response = resource().path("group").path("list") |
| 98 | .header(Attributes.AUTHORIZATION, |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 99 | handler.createBasicAuthorizationHeaderValue( |
| 100 | adminUsername, "pass")) |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 101 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 102 | .get(ClientResponse.class); |
| 103 | |
| 104 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 105 | String entity = response.getEntity(String.class); |
| 106 | assertEquals("[]", entity); |
| 107 | } |
| 108 | |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 109 | @Test |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 110 | public void testListByStatusAll () throws UniformInterfaceException, |
| 111 | ClientHandlerException, KustvaktException { |
| 112 | ClientResponse response = |
| 113 | resource().path("group").path("list").path("system-admin") |
| 114 | .header(Attributes.AUTHORIZATION, |
| 115 | handler.createBasicAuthorizationHeaderValue( |
| 116 | adminUsername, "pass")) |
| 117 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 118 | .get(ClientResponse.class); |
| 119 | |
| 120 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 121 | String entity = response.getEntity(String.class); |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 122 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 123 | assertEquals(4, node.size()); |
| 124 | |
| margaretha | b1081b1 | 2018-07-03 23:35:01 +0200 | [diff] [blame] | 125 | boolean containsHiddenStatus = false; |
| 126 | for (int i = 0; i < node.size(); i++) { |
| 127 | if (node.get(i).at("/status").asText().equals("HIDDEN")) { |
| 128 | containsHiddenStatus = true; |
| 129 | } |
| 130 | } |
| 131 | assertEquals(true, containsHiddenStatus); |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testListByStatusHidden () throws UniformInterfaceException, |
| 136 | ClientHandlerException, KustvaktException { |
| 137 | ClientResponse response = resource().path("group").path("list") |
| 138 | .path("system-admin").queryParam("status", "HIDDEN") |
| 139 | .header(Attributes.AUTHORIZATION, |
| 140 | handler.createBasicAuthorizationHeaderValue( |
| 141 | adminUsername, "pass")) |
| 142 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 143 | .get(ClientResponse.class); |
| 144 | |
| 145 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 146 | String entity = response.getEntity(String.class); |
| 147 | JsonNode node = JsonUtils.readTree(entity); |
| 148 | assertEquals(1, node.size()); |
| 149 | assertEquals(3, node.at("/0/id").asInt()); |
| 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void testUserGroup () throws UniformInterfaceException, |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 154 | ClientHandlerException, KustvaktException { |
| 155 | |
| 156 | UserGroupJson json = new UserGroupJson(); |
| 157 | json.setName("admin test group"); |
| 158 | json.setMembers(new String[] { "marlin", "nemo" }); |
| 159 | |
| 160 | ClientResponse response = resource().path("group").path("create") |
| 161 | .type(MediaType.APPLICATION_JSON) |
| 162 | .header(Attributes.AUTHORIZATION, |
| 163 | handler.createBasicAuthorizationHeaderValue( |
| 164 | testUsername, "password")) |
| 165 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").entity(json) |
| 166 | .post(ClientResponse.class); |
| 167 | |
| 168 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 169 | |
| 170 | // list user group |
| 171 | JsonNode node = listGroup(testUsername); |
| 172 | assertEquals(1, node.size()); |
| 173 | node = node.get(0); |
| 174 | assertEquals("admin test group", node.get("name").asText()); |
| 175 | |
| 176 | String groupId = node.get("id").asText(); |
| margaretha | 18533fd | 2018-03-28 16:01:06 +0200 | [diff] [blame] | 177 | testMemberRole("marlin", groupId); |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 178 | testInviteMember(groupId); |
| 179 | testDeleteMember(groupId); |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 180 | testDeleteGroup(groupId); |
| 181 | } |
| 182 | |
| margaretha | 18533fd | 2018-03-28 16:01:06 +0200 | [diff] [blame] | 183 | private void testMemberRole (String memberUsername, String groupId) |
| 184 | throws UniformInterfaceException, ClientHandlerException, |
| 185 | KustvaktException { |
| 186 | |
| 187 | // accept invitation |
| 188 | MultivaluedMap<String, String> form = new MultivaluedMapImpl(); |
| 189 | form.add("groupId", groupId); |
| 190 | |
| 191 | ClientResponse response = resource().path("group").path("subscribe") |
| 192 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| 193 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 194 | .header(Attributes.AUTHORIZATION, |
| 195 | handler.createBasicAuthorizationHeaderValue("marlin", |
| 196 | "pass")) |
| 197 | .entity(form).post(ClientResponse.class); |
| 198 | |
| 199 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 200 | |
| 201 | testAddMemberRoles(groupId, memberUsername); |
| 202 | testDeleteMemberRoles(groupId, memberUsername); |
| 203 | } |
| 204 | |
| 205 | private void testAddMemberRoles (String groupId, String memberUsername) |
| 206 | throws UniformInterfaceException, ClientHandlerException, |
| 207 | KustvaktException { |
| 208 | MultivaluedMap<String, String> map = new MultivaluedHashMap<>(); |
| 209 | map.add("groupId", groupId.toString()); |
| 210 | map.add("memberUsername", memberUsername); |
| 211 | map.add("roleIds", "1"); // USER_GROUP_ADMIN |
| 212 | map.add("roleIds", "2"); // USER_GROUP_MEMBER |
| 213 | |
| 214 | ClientResponse response = |
| 215 | resource().path("group").path("member").path("role").path("add") |
| 216 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| 217 | .header(Attributes.AUTHORIZATION, |
| 218 | handler.createBasicAuthorizationHeaderValue( |
| 219 | adminUsername, "password")) |
| 220 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 221 | .entity(map).post(ClientResponse.class); |
| 222 | |
| 223 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 224 | |
| 225 | JsonNode node = retrieveGroup(groupId).at("/members"); |
| 226 | JsonNode member; |
| 227 | for (int i = 0; i < node.size(); i++) { |
| 228 | member = node.get(i); |
| 229 | if (member.at("/userId").asText().equals(memberUsername)) { |
| 230 | assertEquals(3, member.at("/roles").size()); |
| 231 | assertEquals(PredefinedRole.USER_GROUP_ADMIN.name(), |
| 232 | member.at("/roles/0").asText()); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | private void testDeleteMemberRoles (String groupId, String memberUsername) |
| 239 | throws UniformInterfaceException, ClientHandlerException, |
| 240 | KustvaktException { |
| 241 | MultivaluedMap<String, String> map = new MultivaluedHashMap<>(); |
| 242 | map.add("groupId", groupId.toString()); |
| 243 | map.add("memberUsername", memberUsername); |
| 244 | map.add("roleIds", "1"); // USER_GROUP_ADMIN |
| 245 | |
| 246 | ClientResponse response = resource().path("group").path("member") |
| 247 | .path("role").path("delete") |
| 248 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| 249 | .header(Attributes.AUTHORIZATION, |
| 250 | handler.createBasicAuthorizationHeaderValue( |
| 251 | adminUsername, "password")) |
| 252 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").entity(map) |
| 253 | .post(ClientResponse.class); |
| 254 | |
| 255 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 256 | |
| 257 | JsonNode node = retrieveGroup(groupId).at("/members"); |
| 258 | JsonNode member; |
| 259 | for (int i = 0; i < node.size(); i++) { |
| 260 | member = node.get(i); |
| 261 | if (member.at("/userId").asText().equals(memberUsername)) { |
| 262 | assertEquals(2, member.at("/roles").size()); |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | private JsonNode retrieveGroup (String groupId) |
| 269 | throws UniformInterfaceException, ClientHandlerException, |
| 270 | KustvaktException { |
| 271 | ClientResponse response = resource().path("group").path(groupId) |
| 272 | .header(Attributes.AUTHORIZATION, |
| 273 | handler.createBasicAuthorizationHeaderValue( |
| 274 | adminUsername, "pass")) |
| 275 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 276 | .get(ClientResponse.class); |
| 277 | |
| 278 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 279 | |
| 280 | String entity = response.getEntity(String.class); |
| 281 | JsonNode node = JsonUtils.readTree(entity); |
| 282 | return node; |
| 283 | } |
| 284 | |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 285 | private void testDeleteGroup (String groupId) |
| 286 | throws UniformInterfaceException, ClientHandlerException, |
| 287 | KustvaktException { |
| margaretha | b1081b1 | 2018-07-03 23:35:01 +0200 | [diff] [blame] | 288 | // delete group |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 289 | ClientResponse response = resource().path("group").path("delete") |
| 290 | .queryParam("groupId", groupId) |
| 291 | .header(Attributes.AUTHORIZATION, |
| 292 | handler.createBasicAuthorizationHeaderValue( |
| 293 | adminUsername, "pass")) |
| 294 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 295 | .delete(ClientResponse.class); |
| 296 | |
| 297 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 298 | |
| 299 | // check group |
| margaretha | 293ee03 | 2018-03-20 20:11:52 +0100 | [diff] [blame] | 300 | JsonNode node = listGroup(testUsername); |
| 301 | assertEquals(0, node.size()); |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | private void testDeleteMember (String groupId) |
| 305 | throws UniformInterfaceException, ClientHandlerException, |
| 306 | KustvaktException { |
| 307 | // delete marlin from group |
| 308 | ClientResponse response = resource().path("group").path("member") |
| 309 | .path("delete").queryParam("memberId", "marlin") |
| 310 | .queryParam("groupId", groupId) |
| 311 | .header(Attributes.AUTHORIZATION, |
| 312 | handler.createBasicAuthorizationHeaderValue( |
| 313 | adminUsername, "pass")) |
| 314 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 315 | .delete(ClientResponse.class); |
| 316 | |
| margaretha | e68021a | 2018-04-09 16:13:12 +0200 | [diff] [blame] | 317 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | b1081b1 | 2018-07-03 23:35:01 +0200 | [diff] [blame] | 318 | |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 319 | // check group member |
| 320 | JsonNode node = listGroup(testUsername); |
| 321 | node = node.get(0); |
| 322 | assertEquals(3, node.get("members").size()); |
| 323 | assertEquals("nemo", node.at("/members/1/userId").asText()); |
| 324 | assertEquals(GroupMemberStatus.PENDING.name(), |
| 325 | node.at("/members/1/status").asText()); |
| 326 | } |
| 327 | |
| 328 | private void testInviteMember (String groupId) |
| 329 | throws UniformInterfaceException, ClientHandlerException, |
| 330 | KustvaktException { |
| 331 | String[] members = new String[] { "darla" }; |
| 332 | |
| 333 | UserGroupJson userGroup = new UserGroupJson(); |
| 334 | userGroup.setMembers(members); |
| 335 | userGroup.setId(Integer.parseInt(groupId)); |
| 336 | |
| 337 | ClientResponse response = resource().path("group").path("member") |
| 338 | .path("invite").type(MediaType.APPLICATION_JSON) |
| 339 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 340 | .header(Attributes.AUTHORIZATION, |
| 341 | handler.createBasicAuthorizationHeaderValue( |
| 342 | adminUsername, "pass")) |
| 343 | .entity(userGroup).post(ClientResponse.class); |
| 344 | |
| 345 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 346 | |
| 347 | // list group |
| 348 | JsonNode node = listGroup(testUsername); |
| 349 | node = node.get(0); |
| 350 | assertEquals(4, node.get("members").size()); |
| 351 | |
| 352 | assertEquals("darla", node.at("/members/3/userId").asText()); |
| 353 | assertEquals(GroupMemberStatus.PENDING.name(), |
| 354 | node.at("/members/3/status").asText()); |
| margaretha | 18533fd | 2018-03-28 16:01:06 +0200 | [diff] [blame] | 355 | assertEquals(0, node.at("/members/3/roles").size()); |
| margaretha | cf4b26f | 2018-03-19 21:42:06 +0100 | [diff] [blame] | 356 | } |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 357 | |
| 358 | } |