forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Craig Perkins <[email protected]>
- Loading branch information
Showing
2 changed files
with
58 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
server/src/test/java/org/opensearch/identity/noop/NoopPluginSubjectTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.identity.noop; | ||
|
||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.identity.IdentityService; | ||
import org.opensearch.identity.NamedPrincipal; | ||
import org.opensearch.identity.PluginSubject; | ||
import org.opensearch.plugins.IdentityAwarePlugin; | ||
import org.opensearch.plugins.Plugin; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.opensearch.threadpool.TestThreadPool; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.util.List; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class NoopPluginSubjectTests extends OpenSearchTestCase { | ||
public static class TestPlugin extends Plugin implements IdentityAwarePlugin { | ||
private PluginSubject subject; | ||
|
||
@Override | ||
public void assignSubject(PluginSubject subject) { | ||
this.subject = subject; | ||
} | ||
|
||
public PluginSubject getSubject() { | ||
return subject; | ||
} | ||
} | ||
|
||
public void testInitializeIdentityAwarePlugin() throws Exception { | ||
ThreadPool threadPool = new TestThreadPool(getTestName()); | ||
IdentityService identityService = new IdentityService(Settings.EMPTY, threadPool, List.of()); | ||
|
||
TestPlugin testPlugin = new TestPlugin(); | ||
identityService.initializeIdentityAwarePlugins(List.of(testPlugin)); | ||
|
||
PluginSubject testPluginSubject = new NoopPluginSubject(threadPool); | ||
assertThat(testPlugin.getSubject().getPrincipal().getName(), equalTo(NamedPrincipal.UNAUTHENTICATED.getName())); | ||
assertThat(testPluginSubject.getPrincipal().getName(), equalTo(NamedPrincipal.UNAUTHENTICATED.getName())); | ||
threadPool.getThreadContext().putHeader("test_header", "foo"); | ||
assertThat(threadPool.getThreadContext().getHeader("test_header"), equalTo("foo")); | ||
testPluginSubject.runAs(() -> { | ||
assertNull(threadPool.getThreadContext().getHeader("test_header")); | ||
return null; | ||
}); | ||
assertThat(threadPool.getThreadContext().getHeader("test_header"), equalTo("foo")); | ||
terminate(threadPool); | ||
} | ||
} |