PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-functional-tests/jira-func-tests/src/main/java/com/atlassian/jira/webtests/ztests/bundledplugins2/rest/TestIssueResourceVotes.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 184 lines | 142 code | 31 blank | 11 comment | 3 complexity | 936461a2d1d95c874f60186ef851aeb1 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.webtests.ztests.bundledplugins2.rest;
  2. import com.atlassian.jira.functest.framework.Administration;
  3. import com.atlassian.jira.functest.framework.BaseJiraFuncTest;
  4. import com.atlassian.jira.functest.framework.LoginAs;
  5. import com.atlassian.jira.functest.framework.RestoreBlankInstance;
  6. import com.atlassian.jira.functest.framework.suite.Category;
  7. import com.atlassian.jira.functest.framework.suite.WebTest;
  8. import com.atlassian.jira.testkit.client.restclient.Issue;
  9. import com.atlassian.jira.testkit.client.restclient.IssueClient;
  10. import com.atlassian.jira.testkit.client.restclient.Response;
  11. import com.atlassian.jira.testkit.client.restclient.User;
  12. import com.atlassian.jira.testkit.client.restclient.Vote;
  13. import com.atlassian.jira.testkit.client.restclient.VotesClient;
  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_FULLNAME;
  19. import static com.atlassian.jira.functest.framework.FunctTestConstants.FRED_USERNAME;
  20. import static org.junit.Assert.assertEquals;
  21. import static org.junit.Assert.assertFalse;
  22. import static org.junit.Assert.assertNotNull;
  23. import static org.junit.Assert.assertTrue;
  24. /**
  25. * @since v4.2
  26. */
  27. @WebTest({Category.FUNC_TEST, Category.REST})
  28. @LoginAs(user = ADMIN_USERNAME)
  29. @RestoreBlankInstance
  30. public class TestIssueResourceVotes extends BaseJiraFuncTest {
  31. private String issueKey;
  32. private VotesClient votesClient;
  33. private IssueClient issueClient;
  34. @Inject
  35. private Administration administration;
  36. @Before
  37. public void setUpTest() {
  38. issueClient = new IssueClient(getEnvironmentData());
  39. votesClient = new VotesClient(getEnvironmentData());
  40. issueKey = navigation.issue().createIssue("monkey", "Bug", "Issue for voting test");
  41. // admin can't vote on his own issues so vote as fred
  42. navigation.login(FRED_USERNAME);
  43. navigation.issue().voteIssue(issueKey);
  44. // but switch back to admin because that's what most tests are going to expect
  45. navigation.login(ADMIN_USERNAME);
  46. }
  47. @Test
  48. public void testVote_issueDoesNotExist() throws Exception {
  49. // have to login as fred so he can unvote for himself
  50. final Response response = votesClient.loginAs(FRED_USERNAME).deleteResponse("HSP-204");
  51. assertEquals(404, response.statusCode);
  52. assertTrue(response.entity.errorMessages.contains("Issue Does Not Exist"));
  53. }
  54. @Test
  55. public void testVote_votingDisabled() throws Exception {
  56. administration.generalConfiguration().disableVoting();
  57. backdoor.usersAndGroups().addUser("barney");
  58. final Response response = votesClient.loginAs("barney").deleteResponse(issueKey);
  59. assertEquals(404, response.statusCode);
  60. assertTrue(response.entity.errorMessages.contains("Voting for issues is currently not enabled for this JIRA instance."));
  61. }
  62. @Test
  63. public void testVote_reporter() throws Exception {
  64. final Response response = votesClient.postResponse(issueKey);
  65. assertEquals(404, response.statusCode);
  66. assertTrue(response.entity.errorMessages.contains("You cannot vote for an issue you have reported."));
  67. // ensure that no voting happened
  68. assertEquals(1, getVotes());
  69. assertFalse(i_voted());
  70. }
  71. @Test
  72. public void testVote_successful() throws Exception {
  73. backdoor.usersAndGroups().addUser("barney");
  74. final Response response = votesClient.loginAs("barney").postResponse(issueKey);
  75. assertEquals(204, response.statusCode);
  76. assertEquals(2, getVotes());
  77. assertTrue(i_voted("barney"));
  78. }
  79. @Test
  80. public void testViewVoters_issueDoesNotExist() throws Exception {
  81. final Response response = votesClient.getResponse("HSP-55");
  82. assertEquals(404, response.statusCode);
  83. assertTrue(response.entity.errorMessages.contains("Issue Does Not Exist"));
  84. }
  85. @Test
  86. public void testViewVoters_votingDisabled() throws Exception {
  87. administration.generalConfiguration().disableVoting();
  88. final Response response = votesClient.getResponse(issueKey);
  89. assertEquals(404, response.statusCode);
  90. assertTrue(response.entity.errorMessages.contains("Voting for issues is currently not enabled for this JIRA instance."));
  91. }
  92. @Test
  93. public void testViewVoters_noPermission() throws Exception {
  94. // Fred isn't in jira-developers so he can't view the voters
  95. final Vote vote = votesClient.loginAs(FRED_USERNAME).get(issueKey);
  96. assertEquals(1, vote.votes);
  97. assertEquals(0, vote.voters.size());
  98. }
  99. @Test
  100. public void testViewVoters_successful() throws Exception {
  101. final Vote voters = votesClient.get(issueKey);
  102. assertEquals(1, voters.votes);
  103. final User user = voters.voters.get(0);
  104. assertNotNull(user.self);
  105. assertEquals(FRED_USERNAME, user.name);
  106. assertEquals(FRED_FULLNAME, user.displayName);
  107. }
  108. @Test
  109. public void testUnvote_issueDoesNotExist() throws Exception {
  110. // have to login as fred so he can unvote for himself
  111. final Response response = votesClient.loginAs(FRED_USERNAME).deleteResponse("HSP-204");
  112. assertEquals(404, response.statusCode);
  113. assertTrue(response.entity.errorMessages.contains("Issue Does Not Exist"));
  114. }
  115. @Test
  116. public void testUnvote_votingDisabled() throws Exception {
  117. administration.generalConfiguration().disableVoting();
  118. // have to login as fred so he can unvote for himself
  119. final Response response = votesClient.loginAs(FRED_USERNAME).deleteResponse(issueKey);
  120. assertEquals(404, response.statusCode);
  121. assertTrue(response.entity.errorMessages.contains("Voting for issues is currently not enabled for this JIRA instance."));
  122. }
  123. @Test
  124. public void testUnvote_reporter() throws Exception {
  125. final Response response = votesClient.deleteResponse(issueKey);
  126. assertEquals(404, response.statusCode);
  127. assertTrue(response.entity.errorMessages.contains("You cannot vote for an issue you have reported."));
  128. // ensure that no unvoting happened
  129. assertEquals(1, getVotes());
  130. }
  131. @Test
  132. public void testUnvote_successful() throws Exception {
  133. final Response response = votesClient.loginAs(FRED_USERNAME).deleteResponse(issueKey);
  134. assertEquals(204, response.statusCode);
  135. assertEquals(0, getVotes());
  136. assertFalse(i_voted(FRED_USERNAME));
  137. }
  138. private int getVotes() {
  139. return issueClient.get(issueKey).fields.votes.votes;
  140. }
  141. private boolean i_voted() {
  142. return i_voted(null);
  143. }
  144. private boolean i_voted(String user) {
  145. Issue issue;
  146. if (user != null) {
  147. issue = issueClient.loginAs(user).get(issueKey);
  148. } else {
  149. issue = issueClient.get(issueKey);
  150. }
  151. return issue.fields.votes.hasVoted;
  152. }
  153. }