@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
  @LocalData
  public void testIsAuthenticateionRequiredAsUser() {
    ACL.impersonate(User.get("test1").impersonate());
    assertFalse(Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER));
    // after: not configured
    // before: test2, require re-auth
    // result: false
    assertFalse(
        SpecificUsersAuthorizationStrategy.isAuthenticateionRequired(
            null, new SpecificUsersAuthorizationStrategy("test2", false)));

    // after: test1, require re-auth
    // before: test2, require re-auth
    // result: false
    assertFalse(
        SpecificUsersAuthorizationStrategy.isAuthenticateionRequired(
            new SpecificUsersAuthorizationStrategy("test1", false),
            new SpecificUsersAuthorizationStrategy("test2", false)));

    // after: test1, require re-auth
    // before: not configured
    // result: false
    assertFalse(
        SpecificUsersAuthorizationStrategy.isAuthenticateionRequired(
            new SpecificUsersAuthorizationStrategy("test1", false), null));

    // after: test2, no require re-auth
    // before: not configured
    // result: true
    assertTrue(
        SpecificUsersAuthorizationStrategy.isAuthenticateionRequired(
            new SpecificUsersAuthorizationStrategy("test2", true), null));

    // after: test2, require re-auth
    // before: test2, no require re-auth
    // result: false
    assertFalse(
        SpecificUsersAuthorizationStrategy.isAuthenticateionRequired(
            new SpecificUsersAuthorizationStrategy("test2", false),
            new SpecificUsersAuthorizationStrategy("test2", true)));

    // after: admin, no require re-auth
    // before: test2, no require re-auth
    // result: true
    assertTrue(
        SpecificUsersAuthorizationStrategy.isAuthenticateionRequired(
            new SpecificUsersAuthorizationStrategy("admin", true),
            new SpecificUsersAuthorizationStrategy("test2", true)));

    // after: null, no require re-auth
    // before: null, no require re-auth
    // result: anything (not ABEND)
    SpecificUsersAuthorizationStrategy.isAuthenticateionRequired(
        new SpecificUsersAuthorizationStrategy(null, true),
        new SpecificUsersAuthorizationStrategy(null, true));
  }
 @Test
 public void testGetCurrentStrategy() throws Exception {
   {
     assertNull(SpecificUsersAuthorizationStrategy.getCurrentStrategy(null));
   }
   {
     FreeStyleProject p = j.createFreeStyleProject();
     assertNull(SpecificUsersAuthorizationStrategy.getCurrentStrategy(p));
   }
   {
     FreeStyleProject p = j.createFreeStyleProject();
     p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("foo", "bar")));
     assertNull(SpecificUsersAuthorizationStrategy.getCurrentStrategy(p));
   }
   {
     FreeStyleProject p = j.createFreeStyleProject();
     p.addProperty(new AuthorizeProjectProperty(null));
     assertNull(SpecificUsersAuthorizationStrategy.getCurrentStrategy(p));
   }
   {
     FreeStyleProject p = j.createFreeStyleProject();
     p.addProperty(new AuthorizeProjectProperty(new AnonymousAuthorizationStrategy()));
     assertNull(SpecificUsersAuthorizationStrategy.getCurrentStrategy(p));
   }
   {
     String userid = "foo";
     boolean noNeedReauthentication = true;
     FreeStyleProject p = j.createFreeStyleProject();
     p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("foo", "bar")));
     p.addProperty(
         new AuthorizeProjectProperty(
             new SpecificUsersAuthorizationStrategy(userid, noNeedReauthentication)));
     SpecificUsersAuthorizationStrategy s =
         SpecificUsersAuthorizationStrategy.getCurrentStrategy(p);
     assertNotNull(p);
     assertEquals(userid, s.getUserid());
     assertEquals(noNeedReauthentication, s.isNoNeedReauthentication());
   }
 }
  @Test
  public void testCliSuccess() 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 = 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 test1) to a new project.
    // This should success.
    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);
      assertEquals(stderr.toString(), 0, ret);
    }

    {
      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());
    }
  }