Skip to content

Commit

Permalink
Migrate to junit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
willmostly committed Sep 15, 2023
1 parent 7900850 commit 1e31ca4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 51 deletions.
6 changes: 6 additions & 0 deletions gateway-ha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
<version>42.6.0</version>
</dependency>
<!-- Test deps -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.trino.gateway.ha.security;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;

Expand All @@ -8,17 +10,16 @@
import org.apache.directory.api.ldap.model.message.SearchScope;
import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
import org.apache.directory.ldap.client.template.exception.PasswordException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

@Slf4j
public class LbLdapClientTest extends junit.framework.TestCase {
public class LbLdapClientTest {
class DummyPasswordWarning implements org.apache.directory.ldap.client.template.PasswordWarning {
@Override
public int getTimeBeforeExpiration() {
Expand Down Expand Up @@ -48,21 +49,21 @@ public boolean isChangeAfterReset() {

}

@BeforeMethod
@BeforeEach
public void initMocks() {
log.info("initializing test");
ldapConfig = LdapConfiguration.load("src/test/resources/auth/ldapTestConfig.yml");
org.mockito.MockitoAnnotations.initMocks(this);
}

@AfterMethod
@AfterEach
public void resetMocks() {
log.info("resetting mocks");
Mockito.reset(ldapConnectionTemplate);
Mockito.reset(ldapConfig);
}

@Test(enabled = false)
@Test
public void testAuthenticate() {
String user = "user1";
String password = "pass1";
Expand All @@ -78,7 +79,7 @@ public void testAuthenticate() {
.thenReturn(null);

//Success case
Assert.assertTrue(lbLdapClient.authenticate(user, password));
assertTrue(lbLdapClient.authenticate(user, password));

Mockito
.when(ldapConnectionTemplate.authenticate(ldapConfig.getLdapUserBaseDn(),
Expand All @@ -88,7 +89,7 @@ public void testAuthenticate() {
.thenReturn(new LbLdapClientTest.DummyPasswordWarning());

//Warning case
Assert.assertTrue(lbLdapClient.authenticate(user, password));
assertTrue(lbLdapClient.authenticate(user, password));


Mockito
Expand All @@ -99,7 +100,7 @@ public void testAuthenticate() {
.thenThrow(PasswordException.class);

//failure case
Assert.assertFalse(lbLdapClient.authenticate(user, password));
assertFalse(lbLdapClient.authenticate(user, password));

Mockito
.when(ldapConnectionTemplate.authenticate(ldapConfig.getLdapUserBaseDn(),
Expand All @@ -112,11 +113,11 @@ public void testAuthenticate() {
} catch (PasswordException ex) {
log.error("This should not fail");
//Force the test to fail
Assert.assertFalse(false);
assertFalse(false);
}
}

@Test(enabled = false)
@Test
public void testMemberof() {
String user = "user1";
String[] attributes = new String[]{"memberOf"};
Expand All @@ -137,7 +138,7 @@ public void testMemberof() {
String ret = lbLdapClient.getMemberOf(user);

log.info("ret is {}", ret);
Assert.assertTrue(ret.equals("Admin,User"));
assertTrue(ret.equals("Admin,User"));

org.mockito.Mockito
.when(ldapConnectionTemplate.search(eq(ldapConfig.getLdapUserBaseDn()),
Expand All @@ -148,7 +149,7 @@ public void testMemberof() {
.thenReturn(null);

//failure case
Assert.assertFalse(lbLdapClient.getMemberOf(user).equals("Admin,User"));
assertFalse(lbLdapClient.getMemberOf(user).equals("Admin,User"));

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.trino.gateway.ha.security;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.auth0.jwt.interfaces.Claim;
import io.trino.gateway.ha.config.AuthorizationConfiguration;
import java.util.Map;
Expand All @@ -12,16 +16,14 @@
import javax.ws.rs.core.SecurityContext;

import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.testng.annotations.BeforeClass;


@Slf4j
@Ignore
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class TestLbFilter {

private static final String USER = "username";
Expand All @@ -32,7 +34,7 @@ public class TestLbFilter {
private AuthorizationManager authorizationManager;
private ContainerRequestContext requestContext;

@BeforeClass(alwaysRun = true)
@BeforeAll
public void setup() throws Exception {

// Set authentication manager mock with 'sub' claim
Expand All @@ -44,12 +46,23 @@ public void setup() throws Exception {
Mockito
.when(oauthManager.getClaimsFromIdToken(ID_TOKEN))
.thenReturn(Optional.of(Map.of("sub", claim)));
Mockito.when(oauthManager.getUserIdField()).thenReturn("sub");

// Set authorization manager with membership
authorizationManager = Mockito.mock(AuthorizationManager.class);
Mockito
.when(authorizationManager.searchMemberOf(USER))
.thenReturn(MEMBER_OF);
Mockito
.when(authorizationManager.getPrivileges(USER))
.thenReturn(MEMBER_OF);
/*
This is required if LbAuthenticator.authenticate uses
`.map(sub -> new LbPrincipal(sub, authorizationManager.getPrivileges(sub)));`
If it should actually be using
LbPrincipal(sub, authorizationManager.getMemberOf(sub)))
then this should be removed
*/

// Request context for the auth filter
requestContext = Mockito.mock(ContainerRequestContext.class);
Expand All @@ -71,9 +84,11 @@ public void testSuccessfulCookieAuthentication() throws Exception {
Mockito
.when(requestContext.getHeaders())
.thenReturn(new MultivaluedHashMap());

LbAuthenticator authenticator = new LbAuthenticator(
oauthManager,
authorizationManager);

LbAuthorizer authorizer = new LbAuthorizer(configuration);
LbFilter<LbPrincipal> lbFilter = new LbFilter.Builder<LbPrincipal>()
.setAuthenticator(authenticator)
Expand All @@ -91,8 +106,8 @@ public void testSuccessfulCookieAuthentication() throws Exception {
.setSecurityContext(secContextCaptor.capture());

// Checks authorization for authenticated principal
Assert.assertTrue(secContextCaptor.getValue().isUserInRole("USER"));
Assert.assertFalse(secContextCaptor.getValue().isUserInRole("ADMIN"));
assertTrue(secContextCaptor.getValue().isUserInRole("USER"));
assertFalse(secContextCaptor.getValue().isUserInRole("ADMIN"));
}

@Test
Expand Down Expand Up @@ -126,38 +141,41 @@ public void testSuccessfulHeaderAuthentication() throws Exception {
lbFilter.filter(requestContext);

// SecurityContext must be set with the right authorizer at authentication
Mockito.verify(requestContext, Mockito.times(1)).setSecurityContext(secContextCaptor.capture());
Mockito.verify(requestContext, Mockito.atLeast(1))
.setSecurityContext(secContextCaptor.capture());

// Checks authorization for authenticated principal
Assert.assertTrue(secContextCaptor.getValue().isUserInRole("USER"));
Assert.assertTrue(secContextCaptor.getValue().isUserInRole("ADMIN"));
assertTrue(secContextCaptor.getValue().isUserInRole("USER"));
assertTrue(secContextCaptor.getValue().isUserInRole("ADMIN"));

}

@Test(expected = WebApplicationException.class)
@Test
public void testMissingAuthenticationToken() throws WebApplicationException {
AuthorizationConfiguration configuration = new AuthorizationConfiguration();

MultivaluedHashMap<String, String> headers = new MultivaluedHashMap<>();

Mockito
.when(requestContext.getCookies())
.thenReturn(Map.of());
Mockito
.when(requestContext.getHeaders())
.thenReturn(headers);
LbAuthenticator authenticator = new LbAuthenticator(
oauthManager,
authorizationManager);
LbAuthorizer authorizer = new LbAuthorizer(configuration);
LbFilter<LbPrincipal> lbFilter = new LbFilter.Builder<LbPrincipal>()
.setAuthenticator(authenticator)
.setAuthorizer(authorizer)
.setPrefix("Bearer")
.buildAuthFilter();

// Exception is thrown when the authentication fails
lbFilter.filter(requestContext);

assertThrows(WebApplicationException.class, () -> {

AuthorizationConfiguration configuration = new AuthorizationConfiguration();

MultivaluedHashMap<String, String> headers = new MultivaluedHashMap<>();

Mockito
.when(requestContext.getCookies())
.thenReturn(Map.of());
Mockito
.when(requestContext.getHeaders())
.thenReturn(headers);
LbAuthenticator authenticator = new LbAuthenticator(
oauthManager,
authorizationManager);
LbAuthorizer authorizer = new LbAuthorizer(configuration);
LbFilter<LbPrincipal> lbFilter = new LbFilter.Builder<LbPrincipal>()
.setAuthenticator(authenticator)
.setAuthorizer(authorizer)
.setPrefix("Bearer")
.buildAuthFilter();

// Exception is thrown when the authentication fails
lbFilter.filter(requestContext);
});
}
}
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,20 @@
</dependency>

<!-- Test deps -->
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>mockwebserver</artifactId>
Expand Down

0 comments on commit 1e31ca4

Please sign in to comment.