/test/src/test/java/hudson/cli/ViewManipulationTestBase.java

http://github.com/kohsuke/hudson · Java · 246 lines · 159 code · 61 blank · 26 comment · 0 complexity · 29cb8c8fff3a0054de702573b15ae8a8 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright 2016 Red Hat, Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.cli;
  25. import hudson.model.FreeStyleProject;
  26. import hudson.model.Job;
  27. import hudson.model.ListView;
  28. import hudson.model.View;
  29. import jenkins.model.Jenkins;
  30. import org.junit.Before;
  31. import org.junit.Rule;
  32. import org.junit.Test;
  33. import org.jvnet.hudson.test.JenkinsRule;
  34. import java.io.IOException;
  35. import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
  36. import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput;
  37. import static org.hamcrest.MatcherAssert.assertThat;
  38. import static org.hamcrest.Matchers.containsString;
  39. import static org.hamcrest.Matchers.equalTo;
  40. /**
  41. * @author pjanouse
  42. */
  43. public abstract class ViewManipulationTestBase {
  44. protected CLICommandInvoker command;
  45. abstract CLICommandInvoker getCommand();
  46. @Rule public final JenkinsRule j = new JenkinsRule();
  47. @Before public void setUp() {
  48. command = getCommand();
  49. }
  50. @Test public void jobViewManipulationShouldFailWithJenkinsReadPermissionOnly() throws IOException {
  51. j.jenkins.addView(new ListView("aView"));
  52. j.createFreeStyleProject("aProject");
  53. final CLICommandInvoker.Result result = command
  54. .authorizedTo(Jenkins.READ)
  55. .invokeWithArgs("aView", "aProject");
  56. assertThat(result, failedWith(6));
  57. assertThat(result, hasNoStandardOutput());
  58. assertThat(result.stderr(), containsString("ERROR: user is missing the View/Read permission"));
  59. }
  60. @Test public void jobViewManipulationShouldFailWithViewReadPermissionOnly() throws IOException {
  61. j.jenkins.addView(new ListView("aView"));
  62. j.createFreeStyleProject("aProject");
  63. final CLICommandInvoker.Result result = command
  64. .authorizedTo(Jenkins.READ, View.READ)
  65. .invokeWithArgs("aView", "aProject");
  66. assertThat(result, failedWith(3));
  67. assertThat(result, hasNoStandardOutput());
  68. assertThat(result.stderr(), containsString("ERROR: No such job 'aProject'"));
  69. }
  70. @Test public void jobViewManipulationShouldFailWithViewReadAndJobReadPermissionsOnly() throws IOException {
  71. j.jenkins.addView(new ListView("aView"));
  72. j.createFreeStyleProject("aProject");
  73. final CLICommandInvoker.Result result = command
  74. .authorizedTo(Jenkins.READ, View.READ, Job.READ)
  75. .invokeWithArgs("aView", "aProject");
  76. assertThat(result, failedWith(6));
  77. assertThat(result, hasNoStandardOutput());
  78. assertThat(result.stderr(), containsString("ERROR: user is missing the View/Configure permission"));
  79. }
  80. @Test public void jobViewManipulationShouldFailIfTheViewIsNotDirectlyModifiable() throws Exception {
  81. FreeStyleProject project = j.createFreeStyleProject("aProject");
  82. assertThat(j.jenkins.getView("All").getAllItems().size(), equalTo(1));
  83. assertThat(j.jenkins.getView("All").contains(project), equalTo(true));
  84. final CLICommandInvoker.Result result = command
  85. .authorizedTo(Jenkins.READ, View.READ, Job.READ, View.CONFIGURE)
  86. .invokeWithArgs("All", "aProject");
  87. assertThat(result, failedWith(4));
  88. assertThat(result.stderr(), containsString("ERROR: 'All' view can not be modified directly"));
  89. assertThat(j.jenkins.getView("All").getAllItems().size(), equalTo(1));
  90. assertThat(j.jenkins.getView("All").contains(project), equalTo(true));
  91. }
  92. @Test public void jobViewManipulationShouldFailIfTheJobDoesNotExist() throws Exception {
  93. j.jenkins.addView(new ListView("aView"));
  94. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  95. CLICommandInvoker.Result result = command
  96. .authorizedTo(Jenkins.READ, View.READ, Job.READ, View.CONFIGURE)
  97. .invokeWithArgs("aView", "never_created");
  98. assertThat(result, failedWith(3));
  99. assertThat(result.stderr(), containsString("ERROR: No such job 'never_created'"));
  100. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  101. FreeStyleProject project = j.createFreeStyleProject("aProject");
  102. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  103. result = command
  104. .authorizedTo(Jenkins.READ, View.READ, Job.READ, View.CONFIGURE)
  105. .invokeWithArgs("aView", "aProject1");
  106. assertThat(result, failedWith(3));
  107. assertThat(result.stderr(), containsString("ERROR: No such job 'aProject1'; perhaps you meant 'aProject'?"));
  108. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  109. assertThat(j.jenkins.getView("aView").contains(project), equalTo(false));
  110. }
  111. @Test public void jobViewManipulationShouldFailIfTheJobNameIsEmpty() throws Exception {
  112. j.jenkins.addView(new ListView("aView"));
  113. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  114. final CLICommandInvoker.Result result = command
  115. .authorizedTo(Jenkins.READ, View.READ, Job.READ, View.CONFIGURE)
  116. .invokeWithArgs("aView", "");
  117. assertThat(result, failedWith(3));
  118. assertThat(result.stderr(), containsString("ERROR: No such job ''"));
  119. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  120. }
  121. @Test public void jobViewManipulationManyShouldFailIfFirstJobDoesNotExist() throws Exception {
  122. j.jenkins.addView(new ListView("aView"));
  123. FreeStyleProject project1 = j.createFreeStyleProject("aProject1");
  124. FreeStyleProject project2 = j.createFreeStyleProject("aProject2");
  125. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  126. assertThat(j.jenkins.getView("aView").contains(project1), equalTo(false));
  127. assertThat(j.jenkins.getView("aView").contains(project2), equalTo(false));
  128. final CLICommandInvoker.Result result = command
  129. .authorizedTo(Jenkins.READ, View.READ, Job.READ, View.CONFIGURE)
  130. .invokeWithArgs("aView", "never_created", "aProject1", "aProject2");
  131. assertThat(result, failedWith(3));
  132. assertThat(result.stderr(), containsString("ERROR: No such job 'never_created'; perhaps you meant 'aProject1'?"));
  133. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  134. assertThat(j.jenkins.getView("aView").contains(project1), equalTo(false));
  135. assertThat(j.jenkins.getView("aView").contains(project2), equalTo(false));
  136. }
  137. @Test public void jobViewManipulationManyShouldFailIfMiddleJobDoesNotExist() throws Exception {
  138. j.jenkins.addView(new ListView("aView"));
  139. FreeStyleProject project1 = j.createFreeStyleProject("aProject1");
  140. FreeStyleProject project2 = j.createFreeStyleProject("aProject2");
  141. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  142. assertThat(j.jenkins.getView("aView").contains(project1), equalTo(false));
  143. assertThat(j.jenkins.getView("aView").contains(project2), equalTo(false));
  144. final CLICommandInvoker.Result result = command
  145. .authorizedTo(Jenkins.READ, View.READ, Job.READ, View.CONFIGURE)
  146. .invokeWithArgs("aView", "aProject1", "never_created", "aProject2");
  147. assertThat(result, failedWith(3));
  148. assertThat(result.stderr(), containsString("ERROR: No such job 'never_created'; perhaps you meant 'aProject1'?"));
  149. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  150. assertThat(j.jenkins.getView("aView").contains(project1), equalTo(false));
  151. assertThat(j.jenkins.getView("aView").contains(project2), equalTo(false));
  152. }
  153. @Test public void jobViewManipulationManyShouldFailIfLastJobDoesNotExist() throws Exception {
  154. j.jenkins.addView(new ListView("aView"));
  155. FreeStyleProject project1 = j.createFreeStyleProject("aProject1");
  156. FreeStyleProject project2 = j.createFreeStyleProject("aProject2");
  157. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  158. assertThat(j.jenkins.getView("aView").contains(project1), equalTo(false));
  159. assertThat(j.jenkins.getView("aView").contains(project2), equalTo(false));
  160. final CLICommandInvoker.Result result = command
  161. .authorizedTo(Jenkins.READ, View.READ, Job.READ, View.CONFIGURE)
  162. .invokeWithArgs("aView", "aProject1", "aProject2", "never_created");
  163. assertThat(result, failedWith(3));
  164. assertThat(result.stderr(), containsString("ERROR: No such job 'never_created'; perhaps you meant 'aProject1'?"));
  165. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  166. assertThat(j.jenkins.getView("aView").contains(project1), equalTo(false));
  167. assertThat(j.jenkins.getView("aView").contains(project2), equalTo(false));
  168. }
  169. @Test public void jobViewManipulationManyShouldFailIfMoreJobsDoNotExist() throws Exception {
  170. j.jenkins.addView(new ListView("aView"));
  171. FreeStyleProject project1 = j.createFreeStyleProject("aProject1");
  172. FreeStyleProject project2 = j.createFreeStyleProject("aProject2");
  173. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  174. assertThat(j.jenkins.getView("aView").contains(project1), equalTo(false));
  175. assertThat(j.jenkins.getView("aView").contains(project2), equalTo(false));
  176. final CLICommandInvoker.Result result = command
  177. .authorizedTo(Jenkins.READ, View.READ, Job.READ, View.CONFIGURE)
  178. .invokeWithArgs("aView", "aProject1", "never_created", "aProject2", "never_created");
  179. assertThat(result, failedWith(3));
  180. assertThat(result.stderr(), containsString("ERROR: No such job 'never_created'; perhaps you meant 'aProject1'?"));
  181. assertThat(j.jenkins.getView("aView").getAllItems().size(), equalTo(0));
  182. assertThat(j.jenkins.getView("aView").contains(project1), equalTo(false));
  183. assertThat(j.jenkins.getView("aView").contains(project2), equalTo(false));
  184. }
  185. }