|
20 | 20 | import java.util.HashMap;
|
21 | 21 | import java.util.Map;
|
22 | 22 |
|
| 23 | +import org.junit.Before; |
23 | 24 | import org.junit.Test;
|
24 | 25 | import org.springframework.beans.MutablePropertyValues;
|
25 | 26 | import org.springframework.boot.bind.RelaxedDataBinder;
|
|
36 | 37 | */
|
37 | 38 | public class SecurityPropertiesTests {
|
38 | 39 |
|
| 40 | + private SecurityProperties security = new SecurityProperties(); |
| 41 | + private RelaxedDataBinder binder = new RelaxedDataBinder(this.security, "security"); |
| 42 | + |
| 43 | + @Before |
| 44 | + public void init() { |
| 45 | + this.binder.setIgnoreUnknownFields(false); |
| 46 | + this.binder.setConversionService(new DefaultConversionService()); |
| 47 | + } |
| 48 | + |
39 | 49 | @Test
|
40 | 50 | public void testBindingIgnoredSingleValued() {
|
41 |
| - SecurityProperties security = new SecurityProperties(); |
42 |
| - RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); |
43 |
| - binder.bind(new MutablePropertyValues(Collections.singletonMap( |
| 51 | + this.binder.bind(new MutablePropertyValues(Collections.singletonMap( |
44 | 52 | "security.ignored", "/css/**")));
|
45 |
| - assertFalse(binder.getBindingResult().hasErrors()); |
46 |
| - assertEquals(1, security.getIgnored().size()); |
| 53 | + assertFalse(this.binder.getBindingResult().hasErrors()); |
| 54 | + assertEquals(1, this.security.getIgnored().size()); |
47 | 55 | }
|
48 | 56 |
|
49 | 57 | @Test
|
50 | 58 | public void testBindingIgnoredEmpty() {
|
51 |
| - SecurityProperties security = new SecurityProperties(); |
52 |
| - RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); |
53 |
| - binder.setConversionService(new DefaultConversionService()); |
54 |
| - binder.bind(new MutablePropertyValues(Collections.singletonMap( |
| 59 | + this.binder.bind(new MutablePropertyValues(Collections.singletonMap( |
55 | 60 | "security.ignored", "")));
|
56 |
| - assertFalse(binder.getBindingResult().hasErrors()); |
57 |
| - assertEquals(0, security.getIgnored().size()); |
| 61 | + assertFalse(this.binder.getBindingResult().hasErrors()); |
| 62 | + assertEquals(0, this.security.getIgnored().size()); |
58 | 63 | }
|
59 | 64 |
|
60 | 65 | @Test
|
61 | 66 | public void testBindingIgnoredDisable() {
|
62 |
| - SecurityProperties security = new SecurityProperties(); |
63 |
| - RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); |
64 |
| - binder.setConversionService(new DefaultConversionService()); |
65 |
| - binder.bind(new MutablePropertyValues(Collections.singletonMap( |
| 67 | + this.binder.bind(new MutablePropertyValues(Collections.singletonMap( |
66 | 68 | "security.ignored", "none")));
|
67 |
| - assertFalse(binder.getBindingResult().hasErrors()); |
68 |
| - assertEquals(1, security.getIgnored().size()); |
| 69 | + assertFalse(this.binder.getBindingResult().hasErrors()); |
| 70 | + assertEquals(1, this.security.getIgnored().size()); |
69 | 71 | }
|
70 | 72 |
|
71 | 73 | @Test
|
72 | 74 | public void testBindingIgnoredMultiValued() {
|
73 |
| - SecurityProperties security = new SecurityProperties(); |
74 |
| - RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); |
75 |
| - binder.setConversionService(new DefaultConversionService()); |
76 |
| - binder.bind(new MutablePropertyValues(Collections.singletonMap( |
| 75 | + this.binder.bind(new MutablePropertyValues(Collections.singletonMap( |
77 | 76 | "security.ignored", "/css/**,/images/**")));
|
78 |
| - assertFalse(binder.getBindingResult().hasErrors()); |
79 |
| - assertEquals(2, security.getIgnored().size()); |
| 77 | + assertFalse(this.binder.getBindingResult().hasErrors()); |
| 78 | + assertEquals(2, this.security.getIgnored().size()); |
80 | 79 | }
|
81 | 80 |
|
82 | 81 | @Test
|
83 | 82 | public void testBindingIgnoredMultiValuedList() {
|
84 |
| - SecurityProperties security = new SecurityProperties(); |
85 |
| - RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); |
86 |
| - binder.setConversionService(new DefaultConversionService()); |
87 | 83 | Map<String, String> map = new HashMap<String, String>();
|
88 | 84 | map.put("security.ignored[0]", "/css/**");
|
89 | 85 | map.put("security.ignored[1]", "/foo/**");
|
90 |
| - binder.bind(new MutablePropertyValues(map)); |
91 |
| - assertFalse(binder.getBindingResult().hasErrors()); |
92 |
| - assertEquals(2, security.getIgnored().size()); |
93 |
| - assertTrue(security.getIgnored().contains("/foo/**")); |
| 86 | + this.binder.bind(new MutablePropertyValues(map)); |
| 87 | + assertFalse(this.binder.getBindingResult().hasErrors()); |
| 88 | + assertEquals(2, this.security.getIgnored().size()); |
| 89 | + assertTrue(this.security.getIgnored().contains("/foo/**")); |
94 | 90 | }
|
95 | 91 |
|
96 | 92 | @Test
|
97 | 93 | public void testDefaultPasswordAutogeneratedIfUnresolovedPlaceholder() {
|
98 |
| - SecurityProperties security = new SecurityProperties(); |
99 |
| - RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); |
100 |
| - binder.bind(new MutablePropertyValues(Collections.singletonMap( |
| 94 | + this.binder.bind(new MutablePropertyValues(Collections.singletonMap( |
101 | 95 | "security.user.password", "${ADMIN_PASSWORD}")));
|
102 |
| - assertFalse(binder.getBindingResult().hasErrors()); |
103 |
| - assertTrue(security.getUser().isDefaultPassword()); |
| 96 | + assertFalse(this.binder.getBindingResult().hasErrors()); |
| 97 | + assertTrue(this.security.getUser().isDefaultPassword()); |
104 | 98 | }
|
105 | 99 |
|
106 | 100 | @Test
|
107 | 101 | public void testDefaultPasswordAutogeneratedIfEmpty() {
|
108 |
| - SecurityProperties security = new SecurityProperties(); |
109 |
| - RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); |
110 |
| - binder.bind(new MutablePropertyValues(Collections.singletonMap( |
| 102 | + this.binder.bind(new MutablePropertyValues(Collections.singletonMap( |
111 | 103 | "security.user.password", "")));
|
112 |
| - assertFalse(binder.getBindingResult().hasErrors()); |
113 |
| - assertTrue(security.getUser().isDefaultPassword()); |
| 104 | + assertFalse(this.binder.getBindingResult().hasErrors()); |
| 105 | + assertTrue(this.security.getUser().isDefaultPassword()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testRoles() { |
| 110 | + this.binder.bind(new MutablePropertyValues(Collections.singletonMap( |
| 111 | + "security.user.role", "USER,ADMIN"))); |
| 112 | + assertFalse(this.binder.getBindingResult().hasErrors()); |
| 113 | + assertEquals("[USER, ADMIN]", this.security.getUser().getRole().toString()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testRole() { |
| 118 | + this.binder.bind(new MutablePropertyValues(Collections.singletonMap( |
| 119 | + "security.user.role", "ADMIN"))); |
| 120 | + assertFalse(this.binder.getBindingResult().hasErrors()); |
| 121 | + assertEquals("[ADMIN]", this.security.getUser().getRole().toString()); |
114 | 122 | }
|
115 | 123 |
|
116 | 124 | }
|
0 commit comments