From 2401b9dbb69afc5dba4a1ae3956fcbba0299cf91 Mon Sep 17 00:00:00 2001 From: Sam Leeflang Date: Thu, 21 Mar 2024 17:02:32 +0100 Subject: [PATCH] Add test for jwt token converter --- .../security/JwtAuthConverterTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/java/eu/dissco/orchestration/backend/security/JwtAuthConverterTest.java diff --git a/src/test/java/eu/dissco/orchestration/backend/security/JwtAuthConverterTest.java b/src/test/java/eu/dissco/orchestration/backend/security/JwtAuthConverterTest.java new file mode 100644 index 0000000..f683a0b --- /dev/null +++ b/src/test/java/eu/dissco/orchestration/backend/security/JwtAuthConverterTest.java @@ -0,0 +1,58 @@ +package eu.dissco.orchestration.backend.security; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.Instant; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.security.oauth2.jwt.Jwt; + +@ExtendWith(MockitoExtension.class) +class JwtAuthConverterTest { + + private JwtAuthConverter converter; + + @BeforeEach + void setup() { + converter = new JwtAuthConverter(); + } + + @Test + void testEmptyToken() { + // Given + var jwt = new Jwt("SomeRandomStringWithTheFullValue", Instant.now(), Instant.now(), + Map.of("kid", "SomeRandom", "typ", "JWT", "alg", "RS256"), + Map.of("sub", "adf294ba-bb03-4962-8042-a37f1648458e")); + + // When + var token = converter.convert(jwt); + + // Then + assertThat(token.isAuthenticated()).isTrue(); + assertThat(token.getName()).isEqualTo("adf294ba-bb03-4962-8042-a37f1648458e"); + assertThat(token.getAuthorities()).isEmpty(); + } + + @Test + void testTokenRoles() { + // Given + var jwt = new Jwt("SomeRandomStringWithTheFullValue", Instant.now(), Instant.now(), + Map.of("kid", "SomeRandom", "typ", "JWT", "alg", "RS256"), + Map.of("sub", "adf294ba-bb03-4962-8042-a37f1648458e", + "resource_access", Map.of("orchestration-service", + Map.of("roles", List.of("orchestration-admin"))))); + + // When + var token = converter.convert(jwt); + + // Then + assertThat(token.isAuthenticated()).isTrue(); + assertThat(token.getName()).isEqualTo("adf294ba-bb03-4962-8042-a37f1648458e"); + assertThat(token.getAuthorities()).hasSize(1); + } + +}