<commit_before>/*
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.gwt.oauth2.client;

import com.google.api.gwt.oauth2.client.Auth.TokenInfo;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.core.client.testing.StubScheduler;

import junit.framework.TestCase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Tests for {@link Auth}.
 *
 * @author <EMAIL> (<NAME>)
 */
public class AuthTest extends TestCase {

  private MockAuth auth;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    auth = new MockAuth();
  }

  /**
   * When the request does not have a token stored, the popup is used to get the
   * token.
   */
  public void testLogin_noToken() {
    AuthRequest req = new AuthRequest("url", "clientId").withScopes("scope");
    MockCallback callback = new MockCallback();
    auth.login(req, callback);

    // The popup was used and the iframe wasn't.
    assertTrue(auth.loggedInViaPopup);
    assertEquals("url?client_id=clientId&response_type=token&scope=scope&redirect_uri=popup.html",
        auth.lastUrl);
  }

  /**
   * When the token is found in cookies, but may expire soon, the popup will be
   * used to refresh the token.
   */
  public void testLogin_expiringSoon() {
    AuthRequest req = new AuthRequest("url", "clientId").withScopes("scope");

    // Storing a token that expires soon (in just under 10 minutes)
    TokenInfo info = new TokenInfo();
    info.accessToken = "expired";
    info.expires = String.valueOf(MockClock.now + 10 * 60 * 1000 - 1);
    auth.setToken(req, info);

    MockCallback callback = new MockCallback();
    auth.login(req, callback);

    assertTrue(auth.expiringSoon(info));

    assertTrue(auth.loggedInViaPopup);
    assertEquals("url?client_id=clientId&response_type=token&scope=scope&redirect_uri=popup.html",
        auth.lastUrl);
  }

  /**
   * When the token is found in cookies and will not expire soon, neither popup
   * nor iframe is used, and the token is immediately passed to the callback.
   */
  public void testLogin_notExpiringSoon() {
    AuthRequest req = new AuthRequest("url", "clientId").withScopes("scope");

    // Storing a token that does not expire soon (in exactly 10 minutes)
    TokenInfo info = new TokenInfo();
    info.accessToken = "<PASSWORD>";
    info.expires = String.valueOf(MockClock.now + 10 * 60 * 1000);
    auth.setToken(req, info);

    MockCallback callback = new MockCallback();
    auth.login(req, callback);

    // A deferred command will have been scheduled. Execute it.
    List<ScheduledCommand> deferred = ((StubScheduler) auth.scheduler).getScheduledCommands();
    assertEquals(1, deferred.size());
    deferred.get(0).execute();

    // The iframe was used and the popup wasn't.
    assertFalse(auth.loggedInViaPopup);

    // onSuccess() was called and onFailure() wasn't.
    assertEquals("notExpiringSoon", callback.token);
    assertNull(callback.failure);
  }

  /**
   * When the token is found in cookies and does not specify an expire time, the
   * iframe will be used to refresh the token without displaying the popup.
   */
  public void testLogin_nullExpires() {
    AuthRequest req = new AuthRequest("url", "clientId").withScopes("scope");

    // Storing a token with a null expires time
    TokenInfo info = new TokenInfo();
    info.accessToken = "longToken";
    info.expires = null;
    auth.setToken(req, info);

    MockCallback callback = new MockCallback();
    auth.login(req, callback);

    // TODO(jasonhall): When Auth supports immediate mode for supporting
    // providers, a null expiration will trigger an iframe immediate-mode
    // refresh. Until then, the popup is always used.
    assertTrue(auth.loggedInViaPopup);
  }

  /**
   * When finish() is called, the callback passed to login() is executed with
   * the correct token, and a cookie is set with relevant information, expiring
   * in the correct amount of time.
   */
  public void testFinish() {<commit_msg>Fix AuthTest#testFinish() jUnit test fixes #2<commit_after>/*
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.gwt.oauth2.client;

import com.google.api.gwt.oauth2.client.Auth.TokenInfo;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.core.client.testing.StubScheduler;

import junit.framework.TestCase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Tests for {@link Auth}.
 *
 * @author <EMAIL> (<NAME>)
 */
public class AuthTest extends TestCase {

  private MockAuth auth;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    auth = new MockAuth();
  }

  /**
   * When the request does not have a token stored, the popup is used to get the
   * token.
   */
  public void testLogin_noToken() {
    AuthRequest req = new AuthRequest("url", "clientId").withScopes("scope");
    MockCallback callback = new MockCallback();
    auth.login(req, callback);

    // The popup was used and the iframe wasn't.
    assertTrue(auth.loggedInViaPopup);
    assertEquals("url?client_id=clientId&response_type=token&scope=scope&redirect_uri=popup.html",
        auth.lastUrl);
  }

  /**
   * When the token is found in cookies, but may expire soon, the popup will be
   * used to refresh the token.
   */
  public void testLogin_expiringSoon() {
    AuthRequest req = new AuthRequest("url", "clientId").withScopes("scope");

    // Storing a token that expires soon (in just under 10 minutes)
    TokenInfo info = new TokenInfo();
    info.accessToken = "expired";
    info.expires = String.valueOf(MockClock.now + 10 * 60 * 1000 - 1);
    auth.setToken(req, info);

    MockCallback callback = new MockCallback();
    auth.login(req, callback);

    assertTrue(auth.expiringSoon(info));

    assertTrue(auth.loggedInViaPopup);
    assertEquals("url?client_id=clientId&response_type=token&scope=scope&redirect_uri=popup.html",
        auth.lastUrl);
  }

  /**
   * When the token is found in cookies and will not expire soon, neither popup
   * nor iframe is used, and the token is immediately passed to the callback.
   */
  public void testLogin_notExpiringSoon() {
    AuthRequest req = new AuthRequest("url", "clientId").withScopes("scope");

    // Storing a token that does not expire soon (in exactly 10 minutes)
    TokenInfo info = new TokenInfo();
    info.accessToken = "<PASSWORD>";
    info.expires = String.valueOf(MockClock.now + 10 * 60 * 1000);
    auth.setToken(req, info);

    MockCallback callback = new MockCallback();
    auth.login(req, callback);

    // A deferred command will have been scheduled. Execute it.
    List<ScheduledCommand> deferred = ((StubScheduler) auth.scheduler).getScheduledCommands();
    assertEquals(1, deferred.size());
    deferred.get(0).execute();

    // The iframe was used and the popup wasn't.
    assertFalse(auth.loggedInViaPopup);

    // onSuccess() was called and onFailure() wasn't.
    assertEquals("notExpiringSoon", callback.token);
    assertNull(callback.failure);
  }

  /**
   * When the token is found in cookies and does not specify an expire time, the
   * iframe will be used to refresh the token without displaying the popup.
   */
  public void testLogin_nullExpires() {
    AuthRequest req = new AuthRequest("url", "clientId").withScopes("scope");

    // Storing a token with a null expires time
    TokenInfo info = new TokenInfo();
    info.accessToken = "longToken";
    info.expires = null;
    auth.setToken(req, info);

    MockCallback callback = new MockCallback();
    auth.login(req, callback);

    // TODO(jasonhall): When Auth supports immediate mode for supporting
    // providers, a null expiration will trigger an iframe immediate-mode
    // refresh. Until then, the popup is always used.
    assertTrue(auth.loggedInViaPopup);
  }

  /**
   * When finish() is called, the callback passed to login() is executed with
   * the correct token, and a cookie is set with relevant information, expiring
   * in the correct amount of time.
   */
  public void testFinish() {
    // Reset the default value
    MockClock.now = 5000;

{'fraction_non_alphanumeric': 0.10684491978609625, 'fraction_numerical': 0.007593582887700534, 'mean_word_length': 3.574853228962818, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4258750', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2663, 'n_words': 911}