2
2
3
3
import com .fasterxml .jackson .databind .ObjectMapper ;
4
4
import de .muenchen .captchaservice .TestConstants ;
5
+ import de .muenchen .captchaservice .configuration .captcha .CaptchaProperties ;
5
6
import de .muenchen .captchaservice .controller .captcha .request .PostChallengeRequest ;
6
7
import de .muenchen .captchaservice .controller .captcha .request .PostVerifyRequest ;
7
8
import de .muenchen .captchaservice .data .ExtendedPayload ;
17
18
import org .mockito .MockedStatic ;
18
19
import org .mockito .Mockito ;
19
20
import org .springframework .beans .factory .annotation .Autowired ;
20
- import org .springframework .beans .factory .annotation .Value ;
21
21
import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
22
22
import org .springframework .boot .test .context .SpringBootTest ;
23
23
import org .springframework .boot .testcontainers .service .connection .ServiceConnection ;
@@ -57,6 +57,7 @@ class CaptchaControllerTest {
57
57
private static final String TEST_SITE_KEY = "test_site" ;
58
58
private static final String TEST_SITE_SECRET = "test_secret" ;
59
59
private static final String TEST_HMAC_KEY = "secret" ;
60
+ private static final String TEST_CLIENT_ADDRESS = "1.2.3.4" ;
60
61
private static final ExtendedPayload TEST_PAYLOAD ;
61
62
62
63
static {
@@ -82,8 +83,7 @@ class CaptchaControllerTest {
82
83
private CaptchaRequestRepository captchaRequestRepository ;
83
84
84
85
@ Autowired
85
- @ Value ("${captcha.captcha-timeout-seconds}" )
86
- private int captchaTimeoutSeconds ;
86
+ private CaptchaProperties captchaProperties ;
87
87
88
88
@ Autowired
89
89
private ExpiredDataService expiredDataService ;
@@ -109,7 +109,7 @@ void postChallenge_basic() {
109
109
TEST_HMAC_KEY .equals (co .hmacKey ))))
110
110
.thenReturn (challenge );
111
111
112
- final PostChallengeRequest request = new PostChallengeRequest (TEST_SITE_KEY , TEST_SITE_SECRET , "1.2.3.4" );
112
+ final PostChallengeRequest request = new PostChallengeRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_CLIENT_ADDRESS );
113
113
final String requestBody = objectMapper .writeValueAsString (request );
114
114
// --
115
115
mockMvc .perform (
@@ -136,15 +136,15 @@ void postChallenge_basic() {
136
136
@ SneakyThrows
137
137
void postChallenge_validIpv4 () {
138
138
databaseTestUtil .clearDatabase ();
139
- final PostChallengeRequest request = new PostChallengeRequest (TEST_SITE_KEY , TEST_SITE_SECRET , "1.2.3.4" );
139
+ final PostChallengeRequest request = new PostChallengeRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_CLIENT_ADDRESS );
140
140
final String requestBody = objectMapper .writeValueAsString (request );
141
141
mockMvc .perform (
142
142
post ("/api/v1/captcha/challenge" )
143
143
.content (requestBody )
144
144
.contentType (MediaType .APPLICATION_JSON ))
145
145
.andExpect (status ().isOk ())
146
146
.andExpect (content ().contentType (MediaType .APPLICATION_JSON ));
147
- assertEquals (1 , captchaRequestRepository .countBySourceAddressHashIgnoreCase (DigestUtils .sha256Hex ("1.2.3.4" )));
147
+ assertEquals (1 , captchaRequestRepository .countBySourceAddressHashIgnoreCase (DigestUtils .sha256Hex (TEST_CLIENT_ADDRESS )));
148
148
}
149
149
150
150
@ Test
@@ -177,7 +177,7 @@ void postChallenge_invalidSourceAddress() {
177
177
@ Test
178
178
@ SneakyThrows
179
179
void postVerify_success () {
180
- final PostVerifyRequest request = new PostVerifyRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_PAYLOAD );
180
+ final PostVerifyRequest request = new PostVerifyRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_CLIENT_ADDRESS , TEST_PAYLOAD );
181
181
final String requestBody = objectMapper .writeValueAsString (request );
182
182
try (MockedStatic <Altcha > mock = Mockito .mockStatic (Altcha .class )) {
183
183
mock
@@ -201,7 +201,7 @@ void postVerify_success() {
201
201
@ Test
202
202
@ SneakyThrows
203
203
void postVerify_unauthorized () {
204
- final PostVerifyRequest request = new PostVerifyRequest ("test_site1" , "test_secret1" , TEST_PAYLOAD );
204
+ final PostVerifyRequest request = new PostVerifyRequest ("test_site1" , "test_secret1" , TEST_CLIENT_ADDRESS , TEST_PAYLOAD );
205
205
final String requestBody = objectMapper .writeValueAsString (request );
206
206
// --
207
207
mockMvc .perform (
@@ -215,7 +215,7 @@ void postVerify_unauthorized() {
215
215
@ SneakyThrows
216
216
void postVerify_expired () {
217
217
databaseTestUtil .clearDatabase ();
218
- final PostVerifyRequest request = new PostVerifyRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_PAYLOAD );
218
+ final PostVerifyRequest request = new PostVerifyRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_CLIENT_ADDRESS , TEST_PAYLOAD );
219
219
final String requestBody = objectMapper .writeValueAsString (request );
220
220
try (MockedStatic <Altcha > mock = Mockito .mockStatic (Altcha .class )) {
221
221
// Successful request
@@ -252,7 +252,7 @@ void postVerify_expired() {
252
252
@ SneakyThrows
253
253
void testChallengeMetricsIncrement () {
254
254
int calls = 3 ;
255
- final PostChallengeRequest challengeRequest = new PostChallengeRequest (TEST_SITE_KEY , TEST_SITE_SECRET , "1.2.3.4" );
255
+ final PostChallengeRequest challengeRequest = new PostChallengeRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_CLIENT_ADDRESS );
256
256
final String challengeRequestBody = objectMapper .writeValueAsString (challengeRequest );
257
257
258
258
for (int i = 1 ; i <= calls ; i ++) {
@@ -265,7 +265,9 @@ void testChallengeMetricsIncrement() {
265
265
mockMvc .perform (
266
266
get ("/actuator/metrics/captcha.challenge.requests" ))
267
267
.andExpect (status ().isOk ())
268
- .andExpect (jsonPath ("$.measurements[0].value" , is ((double ) i )));
268
+ .andExpect (jsonPath ("$.measurements[0].value" , is ((double ) i )))
269
+ .andExpect (jsonPath ("$.availableTags[?(@.tag=='difficulty')].values[0]" , hasItem ("1000" )))
270
+ .andExpect (jsonPath ("$.availableTags[?(@.tag=='site_key')].values[0]" , hasItem ("test_site" )));
269
271
}
270
272
}
271
273
@@ -274,7 +276,7 @@ void testChallengeMetricsIncrement() {
274
276
void testVerifyMetricsIncrement () {
275
277
databaseTestUtil .clearDatabase ();
276
278
int calls = 4 ;
277
- final PostVerifyRequest verifyRequest = new PostVerifyRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_PAYLOAD );
279
+ final PostVerifyRequest verifyRequest = new PostVerifyRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_CLIENT_ADDRESS , TEST_PAYLOAD );
278
280
final String verifyRequestBody = objectMapper .writeValueAsString (verifyRequest );
279
281
280
282
try (MockedStatic <Altcha > mock = Mockito .mockStatic (Altcha .class )) {
@@ -293,15 +295,17 @@ void testVerifyMetricsIncrement() {
293
295
294
296
mockMvc .perform (get ("/actuator/metrics/captcha.verify.success" ))
295
297
.andExpect (status ().isOk ())
296
- .andExpect (jsonPath ("$.measurements[0].value" , is ((double ) i )));
298
+ .andExpect (jsonPath ("$.measurements[0].value" , is ((double ) i )))
299
+ .andExpect (jsonPath ("$.availableTags[?(@.tag=='difficulty')].values[0]" , hasItem ("1000" )))
300
+ .andExpect (jsonPath ("$.availableTags[?(@.tag=='site_key')].values[0]" , hasItem ("test_site" )));
297
301
298
- mockMvc .perform (get ("/actuator/metrics/captcha.verify.took .time" ))
302
+ mockMvc .perform (get ("/actuator/metrics/captcha.client.solve .time" ))
299
303
.andExpect (status ().isOk ())
300
- .andExpect (jsonPath ("$.measurements[?(@.statistic=='COUNT')].value" , hasItem ((double ) i )));
301
-
302
- mockMvc . perform ( get ( "/actuator/metrics/captcha.verify.took.time" ))
303
- .andExpect (status (). isOk ( ))
304
- .andExpect (jsonPath ("$.measurements [?(@.statistic =='TOTAL ')].value " , hasItem (( double ) i * TEST_PAYLOAD . getTook () )));
304
+ .andExpect (jsonPath ("$.measurements[?(@.statistic=='COUNT')].value" , hasItem ((double ) i )))
305
+ . andExpect ( jsonPath ( "$.measurements[?(@.statistic=='TOTAL')].value" , hasItem (( double ) i * TEST_PAYLOAD . getTook ())))
306
+ . andExpect ( jsonPath ( "$.measurements[?(@.statistic=='MAX')].value" , hasItem (( double ) TEST_PAYLOAD . getTook ()) ))
307
+ .andExpect (jsonPath ( "$.availableTags[?(@.tag=='difficulty')].values[0]" , hasItem ( "1000" ) ))
308
+ .andExpect (jsonPath ("$.availableTags [?(@.tag =='site_key ')].values[0] " , hasItem ("test_site" )));
305
309
306
310
databaseTestUtil .clearDatabase ();
307
311
}
@@ -312,8 +316,7 @@ void testVerifyMetricsIncrement() {
312
316
@ SneakyThrows
313
317
void testInvalidatedPayloadsGauge () {
314
318
databaseTestUtil .clearDatabase ();
315
- captchaService .resetInvalidatedPayloadCount ();
316
- final PostVerifyRequest verifyRequest = new PostVerifyRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_PAYLOAD );
319
+ final PostVerifyRequest verifyRequest = new PostVerifyRequest (TEST_SITE_KEY , TEST_SITE_SECRET , TEST_CLIENT_ADDRESS , TEST_PAYLOAD );
317
320
final String verifyRequestBody = objectMapper .writeValueAsString (verifyRequest );
318
321
319
322
try (MockedStatic <Altcha > mock = Mockito .mockStatic (Altcha .class )) {
@@ -334,7 +337,7 @@ void testInvalidatedPayloadsGauge() {
334
337
.andExpect (jsonPath ("$.measurements[0].value" , is (1.0 )));
335
338
336
339
// Simulate the expiration of the payload
337
- Thread .sleep (captchaTimeoutSeconds * 1000 );
340
+ Thread .sleep (captchaProperties . captchaTimeoutSeconds () * 1000 );
338
341
expiredDataService .deleteExpiredData ();
339
342
340
343
mockMvc .perform (get ("/actuator/metrics/captcha.invalidated.payloads" ))
0 commit comments