Skip to content

Commit

Permalink
SLCORE-530 Add test for invalid range and direct proxy (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
nquinquenel authored Aug 7, 2023
1 parent cbdf4bd commit d28e6a9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
57 changes: 56 additions & 1 deletion core/src/test/java/mediumtest/http/ProxyMediumTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d28e6a9

Please sign in to comment.