17
17
package org .springframework .security .authentication .ott ;
18
18
19
19
import java .time .Clock ;
20
+ import java .time .Duration ;
20
21
import java .time .Instant ;
21
22
import java .time .ZoneOffset ;
22
23
import java .time .temporal .ChronoUnit ;
23
- import java .util .List ;
24
24
25
25
import org .junit .jupiter .api .AfterEach ;
26
26
import org .junit .jupiter .api .BeforeEach ;
27
27
import org .junit .jupiter .api .Test ;
28
28
29
- import org .springframework .jdbc .core .ArgumentPreparedStatementSetter ;
30
29
import org .springframework .jdbc .core .JdbcOperations ;
31
30
import org .springframework .jdbc .core .JdbcTemplate ;
32
- import org .springframework .jdbc .core .PreparedStatementSetter ;
33
- import org .springframework .jdbc .core .SqlParameterValue ;
34
31
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabase ;
35
32
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
36
33
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseType ;
37
- import org .springframework .util .CollectionUtils ;
38
34
39
35
import static org .assertj .core .api .Assertions .assertThat ;
40
36
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
37
+ import static org .mockito .BDDMockito .given ;
38
+ import static org .mockito .Mockito .mock ;
41
39
42
40
/**
43
41
* Tests for {@link JdbcOneTimeTokenService}.
@@ -58,8 +56,6 @@ public class JdbcOneTimeTokenServiceTests {
58
56
59
57
private JdbcOneTimeTokenService oneTimeTokenService ;
60
58
61
- private final JdbcOneTimeTokenService .OneTimeTokenParametersMapper oneTimeTokenParametersMapper = new JdbcOneTimeTokenService .OneTimeTokenParametersMapper ();
62
-
63
59
@ BeforeEach
64
60
void setUp () {
65
61
this .db = createDb ();
@@ -115,7 +111,8 @@ void consumeWhenAuthenticationTokenIsNullThenThrowIllegalArgumentException() {
115
111
void generateThenTokenValueShouldBeValidUuidAndProvidedUsernameIsUsed () {
116
112
OneTimeToken oneTimeToken = this .oneTimeTokenService .generate (new GenerateOneTimeTokenRequest (USERNAME ));
117
113
118
- OneTimeToken persistedOneTimeToken = selectOneTimeToken (oneTimeToken .getTokenValue ());
114
+ OneTimeToken persistedOneTimeToken = this .oneTimeTokenService
115
+ .consume (new OneTimeTokenAuthenticationToken (oneTimeToken .getTokenValue ()));
119
116
assertThat (persistedOneTimeToken ).isNotNull ();
120
117
assertThat (persistedOneTimeToken .getUsername ()).isNotNull ();
121
118
assertThat (persistedOneTimeToken .getTokenValue ()).isNotNull ();
@@ -134,7 +131,8 @@ void consumeWhenTokenExistsThenReturnItself() {
134
131
assertThat (consumedOneTimeToken .getUsername ()).isNotNull ();
135
132
assertThat (consumedOneTimeToken .getTokenValue ()).isNotNull ();
136
133
assertThat (consumedOneTimeToken .getExpiresAt ()).isNotNull ();
137
- OneTimeToken persistedOneTimeToken = selectOneTimeToken (consumedOneTimeToken .getTokenValue ());
134
+ OneTimeToken persistedOneTimeToken = this .oneTimeTokenService
135
+ .consume (new OneTimeTokenAuthenticationToken (consumedOneTimeToken .getTokenValue ()));
138
136
assertThat (persistedOneTimeToken ).isNull ();
139
137
}
140
138
@@ -162,15 +160,19 @@ void consumeWhenTokenIsExpiredThenReturnNull() {
162
160
163
161
@ Test
164
162
void cleanupExpiredTokens () {
165
- OneTimeToken token1 = new DefaultOneTimeToken ("123" , USERNAME , Instant .now ().minusSeconds (300 ));
166
- OneTimeToken token2 = new DefaultOneTimeToken ("456" , USERNAME , Instant .now ().minusSeconds (300 ));
167
- saveToken (token1 );
168
- saveToken (token2 );
163
+ Clock clock = mock (Clock .class );
164
+ Instant fiveMinutesAgo = Instant .now ().minus (Duration .ofMinutes (5 ));
165
+ given (clock .instant ()).willReturn (fiveMinutesAgo );
166
+ this .oneTimeTokenService .setClock (clock );
167
+ OneTimeToken token1 = this .oneTimeTokenService .generate (new GenerateOneTimeTokenRequest (USERNAME ));
168
+ OneTimeToken token2 = this .oneTimeTokenService .generate (new GenerateOneTimeTokenRequest (USERNAME ));
169
169
170
170
this .oneTimeTokenService .cleanupExpiredTokens ();
171
171
172
- OneTimeToken deletedOneTimeToken1 = selectOneTimeToken ("123" );
173
- OneTimeToken deletedOneTimeToken2 = selectOneTimeToken ("456" );
172
+ OneTimeToken deletedOneTimeToken1 = this .oneTimeTokenService
173
+ .consume (new OneTimeTokenAuthenticationToken (token1 .getTokenValue ()));
174
+ OneTimeToken deletedOneTimeToken2 = this .oneTimeTokenService
175
+ .consume (new OneTimeTokenAuthenticationToken (token2 .getTokenValue ()));
174
176
assertThat (deletedOneTimeToken1 ).isNull ();
175
177
assertThat (deletedOneTimeToken2 ).isNull ();
176
178
}
@@ -186,23 +188,4 @@ void setCleanupChronWhenAlreadyNullThenNoException() {
186
188
this .oneTimeTokenService .setCleanupCron (null );
187
189
}
188
190
189
- private void saveToken (OneTimeToken oneTimeToken ) {
190
- List <SqlParameterValue > parameters = this .oneTimeTokenParametersMapper .apply (oneTimeToken );
191
- PreparedStatementSetter pss = new ArgumentPreparedStatementSetter (parameters .toArray ());
192
- this .jdbcOperations .update ("INSERT INTO one_time_tokens (token_value, username, expires_at) VALUES (?, ?, ?)" ,
193
- pss );
194
- }
195
-
196
- private OneTimeToken selectOneTimeToken (String tokenValue ) {
197
- // @formatter:off
198
- List <OneTimeToken > result = this .jdbcOperations .query (
199
- "select token_value, username, expires_at from one_time_tokens where token_value = ?" ,
200
- new JdbcOneTimeTokenService .OneTimeTokenRowMapper (), tokenValue );
201
- if (CollectionUtils .isEmpty (result )) {
202
- return null ;
203
- }
204
- return result .get (0 );
205
- // @formatter:on
206
- }
207
-
208
191
}
0 commit comments