|
| 1 | +package io.kafbat.ui; |
| 2 | + |
| 3 | +import static io.kafbat.ui.AbstractIntegrationTest.LOCAL; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +import io.kafbat.ui.api.model.Action; |
| 10 | +import io.kafbat.ui.container.OpenLdapContainer; |
| 11 | +import io.kafbat.ui.model.AuthenticationInfoDTO; |
| 12 | +import io.kafbat.ui.model.ResourceTypeDTO; |
| 13 | +import io.kafbat.ui.model.UserPermissionDTO; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Objects; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; |
| 21 | +import org.springframework.boot.test.context.SpringBootTest; |
| 22 | +import org.springframework.context.ApplicationContextInitializer; |
| 23 | +import org.springframework.context.ConfigurableApplicationContext; |
| 24 | +import org.springframework.http.MediaType; |
| 25 | +import org.springframework.test.context.ActiveProfiles; |
| 26 | +import org.springframework.test.context.ContextConfiguration; |
| 27 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 28 | +import org.springframework.test.context.DynamicPropertySource; |
| 29 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 30 | +import org.springframework.web.reactive.function.BodyInserters; |
| 31 | + |
| 32 | +@SpringBootTest |
| 33 | +@ActiveProfiles("rbac-ldap") |
| 34 | +@AutoConfigureWebTestClient(timeout = "60000") |
| 35 | +@ContextConfiguration(initializers = {OpenLdapPIntegrationTest.Initializer.class}) |
| 36 | +class OpenLdapPIntegrationTest { |
| 37 | + private static final String SESSION = "SESSION"; |
| 38 | + private static final OpenLdapContainer LDAP_CONTAINER = new OpenLdapContainer(); |
| 39 | + |
| 40 | + @Autowired |
| 41 | + private WebTestClient webTestClient; |
| 42 | + |
| 43 | + @DynamicPropertySource |
| 44 | + static void neo4jProperties(DynamicPropertyRegistry registry) { |
| 45 | + registry.add("spring.ldap.urls", LDAP_CONTAINER::getLdapUrl); |
| 46 | + } |
| 47 | + |
| 48 | + @BeforeAll |
| 49 | + static void setup() { |
| 50 | + LDAP_CONTAINER.start(); |
| 51 | + } |
| 52 | + |
| 53 | + @AfterAll |
| 54 | + static void shutdown() { |
| 55 | + LDAP_CONTAINER.stop(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testUserPermissions() { |
| 60 | + AuthenticationInfoDTO info = authenticationInfo("johndoe"); |
| 61 | + |
| 62 | + assertNotNull(info); |
| 63 | + assertTrue(info.getRbacEnabled()); |
| 64 | + List<UserPermissionDTO> permissions = info.getUserInfo().getPermissions(); |
| 65 | + assertFalse(permissions.isEmpty()); |
| 66 | + assertTrue(permissions.stream().anyMatch(permission -> |
| 67 | + permission.getClusters().contains(LOCAL) |
| 68 | + && permission.getResource() == ResourceTypeDTO.TOPIC |
| 69 | + && permission.getActions().stream() |
| 70 | + .allMatch(action -> Action.fromValue(action.getValue()) != Action.ALL) |
| 71 | + ) |
| 72 | + ); |
| 73 | + assertEquals(permissions, authenticationInfo("johnwick").getUserInfo().getPermissions()); |
| 74 | + assertEquals(permissions, authenticationInfo("jacksmith").getUserInfo().getPermissions()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testEmptyPermissions() { |
| 79 | + assertTrue(Objects.requireNonNull(authenticationInfo("johnjames")) |
| 80 | + .getUserInfo() |
| 81 | + .getPermissions() |
| 82 | + .isEmpty() |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private String session(String name) { |
| 87 | + return Objects.requireNonNull( |
| 88 | + webTestClient |
| 89 | + .post() |
| 90 | + .uri("/login") |
| 91 | + .contentType(MediaType.APPLICATION_FORM_URLENCODED) |
| 92 | + .body(BodyInserters.fromFormData("username", name).with("password", name + "@kafbat.io")) |
| 93 | + .exchange() |
| 94 | + .expectStatus() |
| 95 | + .isFound() |
| 96 | + .returnResult(String.class) |
| 97 | + .getResponseCookies() |
| 98 | + .getFirst(SESSION)) |
| 99 | + .getValue(); |
| 100 | + } |
| 101 | + |
| 102 | + private AuthenticationInfoDTO authenticationInfo(String name) { |
| 103 | + return webTestClient |
| 104 | + .get() |
| 105 | + .uri("/api/authorization") |
| 106 | + .cookie(SESSION, session(name)) |
| 107 | + .exchange() |
| 108 | + .expectStatus() |
| 109 | + .isOk() |
| 110 | + .returnResult(AuthenticationInfoDTO.class) |
| 111 | + .getResponseBody() |
| 112 | + .blockFirst(); |
| 113 | + } |
| 114 | + |
| 115 | + public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { |
| 116 | + |
| 117 | + @Override |
| 118 | + public void initialize(ConfigurableApplicationContext context) { |
| 119 | + System.setProperty("spring.ldap.urls", LDAP_CONTAINER.getLdapUrl()); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments