-
Notifications
You must be signed in to change notification settings - Fork 280
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: Ryan Liang <jiallian@amazon.com>
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/test/java/org/opensearch/security/authtoken/jwt/AuthTokenUtilsTest.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,67 @@ | ||
package org.opensearch.security.authtoken.jwt; | ||
|
||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.security.util.AuthTokenUtils; | ||
import org.opensearch.test.rest.FakeRestRequest; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class AuthTokenUtilsTest { | ||
|
||
@Test | ||
public void testIsAccessToRestrictedEndpointsForOnBehalfOfToken() { | ||
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(Collections.emptyList()); | ||
|
||
FakeRestRequest request = new FakeRestRequest.Builder(namedXContentRegistry).withPath("/api/generateonbehalfoftoken") | ||
.withMethod(RestRequest.Method.POST) | ||
.build(); | ||
|
||
assertTrue(AuthTokenUtils.isAccessToRestrictedEndpoints(request, "api/generateonbehalfoftoken")); | ||
} | ||
|
||
@Test | ||
public void testIsAccessToRestrictedEndpointsForAccount() { | ||
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(Collections.emptyList()); | ||
|
||
FakeRestRequest request = new FakeRestRequest.Builder(namedXContentRegistry).withPath("/api/account") | ||
.withMethod(RestRequest.Method.PUT) | ||
.build(); | ||
|
||
assertTrue(AuthTokenUtils.isAccessToRestrictedEndpoints(request, "api/account")); | ||
} | ||
|
||
@Test | ||
public void testIsAccessToRestrictedEndpointsFalseCase() { | ||
NamedXContentRegistry namedXContentRegistry = new NamedXContentRegistry(Collections.emptyList()); | ||
|
||
FakeRestRequest request = new FakeRestRequest.Builder(namedXContentRegistry).withPath("/api/someotherendpoint") | ||
.withMethod(RestRequest.Method.GET) | ||
.build(); | ||
|
||
assertFalse(AuthTokenUtils.isAccessToRestrictedEndpoints(request, "api/someotherendpoint")); | ||
} | ||
|
||
@Test | ||
public void testIsKeyNullWithNullValue() { | ||
Settings settings = Settings.builder().put("someKey", (String) null).build(); | ||
assertTrue(AuthTokenUtils.isKeyNull(settings, "someKey")); | ||
} | ||
|
||
@Test | ||
public void testIsKeyNullWithNonNullValue() { | ||
Settings settings = Settings.builder().put("someKey", "value").build(); | ||
assertFalse(AuthTokenUtils.isKeyNull(settings, "someKey")); | ||
} | ||
|
||
@Test | ||
public void testIsKeyNullWithAbsentKey() { | ||
Settings settings = Settings.builder().build(); | ||
assertTrue(AuthTokenUtils.isKeyNull(settings, "absentKey")); | ||
} | ||
} |