OLD | NEW |
(Empty) | |
| 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 OAuth2MintTokenFlow. |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/scoped_ptr.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/gaia/oauth2_access_token_consumer.h" |
| 14 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" |
| 15 #include "chrome/common/net/gaia/oauth2_api_call_flow.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_url_fetcher_factory.h" |
| 21 #include "net/http/http_status_code.h" |
| 22 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_status.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 |
| 27 using content::URLFetcher; |
| 28 using content::URLFetcherDelegate; |
| 29 using content::URLFetcherFactory; |
| 30 using net::URLRequestStatus; |
| 31 using testing::_; |
| 32 using testing::Return; |
| 33 |
| 34 namespace { |
| 35 static GURL CreateApiUrl() { |
| 36 return GURL("https://www.googleapis.com/someapi"); |
| 37 } |
| 38 |
| 39 static std::vector<std::string> CreateTestScopes() { |
| 40 std::vector<std::string> scopes; |
| 41 scopes.push_back("scope1"); |
| 42 scopes.push_back("scope2"); |
| 43 return scopes; |
| 44 } |
| 45 } |
| 46 |
| 47 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, |
| 48 public URLFetcherFactory { |
| 49 public: |
| 50 MockUrlFetcherFactory() |
| 51 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 52 } |
| 53 virtual ~MockUrlFetcherFactory() {} |
| 54 |
| 55 MOCK_METHOD4( |
| 56 CreateURLFetcher, |
| 57 URLFetcher* (int id, |
| 58 const GURL& url, |
| 59 URLFetcher::RequestType request_type, |
| 60 URLFetcherDelegate* d)); |
| 61 }; |
| 62 |
| 63 class MockAccessTokenFetcher : public OAuth2AccessTokenFetcher { |
| 64 public: |
| 65 MockAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer, |
| 66 net::URLRequestContextGetter* getter) |
| 67 : OAuth2AccessTokenFetcher(consumer, getter) {} |
| 68 ~MockAccessTokenFetcher() {} |
| 69 |
| 70 MOCK_METHOD4(Start, |
| 71 void (const std::string& client_id, |
| 72 const std::string& client_secret, |
| 73 const std::string& refresh_token, |
| 74 const std::vector<std::string>& scopes)); |
| 75 }; |
| 76 |
| 77 class MockApiCallFlow : public OAuth2ApiCallFlow { |
| 78 public: |
| 79 MockApiCallFlow(net::URLRequestContextGetter* context, |
| 80 const std::string& refresh_token, |
| 81 const std::string& access_token, |
| 82 const std::vector<std::string>& scopes) |
| 83 : OAuth2ApiCallFlow(context, refresh_token, access_token, scopes) {} |
| 84 ~MockApiCallFlow() {} |
| 85 |
| 86 MOCK_METHOD0(CreateApiCallUrl, GURL ()); |
| 87 MOCK_METHOD0(CreateApiCallBody, std::string ()); |
| 88 MOCK_METHOD1(ProcessApiCallSuccess, |
| 89 void (const content::URLFetcher* source)); |
| 90 MOCK_METHOD1(ProcessApiCallFailure, |
| 91 void (const content::URLFetcher* source)); |
| 92 MOCK_METHOD1(ProcessNewAccessToken, |
| 93 void (const std::string& access_token)); |
| 94 MOCK_METHOD1(ProcessMintAccessTokenFailure, |
| 95 void (const GoogleServiceAuthError& error)); |
| 96 MOCK_METHOD0(CreateAccessTokenFetcher, OAuth2AccessTokenFetcher* ()); |
| 97 // MOCK_METHOD0(CreateURLFetcher, URLFetcher* ()); |
| 98 }; |
| 99 |
| 100 class OAuth2ApiCallFlowTest : public testing::Test { |
| 101 public: |
| 102 OAuth2ApiCallFlowTest() {} |
| 103 virtual ~OAuth2ApiCallFlowTest() {} |
| 104 |
| 105 protected: |
| 106 void SetupAccessTokenFetcher( |
| 107 const std::string& rt, const std::vector<std::string>& scopes) { |
| 108 EXPECT_CALL(*access_token_fetcher_, |
| 109 Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| 110 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
| 111 rt, scopes)) |
| 112 .Times(1); |
| 113 EXPECT_CALL(*flow_, CreateAccessTokenFetcher()) |
| 114 .WillOnce(Return(access_token_fetcher_.release())); |
| 115 } |
| 116 |
| 117 TestURLFetcher* CreateURLFetcher( |
| 118 const GURL& url, bool fetch_succeeds, |
| 119 int response_code, const std::string& body) { |
| 120 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, flow_.get()); |
| 121 URLRequestStatus::Status status = |
| 122 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED; |
| 123 url_fetcher->set_status(URLRequestStatus(status, 0)); |
| 124 |
| 125 if (response_code != 0) |
| 126 url_fetcher->set_response_code(response_code); |
| 127 |
| 128 if (!body.empty()) |
| 129 url_fetcher->SetResponseString(body); |
| 130 |
| 131 return url_fetcher; |
| 132 } |
| 133 |
| 134 void CreateFlow(const std::string& refresh_token, |
| 135 const std::string& access_token, |
| 136 const std::vector<std::string>& scopes) { |
| 137 flow_.reset(new MockApiCallFlow( |
| 138 profile_.GetRequestContext(), |
| 139 refresh_token, |
| 140 access_token, |
| 141 scopes)); |
| 142 access_token_fetcher_.reset(new MockAccessTokenFetcher( |
| 143 flow_.get(), profile_.GetRequestContext())); |
| 144 } |
| 145 |
| 146 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { |
| 147 GURL url(CreateApiUrl()); |
| 148 EXPECT_CALL(*flow_, CreateApiCallBody()).WillOnce(Return("")); |
| 149 EXPECT_CALL(*flow_, CreateApiCallUrl()).WillOnce(Return(url)); |
| 150 TestURLFetcher* url_fetcher = CreateURLFetcher( |
| 151 url, succeeds, status, ""); |
| 152 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) |
| 153 .WillOnce(Return(url_fetcher)); |
| 154 return url_fetcher; |
| 155 } |
| 156 |
| 157 MockUrlFetcherFactory factory_; |
| 158 scoped_ptr<MockApiCallFlow> flow_; |
| 159 scoped_ptr<MockAccessTokenFetcher> access_token_fetcher_; |
| 160 TestingProfile profile_; |
| 161 }; |
| 162 |
| 163 TEST_F(OAuth2ApiCallFlowTest, FirstApiCallSucceeds) { |
| 164 std::string rt = "refresh_token"; |
| 165 std::string at = "access_token"; |
| 166 std::vector<std::string> scopes(CreateTestScopes()); |
| 167 |
| 168 CreateFlow(rt, at, scopes); |
| 169 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); |
| 170 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher)); |
| 171 flow_->Start(); |
| 172 flow_->OnURLFetchComplete(url_fetcher); |
| 173 } |
| 174 |
| 175 TEST_F(OAuth2ApiCallFlowTest, SecondApiCallSucceeds) { |
| 176 std::string rt = "refresh_token"; |
| 177 std::string at = "access_token"; |
| 178 std::vector<std::string> scopes(CreateTestScopes()); |
| 179 |
| 180 CreateFlow(rt, at, scopes); |
| 181 TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED); |
| 182 flow_->Start(); |
| 183 SetupAccessTokenFetcher(rt, scopes); |
| 184 flow_->OnURLFetchComplete(url_fetcher1); |
| 185 TestURLFetcher* url_fetcher2 = SetupApiCall(true, net::HTTP_OK); |
| 186 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher2)); |
| 187 flow_->OnGetTokenSuccess(at); |
| 188 flow_->OnURLFetchComplete(url_fetcher2); |
| 189 } |
| 190 |
| 191 TEST_F(OAuth2ApiCallFlowTest, SecondApiCallFails) { |
| 192 std::string rt = "refresh_token"; |
| 193 std::string at = "access_token"; |
| 194 std::vector<std::string> scopes(CreateTestScopes()); |
| 195 |
| 196 CreateFlow(rt, at, scopes); |
| 197 TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED); |
| 198 flow_->Start(); |
| 199 SetupAccessTokenFetcher(rt, scopes); |
| 200 flow_->OnURLFetchComplete(url_fetcher1); |
| 201 TestURLFetcher* url_fetcher2 = SetupApiCall(false, net::HTTP_UNAUTHORIZED); |
| 202 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher2)); |
| 203 flow_->OnGetTokenSuccess(at); |
| 204 flow_->OnURLFetchComplete(url_fetcher2); |
| 205 } |
| 206 |
| 207 TEST_F(OAuth2ApiCallFlowTest, NewTokenGenerationFails) { |
| 208 std::string rt = "refresh_token"; |
| 209 std::string at = "access_token"; |
| 210 std::vector<std::string> scopes(CreateTestScopes()); |
| 211 |
| 212 CreateFlow(rt, at, scopes); |
| 213 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_UNAUTHORIZED); |
| 214 flow_->Start(); |
| 215 SetupAccessTokenFetcher(rt, scopes); |
| 216 flow_->OnURLFetchComplete(url_fetcher); |
| 217 GoogleServiceAuthError error( |
| 218 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 219 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); |
| 220 flow_->OnGetTokenFailure(error); |
| 221 } |
| 222 |
| 223 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenFirstApiCallSucceeds) { |
| 224 std::string rt = "refresh_token"; |
| 225 std::string at = "access_token"; |
| 226 std::vector<std::string> scopes(CreateTestScopes()); |
| 227 |
| 228 CreateFlow(rt, "", scopes); |
| 229 SetupAccessTokenFetcher(rt, scopes); |
| 230 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); |
| 231 EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher)); |
| 232 flow_->Start(); |
| 233 flow_->OnGetTokenSuccess(at); |
| 234 flow_->OnURLFetchComplete(url_fetcher); |
| 235 } |
| 236 |
| 237 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenApiCallFails) { |
| 238 std::string rt = "refresh_token"; |
| 239 std::string at = "access_token"; |
| 240 std::vector<std::string> scopes(CreateTestScopes()); |
| 241 |
| 242 CreateFlow(rt, "", scopes); |
| 243 SetupAccessTokenFetcher(rt, scopes); |
| 244 TestURLFetcher* url_fetcher = SetupApiCall(false, net::HTTP_BAD_GATEWAY); |
| 245 EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher)); |
| 246 flow_->Start(); |
| 247 flow_->OnGetTokenSuccess(at); |
| 248 flow_->OnURLFetchComplete(url_fetcher); |
| 249 } |
| 250 |
| 251 TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenNewTokenGenerationFails) { |
| 252 std::string rt = "refresh_token"; |
| 253 std::string at = "access_token"; |
| 254 std::vector<std::string> scopes(CreateTestScopes()); |
| 255 |
| 256 CreateFlow(rt, "", scopes); |
| 257 SetupAccessTokenFetcher(rt, scopes); |
| 258 GoogleServiceAuthError error( |
| 259 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 260 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); |
| 261 flow_->Start(); |
| 262 flow_->OnGetTokenFailure(error); |
| 263 } |
OLD | NEW |