Skip to content

Commit 30d94ed

Browse files
committed
Add unit tests for invalidatedPayloads gauge
1 parent f3f1bb9 commit 30d94ed

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

captchaservice-backend/src/test/java/de/muenchen/captchaservice/controller/captcha/CaptchaControllerTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,36 @@ void testVerifyMetricsIncrement() {
295295
}
296296
}
297297

298+
@Test
299+
@SneakyThrows
300+
void testInvalidatedPayloadsGauge() {
301+
databaseTestUtil.clearDatabase();
302+
final PostVerifyRequest verifyRequest = new PostVerifyRequest(TEST_SITE_KEY, TEST_SITE_SECRET, TEST_PAYLOAD);
303+
final String verifyRequestBody = objectMapper.writeValueAsString(verifyRequest);
304+
305+
try (MockedStatic<Altcha> mock = Mockito.mockStatic(Altcha.class)) {
306+
mock.when(() -> Altcha.verifySolution(
307+
ArgumentMatchers.<Altcha.Payload>argThat(p -> p.algorithm.isEmpty()),
308+
eq(TEST_HMAC_KEY),
309+
eq(true)))
310+
.thenReturn(true);
311+
312+
mockMvc.perform(post("/api/v1/captcha/verify")
313+
.content(verifyRequestBody)
314+
.contentType(MediaType.APPLICATION_JSON))
315+
.andExpect(status().isOk())
316+
.andExpect(jsonPath("$.valid", is(true)));
317+
318+
mockMvc.perform(get("/actuator/metrics/captcha.invalidated.payloads"))
319+
.andExpect(status().isOk())
320+
.andExpect(jsonPath("$.measurements[0].value", is(1.0)));
321+
322+
Thread.sleep(5000); // Wait for the payload to expire
323+
324+
mockMvc.perform(get("/actuator/metrics/captcha.invalidated.payloads"))
325+
.andExpect(status().isOk())
326+
.andExpect(jsonPath("$.measurements[0].value", is(0.0)));
327+
}
328+
}
329+
298330
}

captchaservice-backend/src/test/resources/application-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ captcha:
4343
maxNumber: 2000
4444
- minVisits: 4
4545
maxNumber: 3000
46+
captcha-timeout-seconds: 5

0 commit comments

Comments
 (0)