Skip to content

Commit

Permalink
Add test for jwt token converter
Browse files Browse the repository at this point in the history
  • Loading branch information
samleeflang committed Mar 21, 2024
1 parent ec3938a commit 2401b9d
Showing 1 changed file with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 2401b9d

Please sign in to comment.