private String setUpPipelineGroupsWithAdminPermissions() throws IOException { setCurrentUser("ram"); assertThat(CaseInsensitiveString.str(UserHelper.getUserName().getUsername()), is("ram")); configHelper.addPipelineWithGroup("studios", "go", "stage", "build1", "build2"); configHelper.setAdminPermissionForGroup("studios", "barrow"); configHelper.addPipelineWithGroup("consulting", "bcg", "stage", "build1", "build2"); configHelper.setAdminPermissionForGroup("consulting", "ram"); configHelper.addTemplate("newTemplate", "stage"); return goConfigDao.md5OfConfigFile(); }
@Test public void shouldPostGroupWithTemplateAsPartialXml() throws Exception { configHelper.addTemplate("template-1", "dev"); configHelper.addPipelineWithGroup("group", "pipeline", "dev", "linux", "windows"); configHelper.addPipelineWithTemplate("group", "pipeline-with-template", "template-1"); String newXml = NEW_GROUP; String md5 = goConfigDao.md5OfConfigFile(); ModelAndView mav = controller.postGroupAsXmlPartial("group", newXml, md5, response); assertResponseMessage(mav, "Group changed successfully."); assertThat(response.getStatus(), is(SC_OK)); assertThat(response.getContentType(), is(RESPONSE_CHARSET_JSON)); }
@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)); }
@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")); }