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

    ConfigElementImplementationRegistry registry =
        ConfigElementImplementationRegistryMother.withNoPlugins();
    XmlUtils.validate(
        new FileInputStream(configHelper.getConfigFile()),
        GoConfigSchema.getCurrentSchema(),
        new XsdErrorTranslator(),
        new SAXBuilder(),
        registry.xsds());
    controller.postGroupAsXmlPartial(
        TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME, NEW_TEMPLATES, md5, response);

    assertThat(response.getStatus(), is(SC_UNAUTHORIZED));
    XmlUtils.validate(
        new FileInputStream(configHelper.getConfigFile()),
        GoConfigSchema.getCurrentSchema(),
        new XsdErrorTranslator(),
        new SAXBuilder(),
        registry.xsds());

    setCurrentUser("admin");
    controller.postGroupAsXmlPartial(
        TemplatesConfig.PIPELINE_TEMPLATES_FAKE_GROUP_NAME, NEW_TEMPLATES, md5, response);
    assertThat(response.getStatus(), is(SC_OK));
  }
  @Test
  public void postGroupAsXmlPartial_shouldEnforcePipelineGroupAdminPermissions() throws Exception {
    String md5 = setUpPipelineGroupsWithAdminPermissions();

    controller.postGroupAsXmlPartial("studios", NEW_GROUP, md5, response);
    assertThat(response.getStatus(), is(SC_UNAUTHORIZED));

    controller.postGroupAsXmlPartial("consulting", NEW_GROUP, md5, response);
    assertThat(response.getStatus(), is(SC_OK));
  }
 @Test
 public void shouldPostGroupWithChangedNameAsPartialXml() throws Exception {
   configHelper.addPipelineWithGroup("group", "pipeline", "dev", "linux", "windows");
   String newXml = RENAMED_GROUP;
   String md5 = goConfigDao.md5OfConfigFile();
   ModelAndView mav = controller.postGroupAsXmlPartial("group", newXml, md5, response);
   assertThat(response.getStatus(), is(SC_OK));
   assertThat(response.getContentType(), is(RESPONSE_CHARSET_JSON));
   assertResponseMessage(mav, "Group changed successfully.");
 }
 @Test
 public void shouldModifyExistingPipelineTemplateWhenPostedAsPartial() throws Exception {
   configHelper.addPipelineWithGroup(
       "some-group", "some-pipeline", "some-dev", "some-linux", "some-windows");
   configHelper.addAgent("some-home", "UUID");
   String newXml = NEW_TEMPLATES;
   String md5 = goConfigDao.md5OfConfigFile();
   ModelAndView mav =
       controller.postGroupAsXmlPartial("Pipeline Templates", newXml, md5, response);
   assertResponseMessage(mav, "Template changed successfully.");
   assertThat(response.getStatus(), is(SC_OK));
   assertThat(response.getContentType(), is(RESPONSE_CHARSET_JSON));
   assertThat(configHelper.currentConfig().getTemplates().size(), is(1));
 }
  @Test
  public void shouldGetGroupWithTemplatesAsXml() throws Exception {
    configHelper.addTemplate("template-1", "dev");
    configHelper.addPipelineWithGroup("group", "pipeline", "dev", "linux");
    PipelineConfig pipelineWithTemplate =
        configHelper.addPipelineWithTemplate("group", "pipeline-with-template", "template-1");
    assertThat(
        pipelineWithTemplate.size(),
        is(0)); // should not expect mutation of pipeline config passed in
    assertThat(
        configHelper
            .currentConfig()
            .pipelineConfigByName(new CaseInsensitiveString("pipeline-with-template"))
            .size(),
        is(1));

    CruiseConfig config = goConfigService.currentCruiseConfig();
    PipelineConfig pipelineConfig =
        config.pipelineConfigByName(new CaseInsensitiveString("pipeline-with-template"));
    assertThat("Should not modify the original template", pipelineConfig.size(), is(1));

    controller.getGroupAsXmlPartial("group", null, response);
    String content =
        "<pipelines group=\"group\">\n"
            + "  <pipeline name=\"pipeline\">\n"
            + "    <materials>\n"
            + "      <svn url=\"svn:///user:pass@tmp/foo\" />\n"
            + "    </materials>\n"
            + "    <stage name=\"dev\">\n"
            + "      <jobs>\n"
            + "        <job name=\"linux\" />\n"
            + "      </jobs>\n"
            + "    </stage>\n"
            + "  </pipeline>\n"
            + "  <pipeline name=\"pipeline-with-template\" template=\"template-1\">\n"
            + "    <materials>\n"
            + "      <svn url=\"svn:///user:pass@tmp/foo\" />\n"
            + "    </materials>\n"
            + "  </pipeline>\n"
            + "</pipelines>";
    assertValidContentAndStatus(SC_OK, "text/xml", content);

    MockHttpServletResponse postResponse = new MockHttpServletResponse();
    controller.postGroupAsXmlPartial("group", content, null, postResponse);

    config = goConfigService.currentCruiseConfig();
    pipelineConfig =
        config.pipelineConfigByName(new CaseInsensitiveString("pipeline-with-template"));
    assertThat("Should not modify the original template", pipelineConfig.size(), is(1));
  }