|
| 1 | +package org.springframework.cloud.dataflow.unit.test; |
| 2 | + |
| 3 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 4 | + |
| 5 | +import dasniko.testcontainers.keycloak.KeycloakContainer; |
| 6 | +import org.awaitility.Awaitility; |
| 7 | +import org.junit.jupiter.api.Disabled; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | +import org.testcontainers.junit.jupiter.Container; |
| 12 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 13 | + |
| 14 | +import org.springframework.boot.CommandLineRunner; |
| 15 | +import org.springframework.boot.SpringApplication; |
| 16 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 17 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.cloud.dataflow.rest.client.DataFlowOperations; |
| 20 | +import org.springframework.cloud.dataflow.rest.client.config.DataFlowClientAutoConfiguration; |
| 21 | +import org.springframework.cloud.dataflow.rest.resource.about.AboutResource; |
| 22 | +import org.springframework.cloud.dataflow.server.single.DataFlowServerApplication; |
| 23 | +import org.springframework.context.ConfigurableApplicationContext; |
| 24 | +import org.springframework.test.context.ActiveProfiles; |
| 25 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 26 | +import org.springframework.test.context.DynamicPropertySource; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | + |
| 30 | +@ActiveProfiles("keycloak") |
| 31 | +@SpringBootTest(classes = { DataFlowServerApplication.class }, |
| 32 | + webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) |
| 33 | +@Testcontainers |
| 34 | +@Disabled("Determine how to run app and test client in different contexts") |
| 35 | +public class DataFlowAuthenticationTests { |
| 36 | + |
| 37 | + private static final Logger logger = LoggerFactory.getLogger(DataFlowAuthenticationTests.class); |
| 38 | + |
| 39 | + @Container |
| 40 | + static KeycloakContainer keycloakContainer = new KeycloakContainer("keycloak/keycloak:25.0") |
| 41 | + .withRealmImportFiles("/dataflow-realm.json", "/dataflow-users-0.json") |
| 42 | + .withAdminUsername("admin") |
| 43 | + .withAdminPassword("admin") |
| 44 | + .withExposedPorts(8080, 9000) |
| 45 | + .withLogConsumer(outputFrame -> { |
| 46 | + switch (outputFrame.getType()) { |
| 47 | + case STDERR: |
| 48 | + logger.error(outputFrame.getUtf8StringWithoutLineEnding()); |
| 49 | + break; |
| 50 | + default: |
| 51 | + logger.info(outputFrame.getUtf8StringWithoutLineEnding()); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + @DynamicPropertySource |
| 56 | + static void configureProperties(DynamicPropertyRegistry registry) { |
| 57 | + registry.add("keycloak.url", keycloakContainer::getAuthServerUrl); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testAuthentication() throws Exception { |
| 62 | + try (ConfigurableApplicationContext applicationContext = SpringApplication.run(CommandLineApp.class, |
| 63 | + "--spring.profiles.active=keycloak-client", |
| 64 | + "--spring.cloud.dataflow.client.authentication.basic.username=joe", |
| 65 | + "--spring.cloud.dataflow.client.authentication.basic.password=password", |
| 66 | + "--keycloak.url=" + keycloakContainer.getAuthServerUrl(), |
| 67 | + "--spring.cloud.dataflow.client.authentication.token-uri=" + keycloakContainer.getAuthServerUrl() |
| 68 | + + "/realms/dataflow/protocol/openid-connect/token")) { |
| 69 | + DataFlowOperations dataFlowOperations = applicationContext.getBean(DataFlowOperations.class); |
| 70 | + assertThat(dataFlowOperations).isNotNull(); |
| 71 | + AboutResource aboutResource = dataFlowOperations.aboutOperation().get(); |
| 72 | + assertThat(aboutResource).isNotNull(); |
| 73 | + assertThat(aboutResource.getSecurityInfo()).isNotNull(); |
| 74 | + assertThat(aboutResource.getSecurityInfo().isAuthenticated()).isTrue(); |
| 75 | + assertThat(aboutResource.getSecurityInfo().getUsername()).isEqualTo("joe"); |
| 76 | + CommandLineApp.completed.set(true); |
| 77 | + } |
| 78 | + finally { |
| 79 | + CommandLineApp.completed.set(true); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @SpringBootApplication |
| 84 | + @ImportAutoConfiguration(DataFlowClientAutoConfiguration.class) |
| 85 | + public static class CommandLineApp implements CommandLineRunner { |
| 86 | + |
| 87 | + public static AtomicBoolean completed = new AtomicBoolean(false); |
| 88 | + |
| 89 | + @Override |
| 90 | + public void run(String... args) throws Exception { |
| 91 | + Awaitility.await().until(() -> completed.get()); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments