| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.service.full; |
| 2 | |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertNotEquals; |
| 5 | import static org.junit.Assert.assertNotNull; |
| 6 | import static org.junit.Assert.assertTrue; |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 7 | |
| 8 | import java.io.IOException; |
| 9 | import java.net.URI; |
| 10 | import java.text.ParseException; |
| 11 | import java.util.HashMap; |
| 12 | import java.util.Map; |
| 13 | |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 14 | import javax.ws.rs.core.MediaType; |
| 15 | import javax.ws.rs.core.MultivaluedMap; |
| 16 | |
| 17 | import org.apache.commons.collections.map.LinkedMap; |
| 18 | import org.junit.BeforeClass; |
| 19 | import org.junit.Ignore; |
| 20 | import org.junit.Test; |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 21 | import org.springframework.beans.factory.annotation.Autowired; |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 22 | |
| 23 | import com.fasterxml.jackson.databind.JsonNode; |
| 24 | import com.nimbusds.jwt.SignedJWT; |
| 25 | import com.sun.jersey.api.client.ClientResponse; |
| 26 | import com.sun.jersey.core.util.MultivaluedMapImpl; |
| 27 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 28 | import de.ids_mannheim.korap.authentication.framework.HttpAuthorizationHandler; |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 29 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 30 | import de.ids_mannheim.korap.config.AuthenticationType; |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 31 | import de.ids_mannheim.korap.config.BeansFactory; |
| 32 | import de.ids_mannheim.korap.config.JWTSigner; |
| 33 | import de.ids_mannheim.korap.config.TestHelper; |
| 34 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 35 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| margaretha | e6f079d | 2017-10-09 15:57:27 +0200 | [diff] [blame] | 36 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 37 | import de.ids_mannheim.korap.utils.TimeUtils; |
| 38 | import de.ids_mannheim.korap.web.service.FastJerseyTest; |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 39 | |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 40 | /** EM: To do: not implemented in the new DB yet |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 41 | * @author hanl |
| 42 | * @date 21/03/2015 |
| 43 | */ |
| 44 | |
| 45 | // todo: do benchmarks for simple request to check access_token check and user |
| 46 | // retrieval! |
| margaretha | 843c463 | 2017-10-25 19:16:12 +0200 | [diff] [blame] | 47 | @Ignore |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 48 | public class UserServiceTest extends FastJerseyTest { |
| 49 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 50 | @Autowired |
| 51 | HttpAuthorizationHandler handler; |
| 52 | |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 53 | private static String[] credentials; |
| 54 | |
| 55 | @Override |
| 56 | public void initMethod() throws KustvaktException { |
| 57 | helper().setupAccount(); |
| 58 | } |
| 59 | |
| 60 | @BeforeClass |
| 61 | public static void setup() throws Exception { |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 62 | credentials = new String[2]; |
| 63 | credentials[0] = (String) TestHelper.getUserCredentials().get(Attributes.USERNAME); |
| 64 | credentials[1] = (String) TestHelper.getUserCredentials().get(Attributes.PASSWORD); |
| 65 | } |
| 66 | |
| 67 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 68 | public void testRegisterMissingFields() throws KustvaktException{ |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 69 | |
| 70 | MultivaluedMap map = new MultivaluedMapImpl(); |
| 71 | map.putSingle("username", "testuser"); // bodmer funktioniert noch nicht |
| 72 | map.putSingle("email", "hanl@ids-mannheim.de"); |
| 73 | map.putSingle("password", "testPassword1234"); |
| 74 | map.putSingle("firstName", "test"); |
| 75 | map.putSingle("lastName", "user"); |
| 76 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 77 | ClientResponse response = resource().path("user").path("register") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 78 | .header("Content-Type", MediaType.APPLICATION_JSON).post(ClientResponse.class, JsonUtils.toJSON(map)); |
| 79 | assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 80 | String ent = response.getEntity(String.class); |
| 81 | JsonNode node = JsonUtils.readTree(ent); |
| 82 | assertNotNull(node); |
| 83 | assertTrue(node.at("/errors/0/2").asText().contains("address")); |
| 84 | |
| 85 | // map.putSingle("address", "Mannheim"); |
| 86 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 87 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,"testuser", "testPassword1234"); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 88 | response = resource().path("user").path("info") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 89 | .header("Content-Type", MediaType.APPLICATION_JSON).header(Attributes.AUTHORIZATION, enc) |
| 90 | .get(ClientResponse.class); |
| 91 | |
| 92 | assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| 93 | } |
| 94 | |
| 95 | // test if user locked and what error message you get back |
| 96 | @Test |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 97 | public void testregisterWithoutConfirm() throws KustvaktException { |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 98 | MultivaluedMap map = new MultivaluedMapImpl(); |
| 99 | map.putSingle("username", "testuser2"); |
| 100 | map.putSingle("email", "hanl@ids-mannheim.de"); |
| 101 | map.putSingle("password", "testPassword1234"); |
| 102 | map.putSingle("firstName", "test"); |
| 103 | map.putSingle("lastName", "user"); |
| 104 | map.putSingle("address", "Mannheim"); |
| 105 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 106 | ClientResponse response = resource().path("user").path("register") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 107 | .header("Content-Type", MediaType.APPLICATION_JSON).post(ClientResponse.class, map); |
| 108 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 109 | |
| 110 | // run login/ status --> exception or information about locked account |
| 111 | // should appear |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 112 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,"testuser2", "testPassword1234"); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 113 | response = resource().path("user").path("info").header(Attributes.AUTHORIZATION, enc) |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 114 | .get(ClientResponse.class); |
| 115 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 116 | String ent = response.getEntity(String.class); |
| 117 | |
| 118 | } |
| 119 | |
| 120 | // should be run over https, since password is transmitted in plain text |
| 121 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 122 | public void testRegisterAndConfirm() throws KustvaktException{ |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 123 | MultivaluedMap map = new MultivaluedMapImpl(); |
| 124 | map.putSingle("username", "testuser"); |
| 125 | map.putSingle("email", "hanl@ids-mannheim.de"); |
| 126 | map.putSingle("password", "testPassword1234"); |
| 127 | map.putSingle("firstName", "test"); |
| 128 | map.putSingle("lastName", "user"); |
| 129 | map.putSingle("address", "Mannheim"); |
| 130 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 131 | ClientResponse response = resource().path("user").path("register") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 132 | .header("Content-Type", MediaType.APPLICATION_JSON).post(ClientResponse.class, map); |
| 133 | |
| 134 | String ent = response.getEntity(String.class); |
| 135 | |
| 136 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 137 | |
| 138 | JsonNode node = JsonUtils.readTree(ent); |
| 139 | assertNotNull(node); |
| 140 | |
| 141 | String conf_uri = node.path("confirm_uri").asText(); |
| 142 | response = resource().uri(URI.create(conf_uri)).get(ClientResponse.class); |
| 143 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 144 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 145 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,"testuser", "testPassword1234"); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 146 | response = resource().path("user").path("info").header(Attributes.AUTHORIZATION, enc) |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 147 | .get(ClientResponse.class); |
| 148 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 149 | } |
| 150 | |
| 151 | @Test |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 152 | public void loginHTTP() throws KustvaktException { |
| 153 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 154 | ClientResponse response = resource().path("user").path("info") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 155 | .header(Attributes.AUTHORIZATION, enc).get(ClientResponse.class); |
| 156 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 157 | } |
| 158 | |
| 159 | // EM: This test require VPN / IDS Intranet |
| 160 | @Test |
| margaretha | 96b9658 | 2017-06-27 11:08:32 +0200 | [diff] [blame] | 161 | @Ignore |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 162 | public void loginJWT() throws KustvaktException{ |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 163 | String en = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 164 | /* lauffähige Version von Hanl: */ |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 165 | ClientResponse response = resource().path("auth").path("apiToken") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 166 | .header(Attributes.AUTHORIZATION, en).get(ClientResponse.class); |
| 167 | /**/ |
| 168 | /* |
| 169 | * Test : ClientResponse response = null; WebResource webRes = |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 170 | * resource().path("auth") .path("apiToken"); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 171 | * webRes.header(Attributes.AUTHORIZATION, en); |
| 172 | * |
| 173 | * System.out.printf("resource: " + webRes.toString()); |
| 174 | * |
| 175 | * response = webRes.get(ClientResponse.class); |
| 176 | * |
| 177 | */ |
| 178 | |
| 179 | // assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 180 | String entity = response.getEntity(String.class); |
| 181 | System.out.println(entity); |
| 182 | JsonNode node = JsonUtils.readTree(entity); |
| 183 | assertEquals(2022, node.at("/errors/0/0").asInt()); |
| 184 | } |
| 185 | |
| 186 | // EM: cannot do test with LDAP |
| 187 | @Test |
| 188 | @Ignore |
| 189 | public void loginJWTExpired() throws InterruptedException, KustvaktException, ParseException { |
| 190 | |
| 191 | assertTrue(BeansFactory.getKustvaktContext().getConfiguration().getTokenTTL() < 10); |
| 192 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 193 | String en = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 194 | ClientResponse response = resource().path("auth").path("apiToken") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 195 | .header(Attributes.AUTHORIZATION, en).get(ClientResponse.class); |
| 196 | |
| 197 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 198 | |
| 199 | JsonNode node = JsonUtils.readTree(response.getEntity(String.class)); |
| 200 | assertNotNull(node); |
| 201 | String token = node.path("token").asText(); |
| 202 | |
| 203 | JWTSigner sign = new JWTSigner(BeansFactory.getKustvaktContext().getConfiguration().getSharedSecret(), |
| 204 | BeansFactory.getKustvaktContext().getConfiguration().getIssuer(), -1); |
| 205 | |
| 206 | SignedJWT jwt = sign.verifyToken(token); |
| 207 | |
| 208 | while (true) { |
| 209 | if (TimeUtils.isExpired(jwt.getJWTClaimsSet().getExpirationTimeClaim())) |
| 210 | break; |
| 211 | } |
| 212 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 213 | response = resource().path("user").path("info") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 214 | .header(Attributes.AUTHORIZATION, "api_token " + token).get(ClientResponse.class); |
| 215 | assertEquals(ClientResponse.Status.UNAUTHORIZED.getStatusCode(), response.getStatus()); |
| 216 | |
| 217 | } |
| 218 | |
| 219 | @Test |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 220 | public void testGetUserDetails() throws KustvaktException { |
| 221 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 222 | ClientResponse response = resource().path("user").path("details") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 223 | .header(Attributes.AUTHORIZATION, enc).get(ClientResponse.class); |
| 224 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 225 | } |
| 226 | |
| 227 | @Test |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 228 | public void testGetUserDetailsEmbeddedPointer() throws KustvaktException { |
| 229 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 230 | Map m = new LinkedMap(); |
| 231 | m.put("test", "[100, \"error message\", true, \"another message\"]"); |
| 232 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 233 | ClientResponse response = resource().path("user").path("details") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 234 | .header(Attributes.AUTHORIZATION, enc).header("Content-Type", MediaType.APPLICATION_JSON) |
| 235 | .post(ClientResponse.class, m); |
| 236 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 237 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 238 | response = resource().path("user").path("details").queryParam("pointer", "test") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 239 | .header(Attributes.AUTHORIZATION, enc).get(ClientResponse.class); |
| 240 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 241 | String ent = response.getEntity(String.class); |
| 242 | assertEquals("[100, \"error message\", true, \"another message\"]", ent); |
| 243 | } |
| 244 | |
| 245 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 246 | public void testUpdateUserDetailsMerge() throws KustvaktException{ |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 247 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 248 | Map m = new LinkedMap(); |
| 249 | m.put("test", "test value 1"); |
| 250 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 251 | ClientResponse response = resource().path("user").path("details") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 252 | .header(Attributes.AUTHORIZATION, enc).header("Content-Type", MediaType.APPLICATION_JSON) |
| 253 | .post(ClientResponse.class, m); |
| 254 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 255 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 256 | response = resource().path("user").path("details").header(Attributes.AUTHORIZATION, enc) |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 257 | .get(ClientResponse.class); |
| 258 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 259 | String ent = response.getEntity(String.class); |
| 260 | JsonNode node = JsonUtils.readTree(ent); |
| 261 | assertNotNull(node); |
| 262 | assertEquals("test value 1", node.at("/test").asText()); |
| 263 | assertEquals("user", node.at("/lastName").asText()); |
| 264 | assertEquals("test@ids-mannheim.de", node.at("/email").asText()); |
| 265 | } |
| 266 | |
| 267 | @Test |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 268 | public void testGetUserDetailsPointer() throws KustvaktException { |
| 269 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 270 | ClientResponse response = resource().path("user").path("details") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 271 | .queryParam("pointer", "email").header(Attributes.AUTHORIZATION, enc).get(ClientResponse.class); |
| 272 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 273 | String ent = response.getEntity(String.class); |
| 274 | assertEquals("test@ids-mannheim.de", ent); |
| 275 | } |
| 276 | |
| 277 | @Test |
| 278 | public void testGetUserDetailsNonExistent() throws KustvaktException { |
| 279 | helper().setupSimpleAccount("userservicetest", "servicepass"); |
| 280 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 281 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,"userservicetest", "servicepass"); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 282 | ClientResponse response = resource().path("user").path("details") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 283 | .header(Attributes.AUTHORIZATION, enc).get(ClientResponse.class); |
| 284 | assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(), response.getStatus()); |
| 285 | String entity = response.getEntity(String.class); |
| 286 | JsonNode node = JsonUtils.readTree(entity); |
| 287 | assertNotNull(node); |
| margaretha | f6d5a82 | 2017-10-19 19:51:20 +0200 | [diff] [blame] | 288 | assertEquals(StatusCodes.NO_RESULT_FOUND, node.at("/errors/0/0").asInt()); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 289 | assertEquals("UserDetails", node.at("/errors/0/2").asText()); |
| 290 | helper().dropUser("userservicetest"); |
| 291 | } |
| 292 | |
| 293 | @Test |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 294 | public void testGetUserSettings() throws KustvaktException { |
| 295 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 296 | ClientResponse response = resource().path("user").path("settings") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 297 | .header(Attributes.AUTHORIZATION, enc).get(ClientResponse.class); |
| 298 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 299 | } |
| 300 | |
| 301 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 302 | public void testUpdateUserDetailsJson() throws KustvaktException{ |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 303 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 304 | Map m = new LinkedMap(); |
| 305 | m.put("firstName", "newName"); |
| 306 | m.put("lastName", "newLastName"); |
| 307 | m.put("email", "newtest@ids-mannheim.de"); |
| 308 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 309 | ClientResponse response = resource().path("user").path("details") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 310 | .header(Attributes.AUTHORIZATION, enc).header("Content-Type", MediaType.APPLICATION_JSON) |
| 311 | .post(ClientResponse.class, m); |
| 312 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 313 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 314 | response = resource().path("user").path("details").header(Attributes.AUTHORIZATION, enc) |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 315 | .get(ClientResponse.class); |
| 316 | |
| 317 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 318 | JsonNode node = JsonUtils.readTree(response.getEntity(String.class)); |
| 319 | assertNotNull(node); |
| 320 | assertEquals("newName", node.path("firstName").asText()); |
| 321 | assertEquals("newLastName", node.path("lastName").asText()); |
| 322 | assertEquals("newtest@ids-mannheim.de", node.path("email").asText()); |
| 323 | assertEquals("Mannheim", node.path("address").asText()); |
| 324 | |
| 325 | m = new LinkedMap(); |
| 326 | m.put("firstName", "test"); |
| 327 | m.put("lastName", "user"); |
| 328 | m.put("email", "test@ids-mannheim.de"); |
| 329 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 330 | response = resource().path("user").path("details").header(Attributes.AUTHORIZATION, enc) |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 331 | .header("Content-Type", MediaType.APPLICATION_JSON).post(ClientResponse.class, m); |
| 332 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 333 | } |
| 334 | |
| 335 | @Test |
| 336 | @Ignore |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 337 | public void testUpdateUserSettingsForm() throws IOException, KustvaktException{ |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 338 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 339 | MultivaluedMap m = new MultivaluedMapImpl(); |
| 340 | m.putSingle("queryLanguage", "poliqarp_test"); |
| 341 | m.putSingle("pageLength", "200"); |
| 342 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 343 | ClientResponse response = resource().path("user").path("settings") |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 344 | .header(Attributes.AUTHORIZATION, enc).header("Content-Type", "application/x-www-form-urlencodeBase64d") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 345 | .get(ClientResponse.class); |
| 346 | |
| 347 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 348 | |
| 349 | JsonNode map = JsonUtils.readTree(response.getEntity(String.class)); |
| 350 | assertNotNull(map); |
| 351 | |
| 352 | assertNotEquals(m.getFirst("queryLanguage"), map.get("queryLanguage")); |
| 353 | assertNotEquals(m.get("pageLength"), Integer.valueOf((String) m.getFirst("pageLength"))); |
| 354 | |
| 355 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 356 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 357 | response = resource().path("user").path("settings").header(Attributes.AUTHORIZATION, enc) |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 358 | .header("Content-Type", "application/x-www-form-urlencodeBase64d").post(ClientResponse.class, m); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 359 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 360 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 361 | response = resource().path("user").path("settings").header(Attributes.AUTHORIZATION, enc) |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 362 | .header("Content-Type", "application/x-www-form-urlencodeBase64d").get(ClientResponse.class); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 363 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 364 | |
| 365 | map = JsonUtils.readTree(response.getEntity(String.class)); |
| 366 | assertNotNull(map); |
| 367 | |
| 368 | assertEquals(map.get("queryLanguage"), m.getFirst("queryLanguage")); |
| 369 | int p1 = map.path("pageLength").asInt(); |
| 370 | int p2 = Integer.valueOf((String) m.getFirst("pageLength")); |
| 371 | assertEquals(p1, p2); |
| 372 | } |
| 373 | |
| 374 | @Test |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 375 | public void testUpdateUserSettingsJson() throws IOException, KustvaktException { |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 376 | String enc = handler.createAuthorizationHeader(AuthenticationType.BASIC,credentials[0], credentials[1]); |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 377 | Map m = new HashMap<>(); |
| 378 | m.put("queryLanguage", "poliqarp_test"); |
| 379 | m.put("pageLength", "200"); |
| 380 | m.put("setting_1", "value_1"); |
| 381 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 382 | ClientResponse response = resource().path("user").path("settings") |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 383 | .header(Attributes.AUTHORIZATION, enc).header("Content-Type", MediaType.APPLICATION_JSON) |
| 384 | .get(ClientResponse.class); |
| 385 | |
| 386 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 387 | |
| 388 | JsonNode map = JsonUtils.readTree(response.getEntity(String.class)); |
| 389 | assertNotNull(map); |
| 390 | |
| 391 | assertNotEquals(m.get("queryLanguage"), map.get("queryLanguage")); |
| 392 | assertNotEquals(m.get("pageLength"), Integer.valueOf((String) m.get("pageLength"))); |
| 393 | |
| 394 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 395 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 396 | response = resource().path("user").path("settings").header(Attributes.AUTHORIZATION, enc) |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 397 | .header("Content-Type", MediaType.APPLICATION_JSON).post(ClientResponse.class, m); |
| 398 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 399 | |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 400 | response = resource().path("user").path("settings").header(Attributes.AUTHORIZATION, enc) |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 401 | .get(ClientResponse.class); |
| 402 | assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); |
| 403 | |
| 404 | map = JsonUtils.readTree(response.getEntity(String.class)); |
| 405 | assertNotNull(map); |
| 406 | |
| 407 | assertEquals(map.path("queryLanguage").asText(), m.get("queryLanguage")); |
| 408 | int p1 = map.path("pageLength").asInt(); |
| 409 | int p2 = Integer.valueOf((String) m.get("pageLength")); |
| 410 | assertEquals(p1, p2); |
| 411 | } |
| 412 | |
| 413 | @Test |
| 414 | public void registerInvalidPassword() { |
| 415 | final String CREDENTIALS_INVALID = "{\"username\":\"testuser2\"," |
| 416 | + "\"email\":\"hanl@ids-mannheim.de\",\"password\":\"testpassword\"}"; |
| 417 | // Response response = given() |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 418 | // .contentType("application/x-www-form-urlencodeBase64d").when() |
| margaretha | 37c5672 | 2017-05-31 12:43:48 +0200 | [diff] [blame] | 419 | // .body(CREDENTIALS_INVALID).post("/register"); |
| 420 | // String body = response.getBody().asString(); |
| 421 | // Assert.assertEquals("response is in error", true, |
| 422 | // body.contains("error")); |
| 423 | // JsonNode n = JsonUtils.readTree(body); |
| 424 | // String uri = n.path("uri").asText(); |
| 425 | // Response r = given().get(URI.create(uri)); |
| 426 | // System.out.println("RESPONSE " + r.getStatusCode()); |
| 427 | // System.out.println("RESPONSE BODY " + n); |
| 428 | } |
| 429 | |
| 430 | @Test |
| 431 | public void testLoginFailedLockAccount() { |
| 432 | |
| 433 | } |
| 434 | |
| 435 | @Test |
| 436 | @Ignore |
| 437 | public void delete() { |
| 438 | |
| 439 | } |
| 440 | |
| 441 | @Test |
| 442 | @Ignore |
| 443 | public void updatePassword() { |
| 444 | |
| 445 | } |
| 446 | |
| 447 | } |