parent
0aa93036fa
commit
f7c16764e6
|
|
@ -223,6 +223,10 @@ public class SecurityProperties implements SecurityPrequisite {
|
|||
return this.role;
|
||||
}
|
||||
|
||||
public void setRole(List<String> role) {
|
||||
this.role = new ArrayList<String>(role);
|
||||
}
|
||||
|
||||
public boolean isDefaultPassword() {
|
||||
return this.defaultPassword;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import java.util.Collections;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.boot.bind.RelaxedDataBinder;
|
||||
|
|
@ -36,81 +37,88 @@ import static org.junit.Assert.assertTrue;
|
|||
*/
|
||||
public class SecurityPropertiesTests {
|
||||
|
||||
private SecurityProperties security = new SecurityProperties();
|
||||
private RelaxedDataBinder binder = new RelaxedDataBinder(this.security, "security");
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.binder.setIgnoreUnknownFields(false);
|
||||
this.binder.setConversionService(new DefaultConversionService());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredSingleValued() {
|
||||
SecurityProperties security = new SecurityProperties();
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(security, "security");
|
||||
binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
this.binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
"security.ignored", "/css/**")));
|
||||
assertFalse(binder.getBindingResult().hasErrors());
|
||||
assertEquals(1, security.getIgnored().size());
|
||||
assertFalse(this.binder.getBindingResult().hasErrors());
|
||||
assertEquals(1, this.security.getIgnored().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredEmpty() {
|
||||
SecurityProperties security = new SecurityProperties();
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(security, "security");
|
||||
binder.setConversionService(new DefaultConversionService());
|
||||
binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
this.binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
"security.ignored", "")));
|
||||
assertFalse(binder.getBindingResult().hasErrors());
|
||||
assertEquals(0, security.getIgnored().size());
|
||||
assertFalse(this.binder.getBindingResult().hasErrors());
|
||||
assertEquals(0, this.security.getIgnored().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredDisable() {
|
||||
SecurityProperties security = new SecurityProperties();
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(security, "security");
|
||||
binder.setConversionService(new DefaultConversionService());
|
||||
binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
this.binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
"security.ignored", "none")));
|
||||
assertFalse(binder.getBindingResult().hasErrors());
|
||||
assertEquals(1, security.getIgnored().size());
|
||||
assertFalse(this.binder.getBindingResult().hasErrors());
|
||||
assertEquals(1, this.security.getIgnored().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredMultiValued() {
|
||||
SecurityProperties security = new SecurityProperties();
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(security, "security");
|
||||
binder.setConversionService(new DefaultConversionService());
|
||||
binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
this.binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
"security.ignored", "/css/**,/images/**")));
|
||||
assertFalse(binder.getBindingResult().hasErrors());
|
||||
assertEquals(2, security.getIgnored().size());
|
||||
assertFalse(this.binder.getBindingResult().hasErrors());
|
||||
assertEquals(2, this.security.getIgnored().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindingIgnoredMultiValuedList() {
|
||||
SecurityProperties security = new SecurityProperties();
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(security, "security");
|
||||
binder.setConversionService(new DefaultConversionService());
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("security.ignored[0]", "/css/**");
|
||||
map.put("security.ignored[1]", "/foo/**");
|
||||
binder.bind(new MutablePropertyValues(map));
|
||||
assertFalse(binder.getBindingResult().hasErrors());
|
||||
assertEquals(2, security.getIgnored().size());
|
||||
assertTrue(security.getIgnored().contains("/foo/**"));
|
||||
this.binder.bind(new MutablePropertyValues(map));
|
||||
assertFalse(this.binder.getBindingResult().hasErrors());
|
||||
assertEquals(2, this.security.getIgnored().size());
|
||||
assertTrue(this.security.getIgnored().contains("/foo/**"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultPasswordAutogeneratedIfUnresolovedPlaceholder() {
|
||||
SecurityProperties security = new SecurityProperties();
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(security, "security");
|
||||
binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
this.binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
"security.user.password", "${ADMIN_PASSWORD}")));
|
||||
assertFalse(binder.getBindingResult().hasErrors());
|
||||
assertTrue(security.getUser().isDefaultPassword());
|
||||
assertFalse(this.binder.getBindingResult().hasErrors());
|
||||
assertTrue(this.security.getUser().isDefaultPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultPasswordAutogeneratedIfEmpty() {
|
||||
SecurityProperties security = new SecurityProperties();
|
||||
RelaxedDataBinder binder = new RelaxedDataBinder(security, "security");
|
||||
binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
this.binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
"security.user.password", "")));
|
||||
assertFalse(binder.getBindingResult().hasErrors());
|
||||
assertTrue(security.getUser().isDefaultPassword());
|
||||
assertFalse(this.binder.getBindingResult().hasErrors());
|
||||
assertTrue(this.security.getUser().isDefaultPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoles() {
|
||||
this.binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
"security.user.role", "USER,ADMIN")));
|
||||
assertFalse(this.binder.getBindingResult().hasErrors());
|
||||
assertEquals("[USER, ADMIN]", this.security.getUser().getRole().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRole() {
|
||||
this.binder.bind(new MutablePropertyValues(Collections.singletonMap(
|
||||
"security.user.role", "ADMIN")));
|
||||
assertFalse(this.binder.getBindingResult().hasErrors());
|
||||
assertEquals("[ADMIN]", this.security.getUser().getRole().toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue