From 149557ac59ae91088d76fde100cd7935adf7f2f1 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Fri, 3 Apr 2015 15:47:11 +0300 Subject: [PATCH] IDEA-138740 During test connection additionally check that Redmine projects can be downloaded --- .../tasks/redmine/RedmineRepository.java | 26 +++++++++++++------ .../integration/RedmineIntegrationTest.java | 24 ++++++++++++----- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/plugins/tasks/tasks-core/src/com/intellij/tasks/redmine/RedmineRepository.java b/plugins/tasks/tasks-core/src/com/intellij/tasks/redmine/RedmineRepository.java index 1e134badc6c1..303addcb112f 100644 --- a/plugins/tasks/tasks-core/src/com/intellij/tasks/redmine/RedmineRepository.java +++ b/plugins/tasks/tasks-core/src/com/intellij/tasks/redmine/RedmineRepository.java @@ -133,8 +133,13 @@ public class RedmineRepository extends NewBaseRepositoryImpl { HttpResponse httpResponse = client.execute(myCurrentRequest); StatusLine statusLine = httpResponse.getStatusLine(); if (statusLine != null && statusLine.getStatusCode() == HttpStatus.SC_NOT_FOUND) { - myCurrentRequest = new HttpGet(getIssuesUrl(0, 1, true)); + // Check that projects can be downloaded via given URL and the latter is not project-specific + myCurrentRequest = new HttpGet(getProjectsUrl(0, 1)); statusLine = client.execute(myCurrentRequest).getStatusLine(); + if (statusLine != null && statusLine.getStatusCode() == HttpStatus.SC_OK) { + myCurrentRequest = new HttpGet(getIssuesUrl(0, 1, true)); + statusLine = client.execute(myCurrentRequest).getStatusLine(); + } } if (statusLine != null && statusLine.getStatusCode() != HttpStatus.SC_OK) { throw RequestFailedException.forStatusCode(statusLine.getStatusCode(), statusLine.getReasonPhrase()); @@ -192,14 +197,8 @@ public class RedmineRepository extends NewBaseRepositoryImpl { int offset = 0; ProjectsWrapper wrapper; do { - URIBuilder builder = new URIBuilder(getRestApiUrl("projects.json")); - builder.addParameter("offset", String.valueOf(offset)); - builder.addParameter("limit", "50"); - if (isUseApiKeyAuthentication()) { - builder.addParameter("key", myAPIKey); - } - HttpGet method = new HttpGet(builder.toString()); + HttpGet method = new HttpGet(getProjectsUrl(offset, 50)); wrapper = client.execute(method, new GsonSingleObjectDeserializer(GSON, ProjectsWrapper.class)); offset += wrapper.getProjects().size(); allProjects.addAll(wrapper.getProjects()); @@ -210,6 +209,17 @@ public class RedmineRepository extends NewBaseRepositoryImpl { return Collections.unmodifiableList(myProjects); } + @NotNull + private URI getProjectsUrl(int offset, int limit) throws URISyntaxException { + URIBuilder builder = new URIBuilder(getRestApiUrl("projects.json")); + builder.addParameter("offset", String.valueOf(offset)); + builder.addParameter("limit", String.valueOf(limit)); + if (isUseApiKeyAuthentication()) { + builder.addParameter("key", myAPIKey); + } + return builder.build(); + } + @Nullable @Override public Task findTask(@NotNull String id) throws Exception { diff --git a/plugins/tasks/tasks-tests/test/com/intellij/tasks/integration/RedmineIntegrationTest.java b/plugins/tasks/tasks-tests/test/com/intellij/tasks/integration/RedmineIntegrationTest.java index b0d3b9faa431..a918c9bd3154 100644 --- a/plugins/tasks/tasks-tests/test/com/intellij/tasks/integration/RedmineIntegrationTest.java +++ b/plugins/tasks/tasks-tests/test/com/intellij/tasks/integration/RedmineIntegrationTest.java @@ -17,7 +17,7 @@ import java.util.List; * @author Mikhail Golubev */ public class RedmineIntegrationTest extends TaskManagerTestCase { - private static final String REDMINE_2_0_TEST_SERVER_URL = "http://trackers-tests.labs.intellij.net:8072/"; + private static final String REDMINE_2_0_TEST_SERVER_URL = "http://trackers-tests.labs.intellij.net:8072"; private RedmineRepository myRepository; @@ -73,16 +73,28 @@ public class RedmineIntegrationTest extends TaskManagerTestCase { assertEquals("prj-1 7 7 Summary contains 'baz'", TaskUtil.getChangeListComment(localTask)); } - /** - * Redmine doesn't send 401 or 403 errors, when issues are downloaded with wrong credentials, so current user information is - * fetched instead. - */ + // IDEA-122845 + // Redmine doesn't send 401 or 403 errors, when issues are downloaded with wrong credentials (and anonymous access is allowed), + // so current user information is fetched instead. public void testCredentialsCheck() throws Exception { myRepository.setPassword("wrong-password"); try { //noinspection ConstantConditions final Exception exception = myRepository.createCancellableConnection().call(); - assertNotNull("Test connection must fail when wrong credentials specified", exception); + assertNotNull("Test connection must fail when wrong credentials are specified", exception); + } + catch (Exception e) { + assertEquals(TaskBundle.message("failure.login"), e.getMessage()); + } + } + + // IDEA-138740 + public void testProjectSpecificUrlCheck() throws Exception { + myRepository.setUrl(REDMINE_2_0_TEST_SERVER_URL + "/projects/prj-1"); + try { + //noinspection ConstantConditions + final Exception exception = myRepository.createCancellableConnection().call(); + assertNotNull("Test connection must fail when project-specific URL is specified", exception); } catch (Exception e) { assertEquals(TaskBundle.message("failure.login"), e.getMessage());