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