/
developer.bami
/
DFM
Обзор
Документация
Войти
/
developer.bami
/
DFM
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
v1/src/test/java/com/bami/dfm/security/jwt/TokenAuthenticationSecurityMetersIT.java
93 строки
3 KB
Michael
Initial version of dfm generated by generator-jhipster@8.0.0-beta.1
28 янв 2024, 15:02
28 янв 2024, 15:02
a03b62a
Код
Авторство
О чём код?
package com.bami.dfm.security.jwt; import static com.bami.dfm.security.jwt.JwtAuthenticationTestUtils.*; import static org.assertj.core.api.Assertions.assertThat; import com.bami.dfm.IntegrationTest; import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import java.util.Collection; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.test.web.reactive.server.WebTestClient; @AutoConfigureWebTestClient(timeout = IntegrationTest.DEFAULT_TIMEOUT) @AuthenticationIntegrationTest class TokenAuthenticationSecurityMetersIT { private static final String INVALID_TOKENS_METER_EXPECTED_NAME = "security.authentication.invalid-tokens"; @Autowired private WebTestClient webTestClient; @Value("${jhipster.security.authentication.jwt.base64-secret}") private String jwtKey; @Autowired private MeterRegistry meterRegistry; @Test void testValidTokenShouldNotCountAnything() throws Exception { Collection<Counter> counters = meterRegistry.find(INVALID_TOKENS_METER_EXPECTED_NAME).counters(); var count = aggregate(counters); tryToAuthenticate(createValidToken(jwtKey)); assertThat(aggregate(counters)).isEqualTo(count); } @Test void testTokenExpiredCount() throws Exception { var count = meterRegistry.get(INVALID_TOKENS_METER_EXPECTED_NAME).tag("cause", "expired").counter().count(); tryToAuthenticate(createExpiredToken(jwtKey)); assertThat(meterRegistry.get(INVALID_TOKENS_METER_EXPECTED_NAME).tag("cause", "expired").counter().count()).isEqualTo(count + 1); } @Test void testTokenSignatureInvalidCount() throws Exception { var count = meterRegistry.get(INVALID_TOKENS_METER_EXPECTED_NAME).tag("cause", "invalid-signature").counter().count(); tryToAuthenticate(createTokenWithDifferentSignature()); assertThat(meterRegistry.get(INVALID_TOKENS_METER_EXPECTED_NAME).tag("cause", "invalid-signature").counter().count()) .isEqualTo(count + 1); } @Test void testTokenMalformedCount() throws Exception { var count = meterRegistry.get(INVALID_TOKENS_METER_EXPECTED_NAME).tag("cause", "malformed").counter().count(); tryToAuthenticate(createSignedInvalidJwt(jwtKey)); assertThat(meterRegistry.get(INVALID_TOKENS_METER_EXPECTED_NAME).tag("cause", "malformed").counter().count()).isEqualTo(count + 1); } @Test void testTokenInvalidCount() throws Exception { var count = meterRegistry.get(INVALID_TOKENS_METER_EXPECTED_NAME).tag("cause", "malformed").counter().count(); tryToAuthenticate(createInvalidToken(jwtKey)); assertThat(meterRegistry.get(INVALID_TOKENS_METER_EXPECTED_NAME).tag("cause", "malformed").counter().count()).isEqualTo(count + 1); } private void tryToAuthenticate(String token) { webTestClient .get() .uri("/api/authenticate") .headers(headers -> headers.setBearerAuth(token)) .exchange() .returnResult(String.class) .getResponseBody() .blockLast(); } private double aggregate(Collection<Counter> counters) { return counters.stream().mapToDouble(Counter::count).sum(); } }