Skip to content

Commit

Permalink
Execute filterChain when starting integration tests (login possible v…
Browse files Browse the repository at this point in the history
…ia basic auth - authorization header)
  • Loading branch information
eschleb committed Dec 5, 2024
1 parent 6dcc754 commit f5d0d1f
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 343 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -114,6 +119,11 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
</dependency>
<dependency>
<groupId>info.magnolia</groupId>
<artifactId>magnolia-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MagnoliaIntegrationBeforeAllTestExtension implements BeforeAllCallb
@Override
public void beforeAll(final ExtensionContext context) throws Exception {
magnoliaIntegrationTestInitializer.init(context);
magnoliaIntegrationTestInitializer.start();
magnoliaIntegrationTestInitializer.start(true);
new RepositoryUtil().load(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MagnoliaIntegrationTestExtension implements BeforeEachCallback, Aft
@Override
public void beforeEach(final ExtensionContext context) throws Exception {
magnoliaIntegrationTestInitializer.init(context);
magnoliaIntegrationTestInitializer.start();
magnoliaIntegrationTestInitializer.start(true);
new RepositoryUtil().load(context);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.merkle.oss.magnolia.testing.configuration;

import info.magnolia.cms.security.SecuritySupport;
import info.magnolia.cms.security.User;
import info.magnolia.cms.filters.MgnlMainFilter;
import info.magnolia.cms.util.CustomFilterConfig;
import info.magnolia.context.AbstractSystemContext;
import info.magnolia.context.DefaultRepositoryStrategy;
import info.magnolia.context.MgnlContext;
import info.magnolia.context.SystemContext;
import info.magnolia.context.WebContext;
import info.magnolia.init.MagnoliaConfigurationProperties;
import info.magnolia.init.MagnoliaServletContextListener;
import info.magnolia.license.LicenseManager;
Expand All @@ -21,19 +19,18 @@
import info.magnolia.objectfactory.guice.GuiceComponentProviderBuilder;
import info.magnolia.repository.RepositoryManager;
import info.magnolia.test.TestMagnoliaConfigurationProperties;
import info.magnolia.test.mock.MockWebContext;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.commons.lang3.time.StopWatch;
import org.junit.jupiter.api.extension.ExtensionContext;
Expand All @@ -43,7 +40,8 @@
import com.machinezoo.noexception.Exceptions;
import com.merkle.oss.magnolia.testing.module.LicenseManagerProvider;
import com.merkle.oss.magnolia.testing.properties.IntegrationTestMagnoliaConfigurationProperties;
import com.merkle.oss.magnolia.testing.servlet.MockServletContext;
import com.merkle.oss.magnolia.testing.servlet.MockFilterChain;
import com.merkle.oss.magnolia.testing.servlet.ServletContextProvider;

public class MagnoliaIntegrationTestInitializer {

Expand All @@ -70,44 +68,45 @@ public void init(final ExtensionContext extensionContext) throws Exception {
throw new RuntimeException("Failed to init: " + e.getErrorMessages(), e);
}
watch.stop();
System.out.println("Initialization took "+watch.getDuration().toMillis()+"ms");
System.out.println("Initialization took " + watch.getDuration().toMillis() + "ms");
}

public void destroy() {
((AbstractSystemContext)Components.getComponent(SystemContext.class)).getRepositoryStrategy().release();
((AbstractSystemContext) Components.getComponent(SystemContext.class)).getRepositoryStrategy().release();
MgnlContext.setInstance(null);
}

public void start() throws ModuleManagementException {
public void start(final boolean executeFilterChain) throws ModuleManagementException, ServletException, IOException {
final StopWatch watch = new StopWatch();
watch.start();
System.setProperty("productionMode", "true"); //com.vaadin.server.Constants.SERVLET_PARAMETER_PRODUCTION_MODE
final ModuleManager moduleManager = Components.getComponent(ModuleManager.class);
moduleManager.checkForInstallOrUpdates();
moduleManager.performInstallOrUpdate();
moduleManager.startModules();
MgnlContext.setInstance(getUserContext());
if(executeFilterChain) {
executeFilterChain();
}
watch.stop();
System.out.println("Start took "+watch.getDuration().toMillis()+"ms");
System.out.println("Start took " + watch.getDuration().toMillis() + "ms");
}

private void executeFilterChain() throws ServletException, IOException {
final ServletContextProvider servletContextProvider = Components.getComponent(ServletContextProvider.class);
final MgnlMainFilter mainFilter = new MgnlMainFilter();
mainFilter.init(new CustomFilterConfig("magnoliaFilterChain", Components.getComponent(ServletContext.class)));
mainFilter.doFilter(servletContextProvider.getRequest(), servletContextProvider.getResponse(), new MockFilterChain());
}

public void stop() {
final StopWatch watch = new StopWatch();
watch.start();
final ModuleManager moduleManager = Components.getComponent(ModuleManager.class);
moduleManager.stopModules();
((AbstractSystemContext)Components.getComponent(SystemContext.class)).getRepositoryStrategy().release();
((AbstractSystemContext) Components.getComponent(SystemContext.class)).getRepositoryStrategy().release();
MgnlContext.setInstance(null);
watch.stop();
System.out.println("Stop took "+watch.getDuration().toMillis()+"ms");
}

private WebContext getUserContext() {
final RepositoryManager repositoryManager = Components.getComponent(RepositoryManager.class);
final MockWebContext webContext = new MockWebContext();
final User user = Components.getComponent(SecuritySupport.class).getUserManager().getUser("superuser");
webContext.setUser(user);
webContext.setRepositoryStrategy(new DefaultRepositoryStrategy(repositoryManager, webContext));
return webContext;
System.out.println("Stop took " + watch.getDuration().toMillis() + "ms");
}

private GuiceComponentProvider getPlatformComponentProvider(final Path appRootDir, final ExtensionContext extensionContext) throws IOException {
Expand All @@ -116,7 +115,8 @@ private GuiceComponentProvider getPlatformComponentProvider(final Path appRootDi
MagnoliaServletContextListener.DEFAULT_PLATFORM_COMPONENTS_CONFIG_LOCATION,
"/configuration/platform-components.xml"
), "platform");
config.registerInstance(ServletContext.class, new MockServletContext());

config.registerProvider(ServletContext.class, ServletContextProvider.class);
applyAnnotationComponents(extensionContext, TestConfiguration.Component.Provider.PLATFORM, config);
config.registerInstance(MagnoliaConfigurationProperties.class, properties);
config.registerInstance(ExtensionContext.class, extensionContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@

import java.util.List;

import com.merkle.oss.magnolia.testing.servlet.DeleteFilterTask;
import com.merkle.oss.magnolia.testing.servlet.TestContextFilter;
import com.merkle.oss.magnolia.testing.servlet.TestLoginFilter;

public class TestModuleVersionHandler extends DefaultModuleVersionHandler {

@Override
protected final List<Task> getExtraInstallTasks(final InstallContext installContext) {
return List.of(
new TestContextFilter.InstallTask(),
new TestLoginFilter.InstallTask(),
new DeleteFilterTask("logout"),
new DeleteFilterTask("securityCallback")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.merkle.oss.magnolia.testing.servlet;

import info.magnolia.module.InstallContext;
import info.magnolia.module.delta.AbstractRepositoryTask;
import info.magnolia.repository.RepositoryConstants;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

public class DeleteFilterTask extends AbstractRepositoryTask {
private final String name;

public DeleteFilterTask(final String name) {
super("Delete" + name + "Filter", "Deletes " + name + " filter");
this.name = name;
}

@Override
protected void doExecute(final InstallContext installContext) throws RepositoryException {
final Session session = installContext.getJCRSession(RepositoryConstants.CONFIG);
session.removeItem("/server/filters/"+name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.merkle.oss.magnolia.testing.servlet;

import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MockFilterChain implements FilterChain {
@Override
public void doFilter(final ServletRequest request, final ServletResponse response) {}
}
Loading

0 comments on commit f5d0d1f

Please sign in to comment.