@Test public void testRestInterfaceSuccess() throws Exception { prepareSecurity(); FreeStyleProject srcProject = j.createFreeStyleProject(); srcProject.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("test1", false))); srcProject.save(); WebClient wc = j.createWebClient(); wc.login("test1", "test1"); // GET config.xml of srcProject (userid is set to test1) String configXml = getConfigXml(wc.goToXml(String.format("%s/config.xml", srcProject.getUrl()))); // POST config.xml of srcProject (userid is set to test1) to a new project. // This should success. FreeStyleProject destProject = j.createFreeStyleProject(); destProject.save(); String projectName = destProject.getFullName(); WebRequestSettings req = new WebRequestSettings( wc.createCrumbedUrl(String.format("%s/config.xml", destProject.getUrl())), HttpMethod.POST); req.setRequestBody(configXml); wc.getPage(req); { FreeStyleProject p = j.jenkins.getItemByFullName(projectName, FreeStyleProject.class); assertNotNull(p); AuthorizeProjectProperty prop = p.getProperty(AuthorizeProjectProperty.class); assertNotNull(prop); assertEquals(SpecificUsersAuthorizationStrategy.class, prop.getStrategy().getClass()); SpecificUsersAuthorizationStrategy strategy = (SpecificUsersAuthorizationStrategy) prop.getStrategy(); assertEquals("test1", strategy.getUserid()); } j.jenkins.reload(); { FreeStyleProject p = j.jenkins.getItemByFullName(projectName, FreeStyleProject.class); assertNotNull(p); AuthorizeProjectProperty prop = p.getProperty(AuthorizeProjectProperty.class); assertNotNull(prop); assertEquals(SpecificUsersAuthorizationStrategy.class, prop.getStrategy().getClass()); SpecificUsersAuthorizationStrategy strategy = (SpecificUsersAuthorizationStrategy) prop.getStrategy(); assertEquals("test1", strategy.getUserid()); } }
@Test public void testRestInterfaceFailure() throws Exception { prepareSecurity(); FreeStyleProject srcProject = j.createFreeStyleProject(); srcProject.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", false))); srcProject.save(); WebClient wc = j.createWebClient(); wc.login("test1", "test1"); // GET config.xml of srcProject (userid is set to admin) String configXml = getConfigXml(wc.goToXml(String.format("%s/config.xml", srcProject.getUrl()))); // POST config.xml of srcProject (userid is set to admin) to a new project. // This should fail. FreeStyleProject destProject = j.createFreeStyleProject(); destProject.save(); String projectName = destProject.getFullName(); WebRequestSettings req = new WebRequestSettings( wc.createCrumbedUrl(String.format("%s/config.xml", destProject.getUrl())), HttpMethod.POST); req.setRequestBody(configXml); try { wc.getPage(req); fail(); } catch (FailingHttpStatusCodeException e) { } { FreeStyleProject p = j.jenkins.getItemByFullName(projectName, FreeStyleProject.class); assertNotNull(p); AuthorizeProjectProperty prop = p.getProperty(AuthorizeProjectProperty.class); assertTrue(prop == null || prop.getStrategy() == null); } j.jenkins.reload(); { FreeStyleProject p = j.jenkins.getItemByFullName(projectName, FreeStyleProject.class); assertNotNull(p); AuthorizeProjectProperty prop = p.getProperty(AuthorizeProjectProperty.class); assertTrue(prop == null || prop.getStrategy() == null); } }
@Issue("JENKINS-17302") @Test public void doConfigDotXml() throws Exception { ListView view = listView("v"); view.description = "one"; WebClient wc = j.createWebClient(); String xml = wc.goToXml("view/v/config.xml").getContent(); assertTrue(xml, xml.contains("<description>one</description>")); xml = xml.replace("<description>one</description>", "<description>two</description>"); WebRequestSettings req = new WebRequestSettings(wc.createCrumbedUrl("view/v/config.xml"), HttpMethod.POST); req.setRequestBody(xml); wc.getPage(req); assertEquals("two", view.getDescription()); xml = new XmlFile(Jenkins.XSTREAM, new File(j.jenkins.getRootDir(), "config.xml")).asString(); assertTrue(xml, xml.contains("<description>two</description>")); }