Home | History | Annotate | Download | only in gaia
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 //
      5 // A complete set of unit tests for GaiaOAuthFetcher.
      6 // Originally ported from GaiaAuthFetcher tests.
      7 
      8 #include <string>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "chrome/browser/net/gaia/gaia_oauth_consumer.h"
     12 #include "chrome/browser/net/gaia/gaia_oauth_fetcher.h"
     13 #include "chrome/test/base/testing_profile.h"
     14 #include "content/public/test/test_browser_thread_bundle.h"
     15 #include "google_apis/gaia/gaia_constants.h"
     16 #include "google_apis/gaia/gaia_urls.h"
     17 #include "google_apis/gaia/google_service_auth_error.h"
     18 #include "net/base/net_errors.h"
     19 #include "net/http/http_status_code.h"
     20 #include "net/url_request/test_url_fetcher_factory.h"
     21 #include "net/url_request/url_request_status.h"
     22 #include "testing/gmock/include/gmock/gmock.h"
     23 #include "testing/gtest/include/gtest/gtest.h"
     24 #include "url/gurl.h"
     25 
     26 class MockGaiaOAuthConsumer : public GaiaOAuthConsumer {
     27  public:
     28   MockGaiaOAuthConsumer() {}
     29   ~MockGaiaOAuthConsumer() {}
     30 
     31   MOCK_METHOD1(OnGetOAuthTokenSuccess, void(const std::string& oauth_token));
     32   MOCK_METHOD1(OnGetOAuthTokenFailure,
     33                void(const GoogleServiceAuthError& error));
     34 
     35   MOCK_METHOD2(OnOAuthGetAccessTokenSuccess, void(const std::string& token,
     36                                                   const std::string& secret));
     37   MOCK_METHOD1(OnOAuthGetAccessTokenFailure,
     38                void(const GoogleServiceAuthError& error));
     39 
     40   MOCK_METHOD3(OnOAuthWrapBridgeSuccess,
     41                void(const std::string& service_scope,
     42                     const std::string& token,
     43                     const std::string& expires_in));
     44   MOCK_METHOD2(OnOAuthWrapBridgeFailure,
     45                void(const std::string& service_scope,
     46                     const GoogleServiceAuthError& error));
     47 
     48   MOCK_METHOD1(OnUserInfoSuccess, void(const std::string& email));
     49   MOCK_METHOD1(OnUserInfoFailure, void(const GoogleServiceAuthError& error));
     50 
     51   MOCK_METHOD0(OnOAuthRevokeTokenSuccess, void());
     52   MOCK_METHOD1(OnOAuthRevokeTokenFailure,
     53                void(const GoogleServiceAuthError& error));
     54 };
     55 
     56 class MockGaiaOAuthFetcher : public GaiaOAuthFetcher {
     57  public:
     58   MockGaiaOAuthFetcher(GaiaOAuthConsumer* consumer,
     59                        net::URLRequestContextGetter* getter,
     60                        const std::string& service_scope)
     61       : GaiaOAuthFetcher(
     62           consumer, getter, service_scope) {}
     63 
     64   ~MockGaiaOAuthFetcher() {}
     65 
     66   void set_request_type(RequestType type) {
     67     request_type_ = type;
     68   }
     69 
     70   MOCK_METHOD1(StartOAuthGetAccessToken,
     71                void(const std::string& oauth1_request_token));
     72 
     73   MOCK_METHOD4(StartOAuthWrapBridge,
     74                void(const std::string& oauth1_access_token,
     75                     const std::string& oauth1_access_token_secret,
     76                     const std::string& wrap_token_duration,
     77                     const std::string& oauth2_scope));
     78 
     79   MOCK_METHOD1(StartUserInfo, void(const std::string& oauth2_access_token));
     80 };
     81 
     82 #if 0  // Suppressing for now
     83 TEST(GaiaOAuthFetcherTest, GetOAuthToken) {
     84   const std::string oauth_token = "4/OAuth1-Request_Token-1234567";
     85   base::Time creation = base::Time::Now();
     86   base::Time expiration = base::Time::Time();
     87 
     88   scoped_ptr<net::CanonicalCookie> canonical_cookie;
     89   canonical_cookie.reset(
     90       new net::CanonicalCookie(
     91           GURL("http://www.google.com/"),  // url
     92           "oauth_token",                   // name
     93           oauth_token,                     // value
     94           "www.google.com",                // domain
     95           "/accounts/o8/GetOAuthToken",    // path
     96           "",                              // mac_key
     97           "",                              // mac_algorithm
     98           creation,                        // creation
     99           expiration,                      // expiration
    100           creation,                        // last_access
    101           true,                            // secure
    102           true,                            // httponly
    103           false));                         // has_expires
    104 
    105   scoped_ptr<ChromeCookieDetails::ChromeCookieDetails> cookie_details;
    106   cookie_details.reset(
    107       new ChromeCookieDetails::ChromeCookieDetails(
    108           canonical_cookie.get(),
    109           false,
    110           net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT));
    111 
    112   MockGaiaOAuthConsumer consumer;
    113   EXPECT_CALL(consumer, OnGetOAuthTokenSuccess(oauth_token)).Times(1);
    114 
    115   TestingProfile profile;
    116 
    117   MockGaiaOAuthFetcher oauth_fetcher(&consumer,
    118                                      profile.GetRequestContext(),
    119                                      std::string());
    120   EXPECT_CALL(oauth_fetcher, StartOAuthGetAccessToken(oauth_token)).Times(1);
    121 }
    122 #endif  // 0  // Suppressing for now
    123 
    124 class GaiaOAuthFetcherTest : public testing::Test {
    125  private:
    126   content::TestBrowserThreadBundle thread_bundle_;
    127 };
    128 
    129 TEST_F(GaiaOAuthFetcherTest, OAuthGetAccessToken) {
    130   const std::string oauth_token =
    131       "1/OAuth1-Access_Token-1234567890abcdefghijklm";
    132   const std::string oauth_token_secret = "Dont_tell_the_secret-123";
    133   const std::string data("oauth_token="
    134                          "1%2FOAuth1-Access_Token-1234567890abcdefghijklm"
    135                          "&oauth_token_secret=Dont_tell_the_secret-123");
    136 
    137   MockGaiaOAuthConsumer consumer;
    138   EXPECT_CALL(consumer,
    139               OnOAuthGetAccessTokenSuccess(oauth_token,
    140                                            oauth_token_secret)).Times(1);
    141 
    142   TestingProfile profile;
    143   MockGaiaOAuthFetcher oauth_fetcher(&consumer,
    144                                      profile.GetRequestContext(),
    145                                      "service_scope-JnG18MEE");
    146   oauth_fetcher.set_request_type(GaiaOAuthFetcher::OAUTH1_ALL_ACCESS_TOKEN);
    147   EXPECT_CALL(oauth_fetcher,
    148               StartOAuthWrapBridge(oauth_token,
    149                                    oauth_token_secret,
    150                                    "3600",
    151                                    "service_scope-JnG18MEE")).Times(1);
    152 
    153   net::ResponseCookies cookies;
    154   net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
    155   GURL url(GaiaUrls::GetInstance()->oauth_get_access_token_url());
    156 
    157   net::TestURLFetcher test_fetcher(0, GURL(), &oauth_fetcher);
    158   test_fetcher.set_url(url);
    159   test_fetcher.set_status(status);
    160   test_fetcher.set_response_code(net::HTTP_OK);
    161   test_fetcher.set_cookies(cookies);
    162   test_fetcher.SetResponseString(data);
    163   oauth_fetcher.OnURLFetchComplete(&test_fetcher);
    164 }
    165 
    166 TEST_F(GaiaOAuthFetcherTest, OAuthWrapBridge) {
    167   const std::string wrap_token =
    168       "1/OAuth2-Access_Token-nopqrstuvwxyz1234567890";
    169   const std::string expires_in = "3600";
    170 
    171   const std::string data("wrap_access_token="
    172                          "1%2FOAuth2-Access_Token-nopqrstuvwxyz1234567890"
    173                          "&wrap_access_token_expires_in=3600");
    174 
    175   MockGaiaOAuthConsumer consumer;
    176   EXPECT_CALL(consumer,
    177               OnOAuthWrapBridgeSuccess("service_scope-0fL85iOi",
    178                                        wrap_token,
    179                                        expires_in)).Times(1);
    180 
    181   TestingProfile profile;
    182   MockGaiaOAuthFetcher oauth_fetcher(&consumer,
    183                                      profile.GetRequestContext(),
    184                                      "service_scope-0fL85iOi");
    185   oauth_fetcher.set_request_type(GaiaOAuthFetcher::OAUTH2_SERVICE_ACCESS_TOKEN);
    186   EXPECT_CALL(oauth_fetcher, StartUserInfo(wrap_token)).Times(1);
    187 
    188   net::ResponseCookies cookies;
    189   net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
    190   GURL url(GaiaUrls::GetInstance()->oauth_wrap_bridge_url());
    191 
    192   net::TestURLFetcher test_fetcher(0, GURL(), &oauth_fetcher);
    193   test_fetcher.set_url(url);
    194   test_fetcher.set_status(status);
    195   test_fetcher.set_response_code(net::HTTP_OK);
    196   test_fetcher.set_cookies(cookies);
    197   test_fetcher.SetResponseString(data);
    198   oauth_fetcher.OnURLFetchComplete(&test_fetcher);
    199 }
    200 
    201 TEST_F(GaiaOAuthFetcherTest, UserInfo) {
    202   const std::string email_address = "someone (at) somewhere.net";
    203   const std::string wrap_token =
    204       "1/OAuth2-Access_Token-nopqrstuvwxyz1234567890";
    205   const std::string expires_in = "3600";
    206   const std::string data("{\n \"email\": \"someone (at) somewhere.net\",\n"
    207                          " \"verified_email\": true\n}\n");
    208   MockGaiaOAuthConsumer consumer;
    209   EXPECT_CALL(consumer,
    210               OnUserInfoSuccess(email_address)).Times(1);
    211 
    212   TestingProfile profile;
    213   MockGaiaOAuthFetcher oauth_fetcher(&consumer,
    214                                      profile.GetRequestContext(),
    215                                      "service_scope-Nrj4LmgU");
    216   oauth_fetcher.set_request_type(GaiaOAuthFetcher::USER_INFO);
    217 
    218   net::ResponseCookies cookies;
    219   net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
    220   GURL url(GaiaUrls::GetInstance()->oauth_user_info_url());
    221 
    222   net::TestURLFetcher test_fetcher(0, GURL(), &oauth_fetcher);
    223   test_fetcher.set_url(url);
    224   test_fetcher.set_status(status);
    225   test_fetcher.set_response_code(net::HTTP_OK);
    226   test_fetcher.set_cookies(cookies);
    227   test_fetcher.SetResponseString(data);
    228   oauth_fetcher.OnURLFetchComplete(&test_fetcher);
    229 }
    230 
    231 TEST_F(GaiaOAuthFetcherTest, OAuthRevokeToken) {
    232   const std::string token = "1/OAuth2-Access_Token-nopqrstuvwxyz1234567890";
    233   MockGaiaOAuthConsumer consumer;
    234   EXPECT_CALL(consumer,
    235               OnOAuthRevokeTokenSuccess()).Times(1);
    236 
    237   TestingProfile profile;
    238   MockGaiaOAuthFetcher oauth_fetcher(&consumer,
    239                                      profile.GetRequestContext(),
    240                                      "service_scope-Nrj4LmgU");
    241   oauth_fetcher.set_request_type(GaiaOAuthFetcher::OAUTH2_REVOKE_TOKEN);
    242 
    243   net::ResponseCookies cookies;
    244   net::URLRequestStatus status(net::URLRequestStatus::SUCCESS, 0);
    245   GURL url(GaiaUrls::GetInstance()->oauth_revoke_token_url());
    246 
    247   net::TestURLFetcher test_fetcher(0, GURL(), &oauth_fetcher);
    248   test_fetcher.set_url(url);
    249   test_fetcher.set_status(status);
    250   test_fetcher.set_response_code(net::HTTP_OK);
    251   test_fetcher.set_cookies(cookies);
    252   oauth_fetcher.OnURLFetchComplete(&test_fetcher);
    253 }
    254