diff --git a/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java b/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java index a04880ebb12..784f9e214b9 100644 --- a/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java +++ b/elasticsearch/x-pack/security/src/test/java/org/elasticsearch/xpack/security/SecurityTests.java @@ -5,29 +5,71 @@ */ package org.elasticsearch.xpack.security; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; + import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; -import org.junit.Before; +import org.elasticsearch.threadpool.ThreadPool; +import org.elasticsearch.xpack.extensions.XPackExtension; +import org.elasticsearch.xpack.security.authc.Realm; +import org.elasticsearch.xpack.security.authc.Realms; +import org.elasticsearch.xpack.security.authc.file.FileRealm; + +import static org.mockito.Mockito.mock; public class SecurityTests extends ESTestCase { - private Environment env; + public static class DummyExtension extends XPackExtension { + private String realmType; + DummyExtension(String realmType) { + this.realmType = realmType; + } + @Override + public String name() { + return "dummy"; + } + @Override + public String description() { + return "dummy"; + } + @Override + public Map getRealms() { + return Collections.singletonMap(realmType, config -> null); + } + } - @Before - public void setupEnv() { - Settings settings = Settings.builder() + private Collection createComponents(Settings testSettings, XPackExtension... extensions) throws IOException { + Settings settings = Settings.builder().put(testSettings) .put("path.home", createTempDir()).build(); - env = new Environment(settings); + Environment env = new Environment(settings); + Security security = new Security(settings, env); + ThreadPool threadPool = mock(ThreadPool.class); + return security.createComponents(null, threadPool, null, Arrays.asList(extensions)); + } + + private T findComponent(Class type, Collection components) { + for (Object obj : components) { + if (type.isInstance(obj)) { + return type.cast(obj); + } + } + throw new AssertionError("Could not find component of type " + type + " in components"); } public void testCustomRealmExtension() throws Exception { - Security security = new Security(Settings.EMPTY, env); - - //security.createComponents(null, null, null, ) + Collection components = createComponents(Settings.EMPTY, new DummyExtension("myrealm")); + Realms realms = findComponent(Realms.class, components); + assertNotNull(realms.realmFactory("myrealm")); } public void testCustomRealmExtensionConflict() throws Exception { - + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> createComponents(Settings.EMPTY, new DummyExtension(FileRealm.TYPE))); + assertEquals("Realm type [" + FileRealm.TYPE + "] is already registered", e.getMessage()); } }