| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 OAuth2AccessTokenFetcher. | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "chrome/common/net/gaia/gaia_urls.h" | |
| 12 #include "chrome/common/net/gaia/google_service_auth_error.h" | |
| 13 #include "chrome/common/net/http_return.h" | |
| 14 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" | |
| 15 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" | |
| 16 #include "chrome/test/base/testing_profile.h" | |
| 17 #include "content/public/common/url_fetcher.h" | |
| 18 #include "content/public/common/url_fetcher_delegate.h" | |
| 19 #include "content/public/common/url_fetcher_factory.h" | |
| 20 #include "content/test/test_browser_thread.h" | |
| 21 #include "content/test/test_url_fetcher_factory.h" | |
| 22 #include "googleurl/src/gurl.h" | |
| 23 #include "net/url_request/url_request.h" | |
| 24 #include "net/url_request/url_request_status.h" | |
| 25 #include "testing/gmock/include/gmock/gmock.h" | |
| 26 #include "testing/gtest/include/gtest/gtest.h" | |
| 27 | |
| 28 using content::BrowserThread; | |
| 29 using content::URLFetcher; | |
| 30 using content::URLFetcherDelegate; | |
| 31 using content::URLFetcherFactory; | |
| 32 using net::ResponseCookies; | |
| 33 using net::URLRequestStatus; | |
| 34 using testing::_; | |
| 35 using testing::Return; | |
| 36 | |
| 37 namespace { | |
| 38 static const char kValidTokenResponse[] = | |
| 39 "{" | |
| 40 " \"access_token\": \"at1\"," | |
| 41 " \"expires_in\": 3600," | |
| 42 " \"token_type\": \"Bearer\"" | |
| 43 "}"; | |
| 44 static const char kTokenResponseNoAccessToken[] = | |
| 45 "{" | |
| 46 " \"expires_in\": 3600," | |
| 47 " \"token_type\": \"Bearer\"" | |
| 48 "}"; | |
| 49 } | |
| 50 | |
| 51 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, | |
| 52 public URLFetcherFactory { | |
| 53 public: | |
| 54 MockUrlFetcherFactory() | |
| 55 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 56 } | |
| 57 virtual ~MockUrlFetcherFactory() {} | |
| 58 | |
| 59 MOCK_METHOD4( | |
| 60 CreateURLFetcher, | |
| 61 URLFetcher* (int id, | |
| 62 const GURL& url, | |
| 63 URLFetcher::RequestType request_type, | |
| 64 URLFetcherDelegate* d)); | |
| 65 }; | |
| 66 | |
| 67 class MockOAuth2AccessTokenConsumer : public OAuth2AccessTokenConsumer { | |
| 68 public: | |
| 69 MockOAuth2AccessTokenConsumer() {} | |
| 70 ~MockOAuth2AccessTokenConsumer() {} | |
| 71 | |
| 72 MOCK_METHOD1(OnGetTokenSuccess, void(const std::string& access_token)); | |
| 73 MOCK_METHOD1(OnGetTokenFailure, | |
| 74 void(const GoogleServiceAuthError& error)); | |
| 75 }; | |
| 76 | |
| 77 class OAuth2AccessTokenFetcherTest : public testing::Test { | |
| 78 public: | |
| 79 OAuth2AccessTokenFetcherTest() | |
| 80 : ui_thread_(BrowserThread::UI, &message_loop_), | |
| 81 fetcher_(&consumer_, profile_.GetRequestContext(), "test") { | |
| 82 } | |
| 83 | |
| 84 virtual ~OAuth2AccessTokenFetcherTest() { } | |
| 85 | |
| 86 virtual TestURLFetcher* SetupGetAccessToken( | |
| 87 bool fetch_succeeds, int response_code, const std::string& body) { | |
| 88 GURL url(GaiaUrls::GetInstance()->oauth2_token_url()); | |
| 89 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &fetcher_); | |
| 90 URLRequestStatus::Status status = | |
| 91 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED; | |
| 92 url_fetcher->set_status(URLRequestStatus(status, 0)); | |
| 93 | |
| 94 if (response_code != 0) | |
| 95 url_fetcher->set_response_code(response_code); | |
| 96 | |
| 97 if (!body.empty()) | |
| 98 url_fetcher->SetResponseString(body); | |
| 99 | |
| 100 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) | |
| 101 .WillOnce(Return(url_fetcher)); | |
| 102 return url_fetcher; | |
| 103 } | |
| 104 | |
| 105 protected: | |
| 106 MessageLoop message_loop_; | |
| 107 content::TestBrowserThread ui_thread_; | |
| 108 MockUrlFetcherFactory factory_; | |
| 109 MockOAuth2AccessTokenConsumer consumer_; | |
| 110 TestingProfile profile_; | |
| 111 OAuth2AccessTokenFetcher fetcher_; | |
| 112 }; | |
| 113 | |
| 114 TEST_F(OAuth2AccessTokenFetcherTest, GetAccessTokenRequestFailure) { | |
| 115 TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, ""); | |
| 116 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); | |
| 117 fetcher_.Start("refresh_token"); | |
| 118 fetcher_.OnURLFetchComplete(url_fetcher); | |
| 119 } | |
| 120 | |
| 121 TEST_F(OAuth2AccessTokenFetcherTest, GetAccessTokenResponseCodeFailure) { | |
| 122 TestURLFetcher* url_fetcher = SetupGetAccessToken(true, RC_FORBIDDEN, ""); | |
| 123 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); | |
| 124 fetcher_.Start("refresh_token"); | |
| 125 fetcher_.OnURLFetchComplete(url_fetcher); | |
| 126 } | |
| 127 | |
| 128 TEST_F(OAuth2AccessTokenFetcherTest, GetAccessTokenResponseNoAccessToken) { | |
| 129 TestURLFetcher* url_fetcher = SetupGetAccessToken( | |
| 130 true, RC_REQUEST_OK, kTokenResponseNoAccessToken); | |
| 131 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1); | |
| 132 fetcher_.Start("refresh_token"); | |
| 133 fetcher_.OnURLFetchComplete(url_fetcher); | |
| 134 } | |
| 135 | |
| 136 TEST_F(OAuth2AccessTokenFetcherTest, Success) { | |
| 137 TestURLFetcher* url_fetcher = SetupGetAccessToken( | |
| 138 true, RC_REQUEST_OK, kValidTokenResponse); | |
| 139 EXPECT_CALL(consumer_, OnGetTokenSuccess("at1")).Times(1); | |
| 140 fetcher_.Start("refresh_token"); | |
| 141 fetcher_.OnURLFetchComplete(url_fetcher); | |
| 142 } | |
| 143 | |
| 144 TEST_F(OAuth2AccessTokenFetcherTest, ParseGetAccessTokenResponse) { | |
| 145 { // No body. | |
| 146 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | |
| 147 | |
| 148 std::string at; | |
| 149 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | |
| 150 &url_fetcher, &at)); | |
| 151 EXPECT_TRUE(at.empty()); | |
| 152 } | |
| 153 { // Bad json. | |
| 154 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | |
| 155 url_fetcher.SetResponseString("foo"); | |
| 156 | |
| 157 std::string at; | |
| 158 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | |
| 159 &url_fetcher, &at)); | |
| 160 EXPECT_TRUE(at.empty()); | |
| 161 } | |
| 162 { // Valid json: access token missing. | |
| 163 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | |
| 164 url_fetcher.SetResponseString(kTokenResponseNoAccessToken); | |
| 165 | |
| 166 std::string at; | |
| 167 EXPECT_FALSE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | |
| 168 &url_fetcher, &at)); | |
| 169 EXPECT_TRUE(at.empty()); | |
| 170 } | |
| 171 { // Valid json: all good. | |
| 172 TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL); | |
| 173 url_fetcher.SetResponseString(kValidTokenResponse); | |
| 174 | |
| 175 std::string at; | |
| 176 EXPECT_TRUE(OAuth2AccessTokenFetcher::ParseGetAccessTokenResponse( | |
| 177 &url_fetcher, &at)); | |
| 178 EXPECT_EQ("at1", at); | |
| 179 } | |
| 180 } | |
| OLD | NEW |