From d28e6a9c5413bd3bce8e99a740fe0e8492f69a1c Mon Sep 17 00:00:00 2001 From: Nicolas QUINQUENEL Date: Mon, 7 Aug 2023 10:33:12 +0200 Subject: [PATCH] SLCORE-530 Add test for invalid range and direct proxy (#674) --- .../fixtures/SonarLintBackendFixture.java | 5 ++ .../mediumtest/http/ProxyMediumTests.java | 57 ++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/mediumtest/fixtures/SonarLintBackendFixture.java b/core/src/test/java/mediumtest/fixtures/SonarLintBackendFixture.java index b4369abe92..723ee79a58 100644 --- a/core/src/test/java/mediumtest/fixtures/SonarLintBackendFixture.java +++ b/core/src/test/java/mediumtest/fixtures/SonarLintBackendFixture.java @@ -348,6 +348,11 @@ public SonarLintClientBuilder withHttpProxy(String hostname, int port) { return this; } + public SonarLintClientBuilder withDirectProxy() { + this.proxy = ProxyDto.NO_PROXY; + return this; + } + public SonarLintClientBuilder withHttpProxyAuth(String username, String password) { this.proxyAuth = new GetProxyPasswordAuthenticationResponse(username, password); return this; diff --git a/core/src/test/java/mediumtest/http/ProxyMediumTests.java b/core/src/test/java/mediumtest/http/ProxyMediumTests.java index 4e0fd0ff57..a3ec8c7686 100644 --- a/core/src/test/java/mediumtest/http/ProxyMediumTests.java +++ b/core/src/test/java/mediumtest/http/ProxyMediumTests.java @@ -121,6 +121,32 @@ void it_should_honor_http_proxy_settings() { proxyMock.verify(getRequestedFor(urlEqualTo("/api/rules/show.protobuf?key=python:S139"))); } + @Test + void it_should_honor_http_direct_proxy_settings() { + var fakeClient = newFakeClient() + .withDirectProxy() + .build(); + backend = newBackend() + .withSonarQubeConnection("connectionId", sonarqubeMock.baseUrl(), storage -> storage.withProject("projectKey", + projectStorage -> projectStorage.withRuleSet(Language.PYTHON.getLanguageKey(), + ruleSet -> ruleSet.withActiveRule("python:S139", "INFO", Map.of("legalTrailingCommentPattern", "blah"))))) + .withBoundConfigScope("scopeId", "connectionId", "projectKey") + .withConnectedEmbeddedPluginAndEnabledLanguage(TestPlugin.PYTHON) + .build(fakeClient); + sonarqubeMock.stubFor(get("/api/rules/show.protobuf?key=python:S139") + .willReturn(aResponse().withStatus(200).withResponseBody(protobufBody(Rules.ShowResponse.newBuilder() + .setRule(Rules.Rule.newBuilder().setName("newName").setSeverity("INFO").setType(Common.RuleType.BUG).setLang("py").setHtmlDesc( + "desc").setHtmlNote("extendedDesc from server").build()) + .build())))); + + var details = getEffectiveRuleDetails("scopeId", "python:S139"); + + assertThat(details.getDescription().getLeft().getHtmlContent()).contains("extendedDesc from server"); + + sonarqubeMock.verify(getRequestedFor(urlEqualTo("/api/rules/show.protobuf?key=python:S139"))); + proxyMock.verify(0, getRequestedFor(urlEqualTo("/api/rules/show.protobuf?key=python:S139"))); + } + @Test @Tag(PROXY_AUTH_ENABLED) void it_should_honor_http_proxy_authentication() { @@ -182,7 +208,36 @@ void it_should_honor_http_proxy_authentication_with_null_password() { @Test @Tag(PROXY_AUTH_ENABLED) - void it_should_fail_if_proxy_port_is_not_valid() { + void it_should_fail_if_proxy_port_is_smaller_than_valid_range() { + var proxyLogin = "proxyLogin"; + var fakeClient = newFakeClient() + .withHttpProxy("localhost", -1) + .withHttpProxyAuth(proxyLogin, null) + .build(); + backend = newBackend() + .withSonarQubeConnection("connectionId", sonarqubeMock.baseUrl(), storage -> storage.withProject("projectKey", + projectStorage -> projectStorage.withRuleSet(Language.PYTHON.getLanguageKey(), + ruleSet -> ruleSet.withActiveRule("python:S139", "INFO", Map.of("legalTrailingCommentPattern", "blah"))))) + .withBoundConfigScope("scopeId", "connectionId", "projectKey") + .withConnectedEmbeddedPluginAndEnabledLanguage(TestPlugin.PYTHON) + .build(fakeClient); + sonarqubeMock.stubFor(get("/api/rules/show.protobuf?key=python:S139") + .willReturn(aResponse().withStatus(200).withResponseBody(protobufBody(Rules.ShowResponse.newBuilder() + .setRule(Rules.Rule.newBuilder().setName("newName").setSeverity("INFO").setType(Common.RuleType.BUG).setLang("py").setHtmlDesc( + "desc").setHtmlNote("extendedDesc from server").build()) + .build())))); + + var details = getEffectiveRuleDetails("scopeId", "python:S139"); + + assertThat(details.getDescription().getLeft().getHtmlContent()).contains("extendedDesc from server"); + + assertThat(logTester.logs()).contains("Unable to get proxy"); + assertThat(logTester.logs()).anyMatch(s -> s.contains("Port is outside the valid range for hostname: localhost")); + } + + @Test + @Tag(PROXY_AUTH_ENABLED) + void it_should_fail_if_proxy_port_is_higher_than_valid_range() { var proxyLogin = "proxyLogin"; var fakeClient = newFakeClient() .withHttpProxy("localhost", 70000)