| margaretha | 541b8cc | 2018-01-10 13:02:46 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 5 | import javax.ws.rs.core.MediaType; |
| 6 | import javax.ws.rs.core.MultivaluedMap; |
| 7 | |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 8 | import org.junit.Test; |
| 9 | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | |
| 11 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 58e1863 | 2018-02-15 13:04:42 +0100 | [diff] [blame] | 12 | import com.google.common.net.HttpHeaders; |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 13 | import com.sun.jersey.api.client.ClientHandlerException; |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 14 | import com.sun.jersey.api.client.ClientResponse; |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 15 | import com.sun.jersey.api.client.ClientResponse.Status; |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 16 | import com.sun.jersey.api.client.UniformInterfaceException; |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 17 | import com.sun.jersey.core.util.MultivaluedMapImpl; |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 18 | |
| 19 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| 20 | import de.ids_mannheim.korap.config.Attributes; |
| 21 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 22 | import de.ids_mannheim.korap.constant.GroupMemberStatus; |
| 23 | import de.ids_mannheim.korap.constant.PredefinedRole; |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 24 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 25 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 26 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | b874ef5 | 2018-01-23 20:26:31 +0100 | [diff] [blame] | 27 | import de.ids_mannheim.korap.web.input.UserGroupJson; |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 28 | |
| margaretha | 541b8cc | 2018-01-10 13:02:46 +0100 | [diff] [blame] | 29 | public class UserGroupControllerTest extends SpringJerseyTest { |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 30 | |
| 31 | @Autowired |
| 32 | private HttpAuthorizationHandler handler; |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 33 | private String username = "UserGroupControllerTest"; |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 34 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 35 | private JsonNode retrieveUserGroups (String username) |
| 36 | throws UniformInterfaceException, ClientHandlerException, |
| 37 | KustvaktException { |
| 38 | ClientResponse response = resource().path("group").path("list") |
| 39 | .header(Attributes.AUTHORIZATION, |
| 40 | handler.createBasicAuthorizationHeaderValue(username, |
| 41 | "pass")) |
| 42 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 43 | .get(ClientResponse.class); |
| 44 | String entity = response.getEntity(String.class); |
| 45 | |
| 46 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 47 | |
| 48 | return JsonUtils.readTree(entity); |
| 49 | } |
| 50 | |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 51 | // dory is a group admin in dory group |
| 52 | @Test |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 53 | public void testListDoryGroups () throws KustvaktException { |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 54 | ClientResponse response = resource().path("group").path("list") |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 55 | .header(Attributes.AUTHORIZATION, |
| 56 | handler.createBasicAuthorizationHeaderValue("dory", |
| 57 | "pass")) |
| 58 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 59 | .get(ClientResponse.class); |
| 60 | String entity = response.getEntity(String.class); |
| 61 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 62 | // System.out.println(entity); |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 63 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 64 | |
| margaretha | b874ef5 | 2018-01-23 20:26:31 +0100 | [diff] [blame] | 65 | JsonNode group = node.get(1); |
| 66 | assertEquals(2, group.at("/id").asInt()); |
| 67 | assertEquals("dory group", group.at("/name").asText()); |
| 68 | assertEquals("dory", group.at("/owner").asText()); |
| 69 | assertEquals(3, group.at("/members").size()); |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 70 | } |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 71 | |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 72 | // nemo is a group member in dory group |
| 73 | @Test |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 74 | public void testListNemoGroups () throws KustvaktException { |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 75 | ClientResponse response = resource().path("group").path("list") |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 76 | .header(Attributes.AUTHORIZATION, |
| 77 | handler.createBasicAuthorizationHeaderValue("nemo", |
| 78 | "pass")) |
| 79 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 80 | .get(ClientResponse.class); |
| 81 | String entity = response.getEntity(String.class); |
| 82 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 83 | // System.out.println(entity); |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 84 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 85 | |
| margaretha | 45dde68 | 2018-01-04 21:33:46 +0100 | [diff] [blame] | 86 | assertEquals(2, node.at("/0/id").asInt()); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 87 | assertEquals("dory group", node.at("/0/name").asText()); |
| 88 | assertEquals("dory", node.at("/0/owner").asText()); |
| 89 | // group members are not allowed to see other members |
| 90 | assertEquals(0, node.at("/0/members").size()); |
| 91 | } |
| 92 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 93 | // marlin has 2 groups |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 94 | @Test |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 95 | public void testListMarlinGroups () throws KustvaktException { |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 96 | ClientResponse response = resource().path("group").path("list") |
| 97 | .header(Attributes.AUTHORIZATION, |
| 98 | handler.createBasicAuthorizationHeaderValue("marlin", |
| 99 | "pass")) |
| 100 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 101 | .get(ClientResponse.class); |
| 102 | String entity = response.getEntity(String.class); |
| 103 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 104 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 105 | assertEquals(2, node.size()); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| margaretha | 98ec15b | 2018-01-22 17:14:02 +0100 | [diff] [blame] | 108 | |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 109 | @Test |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 110 | public void testListUserGroupUnauthorized () throws KustvaktException { |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 111 | ClientResponse response = resource().path("group").path("list") |
| 112 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 113 | .get(ClientResponse.class); |
| 114 | String entity = response.getEntity(String.class); |
| margaretha | 98ec15b | 2018-01-22 17:14:02 +0100 | [diff] [blame] | 115 | // System.out.println(entity); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 116 | JsonNode node = JsonUtils.readTree(entity); |
| 117 | |
| 118 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| 119 | assertEquals(StatusCodes.AUTHORIZATION_FAILED, |
| 120 | node.at("/errors/0/0").asInt()); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 121 | assertEquals("Unauthorized operation for user: guest", |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 122 | node.at("/errors/0/1").asText()); |
| 123 | } |
| 124 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 125 | @Test |
| 126 | public void testCreateUserGroup () throws UniformInterfaceException, |
| 127 | ClientHandlerException, KustvaktException { |
| margaretha | b874ef5 | 2018-01-23 20:26:31 +0100 | [diff] [blame] | 128 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 129 | UserGroupJson json = new UserGroupJson(); |
| 130 | json.setName("new user group"); |
| 131 | json.setMembers(new String[] { "marlin", "nemo" }); |
| margaretha | b874ef5 | 2018-01-23 20:26:31 +0100 | [diff] [blame] | 132 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 133 | ClientResponse response = resource().path("group").path("create") |
| 134 | .type(MediaType.APPLICATION_JSON) |
| 135 | .header(Attributes.AUTHORIZATION, |
| 136 | handler.createBasicAuthorizationHeaderValue(username, |
| 137 | "pass")) |
| 138 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32").entity(json) |
| 139 | .post(ClientResponse.class); |
| 140 | |
| 141 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 142 | |
| 143 | // list user group |
| 144 | response = resource().path("group").path("list") |
| 145 | .header(Attributes.AUTHORIZATION, |
| 146 | handler.createBasicAuthorizationHeaderValue(username, |
| 147 | "pass")) |
| 148 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 149 | .get(ClientResponse.class); |
| 150 | |
| 151 | String entity = response.getEntity(String.class); |
| 152 | // System.out.println(entity); |
| 153 | JsonNode node = JsonUtils.readTree(entity); |
| 154 | assertEquals(1, node.size()); |
| 155 | node = node.get(0); |
| 156 | assertEquals("new user group", node.get("name").asText()); |
| 157 | String groupId = node.get("id").asText(); |
| 158 | |
| 159 | assertEquals(username, node.get("owner").asText()); |
| 160 | assertEquals(3, node.get("members").size()); |
| 161 | assertEquals(username, node.at("/members/0/userId").asText()); |
| 162 | assertEquals(GroupMemberStatus.ACTIVE.name(), |
| 163 | node.at("/members/0/status").asText()); |
| 164 | assertEquals(PredefinedRole.USER_GROUP_ADMIN.name(), |
| 165 | node.at("/members/0/roles/0").asText()); |
| 166 | assertEquals(PredefinedRole.VC_ACCESS_ADMIN.name(), |
| 167 | node.at("/members/0/roles/1").asText()); |
| 168 | |
| 169 | assertEquals("marlin", node.at("/members/1/userId").asText()); |
| 170 | assertEquals(GroupMemberStatus.PENDING.name(), |
| 171 | node.at("/members/1/status").asText()); |
| 172 | assertEquals(PredefinedRole.USER_GROUP_MEMBER.name(), |
| 173 | node.at("/members/1/roles/0").asText()); |
| 174 | assertEquals(PredefinedRole.VC_ACCESS_MEMBER.name(), |
| 175 | node.at("/members/1/roles/1").asText()); |
| 176 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 177 | |
| 178 | testInviteMember(groupId); |
| 179 | |
| 180 | testDeleteMemberUnauthorized(groupId); |
| 181 | testDeleteMember(groupId); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 182 | testDeleteGroup(groupId); |
| 183 | } |
| 184 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 185 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 186 | private void testDeleteMember (String groupId) |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 187 | throws UniformInterfaceException, ClientHandlerException, |
| 188 | KustvaktException { |
| 189 | // delete marlin from group |
| 190 | ClientResponse response = resource().path("group").path("member") |
| 191 | .path("delete").queryParam("memberId", "marlin") |
| 192 | .queryParam("groupId", groupId) |
| 193 | .header(Attributes.AUTHORIZATION, |
| 194 | handler.createBasicAuthorizationHeaderValue(username, |
| 195 | "pass")) |
| 196 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 197 | .delete(ClientResponse.class); |
| 198 | |
| 199 | // check group member |
| 200 | response = resource().path("group").path("list") |
| 201 | .header(Attributes.AUTHORIZATION, |
| 202 | handler.createBasicAuthorizationHeaderValue(username, |
| 203 | "pass")) |
| 204 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 205 | .get(ClientResponse.class); |
| 206 | String entity = response.getEntity(String.class); |
| 207 | JsonNode node = JsonUtils.readTree(entity); |
| 208 | node = node.get(0); |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 209 | assertEquals(3, node.get("members").size()); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 210 | assertEquals("nemo", node.at("/members/1/userId").asText()); |
| 211 | assertEquals(GroupMemberStatus.PENDING.name(), |
| 212 | node.at("/members/1/status").asText()); |
| 213 | |
| 214 | } |
| 215 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 216 | private void testDeleteMemberUnauthorized (String groupId) |
| 217 | throws UniformInterfaceException, ClientHandlerException, |
| 218 | KustvaktException { |
| 219 | // nemo is a group member |
| 220 | ClientResponse response = resource().path("group").path("member") |
| 221 | .path("delete").queryParam("memberId", "marlin") |
| 222 | .queryParam("groupId", groupId) |
| 223 | .header(Attributes.AUTHORIZATION, |
| 224 | handler.createBasicAuthorizationHeaderValue("nemo", |
| 225 | "pass")) |
| 226 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 227 | .delete(ClientResponse.class); |
| 228 | |
| 229 | String entity = response.getEntity(String.class); |
| 230 | // System.out.println(entity); |
| 231 | JsonNode node = JsonUtils.readTree(entity); |
| 232 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| 233 | assertEquals(StatusCodes.AUTHORIZATION_FAILED, |
| 234 | node.at("/errors/0/0").asInt()); |
| 235 | assertEquals("Unauthorized operation for user: nemo", |
| 236 | node.at("/errors/0/1").asText()); |
| 237 | } |
| 238 | |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 239 | // EM: same as cancel invitation |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 240 | private void testDeletePendingMember () throws UniformInterfaceException, |
| 241 | ClientHandlerException, KustvaktException { |
| 242 | // dory delete pearl |
| 243 | ClientResponse response = resource().path("group").path("member") |
| 244 | .path("delete").queryParam("memberId", "pearl") |
| 245 | // dory group |
| 246 | .queryParam("groupId", "2") |
| 247 | .header(Attributes.AUTHORIZATION, |
| 248 | handler.createBasicAuthorizationHeaderValue("dory", |
| 249 | "pass")) |
| 250 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 251 | .delete(ClientResponse.class); |
| 252 | |
| 253 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 254 | |
| 255 | // check member |
| 256 | JsonNode node = retrieveUserGroups("pearl"); |
| 257 | assertEquals(0, node.size()); |
| 258 | } |
| 259 | |
| 260 | @Test |
| 261 | public void testDeleteDeletedMember () throws UniformInterfaceException, |
| 262 | ClientHandlerException, KustvaktException { |
| 263 | ClientResponse response = resource().path("group").path("member") |
| 264 | .path("delete").queryParam("memberId", "pearl") |
| 265 | // dory group |
| 266 | .queryParam("groupId", "2") |
| 267 | .header(Attributes.AUTHORIZATION, |
| 268 | handler.createBasicAuthorizationHeaderValue("dory", |
| 269 | "pass")) |
| 270 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 271 | .delete(ClientResponse.class); |
| 272 | |
| 273 | String entity = response.getEntity(String.class); |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 274 | // System.out.println(entity); |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 275 | JsonNode node = JsonUtils.readTree(entity); |
| 276 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 277 | assertEquals(StatusCodes.GROUP_MEMBER_DELETED, |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 278 | node.at("/errors/0/0").asInt()); |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 279 | assertEquals("pearl has already been deleted from the group dory group", |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 280 | node.at("/errors/0/1").asText()); |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 281 | assertEquals("[pearl, dory group]", node.at("/errors/0/2").asText()); |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 282 | } |
| 283 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 284 | private void testDeleteGroup (String groupId) |
| 285 | throws UniformInterfaceException, ClientHandlerException, |
| 286 | KustvaktException { |
| 287 | //delete group |
| 288 | ClientResponse response = resource().path("group").path("delete") |
| 289 | .queryParam("groupId", groupId) |
| 290 | .header(Attributes.AUTHORIZATION, |
| 291 | handler.createBasicAuthorizationHeaderValue(username, |
| 292 | "pass")) |
| 293 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 294 | .delete(ClientResponse.class); |
| 295 | |
| 296 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 297 | |
| 298 | // check group |
| 299 | response = resource().path("group").path("list") |
| 300 | .header(Attributes.AUTHORIZATION, |
| 301 | handler.createBasicAuthorizationHeaderValue(username, |
| 302 | "pass")) |
| 303 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 304 | .get(ClientResponse.class); |
| 305 | String entity = response.getEntity(String.class); |
| 306 | assertEquals("[]", entity); |
| 307 | } |
| 308 | |
| 309 | @Test |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 310 | public void testDeleteGroupUnauthorized () throws UniformInterfaceException, |
| 311 | ClientHandlerException, KustvaktException { |
| 312 | // dory is a group admin in marlin group |
| 313 | ClientResponse response = resource().path("group").path("delete") |
| 314 | .queryParam("groupId", "1") |
| 315 | .header(Attributes.AUTHORIZATION, |
| 316 | handler.createBasicAuthorizationHeaderValue("dory", |
| 317 | "pass")) |
| 318 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 319 | .delete(ClientResponse.class); |
| 320 | |
| 321 | String entity = response.getEntity(String.class); |
| 322 | // System.out.println(entity); |
| 323 | JsonNode node = JsonUtils.readTree(entity); |
| 324 | assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| 325 | assertEquals(StatusCodes.AUTHORIZATION_FAILED, |
| 326 | node.at("/errors/0/0").asInt()); |
| 327 | assertEquals("Unauthorized operation for user: dory", |
| 328 | node.at("/errors/0/1").asText()); |
| 329 | } |
| 330 | |
| 331 | @Test |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 332 | public void testDeleteGroupOwner () throws UniformInterfaceException, |
| 333 | ClientHandlerException, KustvaktException { |
| 334 | // delete marlin from marlin group |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 335 | // dory is a group admin in marlin group |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 336 | ClientResponse response = resource().path("group").path("member") |
| 337 | .path("delete").queryParam("memberId", "marlin") |
| 338 | .queryParam("groupId", "1") |
| 339 | .header(Attributes.AUTHORIZATION, |
| 340 | handler.createBasicAuthorizationHeaderValue("dory", |
| 341 | "pass")) |
| 342 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 343 | .delete(ClientResponse.class); |
| 344 | |
| 345 | String entity = response.getEntity(String.class); |
| 346 | // System.out.println(entity); |
| 347 | JsonNode node = JsonUtils.readTree(entity); |
| 348 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 349 | assertEquals(StatusCodes.NOT_ALLOWED, node.at("/errors/0/0").asInt()); |
| 350 | assertEquals("Operation 'delete group owner'is not allowed.", |
| 351 | node.at("/errors/0/1").asText()); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 352 | } |
| 353 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 354 | private void testInviteMember (String groupId) |
| 355 | throws UniformInterfaceException, ClientHandlerException, |
| 356 | KustvaktException { |
| 357 | String[] members = new String[] { "darla" }; |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 358 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 359 | UserGroupJson userGroup = new UserGroupJson(); |
| 360 | userGroup.setMembers(members); |
| 361 | userGroup.setId(Integer.parseInt(groupId)); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 362 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 363 | ClientResponse response = resource().path("group").path("member") |
| 364 | .path("invite").type(MediaType.APPLICATION_JSON) |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 365 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 366 | .header(Attributes.AUTHORIZATION, |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 367 | handler.createBasicAuthorizationHeaderValue(username, |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 368 | "pass")) |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 369 | .entity(userGroup).post(ClientResponse.class); |
| margaretha | 98ec15b | 2018-01-22 17:14:02 +0100 | [diff] [blame] | 370 | |
| 371 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 372 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 373 | // list group |
| 374 | response = resource().path("group").path("list") |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 375 | .header(Attributes.AUTHORIZATION, |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 376 | handler.createBasicAuthorizationHeaderValue(username, |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 377 | "pass")) |
| 378 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 379 | .get(ClientResponse.class); |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 380 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 381 | String entity = response.getEntity(String.class); |
| 382 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 383 | JsonNode node = JsonUtils.readTree(entity); |
| 384 | node = node.get(0); |
| 385 | assertEquals(4, node.get("members").size()); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 386 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 387 | assertEquals("darla", node.at("/members/3/userId").asText()); |
| 388 | assertEquals(GroupMemberStatus.PENDING.name(), |
| 389 | node.at("/members/3/status").asText()); |
| 390 | assertEquals(PredefinedRole.USER_GROUP_MEMBER.name(), |
| 391 | node.at("/members/3/roles/0").asText()); |
| 392 | assertEquals(PredefinedRole.VC_ACCESS_MEMBER.name(), |
| 393 | node.at("/members/3/roles/1").asText()); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 396 | private void testInviteDeletedMember () throws UniformInterfaceException, |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 397 | ClientHandlerException, KustvaktException { |
| 398 | String[] members = new String[] { "marlin" }; |
| margaretha | b874ef5 | 2018-01-23 20:26:31 +0100 | [diff] [blame] | 399 | |
| 400 | UserGroupJson userGroup = new UserGroupJson(); |
| 401 | userGroup.setMembers(members); |
| 402 | // dory group |
| 403 | userGroup.setId(2); |
| 404 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 405 | ClientResponse response = resource().path("group").path("member") |
| 406 | .path("invite").type(MediaType.APPLICATION_JSON) |
| margaretha | b874ef5 | 2018-01-23 20:26:31 +0100 | [diff] [blame] | 407 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 408 | .header(Attributes.AUTHORIZATION, |
| 409 | handler.createBasicAuthorizationHeaderValue("dory", |
| 410 | "pass")) |
| 411 | .entity(userGroup).post(ClientResponse.class); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 412 | |
| 413 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 414 | |
| 415 | // check member |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 416 | JsonNode node = retrieveUserGroups("marlin"); |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 417 | assertEquals(2, node.size()); |
| 418 | JsonNode group = node.get(1); |
| 419 | assertEquals(GroupMemberStatus.PENDING.name(), |
| 420 | group.at("/userMemberStatus").asText()); |
| 421 | |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 422 | } |
| margaretha | 98ec15b | 2018-01-22 17:14:02 +0100 | [diff] [blame] | 423 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 424 | @Test |
| 425 | public void testInvitePendingMember () throws UniformInterfaceException, |
| 426 | ClientHandlerException, KustvaktException { |
| 427 | // marlin has status PENDING in dory group |
| 428 | String[] members = new String[] { "marlin" }; |
| 429 | |
| 430 | UserGroupJson userGroup = new UserGroupJson(); |
| 431 | userGroup.setMembers(members); |
| 432 | // dory group |
| 433 | userGroup.setId(2); |
| 434 | |
| 435 | ClientResponse response = resource().path("group").path("member") |
| 436 | .path("invite").type(MediaType.APPLICATION_JSON) |
| 437 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 438 | .header(Attributes.AUTHORIZATION, |
| 439 | handler.createBasicAuthorizationHeaderValue("dory", |
| 440 | "pass")) |
| 441 | .entity(userGroup).post(ClientResponse.class); |
| 442 | String entity = response.getEntity(String.class); |
| 443 | // System.out.println(entity); |
| 444 | JsonNode node = JsonUtils.readTree(entity); |
| 445 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 446 | assertEquals(StatusCodes.GROUP_MEMBER_EXISTS, |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 447 | node.at("/errors/0/0").asInt()); |
| margaretha | 7f0a4d4 | 2018-02-20 19:16:44 +0100 | [diff] [blame^] | 448 | assertEquals( |
| 449 | "Username marlin with status PENDING exists in the user-group " |
| 450 | + "dory group", |
| 451 | node.at("/errors/0/1").asText()); |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 452 | assertEquals("[marlin, PENDING, dory group]", |
| 453 | node.at("/errors/0/2").asText()); |
| 454 | } |
| 455 | |
| 456 | @Test |
| 457 | public void testInviteDeletedMember2 () throws UniformInterfaceException, |
| 458 | ClientHandlerException, KustvaktException { |
| 459 | // pearl has status deleted in dory group |
| 460 | String[] members = new String[] { "pearl" }; |
| 461 | |
| 462 | UserGroupJson userGroup = new UserGroupJson(); |
| 463 | userGroup.setMembers(members); |
| 464 | // dory group |
| 465 | userGroup.setId(2); |
| 466 | |
| 467 | ClientResponse response = resource().path("group").path("member") |
| 468 | .path("invite").type(MediaType.APPLICATION_JSON) |
| 469 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 470 | .header(Attributes.AUTHORIZATION, |
| 471 | handler.createBasicAuthorizationHeaderValue("dory", |
| 472 | "pass")) |
| 473 | .entity(userGroup).post(ClientResponse.class); |
| 474 | |
| margaretha | 7f0a4d4 | 2018-02-20 19:16:44 +0100 | [diff] [blame^] | 475 | String entity = response.getEntity(String.class); |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 476 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 477 | |
| 478 | // check member |
| 479 | JsonNode node = retrieveUserGroups("pearl"); |
| 480 | assertEquals(1, node.size()); |
| 481 | JsonNode group = node.get(0); |
| 482 | assertEquals(GroupMemberStatus.PENDING.name(), |
| 483 | group.at("/userMemberStatus").asText()); |
| 484 | |
| 485 | testDeletePendingMember(); |
| 486 | } |
| 487 | |
| 488 | |
| 489 | // marlin has GroupMemberStatus.PENDING in dory group |
| 490 | @Test |
| 491 | public void testSubscribePendingMember () throws KustvaktException { |
| 492 | MultivaluedMap<String, String> form = new MultivaluedMapImpl(); |
| 493 | form.add("groupId", "2"); |
| 494 | |
| 495 | ClientResponse response = resource().path("group").path("subscribe") |
| 496 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| 497 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 498 | .header(Attributes.AUTHORIZATION, |
| 499 | handler.createBasicAuthorizationHeaderValue("marlin", |
| 500 | "pass")) |
| 501 | .entity(form).post(ClientResponse.class); |
| 502 | |
| 503 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 504 | |
| 505 | // retrieve marlin group |
| 506 | JsonNode node = retrieveUserGroups("marlin"); |
| 507 | // System.out.println(node); |
| 508 | assertEquals(2, node.size()); |
| 509 | |
| 510 | JsonNode group = node.get(1); |
| 511 | assertEquals(2, group.at("/id").asInt()); |
| 512 | assertEquals("dory group", group.at("/name").asText()); |
| 513 | assertEquals("dory", group.at("/owner").asText()); |
| 514 | // group members are not allowed to see other members |
| 515 | assertEquals(0, group.at("/members").size()); |
| 516 | assertEquals(GroupMemberStatus.ACTIVE.name(), |
| 517 | group.at("/userMemberStatus").asText()); |
| 518 | |
| 519 | // unsubscribe marlin from dory group |
| 520 | testUnsubscribeActiveMember(form); |
| 521 | |
| 522 | // invite marlin to dory group to set back the GroupMemberStatus.PENDING |
| 523 | testInviteDeletedMember(); |
| 524 | } |
| 525 | |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 526 | // pearl has GroupMemberStatus.DELETED in dory group |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 527 | @Test |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 528 | public void testSubscribeDeletedMember () throws KustvaktException { |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 529 | MultivaluedMap<String, String> form = new MultivaluedMapImpl(); |
| margaretha | 45dde68 | 2018-01-04 21:33:46 +0100 | [diff] [blame] | 530 | form.add("groupId", "2"); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 531 | |
| 532 | ClientResponse response = resource().path("group").path("subscribe") |
| 533 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 534 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 535 | .header(Attributes.AUTHORIZATION, |
| 536 | handler.createBasicAuthorizationHeaderValue("pearl", |
| 537 | "pass")) |
| 538 | .entity(form).post(ClientResponse.class); |
| 539 | String entity = response.getEntity(String.class); |
| 540 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 98ec15b | 2018-01-22 17:14:02 +0100 | [diff] [blame] | 541 | |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 542 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | c9f1dfa | 2018-02-07 17:50:33 +0100 | [diff] [blame] | 543 | assertEquals(StatusCodes.GROUP_MEMBER_DELETED, |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 544 | node.at("/errors/0/0").asInt()); |
| margaretha | c9f1dfa | 2018-02-07 17:50:33 +0100 | [diff] [blame] | 545 | assertEquals("pearl has already been deleted from the group dory group", |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 546 | node.at("/errors/0/1").asText()); |
| 547 | } |
| margaretha | 98ec15b | 2018-01-22 17:14:02 +0100 | [diff] [blame] | 548 | |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 549 | @Test |
| 550 | public void testSubscribeMissingGroupId () throws KustvaktException { |
| 551 | ClientResponse response = resource().path("group").path("subscribe") |
| 552 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 553 | .header(Attributes.AUTHORIZATION, handler |
| 554 | .createBasicAuthorizationHeaderValue("bruce", "pass")) |
| 555 | .post(ClientResponse.class); |
| 556 | String entity = response.getEntity(String.class); |
| 557 | JsonNode node = JsonUtils.readTree(entity); |
| 558 | |
| 559 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | fc7d777 | 2018-01-16 17:48:17 +0100 | [diff] [blame] | 560 | assertEquals(StatusCodes.MISSING_ARGUMENT, |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 561 | node.at("/errors/0/0").asInt()); |
| 562 | assertEquals("groupId", node.at("/errors/0/1").asText()); |
| 563 | assertEquals("0", node.at("/errors/0/2").asText()); |
| 564 | } |
| 565 | |
| 566 | @Test |
| 567 | public void testSubscribeNonExistentMember () throws KustvaktException { |
| 568 | MultivaluedMap<String, String> form = new MultivaluedMapImpl(); |
| margaretha | 45dde68 | 2018-01-04 21:33:46 +0100 | [diff] [blame] | 569 | form.add("groupId", "2"); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 570 | |
| 571 | ClientResponse response = resource().path("group").path("subscribe") |
| 572 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| 573 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 574 | .header(Attributes.AUTHORIZATION, |
| 575 | handler.createBasicAuthorizationHeaderValue("bruce", |
| 576 | "pass")) |
| 577 | .entity(form).post(ClientResponse.class); |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 578 | String entity = response.getEntity(String.class); |
| margaretha | 98ec15b | 2018-01-22 17:14:02 +0100 | [diff] [blame] | 579 | // System.out.println(entity); |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 580 | JsonNode node = JsonUtils.readTree(entity); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 581 | |
| 582 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | c9f1dfa | 2018-02-07 17:50:33 +0100 | [diff] [blame] | 583 | assertEquals(StatusCodes.GROUP_MEMBER_NOT_FOUND, |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 584 | node.at("/errors/0/0").asInt()); |
| margaretha | c9f1dfa | 2018-02-07 17:50:33 +0100 | [diff] [blame] | 585 | assertEquals("bruce is not found in the group", |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 586 | node.at("/errors/0/1").asText()); |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 587 | } |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 588 | |
| 589 | @Test |
| 590 | public void testSubscribeToNonExistentGroup () throws KustvaktException { |
| 591 | MultivaluedMap<String, String> form = new MultivaluedMapImpl(); |
| 592 | form.add("groupId", "100"); |
| 593 | |
| 594 | ClientResponse response = resource().path("group").path("subscribe") |
| 595 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| 596 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 597 | .header(Attributes.AUTHORIZATION, |
| 598 | handler.createBasicAuthorizationHeaderValue("pearl", |
| 599 | "pass")) |
| 600 | .entity(form).post(ClientResponse.class); |
| 601 | String entity = response.getEntity(String.class); |
| margaretha | 98ec15b | 2018-01-22 17:14:02 +0100 | [diff] [blame] | 602 | // System.out.println(entity); |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 603 | JsonNode node = JsonUtils.readTree(entity); |
| 604 | |
| 605 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| margaretha | c9f1dfa | 2018-02-07 17:50:33 +0100 | [diff] [blame] | 606 | assertEquals(StatusCodes.GROUP_NOT_FOUND, |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 607 | node.at("/errors/0/0").asInt()); |
| margaretha | c9f1dfa | 2018-02-07 17:50:33 +0100 | [diff] [blame] | 608 | assertEquals("Group with id 100 is not found", |
| margaretha | 9d3eb04 | 2017-12-22 11:02:30 +0100 | [diff] [blame] | 609 | node.at("/errors/0/1").asText()); |
| 610 | } |
| 611 | |
| margaretha | 2c019fa | 2018-02-01 19:50:51 +0100 | [diff] [blame] | 612 | private void testUnsubscribeActiveMember ( |
| 613 | MultivaluedMap<String, String> form) |
| 614 | throws UniformInterfaceException, ClientHandlerException, |
| 615 | KustvaktException { |
| 616 | ClientResponse response = resource().path("group").path("unsubscribe") |
| 617 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| 618 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 619 | .header(Attributes.AUTHORIZATION, |
| 620 | handler.createBasicAuthorizationHeaderValue("marlin", |
| 621 | "pass")) |
| 622 | .entity(form).post(ClientResponse.class); |
| 623 | |
| 624 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 625 | |
| 626 | JsonNode node = retrieveUserGroups("marlin"); |
| 627 | assertEquals(1, node.size()); |
| 628 | } |
| 629 | |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 630 | @Test |
| 631 | public void testUnsubscribeDeletedMember () |
| 632 | throws UniformInterfaceException, ClientHandlerException, |
| 633 | KustvaktException { |
| 634 | // pearl unsubscribes from dory group |
| 635 | MultivaluedMap<String, String> form = new MultivaluedMapImpl(); |
| 636 | // dory group |
| 637 | form.add("groupId", "2"); |
| 638 | |
| 639 | ClientResponse response = resource().path("group").path("unsubscribe") |
| 640 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| 641 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 642 | .header(Attributes.AUTHORIZATION, |
| 643 | handler.createBasicAuthorizationHeaderValue("pearl", |
| 644 | "pass")) |
| 645 | .entity(form).post(ClientResponse.class); |
| 646 | |
| 647 | String entity = response.getEntity(String.class); |
| 648 | // System.out.println(entity); |
| 649 | JsonNode node = JsonUtils.readTree(entity); |
| 650 | assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 651 | assertEquals(StatusCodes.GROUP_MEMBER_DELETED, |
| 652 | node.at("/errors/0/0").asInt()); |
| 653 | assertEquals("pearl has already been deleted from the group dory group", |
| 654 | node.at("/errors/0/1").asText()); |
| 655 | assertEquals("[pearl, dory group]", node.at("/errors/0/2").asText()); |
| 656 | } |
| margaretha | 7f0a4d4 | 2018-02-20 19:16:44 +0100 | [diff] [blame^] | 657 | |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 658 | @Test |
| 659 | public void testUnsubscribePendingMember () |
| 660 | throws UniformInterfaceException, ClientHandlerException, |
| 661 | KustvaktException { |
| 662 | |
| 663 | JsonNode node = retrieveUserGroups("marlin"); |
| 664 | assertEquals(2, node.size()); |
| margaretha | 7f0a4d4 | 2018-02-20 19:16:44 +0100 | [diff] [blame^] | 665 | |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 666 | MultivaluedMap<String, String> form = new MultivaluedMapImpl(); |
| 667 | // dory group |
| 668 | form.add("groupId", "2"); |
| 669 | |
| 670 | ClientResponse response = resource().path("group").path("unsubscribe") |
| 671 | .type(MediaType.APPLICATION_FORM_URLENCODED) |
| 672 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 673 | .header(Attributes.AUTHORIZATION, |
| 674 | handler.createBasicAuthorizationHeaderValue("marlin", |
| 675 | "pass")) |
| 676 | .entity(form).post(ClientResponse.class); |
| 677 | |
| 678 | String entity = response.getEntity(String.class); |
| 679 | // System.out.println(entity); |
| 680 | assertEquals(Status.OK.getStatusCode(), response.getStatus()); |
| 681 | |
| margaretha | 7f0a4d4 | 2018-02-20 19:16:44 +0100 | [diff] [blame^] | 682 | node = retrieveUserGroups("marlin"); |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 683 | assertEquals(1, node.size()); |
| margaretha | 7f0a4d4 | 2018-02-20 19:16:44 +0100 | [diff] [blame^] | 684 | |
| margaretha | e6c711b | 2018-02-06 21:55:04 +0100 | [diff] [blame] | 685 | // invite marlin to dory group to set back the GroupMemberStatus.PENDING |
| 686 | testInviteDeletedMember(); |
| 687 | } |
| margaretha | 0b63de4 | 2017-12-20 18:48:09 +0100 | [diff] [blame] | 688 | } |