PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-functional-tests/jira-func-tests/src/main/java/com/atlassian/jira/webtests/ztests/issue/TestVoters.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 306 lines | 238 code | 55 blank | 13 comment | 7 complexity | e9ca0beb3638aee65ddb15ad5f56ea81 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.webtests.ztests.issue;
  2. import com.atlassian.jira.functest.framework.Administration;
  3. import com.atlassian.jira.functest.framework.BaseJiraFuncTest;
  4. import com.atlassian.jira.functest.framework.HtmlPage;
  5. import com.atlassian.jira.functest.framework.LoginAs;
  6. import com.atlassian.jira.functest.framework.locator.IdLocator;
  7. import com.atlassian.jira.functest.framework.locator.WebPageLocator;
  8. import com.atlassian.jira.functest.framework.locator.XPathLocator;
  9. import com.atlassian.jira.functest.framework.suite.Category;
  10. import com.atlassian.jira.functest.framework.suite.WebTest;
  11. import org.apache.commons.lang.StringUtils;
  12. import org.apache.commons.lang.builder.ToStringBuilder;
  13. import org.apache.commons.lang.builder.ToStringStyle;
  14. import org.junit.Before;
  15. import org.junit.Test;
  16. import javax.inject.Inject;
  17. import static com.atlassian.jira.functest.framework.FunctTestConstants.ADMIN_USERNAME;
  18. import static com.atlassian.jira.functest.framework.FunctTestConstants.FRED_USERNAME;
  19. import static org.junit.Assert.assertEquals;
  20. import static org.junit.Assert.fail;
  21. @WebTest({Category.FUNC_TEST, Category.BROWSING})
  22. //todo annotation @LoginAs was added automatically - consider removing it if not needed by this test (otherwise remove this comment)
  23. @LoginAs(user = ADMIN_USERNAME)
  24. public class TestVoters extends BaseJiraFuncTest {
  25. private static final Issue ISSUE_WITH_VOTE = new Issue("HSP-1", 10010);
  26. private static final Issue ISSUE_WITHOUT_VOTE = new Issue("HSP-2", 10020);
  27. private static final Issue ISSUE_RESOLVED_WITHOUT_VOTE = new Issue("HSP-3", 10030);
  28. private static final Issue ISSUE_RESOLVED_WITH_VOTE = new Issue("HSP-4", 10040);
  29. private static final Issue ISSUE_ADMIN_CAN_VOTE = new Issue("HSP-5", 10050);
  30. private static final String ID_VOTE_LINK = "vote-toggle";
  31. private static final String ID_VOTE_SPAN = "vote-label";
  32. private static final String ID_VOTE_COUNT = "vote-data";
  33. private static final String ID_VOTE_COUNT_LINK = "view-voter-list";
  34. private static final String ID_VOTERS_ACTION = "view-voters";
  35. private static final String ID_VOTE_ACTION = "toggle-vote-issue";
  36. private static final String XPATH_VOTE_SPAN_TITLE = String.format("//span[@id='%s']/@title", ID_VOTE_SPAN);
  37. private static final String XPATH_VIEW_VOTERS_LINK = "//a[@id='" + ID_VOTE_COUNT_LINK + "']";
  38. private static final String XPATH_VIEW_VOTERS_SPAN = "//span[@id='" + ID_VOTE_COUNT + "']";
  39. private static final String MSG_CANT_VOTE_ANON = "You have to be logged in to vote for an issue.";
  40. private static final String MSG_CANT_VOTE_RESOLVED = "You cannot vote or change your vote on resolved issues.";
  41. private static final String MSG_CANT_REMOVE_VOTE = "Cannot remove a vote for an issue that the user has not already voted for.";
  42. private static final String MSG_CANT_ADD_VOTE = "Cannot add a vote for an issue that the user has already voted for";
  43. private static final String MSG_CANT_VOTE_ANON_ERROR = "You must log in to access this page.";
  44. private static final String MSG_CANT_VOTE_REPORTER_ERROR = "You cannot vote for an issue you have reported.";
  45. @Inject
  46. private HtmlPage page;
  47. @Inject
  48. private Administration administration;
  49. @Before
  50. public void setUpTest() {
  51. administration.restoreData("TestVoters.xml");
  52. backdoor.darkFeatures().enableForSite("ka.NO_GLOBAL_SHORTCUT_LINKS");
  53. }
  54. @Test
  55. public void testAnonmousCannotVote() {
  56. navigation.logout();
  57. gotoIssue(ISSUE_WITH_VOTE);
  58. assertCantVote(ISSUE_WITH_VOTE, MSG_CANT_VOTE_ANON, MSG_CANT_VOTE_ANON_ERROR, 1, false);
  59. navigation.login(ADMIN_USERNAME);
  60. }
  61. @Test
  62. public void testReporterCannotVote() {
  63. gotoIssue(ISSUE_WITH_VOTE);
  64. assertCantVote(ISSUE_WITH_VOTE, "", MSG_CANT_VOTE_REPORTER_ERROR, 1, true);
  65. }
  66. @Test
  67. public void testUserVoting() {
  68. navigation.login(FRED_USERNAME);
  69. gotoIssue(ISSUE_WITH_VOTE);
  70. assertUserVoted();
  71. assertEquals(1, getVoteCount());
  72. // Click the link which should unvote the issue.
  73. tester.clickLink(ID_VOTE_LINK);
  74. assertUserNotVoted();
  75. assertEquals(0, getVoteCount());
  76. // Click the vote link, which should vote the issue.
  77. tester.clickLink(ID_VOTE_LINK);
  78. assertUserVoted();
  79. assertEquals(1, getVoteCount());
  80. navigation.login(ADMIN_USERNAME);
  81. }
  82. @Test
  83. public void testAnUserCanNotVoteMoreThanOnce() {
  84. navigation.login(FRED_USERNAME);
  85. gotoIssue(ISSUE_WITH_VOTE);
  86. assertUserVoted();
  87. assertEquals(1, getVoteCount());
  88. // Lets try to vote directly and make sure that it does not add a vote, since we already voted.
  89. voteDirectlyForIssue(ISSUE_WITH_VOTE);
  90. gotoIssue(ISSUE_WITH_VOTE);
  91. assertUserVoted();
  92. assertEquals(1, getVoteCount());
  93. }
  94. @Test
  95. public void testAnUserCanNotUnvoteMoreThanOnce() {
  96. navigation.login(FRED_USERNAME);
  97. gotoIssue(ISSUE_WITH_VOTE);
  98. assertUserVoted();
  99. assertEquals(1, getVoteCount());
  100. // Click the link which should unvote the issue.
  101. tester.clickLink(ID_VOTE_LINK);
  102. assertUserNotVoted();
  103. assertEquals(0, getVoteCount());
  104. // Lets try to unvote directly and make sure that it does not remove a vote, since we haven't voted.
  105. unVoteDirectlyForIssue(ISSUE_WITH_VOTE);
  106. gotoIssue(ISSUE_WITH_VOTE);
  107. assertUserNotVoted();
  108. assertEquals(0, getVoteCount());
  109. }
  110. private void assertVotingLinks(boolean voted) {
  111. tester.assertLinkPresent(ID_VOTE_LINK);
  112. tester.assertLinkPresent(ID_VOTE_ACTION);
  113. if (voted) {
  114. tester.assertTextInElement(ID_VOTE_ACTION, "Remove vote");
  115. } else {
  116. tester.assertTextInElement(ID_VOTE_ACTION, "Add vote");
  117. }
  118. }
  119. @Test
  120. public void testCannotVoteOnResolved() {
  121. navigation.login(FRED_USERNAME);
  122. assertCantVote(ISSUE_RESOLVED_WITHOUT_VOTE, MSG_CANT_VOTE_RESOLVED, MSG_CANT_VOTE_RESOLVED, 0, false);
  123. //Make sure you can't unvote for a resolved issue.
  124. unVoteDirectlyForIssue(ISSUE_RESOLVED_WITH_VOTE);
  125. tester.assertTextPresent(MSG_CANT_VOTE_RESOLVED);
  126. gotoIssue(ISSUE_RESOLVED_WITH_VOTE);
  127. assertEquals(1, getVoteCount());
  128. }
  129. @Test
  130. public void testViewVotersWithPermission() throws Exception {
  131. gotoIssue(ISSUE_WITH_VOTE);
  132. assertCanViewVoters();
  133. tester.assertLinkPresent("voter_link_fred");
  134. gotoIssue(ISSUE_ADMIN_CAN_VOTE);
  135. assertCanViewVoters();
  136. tester.assertTextPresent("There are no voters for this issue");
  137. gotoIssue(ISSUE_RESOLVED_WITH_VOTE);
  138. assertCanViewVoters();
  139. tester.assertLinkPresent("voter_link_fred");
  140. }
  141. @Test
  142. public void testViewVotersWithoutPermission() throws Exception {
  143. navigation.login(FRED_USERNAME);
  144. assertCannotViewVoters(ISSUE_WITH_VOTE, "Access Denied");
  145. assertCannotViewVoters(ISSUE_RESOLVED_WITH_VOTE, "Access Denied");
  146. navigation.logout();
  147. assertCannotViewVoters(ISSUE_WITH_VOTE, "You must log in to access this page.");
  148. assertCannotViewVoters(ISSUE_RESOLVED_WITH_VOTE, "You must log in to access this page.");
  149. }
  150. @Test
  151. public void testVoteNoViewPermission() throws Exception {
  152. navigation.login(FRED_USERNAME);
  153. gotoIssue(ISSUE_WITHOUT_VOTE);
  154. assertEquals(0, getVoteCount());
  155. tester.clickLink(ID_VOTE_LINK);
  156. assertEquals(1, getVoteCount());
  157. }
  158. private void assertCanViewVoters() {
  159. assertions.assertNodeExists(XPATH_VIEW_VOTERS_LINK);
  160. tester.assertLinkPresent(ID_VOTERS_ACTION);
  161. assertions.assertNodeExists(XPATH_VIEW_VOTERS_SPAN);
  162. tester.clickLink(ID_VOTE_COUNT_LINK);
  163. tester.assertTextPresent("Voters");
  164. }
  165. private void assertCannotViewVoters(Issue issue, final String expectedErrorMsg) {
  166. gotoIssue(issue);
  167. assertions.assertNodeDoesNotExist(XPATH_VIEW_VOTERS_LINK);
  168. tester.assertLinkNotPresent(ID_VOTERS_ACTION);
  169. assertions.assertNodeExists(XPATH_VIEW_VOTERS_SPAN);
  170. gotoViewVotersDirectly(issue);
  171. tester.assertTextPresent(expectedErrorMsg);
  172. }
  173. private void assertUserVoted() {
  174. assertVotingLinks(true);
  175. assertTextPresentInElement(ID_VOTE_LINK, "Remove vote for this issue");
  176. }
  177. private void assertUserNotVoted() {
  178. assertVotingLinks(false);
  179. assertTextPresentInElement(ID_VOTE_LINK, "Vote for this issue");
  180. }
  181. private void assertCantVote(final Issue issue, final String cantVoteTitle, final String cantVoteError, final int votes, final boolean isReporter) {
  182. gotoIssue(issue);
  183. //Should not be able to vote.
  184. tester.assertLinkNotPresent(ID_VOTE_LINK);
  185. tester.assertLinkNotPresent(ID_VOTE_ACTION);
  186. if (isReporter) {
  187. assertTextNotPresentInElement(ID_VOTE_SPAN, "Vote");
  188. } else {
  189. assertTextPresentInElement(ID_VOTE_SPAN, "Vote");
  190. //Should see this title when we can't vote.
  191. assertEquals(cantVoteTitle, getXpathText(XPATH_VOTE_SPAN_TITLE));
  192. }
  193. //Make sure the vote count is 1
  194. assertEquals(votes, getVoteCount());
  195. //Make sure we can't hit the action directly to hack our vote.
  196. voteDirectlyForIssue(issue);
  197. assertions.getTextAssertions().assertTextPresent(new WebPageLocator(tester), cantVoteError);
  198. //Go to the issue page again
  199. gotoIssue(issue);
  200. //Make sure the vote count is still 1
  201. assertEquals(votes, getVoteCount());
  202. }
  203. private void assertTextPresentInElement(final String elementId, final String expectedText) {
  204. assertions.getTextAssertions().assertTextPresent(new IdLocator(tester, elementId), expectedText);
  205. }
  206. private void assertTextNotPresentInElement(final String elementId, final String expectedText) {
  207. assertions.getTextAssertions().assertTextNotPresent(new IdLocator(tester, elementId), expectedText);
  208. }
  209. private void gotoIssue(Issue issue) {
  210. navigation.issue().gotoIssue(issue.getKey());
  211. }
  212. private void voteDirectlyForIssue(Issue issue) {
  213. navigation.gotoPage(page.addXsrfToken(String.format("/secure/VoteOrWatchIssue.jspa?id=%d&vote=vote", issue.getId())));
  214. }
  215. private void unVoteDirectlyForIssue(Issue issue) {
  216. navigation.gotoPage(page.addXsrfToken(String.format("/secure/VoteOrWatchIssue.jspa?id=%d&vote=unvote", issue.getId())));
  217. }
  218. private void gotoViewVotersDirectly(Issue issue) {
  219. navigation.gotoPage(String.format("secure/ViewVoters!default.jspa?id=%d", issue.getId()));
  220. }
  221. private int getVoteCount() {
  222. final String s = StringUtils.trimToNull(new IdLocator(tester, ID_VOTE_COUNT).getText());
  223. if (s != null) {
  224. return Integer.parseInt(s);
  225. } else {
  226. fail("Unable to find voting count.");
  227. return Integer.MIN_VALUE;
  228. }
  229. }
  230. private String getXpathText(final String xpath) {
  231. return StringUtils.trimToNull(new XPathLocator(tester, xpath).getText());
  232. }
  233. private static class Issue {
  234. private final String key;
  235. private final long id;
  236. private Issue(final String key, final long id) {
  237. this.key = key;
  238. this.id = id;
  239. }
  240. public String getKey() {
  241. return key;
  242. }
  243. public long getId() {
  244. return id;
  245. }
  246. @Override
  247. public String toString() {
  248. return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  249. }
  250. }
  251. }