Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to run on Maven 3.9 #17

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions gateway-ha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,28 +148,44 @@
<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>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.24.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</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,19 @@
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.junit.jupiter.api.extension.ExtendWith;
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;
import org.mockito.junit.jupiter.MockitoExtension;

@Slf4j
public class LbLdapClientTest extends junit.framework.TestCase {
@ExtendWith(MockitoExtension.class)
public class LbLdapClientTest {
class DummyPasswordWarning implements org.apache.directory.ldap.client.template.PasswordWarning {
@Override
public int getTimeBeforeExpiration() {
Expand All @@ -40,29 +44,30 @@ public boolean isChangeAfterReset() {
LdapConnectionTemplate ldapConnectionTemplate;

@Spy
LdapConfiguration ldapConfig;
LdapConfiguration ldapConfig =
LdapConfiguration.load("src/test/resources/auth/ldapTestConfig.yml");
@InjectMocks
LbLdapClient lbLdapClient;
LbLdapClient lbLdapClient =
new LbLdapClient(LdapConfiguration.load("src/test/resources/auth/ldapTestConfig.yml"));

LbLdapClientTest() {

}

@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 +83,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 +93,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 +104,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 +117,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 +142,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 +153,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,18 +46,21 @@ 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);

// Request context for the auth filter
requestContext = Mockito.mock(ContainerRequestContext.class);
}


@Test
public void testSuccessfulCookieAuthentication() throws Exception {

Expand All @@ -71,9 +76,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 +98,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 +133,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
willmostly marked this conversation as resolved.
Show resolved Hide resolved
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);
});
}
}
38 changes: 37 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<testng.version>6.10</testng.version>
<mockwebserver.version>1.2.1</mockwebserver.version>

<!-- remove these when airbase is merged -->
<dep.plugin.surefire.version>3.1.2</dep.plugin.surefire.version>
<dep.junit.version>5.10.0</dep.junit.version>
<!---->

<maven.checkstyle.plugin.version>3.0.0</maven.checkstyle.plugin.version>
<puppycrawl.tools.checkstyle.version>7.7</puppycrawl.tools.checkstyle.version>
<checkstyle.violation.ignore>NonEmptyAtclauseDescription,JavadocMethod,AbbreviationAsWordInName,MemberName
Expand Down Expand Up @@ -79,12 +84,20 @@
<scope>compile</scope>
</dependency>

<!-- Test deps -->
<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</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 Expand Up @@ -138,6 +151,29 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${dep.plugin.surefire.version}</version>
<dependencies>
<!-- allow both JUnit and TestNG -->
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
<version>${dep.plugin.surefire.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-testng</artifactId>
<version>${dep.plugin.surefire.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${dep.junit.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>