Skip to content

Commit

Permalink
api key optional
Browse files Browse the repository at this point in the history
  • Loading branch information
pjagielski committed Mar 22, 2016
1 parent e74dced commit 9acc523
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 19 deletions.
17 changes: 10 additions & 7 deletions src/main/java/pl/touk/sputnik/connector/saas/SaasConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

@AllArgsConstructor
@Slf4j
Expand All @@ -27,14 +26,14 @@ public class SaasConnector implements Connector {
private GithubPatchset githubPatchset;
private String apiKey;

public SaasConnector(HttpConnector httpConnector, GithubPatchset githubPatchset) {
this(httpConnector, githubPatchset, null);
}

private static final String API_KEY_PARAM = "key";
private static final String FILES_URL_FORMAT = "/api/github/%s/pulls/%d/files";
private static final String VIOLATIONS_URL_FORMAT = "/api/github/%s/pulls/%d/violations";

public List<String> getReviewFiles() {
return null;
}

@NotNull
@Override
public String listFiles() throws URISyntaxException, IOException {
Expand All @@ -60,7 +59,11 @@ private String createUrl(GithubPatchset patchset, String formatUrl) {
}

@NotNull
private NameValuePair apiKeyParam() {
return new BasicNameValuePair(API_KEY_PARAM, apiKey);
private NameValuePair[] apiKeyParam() {
if (apiKey != null) {
return new BasicNameValuePair[]{new BasicNameValuePair(API_KEY_PARAM, apiKey)};
} else {
return new BasicNameValuePair[]{};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pl.touk.sputnik.connector.saas;

import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
Expand All @@ -9,30 +10,31 @@
import pl.touk.sputnik.configuration.CliOption;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.connector.ConnectorDetails;
import pl.touk.sputnik.connector.github.GithubPatchset;
import pl.touk.sputnik.connector.github.GithubPatchsetBuilder;
import pl.touk.sputnik.connector.http.HttpConnector;
import pl.touk.sputnik.connector.http.HttpHelper;

import static org.apache.commons.lang3.Validate.notBlank;

public class SaasFacadeBuilder {

private HttpHelper httpHelper = new HttpHelper();

@NotNull
public SaasFacade build(Configuration configuration) {
ConnectorDetails connectorDetails = new ConnectorDetails(configuration);

String apiKey = configuration.getProperty(CliOption.API_KEY);
notBlank(apiKey, "You must provide non blank Sputnik API key");

HttpHost httpHost = httpHelper.buildHttpHost(connectorDetails);
HttpClientContext httpClientContext = httpHelper.buildClientContext(httpHost, new BasicScheme());
CloseableHttpClient closeableHttpClient = httpHelper.buildClient(httpHost, connectorDetails);

return new SaasFacade(new SaasConnector(
new HttpConnector(closeableHttpClient, httpClientContext, connectorDetails.getPath()),
GithubPatchsetBuilder.build(configuration), apiKey), new Gson());
HttpConnector httpConnector = new HttpConnector(closeableHttpClient, httpClientContext, connectorDetails.getPath());
GithubPatchset patchset = GithubPatchsetBuilder.build(configuration);

SaasConnector saasConnector = StringUtils.isNotBlank(apiKey) ?
new SaasConnector(httpConnector, patchset, apiKey) :
new SaasConnector(httpConnector, patchset);

return new SaasFacade(saasConnector, new Gson());
}

}
11 changes: 8 additions & 3 deletions src/test/java/pl/touk/sputnik/HttpConnectorEnv.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.touk.sputnik;

import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.client.UrlMatchingStrategy;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
Expand All @@ -9,13 +10,17 @@

public class HttpConnectorEnv {

protected void stubGet(UrlMatchingStrategy url, String responseFile) throws Exception {
protected void stubGet(UrlMatchingStrategy url, ResponseDefinitionBuilder responseDefinitionBuilder) {
stubFor(get(url)
.withHeader("Authorization", equalTo("Basic dXNlcjpwYXNz"))
.willReturn(aResponse()
.willReturn(responseDefinitionBuilder));
}

protected void stubGet(UrlMatchingStrategy url, String responseFile) throws Exception {
stubGet(url, aResponse()
.withStatus(HttpStatus.SC_OK)
.withHeader("Content-Type", "application/json")
.withBody(IOUtils.toString(getClass().getResourceAsStream(responseFile)))));
.withBody(IOUtils.toString(getClass().getResourceAsStream(responseFile))));
}

protected void stubPost(UrlMatchingStrategy url, String responseFile) throws Exception {
Expand Down
36 changes: 35 additions & 1 deletion src/test/java/pl/touk/sputnik/connector/saas/SaasFacadeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,41 @@ public void shouldPublishReview() throws Exception {
}

@Test(expected = SaasException.class)
public void shouldHandleWrongApiKey() throws Exception {
public void shouldThrowOnWrongApiKey() throws Exception {
SaasFacade saasFacade = buildFacade(ImmutableMap.of(
"cli.pullRequestId", SOME_PULL_REQUEST_ID.toString(),
"cli.apiKey", "WRONG_API_KEY",
"connector.repository", SOME_REPOSITORY,
"connector.project", SOME_PROJECT
));
stubGet(urlEqualTo(String.format(
"%s/api/github/%s/%s/pulls/%s/files?key=%s",
FacadeConfigUtil.PATH, SOME_PROJECT, SOME_REPOSITORY, SOME_PULL_REQUEST_ID, "WRONG_API_KEY")),
aResponse().withStatus(403));

saasFacade.listFiles();
}

@Test
public void shouldHandleEmptyApiKey() throws Exception {
SaasFacade saasFacade = buildFacade(ImmutableMap.of(
"cli.pullRequestId", SOME_PULL_REQUEST_ID.toString(),
"connector.repository", SOME_REPOSITORY,
"connector.project", SOME_PROJECT
));

stubGet(urlEqualTo(String.format(
"%s/api/github/%s/%s/pulls/%s/files",
FacadeConfigUtil.PATH, SOME_PROJECT, SOME_REPOSITORY, SOME_PULL_REQUEST_ID)), "/json/saas-files.json");

List<ReviewFile> files = saasFacade.listFiles();

assertThat(files).extracting("reviewFilename").containsOnly("src/main/java/TestFile.java", "src/main/java/TestFile2.java");
}

protected SaasFacade buildFacade(Map<String, String> configMap) {
Configuration config = new ConfigurationSetup().setUp(FacadeConfigUtil.getHttpConfig("saas"), configMap);
return new SaasFacadeBuilder().build(config);
}

}

0 comments on commit 9acc523

Please sign in to comment.