@Test public void privateView() throws Exception { j.createFreeStyleProject("project1"); User user = User.get("me", true); // create user WebClient wc = j.createWebClient(); HtmlPage userPage = wc.goTo("user/me"); HtmlAnchor privateViewsLink = userPage.getFirstAnchorByText("My Views"); assertNotNull("My Views link not available", privateViewsLink); HtmlPage privateViewsPage = (HtmlPage) privateViewsLink.click(); Text viewLabel = (Text) privateViewsPage.getFirstByXPath("//div[@class='tabBar']//div[@class='tab active']/a/text()"); assertTrue("'All' view should be selected", viewLabel.getTextContent().contains(Hudson_ViewName())); View listView = listView("listView"); HtmlPage newViewPage = wc.goTo("user/me/my-views/newView"); HtmlForm form = newViewPage.getFormByName("createItem"); form.getInputByName("name").setValueAttribute("proxy-view"); ((HtmlRadioButtonInput) form.getInputByValue("hudson.model.ProxyView")).setChecked(true); HtmlPage proxyViewConfigurePage = j.submit(form); View proxyView = user.getProperty(MyViewsProperty.class).getView("proxy-view"); assertNotNull(proxyView); form = proxyViewConfigurePage.getFormByName("viewConfig"); form.getSelectByName("proxiedViewName").setSelectedAttribute("listView", true); j.submit(form); assertTrue(proxyView instanceof ProxyView); assertEquals(((ProxyView) proxyView).getProxiedViewName(), "listView"); assertEquals(((ProxyView) proxyView).getProxiedView(), listView); }
@Issue("JENKINS-9367") @Test public void allImagesCanBeLoaded() throws Exception { User.get("user", true); WebClient webClient = j.createWebClient(); webClient.setJavaScriptEnabled(false); j.assertAllImageLoadSuccessfully(webClient.goTo("asynchPeople")); }
@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>")); }
@Test public void deleteView() throws Exception { WebClient wc = j.createWebClient(); ListView v = listView("list"); HtmlPage delete = wc.getPage(v, "delete"); j.submit(delete.getFormByName("delete")); assertNull(j.jenkins.getView("list")); User user = User.get("user", true); MyViewsProperty p = user.getProperty(MyViewsProperty.class); v = new ListView("list", p); p.addView(v); delete = wc.getPage(v, "delete"); j.submit(delete.getFormByName("delete")); assertNull(p.getView("list")); }
@Test @Issue("JENKINS-11543") public void unicodeParametersArePresetCorrectly() throws Exception { final FreeStyleProject p = j.createFreeStyleProject(); ParametersDefinitionProperty pdb = new ParametersDefinitionProperty( new StringParameterDefinition("sname:a¶‱ﻷ", "svalue:a¶‱ﻷ", "sdesc:a¶‱ﻷ"), new FileParameterDefinition("fname:a¶‱ﻷ", "fdesc:a¶‱ﻷ")); p.addProperty(pdb); WebClient wc = j.createWebClient(); wc.setThrowExceptionOnFailingStatusCode(false); // Ignore 405 HtmlPage page = wc.getPage(p, "build"); // java.lang.IllegalArgumentException: No such parameter definition: <gibberish>. wc.setThrowExceptionOnFailingStatusCode(true); final HtmlForm form = page.getFormByName("parameters"); form.submit(form.getButtonByCaption("Build")); }
@Test public void choiceWithLTGT() throws Exception { FreeStyleProject project = j.createFreeStyleProject(); ParametersDefinitionProperty pdp = new ParametersDefinitionProperty( new ChoiceParameterDefinition("choice", "Choice 1\nChoice <2>", "choice description")); project.addProperty(pdp); CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder(); project.getBuildersList().add(builder); WebClient wc = j.createWebClient(); wc.setThrowExceptionOnFailingStatusCode(false); HtmlPage page = wc.goTo("job/" + project.getName() + "/build?delay=0sec"); HtmlForm form = page.getFormByName("parameters"); HtmlElement element = (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='choice']"); assertNotNull(element); assertEquals( "choice description", ((HtmlElement) element .getNextSibling() .getNextSibling() .selectSingleNode("td[@class='setting-description']")) .getTextContent()); assertEquals( "choice", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent()); HtmlOption opt = (HtmlOption) element.selectSingleNode("td/div/select/option[@value='Choice <2>']"); assertNotNull(opt); assertEquals("Choice <2>", opt.asText()); opt.setSelected(true); j.submit(form); Queue.Item q = j.jenkins.getQueue().getItem(project); if (q != null) q.getFuture().get(); else Thread.sleep(1000); assertNotNull(builder.getEnvVars()); assertEquals("Choice <2>", builder.getEnvVars().get("CHOICE")); }
@Test @Issue("JENKINS-3539") public void fileParameterNotSet() throws Exception { FreeStyleProject project = j.createFreeStyleProject(); ParametersDefinitionProperty pdp = new ParametersDefinitionProperty(new FileParameterDefinition("filename", "description")); project.addProperty(pdp); WebClient wc = j.createWebClient(); wc.setThrowExceptionOnFailingStatusCode(false); HtmlPage page = wc.goTo("job/" + project.getName() + "/build?delay=0sec"); HtmlForm form = page.getFormByName("parameters"); j.submit(form); Queue.Item q = j.jenkins.getQueue().getItem(project); if (q != null) q.getFuture().get(); else Thread.sleep(1000); assertFalse("file must not exist", project.getSomeWorkspace().child("filename").exists()); }
@Test public void testConfigurationAuthentication() throws Exception { prepareSecurity(); FreeStyleProject p = j.createFreeStyleProject(); WebClient wc = j.createWebClient(); wc.login("test1"); // Reauthentication is not required if No need for re-authentication is checked p.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", true))); j.submit(wc.getPage(p, "configure").getFormByName("config")); // Reauthentication is required if No need for re-authentication is checked p.removeProperty(AuthorizeProjectProperty.class); p.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", false))); try { j.submit(wc.getPage(p, "configure").getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } // No authentication is required if oneself. { HtmlPage page = wc.getPage(p, "configure"); HtmlTextInput userid = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'userid') and @type='text']"); userid.setValueAttribute("test1"); j.submit(page.getFormByName("config")); assertEquals( "test1", ((SpecificUsersAuthorizationStrategy) p.getProperty(AuthorizeProjectProperty.class).getStrategy()) .getUserid()); } // Reauthentication is required to change userid even if No need for re-authentication is // checked p.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("admin", true))); { HtmlPage page = wc.getPage(p, "configure"); HtmlTextInput userid = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'userid') and @type='text']"); userid.setValueAttribute("test2"); try { j.submit(page.getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } } }
@LocalData @Test public void basic() throws Exception { FreeStyleBuild build = project.scheduleBuild2(0).get(10, TimeUnit.SECONDS); assertTestResults(build); WebClient wc = j.new WebClient(); wc.getPage(project); // project page wc.getPage(build); // build page wc.getPage(build, "testReport"); // test report wc.getPage(build, "testReport/hudson.security"); // package wc.getPage(build, "testReport/hudson.security/HudsonPrivateSecurityRealmTest/"); // class wc.getPage( build, "testReport/hudson.security/HudsonPrivateSecurityRealmTest/testDataCompatibilityWith1_282/"); // method }
@Test public void testConfigureApitoken() throws Exception { prepareSecurity(); FreeStyleProject p = j.createFreeStyleProject(); p.addProperty( new AuthorizeProjectProperty(new SpecificUsersAuthorizationStrategy("test2", false))); WebClient wc = j.createWebClient(); wc.login("test1"); String apitokenForTest2 = User.get("test2").getProperty(ApiTokenProperty.class).getApiToken(); assertNotNull(apitokenForTest2); assertNotEquals("", apitokenForTest2); // authentication fails without apitoken { HtmlPage page = wc.getPage(p, "configure"); HtmlCheckBoxInput useApitoken = page.<HtmlCheckBoxInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']"); useApitoken.setChecked(true); try { j.submit(page.getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } } // authentication succeeds with the good apitoken { HtmlPage page = wc.getPage(p, "configure"); HtmlCheckBoxInput useApitoken = page.<HtmlCheckBoxInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']"); useApitoken.setChecked(true); HtmlTextInput apitoken = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'apitoken') and @type='text']"); apitoken.setValueAttribute(apitokenForTest2); j.submit(page.getFormByName("config")); assertEquals( "test2", ((SpecificUsersAuthorizationStrategy) p.getProperty(AuthorizeProjectProperty.class).getStrategy()) .getUserid()); } // authentication fails with a bad apitoken { HtmlPage page = wc.getPage(p, "configure"); HtmlCheckBoxInput useApitoken = page.<HtmlCheckBoxInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']"); useApitoken.setChecked(true); HtmlTextInput apitoken = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'apitoken') and @type='text']"); apitoken.setValueAttribute(apitokenForTest2 + "xxx"); try { j.submit(page.getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } } // authentication fails if the apitoken is used for password { HtmlPage page = wc.getPage(p, "configure"); HtmlCheckBoxInput useApitoken = page.<HtmlCheckBoxInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'useApitoken') and @type='checkbox']"); useApitoken.setChecked(false); HtmlPasswordInput password = page.<HtmlPasswordInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'password') and @type='password']"); password.setValueAttribute(apitokenForTest2); HtmlTextInput apitoken = page.<HtmlTextInput>getFirstByXPath( "//*[contains(@class, 'specific-user-authorization')]//input[contains(@name, 'apitoken') and @type='text']"); apitoken.setValueAttribute(apitokenForTest2); try { j.submit(page.getFormByName("config")); fail(); } catch (FailingHttpStatusCodeException e) { assertEquals(400, e.getStatusCode()); } } }
@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); } }
@Test public void parameterTypes() throws Exception { FreeStyleProject otherProject = j.createFreeStyleProject(); otherProject.scheduleBuild2(0).get(); FreeStyleProject project = j.createFreeStyleProject(); ParametersDefinitionProperty pdp = new ParametersDefinitionProperty( new StringParameterDefinition("string", "defaultValue", "string description"), new BooleanParameterDefinition("boolean", true, "boolean description"), new ChoiceParameterDefinition("choice", "Choice 1\nChoice 2", "choice description"), new RunParameterDefinition("run", otherProject.getName(), "run description", null)); project.addProperty(pdp); CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder(); project.getBuildersList().add(builder); WebClient wc = j.createWebClient(); wc.setThrowExceptionOnFailingStatusCode(false); HtmlPage page = wc.goTo("job/" + project.getName() + "/build?delay=0sec"); HtmlForm form = page.getFormByName("parameters"); HtmlElement element = (HtmlElement) form.selectSingleNode("//tr[td/div/input/@value='string']"); assertNotNull(element); assertEquals( "string description", ((HtmlElement) element .getNextSibling() .getNextSibling() .selectSingleNode("td[@class='setting-description']")) .getTextContent()); HtmlTextInput stringParameterInput = (HtmlTextInput) element.selectSingleNode(".//input[@name='value']"); assertEquals("defaultValue", stringParameterInput.getAttribute("value")); assertEquals( "string", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent()); stringParameterInput.setAttribute("value", "newValue"); element = (HtmlElement) form.selectSingleNode("//tr[td/div/input/@value='boolean']"); assertNotNull(element); assertEquals( "boolean description", ((HtmlElement) element .getNextSibling() .getNextSibling() .selectSingleNode("td[@class='setting-description']")) .getTextContent()); Object o = element.selectSingleNode(".//input[@name='value']"); System.out.println(o); HtmlCheckBoxInput booleanParameterInput = (HtmlCheckBoxInput) o; assertEquals(true, booleanParameterInput.isChecked()); assertEquals( "boolean", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent()); element = (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='choice']"); assertNotNull(element); assertEquals( "choice description", ((HtmlElement) element .getNextSibling() .getNextSibling() .selectSingleNode("td[@class='setting-description']")) .getTextContent()); assertEquals( "choice", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent()); element = (HtmlElement) form.selectSingleNode(".//tr[td/div/input/@value='run']"); assertNotNull(element); assertEquals( "run description", ((HtmlElement) element .getNextSibling() .getNextSibling() .selectSingleNode("td[@class='setting-description']")) .getTextContent()); assertEquals( "run", ((HtmlElement) element.selectSingleNode("td[@class='setting-name']")).getTextContent()); j.submit(form); Queue.Item q = j.jenkins.getQueue().getItem(project); if (q != null) q.getFuture().get(); else Thread.sleep(1000); assertEquals("newValue", builder.getEnvVars().get("STRING")); assertEquals("true", builder.getEnvVars().get("BOOLEAN")); assertEquals("Choice 1", builder.getEnvVars().get("CHOICE")); assertEquals( j.jenkins.getRootUrl() + otherProject.getLastBuild().getUrl(), builder.getEnvVars().get("RUN")); }