Skip to content

Commit b114f2d

Browse files
committed
[Test] copied some Rspec tests for ValidatedPasswordSetting
1 parent b8b5ff8 commit b114f2d

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. 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 B.V. 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.logstash.settings;
20+
21+
import co.elastic.logstash.api.Password;
22+
import org.apache.logging.log4j.junit.LoggerContextRule;
23+
import org.apache.logging.log4j.test.appender.ListAppender;
24+
import org.junit.Before;
25+
import org.junit.ClassRule;
26+
import org.junit.Test;
27+
28+
import java.util.HashMap;
29+
import java.util.Map;
30+
31+
import static org.junit.Assert.assertNotNull;
32+
import static org.junit.Assert.assertThrows;
33+
import static org.junit.Assert.assertTrue;
34+
35+
/* Test copied from logstash-core/spec/logstash/settings_spec.rb */
36+
public class ValidatedPasswordSettingTest {
37+
38+
private static final String CONFIG = "log4j2-test1.xml";
39+
40+
@ClassRule
41+
public static LoggerContextRule CTX = new LoggerContextRule(CONFIG);
42+
43+
private Map<Object, Object> passwordPolicies;
44+
private ListAppender appender;
45+
46+
@Before
47+
public void setUp() {
48+
passwordPolicies = createPasswordPolicies();
49+
50+
appender = CTX.getListAppender("EventLogger").clear();
51+
}
52+
53+
private Map<Object, Object> createPasswordPolicies() {
54+
return new HashMap<>(Map.<Object, Object>of(
55+
"mode", "ERROR",
56+
"length", Map.of("minimum", 8),
57+
"include", Map.of(
58+
"upper", "REQUIRED",
59+
"lower", "REQUIRED",
60+
"digit", "REQUIRED",
61+
"symbol", "REQUIRED"
62+
)
63+
));
64+
}
65+
66+
@Test
67+
public void givenSomePasswordPolicies_whenCoercingSuppliedValueThatIsNotAPasswordInstance_thenThrowAnError() {
68+
final var ex = assertThrows(IllegalArgumentException.class, () -> {
69+
new ValidatedPasswordSetting("test.validated.password", "testPassword", passwordPolicies);
70+
});
71+
assertTrue(ex.getMessage().contains("Setting `test.validated.password` could not coerce LogStash::Util::Password value to password"));
72+
}
73+
74+
@Test
75+
public void givenSomePasswordPolicies_whenCoercingSuppliedAPasswordNotRespectingPolicies_thenThrowAnError() {
76+
Password password = new Password("Password!");
77+
final var ex = assertThrows(IllegalArgumentException.class, () -> {
78+
new ValidatedPasswordSetting("test.validated.password", password, passwordPolicies);
79+
});
80+
assertTrue(ex.getMessage().contains("Password must contain at least one digit between 0 and 9."));
81+
}
82+
83+
@Test
84+
public void givenSomePasswordPolicies_whenCoercingSuppliedAPasswordRespectingPolicies_thenValidationPasses() {
85+
Password password = new Password("Password123!");
86+
ValidatedPasswordSetting setting = new ValidatedPasswordSetting("test.validated.password", password, passwordPolicies);
87+
assertNotNull("new setting instance should be created", setting);
88+
}
89+
90+
@Test
91+
public void givenPasswordPoliciesWithWarnMode_whenCoercingPasswordNotConform_thenLogsWarningOnValidationFailure() {
92+
passwordPolicies.put("mode", "WARN");
93+
Password password = new Password("NoNumbers!");
94+
95+
new ValidatedPasswordSetting("test.validated.password", password, passwordPolicies);
96+
97+
boolean printStalling = appender.getMessages().stream().anyMatch((msg) -> msg.contains("Password must contain at least one digit between 0 and 9."));
98+
assertTrue(printStalling);
99+
}
100+
}

0 commit comments

Comments
 (0)