|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.security; |
| 20 | + |
| 21 | +import org.elasticsearch.client.security.user.User; |
| 22 | +import org.elasticsearch.common.Strings; |
| 23 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 24 | +import org.elasticsearch.common.xcontent.XContentType; |
| 25 | +import org.elasticsearch.test.ESTestCase; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 33 | +import static org.hamcrest.Matchers.instanceOf; |
| 34 | +import static org.hamcrest.Matchers.is; |
| 35 | + |
| 36 | +public class PutUserRequestTests extends ESTestCase { |
| 37 | + |
| 38 | + public void testBuildRequestWithPassword() throws Exception { |
| 39 | + final User user = new User("hawkeye", Arrays.asList("kibana_user", "avengers"), |
| 40 | + Collections.singletonMap("status", "active"), "Clinton Barton", null); |
| 41 | + final char[] password = "f@rmb0y".toCharArray(); |
| 42 | + final PutUserRequest request = PutUserRequest.withPassword(user, password, true, RefreshPolicy.IMMEDIATE); |
| 43 | + String json = Strings.toString(request); |
| 44 | + final Map<String, Object> requestAsMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), json, false); |
| 45 | + assertThat(requestAsMap.get("username"), is("hawkeye")); |
| 46 | + assertThat(requestAsMap.get("roles"), instanceOf(List.class)); |
| 47 | + assertThat((List<?>) requestAsMap.get("roles"), containsInAnyOrder("kibana_user", "avengers")); |
| 48 | + assertThat(requestAsMap.get("password"), is("f@rmb0y")); |
| 49 | + assertThat(requestAsMap.containsKey("password_hash"), is(false)); |
| 50 | + assertThat(requestAsMap.get("full_name"), is("Clinton Barton")); |
| 51 | + assertThat(requestAsMap.containsKey("email"), is(false)); |
| 52 | + assertThat(requestAsMap.get("enabled"), is(true)); |
| 53 | + assertThat(requestAsMap.get("metadata"), instanceOf(Map.class)); |
| 54 | + final Map<?, ?> metadata = (Map<?, ?>) requestAsMap.get("metadata"); |
| 55 | + assertThat(metadata.size(), is(1)); |
| 56 | + assertThat(metadata.get("status"), is("active")); |
| 57 | + } |
| 58 | + |
| 59 | + public void testBuildRequestWithPasswordHash() throws Exception { |
| 60 | + final User user = new User("hawkeye", Arrays.asList("kibana_user", "avengers"), |
| 61 | + Collections.singletonMap("status", "active"), "Clinton Barton", null); |
| 62 | + final char[] passwordHash = "$2a$04$iu1G4x3ZKVDNi6egZIjkFuIPja6elQXiBF1LdRVauV4TGog6FYOpi".toCharArray(); |
| 63 | + final PutUserRequest request = PutUserRequest.withPasswordHash(user, passwordHash, true, RefreshPolicy.IMMEDIATE); |
| 64 | + String json = Strings.toString(request); |
| 65 | + final Map<String, Object> requestAsMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), json, false); |
| 66 | + assertThat(requestAsMap.get("username"), is("hawkeye")); |
| 67 | + assertThat(requestAsMap.get("roles"), instanceOf(List.class)); |
| 68 | + assertThat((List<?>) requestAsMap.get("roles"), containsInAnyOrder("kibana_user", "avengers")); |
| 69 | + assertThat(requestAsMap.get("password_hash"), is("$2a$04$iu1G4x3ZKVDNi6egZIjkFuIPja6elQXiBF1LdRVauV4TGog6FYOpi")); |
| 70 | + assertThat(requestAsMap.containsKey("password"), is(false)); |
| 71 | + assertThat(requestAsMap.get("full_name"), is("Clinton Barton")); |
| 72 | + assertThat(requestAsMap.containsKey("email"), is(false)); |
| 73 | + assertThat(requestAsMap.get("enabled"), is(true)); |
| 74 | + assertThat(requestAsMap.get("metadata"), instanceOf(Map.class)); |
| 75 | + final Map<?, ?> metadata = (Map<?, ?>) requestAsMap.get("metadata"); |
| 76 | + assertThat(metadata.size(), is(1)); |
| 77 | + assertThat(metadata.get("status"), is("active")); |
| 78 | + } |
| 79 | + |
| 80 | + public void testBuildRequestForUpdateOnly() throws Exception { |
| 81 | + final User user = new User("hawkeye", Arrays.asList("kibana_user", "avengers"), |
| 82 | + Collections.singletonMap("status", "active"), "Clinton Barton", null); |
| 83 | + final char[] passwordHash = "$2a$04$iu1G4x3ZKVDNi6egZIjkFuIPja6elQXiBF1LdRVauV4TGog6FYOpi".toCharArray(); |
| 84 | + final PutUserRequest request = PutUserRequest.updateUser(user, true, RefreshPolicy.IMMEDIATE); |
| 85 | + String json = Strings.toString(request); |
| 86 | + final Map<String, Object> requestAsMap = XContentHelper.convertToMap(XContentType.JSON.xContent(), json, false); |
| 87 | + assertThat(requestAsMap.get("username"), is("hawkeye")); |
| 88 | + assertThat(requestAsMap.get("roles"), instanceOf(List.class)); |
| 89 | + assertThat((List<?>) requestAsMap.get("roles"), containsInAnyOrder("kibana_user", "avengers")); |
| 90 | + assertThat(requestAsMap.containsKey("password"), is(false)); |
| 91 | + assertThat(requestAsMap.containsKey("password_hash"), is(false)); |
| 92 | + assertThat(requestAsMap.get("full_name"), is("Clinton Barton")); |
| 93 | + assertThat(requestAsMap.containsKey("email"), is(false)); |
| 94 | + assertThat(requestAsMap.get("enabled"), is(true)); |
| 95 | + assertThat(requestAsMap.get("metadata"), instanceOf(Map.class)); |
| 96 | + final Map<?, ?> metadata = (Map<?, ?>) requestAsMap.get("metadata"); |
| 97 | + assertThat(metadata.size(), is(1)); |
| 98 | + assertThat(metadata.get("status"), is("active")); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments