@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);
    }
  }
Ejemplo n.º 3
0
  /**
   * Creates a new freestyle project and builds the project with a string parameter. If the build is
   * succesful, a rebuild of the last build is done. The rebuild on the project level should point
   * to the last build
   *
   * @throws Exception Exception
   */
  public void testWhenProjectWithParamsThenRebuildProjectExecutesRebuildOfLastBuild()
      throws Exception {
    FreeStyleProject project = createFreeStyleProject();

    // Build (#1)
    project
        .scheduleBuild2(
            0,
            new Cause.UserIdCause(),
            new ParametersAction(new StringParameterValue("name", "test")))
        .get();
    HtmlPage rebuildConfigPage = createWebClient().getPage(project, "1/rebuild");
    // Rebuild (#2)
    submit(rebuildConfigPage.getFormByName("config"));

    HtmlPage projectPage = createWebClient().getPage(project);
    WebAssert.assertLinkPresentWithText(projectPage, "Rebuild Last");

    HtmlAnchor rebuildHref = projectPage.getAnchorByText("Rebuild Last");
    assertEquals(
        "Rebuild Last should point to the second build",
        rebuildHref.getHrefAttribute(),
        "/" + project.getUrl() + "2/rebuild");
  }