PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/jira/plugins/bitbucket/pageobjects/page/GithubConfigureRepositoriesPage.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 190 lines | 124 code | 25 blank | 41 comment | 8 complexity | b636c8f8eabcd81aa171ff3887aabd6b MD5 | raw file
  1. package com.atlassian.jira.plugins.bitbucket.pageobjects.page;
  2. import junit.framework.Assert;
  3. import org.hamcrest.Matchers;
  4. import org.hamcrest.core.IsEqual;
  5. import org.openqa.selenium.By;
  6. import com.atlassian.pageobjects.elements.ElementBy;
  7. import com.atlassian.pageobjects.elements.Options;
  8. import com.atlassian.pageobjects.elements.PageElement;
  9. import com.atlassian.pageobjects.elements.query.Poller;
  10. /**
  11. * Represents the page to link repositories to projects
  12. */
  13. public class GithubConfigureRepositoriesPage extends BaseConfigureRepositoriesPage
  14. {
  15. @ElementBy(id = "clientID")
  16. PageElement ghClientID;
  17. @ElementBy(id = "clientSecret")
  18. PageElement ghClientSecret;
  19. @ElementBy(id = "gh_messages")
  20. PageElement ghMessagesDiv;
  21. @ElementBy(id = "login_field")
  22. PageElement githubWebLoginField;
  23. @ElementBy(id = "password")
  24. PageElement githubWebPasswordField;
  25. @ElementBy(name = "authorize")
  26. PageElement githubWebAuthorizeButton;
  27. @ElementBy(name = "commit")
  28. PageElement githubWebSubmitButton;
  29. /**
  30. * Links a public repository to the given JIRA project
  31. *
  32. * @param projectKey The JIRA project key
  33. * @param url The url to the bitucket public repo
  34. * @param adminUsername username used to install the service (postcommit hook)
  35. * @param adminPassword password used to install the service (postcommit hook)
  36. * @return BitBucketConfigureRepositoriesPage
  37. */
  38. @Override
  39. public String addPublicRepoToProjectAndInstallService(String projectKey, String url, String adminUsername, String adminPassword)
  40. {
  41. urlTextbox.clear().type(url);
  42. projectSelect.select(Options.value(projectKey));
  43. addRepositoryButton.click();
  44. Poller.waitUntil(addedRepositoryH2.timed().getText(), IsEqual.equalTo("New GitHub repository"));
  45. // postcommit hook
  46. addPostCommitServiceCheckbox.click();
  47. // add
  48. addRepositoryButton.click();
  49. checkAndDoGithubLogin();
  50. String githubWebLoginRedirectUrl = authorizeGithubAppIfRequired();
  51. if (!githubWebLoginRedirectUrl.contains("/jira/"))
  52. {
  53. Assert.fail("Expected was Valid OAuth login and redirect to JIRA!");
  54. }
  55. return addedRepositoryIdSpan.timed().getValue().now();
  56. }
  57. /**
  58. * Links a public repository to the given JIRA project
  59. *
  60. * @param projectKey The JIRA project key
  61. * @param url The url to the bitucket public repo
  62. * @return BitBucketConfigureRepositoriesPage
  63. */
  64. @Override
  65. public BaseConfigureRepositoriesPage addPublicRepoToProjectSuccessfully(String projectKey, String url)
  66. {
  67. projectSelect.select(Options.value(projectKey));
  68. urlTextbox.clear().type(url);
  69. addRepositoryButton.click();
  70. Poller.waitUntil(addedRepositoryH2.timed().getText(), IsEqual.equalTo("New GitHub repository"));
  71. addRepositoryButton.click();
  72. checkSyncProcessSuccess();
  73. return this;
  74. }
  75. /**
  76. * Links a public repository to the given JIRA project
  77. *
  78. * @param projectKey The JIRA project key
  79. * @param url The url to the bitucket public repo
  80. * @return BitBucketConfigureRepositoriesPage
  81. */
  82. @Override
  83. public GithubConfigureRepositoriesPage addRepoToProjectFailingStep1(String projectKey, String url)
  84. {
  85. projectSelect.select(Options.value(projectKey));
  86. urlTextbox.clear().type(url);
  87. addRepositoryButton.click();
  88. Poller.waitUntilTrue("Expected Error message while connecting repository", messageBarDiv.find(By.tagName("strong")).timed()
  89. .hasText("Error!"));
  90. return this;
  91. }
  92. @Override
  93. public BaseConfigureRepositoriesPage addRepoToProjectFailingStep2(String projectKey, String url)
  94. {
  95. projectSelect.select(Options.value(projectKey));
  96. urlTextbox.clear().type(url);
  97. addRepositoryButton.click();
  98. Poller.waitUntil(addedRepositoryH2.timed().getText(), IsEqual.equalTo("New GitHub repository"));
  99. addRepositoryButton.click();
  100. String currentUrl = checkAndDoGithubLogin();
  101. String expectedUrl = "https://github.com/login/oauth/authorize?";
  102. if (!currentUrl.startsWith(expectedUrl) || !currentUrl.contains("client_id=xxx"))
  103. {
  104. Assert.fail("Unexpected url: " + currentUrl);
  105. }
  106. return this;
  107. }
  108. private String checkAndDoGithubLogin()
  109. {
  110. String currentUrl = jiraTestedProduct.getTester().getDriver().getCurrentUrl();
  111. if (currentUrl.contains("https://github.com/login?"))
  112. {
  113. githubWebLoginField.type("jirabitbucketconnector");
  114. githubWebPasswordField.type("jirabitbucketconnector1");
  115. githubWebSubmitButton.click();
  116. }
  117. return jiraTestedProduct.getTester().getDriver().getCurrentUrl();
  118. }
  119. private String authorizeGithubAppIfRequired()
  120. {
  121. String currentUrl = jiraTestedProduct.getTester().getDriver().getCurrentUrl();
  122. if (currentUrl.contains("/github.com/login/oauth"))
  123. {
  124. githubWebAuthorizeButton.click();
  125. }
  126. return jiraTestedProduct.getTester().getDriver().getCurrentUrl();
  127. }
  128. /**
  129. * Links a public repository to the given JIRA project
  130. *
  131. * @param projectKey The JIRA project key
  132. * @param url The url to the bitucket public repo
  133. * @return BitBucketConfigureRepositoriesPage
  134. */
  135. @Override
  136. public GithubConfigureRepositoriesPage addPrivateRepoToProjectSuccessfully(String projectKey, String url)
  137. {
  138. projectSelect.select(Options.value(projectKey));
  139. urlTextbox.clear().type(url);
  140. addRepositoryButton.click();
  141. Poller.waitUntil(addedRepositoryH2.timed().getText(), IsEqual.equalTo("New GitHub repository"));
  142. if (messageBarDiv.isPresent())
  143. {
  144. PageElement messageBarErrorDiv = messageBarDiv.find(By.className("error"));
  145. if (messageBarErrorDiv.isPresent())
  146. {
  147. Assert.fail("We not expected OAuth problem - OAuth should be configured successfully before adding repo in this test!");
  148. }
  149. }
  150. addRepositoryButton.click();
  151. // String currentUrl = jiraTestedProduct.getTester().getDriver().getCurrentUrl();
  152. // final String githubLoginUrl = "https://github.com/login";
  153. // Assert.assertTrue("Expected redirect to github.com to login", currentUrl.startsWith(githubLoginUrl));
  154. String currentUrl = checkAndDoGithubLogin();
  155. currentUrl = authorizeGithubAppIfRequired();
  156. if (!currentUrl.contains("/jira/"))
  157. {
  158. Assert.fail("Expected was automatic continue to jira!");
  159. }
  160. // Poller.waitUntilTrue("Expected sync status message to appear.", syncStatusDiv.timed().isVisible());
  161. // Poller.waitUntil("Expected sync status message to be 'Sync Finished'", syncStatusDiv.find(By.tagName("strong")).timed()
  162. // .getText(), Matchers.startsWith("Sync Finished:"));
  163. return this;
  164. }
  165. }