|
19 | 19 | import org.springframework.boot.test.context.SpringBootTest;
|
20 | 20 | import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
21 | 21 | import org.springframework.http.MediaType;
|
| 22 | +import org.springframework.test.annotation.DirtiesContext; |
22 | 23 | import org.springframework.test.context.ActiveProfiles;
|
23 | 24 | import org.springframework.test.web.servlet.MockMvc;
|
24 | 25 | import org.testcontainers.containers.PostgreSQLContainer;
|
|
27 | 28 |
|
28 | 29 | import static de.muenchen.captchaservice.TestConstants.SPRING_NO_SECURITY_PROFILE;
|
29 | 30 | import static de.muenchen.captchaservice.TestConstants.SPRING_TEST_PROFILE;
|
| 31 | +import static org.hamcrest.Matchers.hasItem; |
30 | 32 | import static org.hamcrest.Matchers.is;
|
31 | 33 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
32 | 34 | import static org.junit.jupiter.api.Assertions.fail;
|
33 | 35 | import static org.mockito.ArgumentMatchers.argThat;
|
34 | 36 | import static org.mockito.ArgumentMatchers.eq;
|
| 37 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
35 | 38 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
36 | 39 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
37 | 40 |
|
38 | 41 | @SpringBootTest
|
39 | 42 | @AutoConfigureMockMvc
|
40 | 43 | @ActiveProfiles(profiles = { SPRING_TEST_PROFILE, SPRING_NO_SECURITY_PROFILE })
|
41 | 44 | @SuppressWarnings({ "PMD.AvoidUsingHardCodedIP", "PMD.AvoidDuplicateLiterals" })
|
| 45 | +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) |
42 | 46 | class CaptchaControllerTest {
|
43 | 47 |
|
44 | 48 | @Container
|
@@ -70,6 +74,7 @@ class CaptchaControllerTest {
|
70 | 74 |
|
71 | 75 | @Autowired
|
72 | 76 | private DatabaseTestUtil databaseTestUtil;
|
| 77 | + |
73 | 78 | @Autowired
|
74 | 79 | private CaptchaRequestRepository captchaRequestRepository;
|
75 | 80 |
|
@@ -230,4 +235,64 @@ void postVerify_expired() {
|
230 | 235 | }
|
231 | 236 | }
|
232 | 237 |
|
| 238 | + @Test |
| 239 | + @SneakyThrows |
| 240 | + void testChallengeMetricsIncrement() { |
| 241 | + int calls = 3; |
| 242 | + final PostChallengeRequest challengeRequest = new PostChallengeRequest(TEST_SITE_KEY, TEST_SITE_SECRET, "1.2.3.4"); |
| 243 | + final String challengeRequestBody = objectMapper.writeValueAsString(challengeRequest); |
| 244 | + |
| 245 | + for (int i = 1; i <= calls; i++) { |
| 246 | + mockMvc.perform( |
| 247 | + post("/api/v1/captcha/challenge") |
| 248 | + .content(challengeRequestBody) |
| 249 | + .contentType(MediaType.APPLICATION_JSON)) |
| 250 | + .andExpect(status().isOk()); |
| 251 | + |
| 252 | + mockMvc.perform( |
| 253 | + get("/actuator/metrics/captcha.challenge.requests")) |
| 254 | + .andExpect(status().isOk()) |
| 255 | + .andExpect(jsonPath("$.measurements[0].value", is((double) i))); |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + @Test |
| 260 | + @SneakyThrows |
| 261 | + void testVerifyMetricsIncrement() { |
| 262 | + databaseTestUtil.clearDatabase(); |
| 263 | + int calls = 4; |
| 264 | + final PostVerifyRequest verifyRequest = new PostVerifyRequest(TEST_SITE_KEY, TEST_SITE_SECRET, TEST_PAYLOAD); |
| 265 | + final String verifyRequestBody = objectMapper.writeValueAsString(verifyRequest); |
| 266 | + |
| 267 | + try (MockedStatic<Altcha> mock = Mockito.mockStatic(Altcha.class)) { |
| 268 | + mock.when(() -> Altcha.verifySolution( |
| 269 | + ArgumentMatchers.<Altcha.Payload>argThat(p -> p.algorithm.isEmpty()), |
| 270 | + eq(TEST_HMAC_KEY), |
| 271 | + eq(true))) |
| 272 | + .thenReturn(true); |
| 273 | + |
| 274 | + for (int i = 1; i <= calls; i++) { |
| 275 | + mockMvc.perform(post("/api/v1/captcha/verify") |
| 276 | + .content(verifyRequestBody) |
| 277 | + .contentType(MediaType.APPLICATION_JSON)) |
| 278 | + .andExpect(status().isOk()) |
| 279 | + .andExpect(jsonPath("$.valid", is(true))); |
| 280 | + |
| 281 | + mockMvc.perform(get("/actuator/metrics/captcha.verify.success")) |
| 282 | + .andExpect(status().isOk()) |
| 283 | + .andExpect(jsonPath("$.measurements[0].value", is((double) i))); |
| 284 | + |
| 285 | + mockMvc.perform(get("/actuator/metrics/captcha.verify.took.time")) |
| 286 | + .andExpect(status().isOk()) |
| 287 | + .andExpect(jsonPath("$.measurements[?(@.statistic=='COUNT')].value", hasItem((double) i))); |
| 288 | + |
| 289 | + mockMvc.perform(get("/actuator/metrics/captcha.verify.took.time")) |
| 290 | + .andExpect(status().isOk()) |
| 291 | + .andExpect(jsonPath("$.measurements[?(@.statistic=='TOTAL')].value", hasItem((double) i * TEST_PAYLOAD.getTook()))); |
| 292 | + |
| 293 | + databaseTestUtil.clearDatabase(); |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + |
233 | 298 | }
|
0 commit comments