Skip to content

Commit e8123a2

Browse files
committed
Add resetInvalidatedPayloadCount method and update tests to utilize it
1 parent fc83bf2 commit e8123a2

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

captchaservice-backend/src/main/java/de/muenchen/captchaservice/service/captcha/CaptchaService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,10 @@ private static String getPayloadHash(final Altcha.Payload payload) {
122122
public void decrementInvalidatedPayloadCount(long count) {
123123
invalidatedPayloadCount.addAndGet(-count);
124124
}
125+
126+
public void resetInvalidatedPayloadCount() {
127+
this.invalidatedPayloadCount.set(
128+
invalidatedPayloadRepository.countByExpiresAtGreaterThan(java.time.Instant.now())
129+
);
130+
}
125131
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import de.muenchen.captchaservice.controller.captcha.request.PostVerifyRequest;
77
import de.muenchen.captchaservice.data.ExtendedPayload;
88
import de.muenchen.captchaservice.repository.CaptchaRequestRepository;
9+
import de.muenchen.captchaservice.service.captcha.CaptchaService;
10+
import de.muenchen.captchaservice.service.expireddata.ExpiredDataService;
911
import de.muenchen.captchaservice.util.DatabaseTestUtil;
1012
import lombok.SneakyThrows;
1113
import org.altcha.altcha.Altcha;
@@ -83,6 +85,12 @@ class CaptchaControllerTest {
8385
@Value("${captcha.captcha-timeout-seconds}")
8486
private int captchaTimeoutSeconds;
8587

88+
@Autowired
89+
private ExpiredDataService expiredDataService;
90+
91+
@Autowired
92+
private CaptchaService captchaService;
93+
8694
@Test
8795
void postChallenge_basic() {
8896
final Altcha.ChallengeOptions challengeOptions = new Altcha.ChallengeOptions();
@@ -304,6 +312,7 @@ void testVerifyMetricsIncrement() {
304312
@SneakyThrows
305313
void testInvalidatedPayloadsGauge() {
306314
databaseTestUtil.clearDatabase();
315+
captchaService.resetInvalidatedPayloadCount();
307316
final PostVerifyRequest verifyRequest = new PostVerifyRequest(TEST_SITE_KEY, TEST_SITE_SECRET, TEST_PAYLOAD);
308317
final String verifyRequestBody = objectMapper.writeValueAsString(verifyRequest);
309318

@@ -324,7 +333,9 @@ void testInvalidatedPayloadsGauge() {
324333
.andExpect(status().isOk())
325334
.andExpect(jsonPath("$.measurements[0].value", is(1.0)));
326335

327-
Thread.sleep(captchaTimeoutSeconds * 1000); // Wait for the payload to expire
336+
// Simulate the expiration of the payload
337+
Thread.sleep(captchaTimeoutSeconds * 1000);
338+
expiredDataService.deleteExpiredData();
328339

329340
mockMvc.perform(get("/actuator/metrics/captcha.invalidated.payloads"))
330341
.andExpect(status().isOk())

0 commit comments

Comments
 (0)