20
20
import java .nio .file .Path ;
21
21
import java .util .stream .Collectors ;
22
22
23
- import org .junit .After ;
24
23
import org .junit .Rule ;
25
24
import org .junit .Test ;
26
25
import org .junit .rules .ExpectedException ;
27
26
28
27
import org .springframework .beans .factory .UnsatisfiedDependencyException ;
29
- import org .springframework .boot .WebApplicationType ;
30
- import org .springframework .boot .builder .SpringApplicationBuilder ;
28
+ import org .springframework .boot .autoconfigure .AutoConfigurations ;
31
29
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
32
- import org .springframework .boot .test .util .TestPropertyValues ;
33
- import org .springframework .context .ConfigurableApplicationContext ;
30
+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
34
31
import org .springframework .context .annotation .Configuration ;
35
32
import org .springframework .context .annotation .Import ;
36
- import org .springframework .core .env .ConfigurableEnvironment ;
37
- import org .springframework .core .env .StandardEnvironment ;
38
33
import org .springframework .core .io .ClassPathResource ;
39
34
import org .springframework .security .oauth2 .provider .token .TokenStore ;
40
35
import org .springframework .security .oauth2 .provider .token .store .JwtAccessTokenConverter ;
41
36
import org .springframework .security .oauth2 .provider .token .store .JwtTokenStore ;
42
37
43
38
import static org .assertj .core .api .Assertions .assertThat ;
44
- import static org .assertj .core .api .Assertions .assertThatCode ;
45
39
46
40
/**
47
41
* Tests for {@link AuthorizationServerTokenServicesConfiguration}.
48
42
*
49
43
* @author Harold Li
50
44
* @author Josh Cummings
45
+ * @author Vladimir Tsanev
51
46
* @since 2.1.0
52
47
*/
53
48
public class AuthorizationServerTokenServicesConfigurationTests {
54
49
55
- private ConfigurableApplicationContext context ;
56
-
57
- private ConfigurableEnvironment environment = new StandardEnvironment ();
50
+ private ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
51
+ .withConfiguration (AutoConfigurations .of (AuthorizationServerConfiguration .class ));
58
52
59
53
@ Rule
60
54
public ExpectedException thrown = ExpectedException .none ();
61
55
62
- @ After
63
- public void close () {
64
- if (this .context != null ) {
65
- this .context .close ();
66
- }
67
- }
68
-
69
56
@ Test
70
57
public void configureWhenPrivateKeyIsProvidedThenExposesJwtAccessTokenConverter () throws Exception {
71
58
Path privateKeyPath = new ClassPathResource ("key.private" , this .getClass ()).getFile ().toPath ();
72
59
String privateKey = Files .readAllLines (privateKeyPath ).stream ().collect (Collectors .joining ("\n " ));
73
60
74
- TestPropertyValues .of ("security.oauth2.authorization.jwt.key-value=" + privateKey ).applyTo (this .environment );
75
- this .context = new SpringApplicationBuilder (AuthorizationServerConfiguration .class )
76
- .environment (this .environment ).web (WebApplicationType .NONE ).run ();
77
-
78
- JwtAccessTokenConverter converter = this .context .getBean (JwtAccessTokenConverter .class );
79
- assertThat (converter .isPublic ()).isTrue ();
61
+ this .contextRunner .withPropertyValues ("security.oauth2.authorization.jwt.key-value=" + privateKey )
62
+ .run (context -> {
63
+ assertThat (context ).getBean (JwtAccessTokenConverter .class )
64
+ .satisfies (JwtAccessTokenConverter ::isPublic );
65
+ });
80
66
}
81
67
82
68
@ Test
83
69
public void configureWhenKeyStoreIsProvidedThenExposesJwtTokenStore () {
84
- TestPropertyValues . of (
70
+ this . contextRunner . withPropertyValues (
85
71
"security.oauth2.authorization.jwt.key-store=classpath:"
86
72
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks" ,
87
73
"security.oauth2.authorization.jwt.key-store-password=changeme" ,
88
- "security.oauth2.authorization.jwt.key-alias=jwt" ).applyTo (this .environment );
89
- this .context = new SpringApplicationBuilder (AuthorizationServerConfiguration .class )
90
- .environment (this .environment ).web (WebApplicationType .NONE ).run ();
91
- assertThat (this .context .getBeansOfType (TokenStore .class )).hasSize (1 );
92
- assertThat (this .context .getBean (TokenStore .class )).isInstanceOf (JwtTokenStore .class );
74
+ "security.oauth2.authorization.jwt.key-alias=jwt" ).run (context -> {
75
+ assertThat (context .getBeansOfType (TokenStore .class )).hasSize (1 );
76
+ assertThat (context .getBean (TokenStore .class )).isInstanceOf (JwtTokenStore .class );
77
+ });
93
78
}
94
79
95
80
@ Test
96
81
public void configureWhenKeyStoreIsProvidedThenExposesJwtAccessTokenConverter () {
97
- TestPropertyValues . of (
82
+ this . contextRunner . withPropertyValues (
98
83
"security.oauth2.authorization.jwt.key-store=classpath:"
99
84
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks" ,
100
85
"security.oauth2.authorization.jwt.key-store-password=changeme" ,
101
- "security.oauth2.authorization.jwt.key-alias=jwt" ).applyTo (this .environment );
102
- this .context = new SpringApplicationBuilder (AuthorizationServerConfiguration .class )
103
- .environment (this .environment ).web (WebApplicationType .NONE ).run ();
104
- assertThat (this .context .getBeansOfType (JwtAccessTokenConverter .class )).hasSize (1 );
86
+ "security.oauth2.authorization.jwt.key-alias=jwt" ).run (context -> {
87
+ assertThat (context .getBeansOfType (JwtAccessTokenConverter .class )).hasSize (1 );
88
+ });
105
89
}
106
90
107
91
@ Test
108
92
public void configureWhenKeyStoreIsProvidedWithKeyPasswordThenExposesJwtAccessTokenConverter () {
109
- TestPropertyValues . of (
93
+ this . contextRunner . withPropertyValues (
110
94
"security.oauth2.authorization.jwt.key-store=classpath:"
111
95
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keyhaspassword.jks" ,
112
96
"security.oauth2.authorization.jwt.key-store-password=changeme" ,
113
97
"security.oauth2.authorization.jwt.key-alias=jwt" ,
114
- "security.oauth2.authorization.jwt.key-password=password" ).applyTo (this .environment );
115
- this .context = new SpringApplicationBuilder (AuthorizationServerConfiguration .class )
116
- .environment (this .environment ).web (WebApplicationType .NONE ).run ();
117
- assertThat (this .context .getBeansOfType (JwtAccessTokenConverter .class )).hasSize (1 );
98
+ "security.oauth2.authorization.jwt.key-password=password" ).run (context -> {
99
+ assertThat (context .getBeansOfType (JwtAccessTokenConverter .class )).hasSize (1 );
100
+ });
118
101
}
119
102
120
103
@ Test
121
104
public void configureWhenKeyStoreIsProvidedButNoAliasThenThrowsException () {
122
- TestPropertyValues . of (
105
+ this . contextRunner . withPropertyValues (
123
106
"security.oauth2.authorization.jwt.key-store=classpath:"
124
107
+ "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks" ,
125
- "security.oauth2.authorization.jwt.key-store-password=changeme" ).applyTo (this .environment );
126
-
127
- assertThatCode (() -> new SpringApplicationBuilder (AuthorizationServerConfiguration .class )
128
- .environment (this .environment ).web (WebApplicationType .NONE ).run ())
129
- .isInstanceOf (UnsatisfiedDependencyException .class );
108
+ "security.oauth2.authorization.jwt.key-store-password=changeme" ).run (context -> {
109
+ assertThat (context ).getFailure ().isInstanceOf (UnsatisfiedDependencyException .class );
110
+ });
130
111
}
131
112
132
113
@ Test
133
114
public void configureWhenKeyStoreIsProvidedButNoPasswordThenThrowsException () {
134
- TestPropertyValues .of (
135
- "security.oauth2.authorization.jwt.key-store=classpath:"
136
- + "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks" ,
137
- "security.oauth2.authorization.jwt.key-alias=jwt" ).applyTo (this .environment );
138
-
139
- assertThatCode (() -> new SpringApplicationBuilder (AuthorizationServerConfiguration .class )
140
- .environment (this .environment ).web (WebApplicationType .NONE ).run ())
141
- .isInstanceOf (UnsatisfiedDependencyException .class );
115
+ this .contextRunner
116
+ .withPropertyValues (
117
+ "security.oauth2.authorization.jwt.key-store=classpath:"
118
+ + "org/springframework/boot/autoconfigure/security/oauth2/authserver/keystore.jks" ,
119
+ "security.oauth2.authorization.jwt.key-alias=jwt" )
120
+ .run (context -> assertThat (context ).getFailure ().isInstanceOf (UnsatisfiedDependencyException .class ));
142
121
}
143
122
144
123
@ Configuration
@@ -148,4 +127,4 @@ protected static class AuthorizationServerConfiguration {
148
127
149
128
}
150
129
151
- }
130
+ }
0 commit comments