/** Evaluates the literal Jelly script passed as a parameter as HTML and returns the page. */
  protected HtmlPage evaluateAsHtml(String jellyScript) throws Exception {
    HudsonTestCase.WebClient wc = new WebClient();

    WebRequestSettings req = new WebRequestSettings(wc.createCrumbedUrl("eval"), POST);
    req.setRequestBody(
        "<j:jelly xmlns:j='jelly:core' xmlns:st='jelly:stapler' xmlns:l='/lib/layout' xmlns:f='/lib/form'>"
            + jellyScript
            + "</j:jelly>");
    Page page = wc.getPage(req);
    return (HtmlPage) page;
  }
  @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);
    }
  }
  /** This test unit is for testing a commit hook using the UUID */
  @Bug(399165)
  @Test
  public void testPrebuiltCommitTrigger() throws Exception {
    hudson.setCrumbIssuer(null);

    // First create repository with 1 file and commit information
    SVNCommitInfo info = createSVNRepository();
    assertNull(info.getErrorMessage());
    assertEquals("Failed to create 1 revision.", 1, info.getNewRevision());

    // Create freestyle project with SVN SCM.
    FreeStyleProject project = createFreeStyleProject();
    project.setScm(new SubversionSCM("file:///tmp/399165"));
    SCMTrigger trigger = new SCMTrigger("0 */6 * * *");
    project.addTrigger(trigger);
    trigger.start(project, true);

    // Execute build (This is critical for fixing eclipse bug: 399165)
    assertBuildStatusSuccess(project.scheduleBuild2(0));

    // Commit a file again.
    info = createSecondCommit();
    assertNull(info.getErrorMessage());
    assertEquals("Failed to create second commit.", 2, info.getNewRevision());

    // Create post-commit hook
    WebClient wc = new WebClient();
    WebRequestSettings wr =
        new WebRequestSettings(
            new URL(
                getURL() + "subversion/" + repository.getRepositoryUUID(false) + "/notifyCommit"),
            HttpMethod.POST);
    wr.setRequestBody("A   dirB/file2.txt");
    wr.setAdditionalHeader("Content-Type", "text/plain;charset=UTF-8");

    wr.setAdditionalHeader("X-Hudson-Subversion-Revision", "2");

    WebConnection conn = wc.getWebConnection();
    System.out.println(wr);
    WebResponse resp = conn.getResponse(wr);
    assertTrue(isGoodHttpStatus(resp.getStatusCode()));

    waitUntilNoActivity();
    FreeStyleBuild b = project.getLastBuild();
    assertNotNull(b);

    assertBuildStatus(Result.SUCCESS, b);

    assertEquals("Failed to execute a buid.", 2, b.getNumber());
  }
Beispiel #5
0
 @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>"));
 }
 /**
  * Tests that configuring an existing project via jenkins http rest doesn't produce duplicated
  * triggers and that the trigger is configured for the new project pattern.
  *
  * @throws Exception if so
  */
 @Test
 @LocalData
 public void testReconfigureUsingRestApi() throws Exception {
   assertNrOfEventListeners(0);
   TopLevelItem testProj = j.jenkins.getItem("testProj");
   String gerritProjectPattern = "someotherproject";
   XmlPage xmlPage = loadConfigXmlViaHttp(testProj);
   Document document = xmlPage.getXmlDocument();
   String xml = changeConfigXml(gerritProjectPattern, document);
   URL url = UrlUtils.toUrlUnsafe(j.getURL().toExternalForm() + testProj.getUrl() + "config.xml");
   WebRequestSettings request = new WebRequestSettings(url, HttpMethod.POST);
   request.setRequestBody(xml);
   j.jenkins.setCrumbIssuer(null);
   Page page = j.createWebClient().getPage(request);
   j.assertGoodStatus(page);
   assertNrOfEventListeners(0);
   assertEventListenerWithSomeOtherProjectSet(gerritProjectPattern);
 }
  @Test
  public void testPostCommitTrigger() throws Exception {
    // Disable crumbs because HTMLUnit refuses to mix request bodies with
    // request parameters
    hudson.setCrumbIssuer(null);

    FreeStyleProject p = createFreeStyleProject();
    String url = "https://tsethudsonsvn.googlecode.com/svn/trunk";
    SCMTrigger trigger = new SCMTrigger("0 */6 * * *");

    p.setScm(new SubversionSCM(url));
    p.addTrigger(trigger);
    trigger.start(p, true);

    String repoUUID = "b703df53-fdd9-0691-3d8c-58db40123d9f";

    WebClient wc = new WebClient();
    WebRequestSettings wr =
        new WebRequestSettings(
            new URL(getURL() + "subversion/" + repoUUID + "/notifyCommit"), HttpMethod.POST);
    wr.setRequestBody("A   trunk/testcommit.txt");
    wr.setAdditionalHeader("Content-Type", "text/plain;charset=UTF-8");

    wr.setAdditionalHeader("X-Hudson-Subversion-Revision", "16");

    WebConnection conn = wc.getWebConnection();
    WebResponse resp = conn.getResponse(wr);
    assertTrue(isGoodHttpStatus(resp.getStatusCode()));

    waitUntilNoActivity();
    FreeStyleBuild b = p.getLastBuild();
    assertNotNull(b);
    assertBuildStatus(Result.SUCCESS, b);

    SVNRevisionState revisionState = b.getAction(SVNRevisionState.class);

    assertNotNull("Failed to find revision", revisionState);

    assertNotNull("Failed to find revision", revisionState.revisions.get(url));

    assertEquals(16, revisionState.revisions.get(url).longValue());
  }
  /** @throws Exception if an error occurs */
  @Test
  public void setRequestHeader() throws Exception {
    final String html =
        "<html><head><script>\n"
            + "  function test() {\n"
            + "    var request = "
            + XHRInstantiation_
            + ";\n"
            + "    request.open('GET', 'second.html', false);\n"
            + "    request.setRequestHeader('Accept', 'text/javascript, application/javascript, */*');\n"
            + "    request.setRequestHeader('Accept-Language', 'ar-eg');\n"
            + "    request.send('');\n"
            + "  }\n"
            + "</script></head><body onload='test()'></body></html>";

    getMockWebConnection().setDefaultResponse("");
    loadPage2(html);

    final WebRequestSettings lastRequest = getMockWebConnection().getLastWebRequestSettings();
    final Map<String, String> headers = lastRequest.getAdditionalHeaders();
    assertEquals("text/javascript, application/javascript, */*", headers.get("Accept"));
    assertEquals("ar-eg", headers.get("Accept-Language"));
  }