Skip to content

Commit

Permalink
Update code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Crawford <steecraw@amazon.com>
  • Loading branch information
stephen-crawford committed May 16, 2023
1 parent c2d4129 commit 3f2d228
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ public void revokeToken(AuthToken token) {

@Override
public void resetToken(AuthToken token) {

if (token instanceof BasicAuthToken) {
final BasicAuthToken basicAuthToken = (BasicAuthToken) token;
basicAuthToken.revoke();
}
}

public String generatePassword() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.opensearch.OpenSearchException;
import org.opensearch.identity.noop.NoopTokenHandler;
import org.opensearch.identity.tokens.AuthToken;
import org.opensearch.identity.tokens.BasicAuthToken;
import org.opensearch.identity.tokens.NoopToken;
Expand All @@ -25,17 +27,19 @@

public class AuthTokenHandlerTests extends OpenSearchTestCase {

private ShiroTokenHandler authTokenHandler;
private ShiroTokenHandler shiroAuthTokenHandler;
private NoopTokenHandler noopTokenHandler;

@Before
public void testSetup() {
authTokenHandler = new ShiroTokenHandler();
shiroAuthTokenHandler = new ShiroTokenHandler();
noopTokenHandler = new NoopTokenHandler();
}

public void testShouldExtractBasicAuthTokenSuccessfully() {
final BasicAuthToken authToken = new BasicAuthToken("Basic YWRtaW46YWRtaW4="); // admin:admin

final AuthenticationToken translatedToken = authTokenHandler.translateAuthToken(authToken).get();
final AuthenticationToken translatedToken = shiroAuthTokenHandler.translateAuthToken(authToken).get();
assertThat(translatedToken, is(instanceOf(UsernamePasswordToken.class)));

final UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) translatedToken;
Expand All @@ -47,7 +51,7 @@ public void testShouldExtractBasicAuthTokenSuccessfully() {
public void testShouldExtractBasicAuthTokenSuccessfully_twoSemiColonPassword() {
final BasicAuthToken authToken = new BasicAuthToken("Basic dGVzdDp0ZTpzdA=="); // test:te:st

final AuthenticationToken translatedToken = authTokenHandler.translateAuthToken(authToken).get();
final AuthenticationToken translatedToken = shiroAuthTokenHandler.translateAuthToken(authToken).get();
assertThat(translatedToken, is(instanceOf(UsernamePasswordToken.class)));

final UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) translatedToken;
Expand All @@ -57,37 +61,59 @@ public void testShouldExtractBasicAuthTokenSuccessfully_twoSemiColonPassword() {
}

public void testShouldReturnNullWhenExtractingNullToken() {
final Optional<AuthenticationToken> translatedToken = authTokenHandler.translateAuthToken(null);
final Optional<AuthenticationToken> translatedToken = shiroAuthTokenHandler.translateAuthToken(null);

assertThat(translatedToken.isEmpty(), is(true));
}

public void testShouldRevokeTokenSuccessfully() {
final BasicAuthToken authToken = new BasicAuthToken("Basic dGVzdDp0ZTpzdA==");
assertTrue(authToken.toString().equals("Basic auth token with user=test, password=te:st"));
authTokenHandler.revokeToken(authToken);
shiroAuthTokenHandler.revokeToken(authToken);
assert (authToken.toString().equals("Basic auth token with user=, password="));
}

public void testShouldFailWhenRevokeToken() {
final NoopToken authToken = new NoopToken();
assert (authToken.getTokenIdentifier().equals("Noop"));
assertThrows(UnsupportedAuthenticationToken.class, () -> authTokenHandler.revokeToken(authToken));
assertThrows(UnsupportedAuthenticationToken.class, () -> shiroAuthTokenHandler.revokeToken(authToken));
}

public void testShouldGetTokenInfoSuccessfully() {
final BasicAuthToken authToken = new BasicAuthToken("Basic dGVzdDp0ZTpzdA==");
assert (authToken.toString().equals(authTokenHandler.getTokenInfo(authToken)));
assert (authToken.toString().equals(shiroAuthTokenHandler.getTokenInfo(authToken)));
final NoopToken noopAuthToken = new NoopToken();
assert (noopTokenHandler.getTokenInfo(noopAuthToken).equals("Token is NoopToken"));
}

public void testShouldFailGetTokenInfo() {
final NoopToken authToken = new NoopToken();
assert (authToken.getTokenIdentifier().equals("Noop"));
assertThrows(UnsupportedAuthenticationToken.class, () -> authTokenHandler.getTokenInfo(authToken));
assertThrows(UnsupportedAuthenticationToken.class, () -> shiroAuthTokenHandler.getTokenInfo(authToken));
}

public void testShouldFailValidateToken() {
final AuthToken authToken = new NoopToken();
assertFalse(authTokenHandler.validateToken(authToken));
assertFalse(shiroAuthTokenHandler.validateToken(authToken));
}

public void testShouldResetToken(AuthToken token) {
BasicAuthToken authToken = new BasicAuthToken("Basic dGVzdDp0ZTpzdA==");
shiroAuthTokenHandler.resetToken(authToken);
assert (authToken.getPassword().equals(""));
assert (authToken.getUser().equals(""));
}

public void testShouldPassThrough() {
final NoopToken authToken = new NoopToken();
noopTokenHandler.resetToken(authToken);
noopTokenHandler.revokeToken(authToken);
}

public void testShouldFailPassThrough() {
BasicAuthToken authToken = new BasicAuthToken("Basic dGVzdDp0ZTpzdA==");
assertThrows(OpenSearchException.class, () -> noopTokenHandler.resetToken(authToken));
assertThrows(OpenSearchException.class, () -> noopTokenHandler.revokeToken(authToken));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@

package org.opensearch.identity.noop;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.OpenSearchException;
import org.opensearch.identity.IdentityService;
import org.opensearch.identity.tokens.AuthToken;
import org.opensearch.identity.tokens.NoopToken;
import org.opensearch.identity.tokens.TokenManager;

/**
* This class represents a Noop Token Manager
*/
public class NoopTokenManager implements TokenManager {
public class NoopTokenHandler implements TokenManager {

private static final Logger log = LogManager.getLogger(IdentityService.class);

/**
* Generate a new Noop Token
Expand Down Expand Up @@ -58,7 +64,11 @@ public String getTokenInfo(AuthToken token) {
*/
@Override
public void revokeToken(AuthToken token) {

if (token instanceof NoopToken) {
log.info("Revoke operation is not supported for NoopTokens");
return;
}
throw new OpenSearchException("Token is not a NoopToken");
}

/**
Expand All @@ -67,6 +77,10 @@ public void revokeToken(AuthToken token) {
*/
@Override
public void resetToken(AuthToken token) {

if (token instanceof NoopToken) {
log.info("Reset operation is not supported for NoopTokens");
return;
}
throw new OpenSearchException("Token is not a NoopToken");
}
}

0 comments on commit 3f2d228

Please sign in to comment.