Skip to content

Commit 5d2feb1

Browse files
committed
Add unit tests
1 parent a317514 commit 5d2feb1

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.boot.test.context.SpringBootTest;
2020
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
2121
import org.springframework.http.MediaType;
22+
import org.springframework.test.annotation.DirtiesContext;
2223
import org.springframework.test.context.ActiveProfiles;
2324
import org.springframework.test.web.servlet.MockMvc;
2425
import org.testcontainers.containers.PostgreSQLContainer;
@@ -27,18 +28,21 @@
2728

2829
import static de.muenchen.captchaservice.TestConstants.SPRING_NO_SECURITY_PROFILE;
2930
import static de.muenchen.captchaservice.TestConstants.SPRING_TEST_PROFILE;
31+
import static org.hamcrest.Matchers.hasItem;
3032
import static org.hamcrest.Matchers.is;
3133
import static org.junit.jupiter.api.Assertions.assertEquals;
3234
import static org.junit.jupiter.api.Assertions.fail;
3335
import static org.mockito.ArgumentMatchers.argThat;
3436
import static org.mockito.ArgumentMatchers.eq;
37+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3538
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
3639
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
3740

3841
@SpringBootTest
3942
@AutoConfigureMockMvc
4043
@ActiveProfiles(profiles = { SPRING_TEST_PROFILE, SPRING_NO_SECURITY_PROFILE })
4144
@SuppressWarnings({ "PMD.AvoidUsingHardCodedIP", "PMD.AvoidDuplicateLiterals" })
45+
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
4246
class CaptchaControllerTest {
4347

4448
@Container
@@ -70,6 +74,7 @@ class CaptchaControllerTest {
7074

7175
@Autowired
7276
private DatabaseTestUtil databaseTestUtil;
77+
7378
@Autowired
7479
private CaptchaRequestRepository captchaRequestRepository;
7580

@@ -230,4 +235,64 @@ void postVerify_expired() {
230235
}
231236
}
232237

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+
233298
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ spring:
1313
# hibernate:
1414
# ddl-auto: create-drop
1515

16+
management:
17+
endpoints:
18+
web:
19+
exposure:
20+
include: metrics
21+
endpoint:
22+
metrics:
23+
access: read_only
24+
1625
security:
1726
# possible values: none, all, changing (With changing, only changing requests such as POST, PUT, DELETE are logged)
1827
# Currently only dummy values, will later be changed for testing security.

0 commit comments

Comments
 (0)