PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/it/com/atlassian/jira/plugins/bitbucket/GithubRepositoriesTest.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 188 lines | 139 code | 41 blank | 8 comment | 0 complexity | 41a6865a6f7a50d5f6560e1f8327a72b MD5 | raw file
  1. package it.com.atlassian.jira.plugins.bitbucket;
  2. import static com.atlassian.jira.plugins.bitbucket.pageobjects.CommitMessageMatcher.*;
  3. import static org.hamcrest.MatcherAssert.*;
  4. import static org.hamcrest.Matchers.*;
  5. import org.apache.commons.httpclient.HttpClient;
  6. import org.apache.commons.httpclient.HttpMethod;
  7. import org.apache.commons.httpclient.UsernamePasswordCredentials;
  8. import org.apache.commons.httpclient.auth.AuthScope;
  9. import org.apache.commons.httpclient.methods.GetMethod;
  10. import org.junit.AfterClass;
  11. import org.junit.BeforeClass;
  12. import org.junit.Test;
  13. import com.atlassian.jira.plugins.bitbucket.pageobjects.page.GithubConfigureRepositoriesPage;
  14. import com.atlassian.jira.plugins.bitbucket.pageobjects.page.GithubLoginPage;
  15. import com.atlassian.jira.plugins.bitbucket.pageobjects.page.GithubOAuthConfigPage;
  16. import com.atlassian.jira.plugins.bitbucket.pageobjects.page.GithubRegisterOAuthAppPage;
  17. import com.atlassian.jira.plugins.bitbucket.pageobjects.page.GithubRegisteredOAuthAppsPage;
  18. /**
  19. * Test to verify behaviour when syncing github repository.
  20. */
  21. public class GithubRepositoriesTest extends BitBucketBaseTest
  22. {
  23. private static final String TEST_REPO_URL = "https://github.com/jirabitbucketconnector/test-project";
  24. private static final String TEST_PRIVATE_REPO_URL = "https://github.com/dusanhornik/my-private-github-repo";
  25. private static final String TEST_NOT_EXISTING_REPO_URL = "https://github.com/jirabitbucketconnector/repo-does-not-exist";
  26. private static String clientID;
  27. private static String clientSecret;
  28. private static String oauthAppName;
  29. private static String oauthAppLink;
  30. @BeforeClass
  31. public static void registerAppToGithub()
  32. {
  33. jira.getTester().gotoUrl(GithubLoginPage.PAGE_URL);
  34. GithubLoginPage ghLoginPage = jira.getPageBinder().bind(GithubLoginPage.class);
  35. ghLoginPage.doLogin();
  36. jira.getTester().gotoUrl(GithubRegisterOAuthAppPage.PAGE_URL);
  37. GithubRegisterOAuthAppPage registerAppPage = jira.getPageBinder().bind(GithubRegisterOAuthAppPage.class);
  38. oauthAppName = "testApp" + System.currentTimeMillis();
  39. String baseUrl = jira.getProductInstance().getBaseUrl();
  40. registerAppPage.registerApp(oauthAppName, baseUrl, baseUrl);
  41. jira.getTester().gotoUrl(GithubRegisteredOAuthAppsPage.PAGE_URL);
  42. GithubRegisteredOAuthAppsPage registeredOAuthAppsPage = jira.getPageBinder().bind(GithubRegisteredOAuthAppsPage.class);
  43. registeredOAuthAppsPage.parseClientIdAndSecret(oauthAppName);
  44. clientID = registeredOAuthAppsPage.getClientID();
  45. clientSecret = registeredOAuthAppsPage.getClientSecret();
  46. oauthAppLink = registeredOAuthAppsPage.getOauthAppUrl();
  47. jira.getTester().gotoUrl(GithubLoginPage.PAGE_URL);
  48. jira.getTester().gotoUrl(GithubLoginPage.LOGOUT_ACTION_URL);
  49. GithubOAuthConfigPage oauthConfigPage = jira.getPageBinder().navigateToAndBind(AnotherLoginPage.class).loginAsSysAdmin(GithubOAuthConfigPage.class);
  50. oauthConfigPage.setCredentials(clientID, clientSecret);
  51. // logout jira
  52. jira.getTester().getDriver().manage().deleteAllCookies();
  53. }
  54. @AfterClass
  55. public static void deregisterAppToGithub()
  56. {
  57. jira.getTester().gotoUrl(GithubLoginPage.LOGOUT_ACTION_URL);
  58. jira.getTester().gotoUrl(GithubLoginPage.PAGE_URL);
  59. GithubLoginPage ghLoginPage = jira.getPageBinder().bind(GithubLoginPage.class);
  60. ghLoginPage.doLogin();
  61. jira.getTester().gotoUrl(oauthAppLink);
  62. GithubRegisterOAuthAppPage registerAppPage = jira.getPageBinder().bind(GithubRegisterOAuthAppPage.class);
  63. registerAppPage.deleteOAuthApp();
  64. jira.getTester().gotoUrl(GithubLoginPage.LOGOUT_ACTION_URL);
  65. }
  66. @SuppressWarnings("rawtypes")
  67. @Override
  68. protected Class getPageClass()
  69. {
  70. return GithubConfigureRepositoriesPage.class;
  71. }
  72. @Test
  73. public void addRepoAppearsOnList()
  74. {
  75. configureRepos.deleteAllRepositories();
  76. configureRepos.addPublicRepoToProjectSuccessfully("QA", TEST_REPO_URL);
  77. assertThat(configureRepos.getRepositories().size(), equalTo(1));
  78. }
  79. @Test
  80. public void addRepoCommitsAppearOnIssues()
  81. {
  82. ensureRepositoryPresent("QA", TEST_REPO_URL);
  83. assertThat(getCommitsForIssue("QA-2"),
  84. hasItem(withMessage("BB modified 1 file to QA-2 and QA-3 from TestRepo-QA")));
  85. assertThat(getCommitsForIssue("QA-3"),
  86. hasItem(withMessage("BB modified 1 file to QA-2 and QA-3 from TestRepo-QA")));
  87. }
  88. @Test
  89. public void addRepoThatDoesNotExist()
  90. {
  91. configureRepos.deleteAllRepositories();
  92. configureRepos.addRepoToProjectFailingStep1("QA", TEST_NOT_EXISTING_REPO_URL);
  93. String errorMessage = configureRepos.getErrorStatusMessage();
  94. assertThat(errorMessage, containsString("Error!The repository url [" + TEST_NOT_EXISTING_REPO_URL + "] is incorrect or the repository is not responding."));
  95. configureRepos.addPublicRepoToProjectSuccessfully("QA", TEST_REPO_URL);
  96. }
  97. @Test
  98. public void addPrivateRepoWithInvalidOAuth()
  99. {
  100. configureRepos.deleteAllRepositories();
  101. goToGithubOAuthConfigPage().setCredentials("xxx", "yyy");
  102. goToRepositoriesConfigPage();
  103. configureRepos.addRepoToProjectFailingStep2("QA", TEST_PRIVATE_REPO_URL);
  104. }
  105. @Test
  106. public void addPrivateRepoWithValidOAuth()
  107. {
  108. configureRepos.deleteAllRepositories();
  109. goToGithubOAuthConfigPage().setCredentials(clientID, clientSecret);
  110. goToRepositoriesConfigPage();
  111. configureRepos.addPrivateRepoToProjectSuccessfully("QA", TEST_PRIVATE_REPO_URL);
  112. configureRepos.assertThatSyncMessage(containsString("Sync Finished"));
  113. configureRepos.assertThatSyncMessage(not(containsString("Sync Failed")));
  114. }
  115. @Test
  116. public void testPostCommitHookAdded() throws Exception
  117. {
  118. String baseUrl = jira.getProductInstance().getBaseUrl();
  119. configureRepos.deleteAllRepositories();
  120. // add repository
  121. String repoId = configureRepos.addPublicRepoToProjectAndInstallService("QA",
  122. TEST_REPO_URL, "jirabitbucketconnector",
  123. "jirabitbucketconnector1");
  124. // check that it created postcommit hook
  125. String githubServiceConfigUrlPath = baseUrl + "/rest/bitbucket/1.0/repository/" + repoId + "/sync";
  126. String hooksURL = "https://github.com/jirabitbucketconnector/test-project/admin/hooks";
  127. String hooksPage = getGithubServices(hooksURL, "jirabitbucketconnector", "jirabitbucketconnector1");
  128. assertThat(hooksPage, containsString(githubServiceConfigUrlPath));
  129. goToRepositoriesConfigPage();
  130. // delete repository
  131. configureRepos.deleteAllRepositories();
  132. // check that postcommit hook is removed
  133. hooksPage = getGithubServices(hooksURL, "jirabitbucketconnector",
  134. "jirabitbucketconnector1");
  135. assertThat(hooksPage, not(containsString(githubServiceConfigUrlPath)));
  136. }
  137. private String getGithubServices(String url, String username, String password) throws Exception
  138. {
  139. HttpClient httpClient = new HttpClient();
  140. HttpMethod method = new GetMethod(url);
  141. AuthScope authScope = new AuthScope(method.getURI().getHost(), AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME);
  142. httpClient.getParams().setAuthenticationPreemptive(true);
  143. httpClient.getState().setCredentials(authScope, new UsernamePasswordCredentials(username, password));
  144. httpClient.executeMethod(method);
  145. return method.getResponseBodyAsString();
  146. }
  147. }