From 1da04f3954bda4a18da4dbade9e672870f6c3d0a Mon Sep 17 00:00:00 2001 From: Thomas Mortagne Date: Wed, 27 Nov 2024 17:55:56 +0100 Subject: [PATCH] [Misc] Add a test to validate that the credntial are taken into account for an HTTP maven repository --- .../pom.xml | 18 +++++ ...SystemHTTPProxyTest.java => HTTPTest.java} | 65 +++++++++++++++++-- 2 files changed, 76 insertions(+), 7 deletions(-) rename xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/src/test/java/org/xwiki/extension/repository/aether/internal/{SystemHTTPProxyTest.java => HTTPTest.java} (56%) diff --git a/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/pom.xml b/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/pom.xml index c86016f699..88213fac95 100644 --- a/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/pom.xml +++ b/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/pom.xml @@ -113,6 +113,12 @@ wiremock-standalone test + + org.xwiki.commons + xwiki-commons-tool-test-component + ${project.version} + test + org.xwiki.commons xwiki-commons-environment-standard @@ -146,6 +152,18 @@ + + org.apache.maven.plugins + maven-jar-plugin + + + test-jar + + test-jar + + + + org.apache.maven.plugins maven-checkstyle-plugin diff --git a/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/src/test/java/org/xwiki/extension/repository/aether/internal/SystemHTTPProxyTest.java b/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/src/test/java/org/xwiki/extension/repository/aether/internal/HTTPTest.java similarity index 56% rename from xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/src/test/java/org/xwiki/extension/repository/aether/internal/SystemHTTPProxyTest.java rename to xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/src/test/java/org/xwiki/extension/repository/aether/internal/HTTPTest.java index 96ae2f042c..d56862f6df 100644 --- a/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/src/test/java/org/xwiki/extension/repository/aether/internal/SystemHTTPProxyTest.java +++ b/xwiki-commons-core/xwiki-commons-extension/xwiki-commons-extension-repositories/xwiki-commons-extension-repository-maven/src/test/java/org/xwiki/extension/repository/aether/internal/HTTPTest.java @@ -21,6 +21,7 @@ import java.net.URI; import java.net.URISyntaxException; +import java.util.Map; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -34,7 +35,13 @@ import org.xwiki.test.junit5.mockito.InjectMockComponents; import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.BasicCredentials; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static com.github.tomakehurst.wiremock.matching.RequestPatternBuilder.allRequests; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -46,25 +53,45 @@ */ @AllComponents @ComponentTest -class SystemHTTPProxyTest +class HTTPTest { @InjectMockComponents private AetherExtensionRepositoryFactory repositoryFactory; private WireMockServer wireMockServer; + private String proxyHost; + + private String proxyPort; + @BeforeEach - void proxyToWireMock() + void beforeEach() { - this.wireMockServer = new WireMockServer(8888); + this.wireMockServer = new WireMockServer(options().dynamicPort()); this.wireMockServer.start(); + + // Remember the standard system properties + this.proxyHost = System.getProperty("http.proxyHost"); + this.proxyPort = System.getProperty("http.proxyPort"); } @AfterEach - void noMoreWireMock() + void afterEach() { this.wireMockServer.stop(); this.wireMockServer = null; + + // Cleanup any leftover in System properties + if (this.proxyHost != null) { + System.setProperty("http.proxyHost", this.proxyHost); + } else { + System.clearProperty("http.proxyHost"); + } + if (this.proxyPort != null) { + System.setProperty("http.proxyPort", this.proxyPort); + } else { + System.clearProperty("http.proxyPort"); + } } @Test @@ -80,12 +107,12 @@ void proxy() throws ExtensionRepositoryException, URISyntaxException // We don't really care if the target artifact exist } - assertTrue(this.wireMockServer.findAll(allRequests()).isEmpty(), "The repository did not request the " - + "proxy server"); + assertTrue(this.wireMockServer.findAll(allRequests()).isEmpty(), + "The repository did not request the " + "proxy server"); // Set the proxy to be wiremock so that it answers. System.setProperty("http.proxyHost", "localhost"); - System.setProperty("http.proxyPort", "8888"); + System.setProperty("http.proxyPort", String.valueOf(this.wireMockServer.port())); try { repository.resolveVersions("groupid:artifactid", 0, -1); @@ -96,4 +123,28 @@ void proxy() throws ExtensionRepositoryException, URISyntaxException assertFalse(this.wireMockServer.findAll(allRequests()).isEmpty(), "The repository did not request the proxy server"); } + + @Test + void credentials() throws ExtensionRepositoryException, URISyntaxException + { + ExtensionRepository repository = + this.repositoryFactory.createRepository(new DefaultExtensionRepositoryDescriptor("id", "maven", + new URI("http://localhost:" + this.wireMockServer.port()), + Map.of("auth.user", "user", "auth.password", "password"))); + + this.wireMockServer.stubFor(get(urlEqualTo("/groupid/artifactid/maven-metadata.xml")) + .willReturn(aResponse().withStatus(401).withHeader("WWW-Authenticate", "Basic"))); + + try { + repository.resolveVersions("groupid:artifactid", 0, -1); + } catch (ResolveException e) { + // We don't really care if the target artifact exist + } + + assertFalse(this.wireMockServer.findAll(allRequests()).isEmpty(), + "The repository did not request the proxy server"); + + this.wireMockServer.verify(getRequestedFor(urlEqualTo("/groupid/artifactid/maven-metadata.xml")) + .withBasicAuth(new BasicCredentials("user", "password"))); + } }