public void testLogInWithOpenIDAndSignUp() throws Exception {
    openid = createServer();

    realm = new HudsonPrivateSecurityRealm(true);
    hudson.setSecurityRealm(realm);

    WebClient wc = new WebClient();
    // Workaround failing ajax requests to build queue
    wc.setThrowExceptionOnFailingAjax(false);

    // Login with OpenID as an unregistered user
    HtmlPage login = wc.goTo("federatedLoginService/openid/login?from=/");
    login
        .getDocumentElement()
        .getOneHtmlElementByAttribute("a", "title", "log in with OpenID")
        .click();
    HtmlForm loginForm = getFormById(login, "openid_form");
    loginForm.getInputByName("openid").setValueAttribute(openid.url);
    HtmlPage signUp = (HtmlPage) loginForm.submit();

    // Sign up user
    HtmlForm signUpForm =
        getFormByAction(signUp, "/securityRealm/createAccountWithFederatedIdentity");
    signUpForm.getInputByName("password1").setValueAttribute("x");
    signUpForm.getInputByName("password2").setValueAttribute("x");
    HtmlPage loggedIn = submit(signUpForm);

    assertNotNull(loggedIn.getAnchorByHref("/logout"));
    assertNotNull(loggedIn.getAnchorByHref("/user/aliceW"));

    wc.goTo("logout");

    // Re-login
    login(wc);
  }
  /** Associates the OpenID identity of the user with {@link #realm}. */
  private void associateUserWithOpenId(User u) throws Exception {
    WebClient wc = new WebClient().login(u.getId(), u.getId() /*assumes password==name*/);

    // Associate an OpenID with an existing user
    HtmlPage associated =
        wc.goTo("federatedLoginService/openid/startAssociate?openid=" + openid.url);
    assertTrue(
        associated.getDocumentURI().endsWith("federatedLoginService/openid/onAssociationSuccess"));
    OpenIdUserProperty p = u.getProperty(OpenIdUserProperty.class);
    assertEquals(1, p.getIdentifiers().size());
    assertEquals(openid.getUserIdentity(), p.getIdentifiers().iterator().next());
  }
  private void login(WebClient wc) throws Exception {
    HtmlPage login = wc.goTo("federatedLoginService/openid/login?from=/");
    login
        .getDocumentElement()
        .getOneHtmlElementByAttribute("a", "title", "log in with OpenID")
        .click();
    HtmlForm loginForm = getFormById(login, "openid_form");
    loginForm.getInputByName("openid").setValueAttribute(openid.url);
    HtmlPage loggedIn = (HtmlPage) loginForm.submit();

    assertNotNull(loggedIn.getAnchorByHref("/logout"));
    assertNotNull(loggedIn.getAnchorByHref("/user/aliceW"));
  }