@Test
  public void getPipelineAsXmlPartial_shouldEnforcePipelineGroupAdminPermissions()
      throws Exception {
    String md5 = setUpPipelineGroupsWithAdminPermissions();

    controller.getPipelineAsXmlPartial(0, "studios", md5, response);
    assertThat(response.getStatus(), is(SC_UNAUTHORIZED));
    assertThat(
        response.getContentAsString(),
        is("User 'ram' does not have permissions to administer pipeline group 'studios'"));

    controller.getPipelineAsXmlPartial(0, "consulting", md5, response);
    assertThat(response.getStatus(), is(SC_OK));
  }
  @Test
  public void shouldGetPipelineAsPartialXml() throws Exception {
    // get the pipeline XML
    configHelper.addPipeline("pipeline", "dev", "linux", "windows");
    groupName = BasicPipelineConfigs.DEFAULT_GROUP;
    controller.getPipelineAsXmlPartial(0, groupName, null, response);
    String xml = response.getContentAsString();
    assertThat(xml, containsString("pass"));

    // save the pipeline XML
    MockHttpServletResponse postResponse = new MockHttpServletResponse();
    String modifiedXml = xml.replace("pass", "secret");
    controller.postPipelineAsXmlPartial(
        0, groupName, modifiedXml, goConfigDao.md5OfConfigFile(), postResponse);

    // get the pipeline XML again
    MockHttpServletResponse getResponse = new MockHttpServletResponse();
    controller.getPipelineAsXmlPartial(0, groupName, null, getResponse);
    assertThat(getResponse.getContentAsString(), containsString("secret"));
    assertThat(getResponse.getContentAsString(), is(modifiedXml));
  }
  @Test
  public void shouldGetTemplateAsPartialXmlOnlyIfUserHasAdminRights() throws Exception {
    // get the pipeline XML
    configHelper.addPipeline("pipeline", "dev", "linux", "windows");
    configHelper.addTemplate("new-template", "dev");
    controller.getPipelineAsXmlPartial(
        0, TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME, null, response);
    String xml = response.getContentAsString();
    assertThat(xml, containsString("new-template"));

    // save the pipeline XML
    MockHttpServletResponse postResponse = new MockHttpServletResponse();
    String modifiedXml = xml.replace("new-template", "new-name-for-template");
    controller.postPipelineAsXmlPartial(
        0,
        TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME,
        modifiedXml,
        goConfigDao.md5OfConfigFile(),
        postResponse);

    // get the pipeline XML again
    MockHttpServletResponse getResponse = new MockHttpServletResponse();
    controller.getPipelineAsXmlPartial(
        0, TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME, null, getResponse);
    assertThat(getResponse.getContentAsString(), containsString("new-name-for-template"));
    assertThat(getResponse.getContentAsString(), is(modifiedXml));

    setCurrentUser("user");
    MockHttpServletResponse nonAdminResponse = new MockHttpServletResponse();
    controller.getPipelineAsXmlPartial(
        0, TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME, null, nonAdminResponse);
    assertThat(nonAdminResponse.getStatus(), is(SC_UNAUTHORIZED));
    assertThat(
        nonAdminResponse.getContentAsString(),
        is("User 'user' does not have permission to administer pipeline templates"));
  }
 @Test
 public void shouldGetErrorMessageWhenPipelineDoesNotExist() throws Exception {
   configHelper.addPipeline("pipeline", "stage", "build1");
   controller.getPipelineAsXmlPartial(1, groupName, null, response);
   assertValidContentAndStatus(SC_NOT_FOUND, RESPONSE_CHARSET, "Pipeline does not exist.");
 }