@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); } }
@Test @LocalData public void testLoadOnStart() throws Exception { // verify that SpecificUserAuthorizationStrategy is loaded correctly from the disk on startup. { FreeStyleProject p = j.jenkins.getItemByFullName("test", 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(); // verify that SpecificUserAuthorizationStrategy is reloaded correctly from the disk. { FreeStyleProject p = j.jenkins.getItemByFullName("test", 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 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 testCliFailure() 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 = null; { CLI cli = new CLI(j.getURL()); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); int ret = cli.execute( Arrays.asList( "get-job", srcProject.getFullName(), "--username", "test1", "--password", "test1"), new NullInputStream(0), stdout, stderr); assertEquals(stderr.toString(), 0, ret); configXml = stdout.toString(); } // 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(); { CLI cli = new CLI(j.getURL()); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); int ret = cli.execute( Arrays.asList( "update-job", destProject.getFullName(), "--username", "test1", "--password", "test1"), new ByteArrayInputStream(configXml.getBytes()), stdout, stderr); assertNotEquals(0, ret); } { 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); } }