/** Tests the case when the user has not started the authorization process (no request token). */
  public final void testCheckAuthorizationNoRequestToken() {
    // Setup.
    LoginFormHandler loginForm = mock(LoginFormHandler.class);
    OAuthClient client = mock(OAuthClient.class);
    PersistenceManager pm = mock(PersistenceManager.class);
    PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class);

    OAuthAccessor accessor =
        buildAccessor(
            CONSUMER_KEY,
            CONSUMER_SECRET,
            REQUEST_TOKEN_URL,
            AUTHORIZE_URL,
            CALLBACK_URL,
            ACCESS_TOKEN_URL);
    accessor.requestToken = REQUEST_TOKEN_STRING;
    oauthService = new OAuthServiceImpl(accessor, client, pmf, USER_RECORD_KEY);
    OAuthUser userWithRequestToken = new OAuthUser(USER_RECORD_KEY, REQUEST_TOKEN_STRING);

    // Expectations.
    when(pmf.getPersistenceManager()).thenReturn(pm);
    when(pm.getObjectById(OAuthUser.class, USER_RECORD_KEY))
        .thenReturn(null, userWithRequestToken, userWithRequestToken);

    assertFalse(oauthService.checkAuthorization(null, loginForm));

    String authUrl = userWithRequestToken.getAuthUrl();
    try {
      new URL(authUrl);
    } catch (MalformedURLException e) {
      fail("Malformed authUrl");
    }

    assertTrue(Pattern.matches(".+(oauth_token){1}.+", authUrl));
    assertTrue(Pattern.matches(".+(oauth_callback){1}.+", authUrl));
  }
  @Override
  public void renderLogin(String userRecordKey, Wavelet wavelet) {
    // Clear login form.
    wavelet.getRootBlip().all().delete();

    PersistenceManager pm = SingletonPersistenceManagerFactory.get().getPersistenceManager();
    OAuthUser userProfile = null;
    try {
      userProfile = pm.getObjectById(OAuthUser.class, userRecordKey);
    } catch (JDOObjectNotFoundException objectNotFound) {
      LOG.severe("Error fetching object from datastore with key: " + userRecordKey);
    } finally {
      pm.close();
    }
    String url = userProfile.getAuthUrl();

    // Add authentication prompt and insert link to service provider log-in page
    // to wavelet.
    wavelet.getRootBlip().all().delete();
    StringBuilder b = new StringBuilder();
    b.append("\n");
    int startIndex = b.length();
    b.append(LOGIN_LINK_TEXT + "\n\n");
    wavelet.getRootBlip().append(b.toString());

    // Add button to click when authentication is complete.
    wavelet
        .getRootBlip()
        .append(new FormElement(ElementType.BUTTON, LOGIN_BUTTON_ID, LOGIN_BUTTON_CAPTION));

    // Linkify the authorization link.
    wavelet
        .getRootBlip()
        .range(startIndex, startIndex + LOGIN_LINK_TEXT.length())
        .annotate(LINK_ANNOTATION_KEY, url);
  }