Roll src/third_party/WebKit d26421b:4b1dbe3 (svn 194839:194840)
[chromium-blink-merge.git] / google_apis / gaia / oauth2_access_token_fetcher_impl_unittest.cc
blob8cd7244c267300357b8e9e0ae9a8427d252ccfb3
1 // Copyright 2014 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 OAuth2AccessTokenFetcherImpl.
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "google_apis/gaia/gaia_urls.h"
12 #include "google_apis/gaia/google_service_auth_error.h"
13 #include "google_apis/gaia/oauth2_access_token_consumer.h"
14 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
15 #include "net/http/http_status_code.h"
16 #include "net/url_request/test_url_fetcher_factory.h"
17 #include "net/url_request/url_fetcher.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19 #include "net/url_request/url_fetcher_factory.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_status.h"
22 #include "net/url_request/url_request_test_util.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "url/gurl.h"
27 using net::ResponseCookies;
28 using net::ScopedURLFetcherFactory;
29 using net::TestURLFetcher;
30 using net::URLFetcher;
31 using net::URLFetcherDelegate;
32 using net::URLFetcherFactory;
33 using net::URLRequestStatus;
34 using testing::_;
35 using testing::Return;
37 namespace {
39 typedef std::vector<std::string> ScopeList;
41 static const char kValidTokenResponse[] =
42 "{"
43 " \"access_token\": \"at1\","
44 " \"expires_in\": 3600,"
45 " \"token_type\": \"Bearer\""
46 "}";
47 static const char kTokenResponseNoAccessToken[] =
48 "{"
49 " \"expires_in\": 3600,"
50 " \"token_type\": \"Bearer\""
51 "}";
53 static const char kValidFailureTokenResponse[] =
54 "{"
55 " \"error\": \"invalid_grant\""
56 "}";
58 class MockUrlFetcherFactory : public ScopedURLFetcherFactory,
59 public URLFetcherFactory {
60 public:
61 MockUrlFetcherFactory() : ScopedURLFetcherFactory(this) {}
62 virtual ~MockUrlFetcherFactory() {}
64 MOCK_METHOD4(CreateURLFetcherMock,
65 URLFetcher*(int id,
66 const GURL& url,
67 URLFetcher::RequestType request_type,
68 URLFetcherDelegate* d));
70 scoped_ptr<URLFetcher> CreateURLFetcher(int id,
71 const GURL& url,
72 URLFetcher::RequestType request_type,
73 URLFetcherDelegate* d) override {
74 return scoped_ptr<URLFetcher>(
75 CreateURLFetcherMock(id, url, request_type, d));
79 class MockOAuth2AccessTokenConsumer : public OAuth2AccessTokenConsumer {
80 public:
81 MockOAuth2AccessTokenConsumer() {}
82 ~MockOAuth2AccessTokenConsumer() {}
84 MOCK_METHOD2(OnGetTokenSuccess,
85 void(const std::string& access_token,
86 const base::Time& expiration_time));
87 MOCK_METHOD1(OnGetTokenFailure, void(const GoogleServiceAuthError& error));
90 } // namespace
92 class OAuth2AccessTokenFetcherImplTest : public testing::Test {
93 public:
94 OAuth2AccessTokenFetcherImplTest()
95 : request_context_getter_(new net::TestURLRequestContextGetter(
96 base::MessageLoopProxy::current())),
97 fetcher_(&consumer_, request_context_getter_.get(), "refresh_token") {
98 base::RunLoop().RunUntilIdle();
101 ~OAuth2AccessTokenFetcherImplTest() override {}
103 virtual TestURLFetcher* SetupGetAccessToken(bool fetch_succeeds,
104 int response_code,
105 const std::string& body) {
106 GURL url(GaiaUrls::GetInstance()->oauth2_token_url());
107 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &fetcher_);
108 URLRequestStatus::Status status =
109 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED;
110 url_fetcher->set_status(URLRequestStatus(status, 0));
112 if (response_code != 0)
113 url_fetcher->set_response_code(response_code);
115 if (!body.empty())
116 url_fetcher->SetResponseString(body);
118 EXPECT_CALL(factory_, CreateURLFetcherMock(_, url, _, _))
119 .WillOnce(Return(url_fetcher));
120 return url_fetcher;
123 protected:
124 base::MessageLoop message_loop_;
125 MockUrlFetcherFactory factory_;
126 MockOAuth2AccessTokenConsumer consumer_;
127 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
128 OAuth2AccessTokenFetcherImpl fetcher_;
131 // These four tests time out, see http://crbug.com/113446.
132 TEST_F(OAuth2AccessTokenFetcherImplTest,
133 DISABLED_GetAccessTokenRequestFailure) {
134 TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, std::string());
135 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1);
136 fetcher_.Start("client_id", "client_secret", ScopeList());
137 fetcher_.OnURLFetchComplete(url_fetcher);
140 TEST_F(OAuth2AccessTokenFetcherImplTest,
141 DISABLED_GetAccessTokenResponseCodeFailure) {
142 TestURLFetcher* url_fetcher =
143 SetupGetAccessToken(true, net::HTTP_FORBIDDEN, std::string());
144 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1);
145 fetcher_.Start("client_id", "client_secret", ScopeList());
146 fetcher_.OnURLFetchComplete(url_fetcher);
149 TEST_F(OAuth2AccessTokenFetcherImplTest, DISABLED_Success) {
150 TestURLFetcher* url_fetcher =
151 SetupGetAccessToken(true, net::HTTP_OK, kValidTokenResponse);
152 EXPECT_CALL(consumer_, OnGetTokenSuccess("at1", _)).Times(1);
153 fetcher_.Start("client_id", "client_secret", ScopeList());
154 fetcher_.OnURLFetchComplete(url_fetcher);
157 TEST_F(OAuth2AccessTokenFetcherImplTest, DISABLED_MakeGetAccessTokenBody) {
158 { // No scope.
159 std::string body =
160 "client_id=cid1&"
161 "client_secret=cs1&"
162 "grant_type=refresh_token&"
163 "refresh_token=rt1";
164 EXPECT_EQ(body,
165 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
166 "cid1", "cs1", "rt1", ScopeList()));
169 { // One scope.
170 std::string body =
171 "client_id=cid1&"
172 "client_secret=cs1&"
173 "grant_type=refresh_token&"
174 "refresh_token=rt1&"
175 "scope=https://www.googleapis.com/foo";
176 ScopeList scopes;
177 scopes.push_back("https://www.googleapis.com/foo");
178 EXPECT_EQ(body,
179 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
180 "cid1", "cs1", "rt1", scopes));
183 { // Multiple scopes.
184 std::string body =
185 "client_id=cid1&"
186 "client_secret=cs1&"
187 "grant_type=refresh_token&"
188 "refresh_token=rt1&"
189 "scope=https://www.googleapis.com/foo+"
190 "https://www.googleapis.com/bar+"
191 "https://www.googleapis.com/baz";
192 ScopeList scopes;
193 scopes.push_back("https://www.googleapis.com/foo");
194 scopes.push_back("https://www.googleapis.com/bar");
195 scopes.push_back("https://www.googleapis.com/baz");
196 EXPECT_EQ(body,
197 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
198 "cid1", "cs1", "rt1", scopes));
202 // http://crbug.com/114215
203 #if defined(OS_WIN)
204 #define MAYBE_ParseGetAccessTokenResponse DISABLED_ParseGetAccessTokenResponse
205 #else
206 #define MAYBE_ParseGetAccessTokenResponse ParseGetAccessTokenResponse
207 #endif // defined(OS_WIN)
208 TEST_F(OAuth2AccessTokenFetcherImplTest, MAYBE_ParseGetAccessTokenResponse) {
209 { // No body.
210 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
212 std::string at;
213 int expires_in;
214 EXPECT_FALSE(
215 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
216 &url_fetcher, &at, &expires_in));
217 EXPECT_TRUE(at.empty());
219 { // Bad json.
220 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
221 url_fetcher.SetResponseString("foo");
223 std::string at;
224 int expires_in;
225 EXPECT_FALSE(
226 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
227 &url_fetcher, &at, &expires_in));
228 EXPECT_TRUE(at.empty());
230 { // Valid json: access token missing.
231 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
232 url_fetcher.SetResponseString(kTokenResponseNoAccessToken);
234 std::string at;
235 int expires_in;
236 EXPECT_FALSE(
237 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
238 &url_fetcher, &at, &expires_in));
239 EXPECT_TRUE(at.empty());
241 { // Valid json: all good.
242 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
243 url_fetcher.SetResponseString(kValidTokenResponse);
245 std::string at;
246 int expires_in;
247 EXPECT_TRUE(
248 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
249 &url_fetcher, &at, &expires_in));
250 EXPECT_EQ("at1", at);
251 EXPECT_EQ(3600, expires_in);
253 { // Valid json: invalid error response.
254 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
255 url_fetcher.SetResponseString(kTokenResponseNoAccessToken);
257 std::string error;
258 EXPECT_FALSE(
259 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
260 &url_fetcher, &error));
261 EXPECT_TRUE(error.empty());
263 { // Valid json: error response.
264 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
265 url_fetcher.SetResponseString(kValidFailureTokenResponse);
267 std::string error;
268 EXPECT_TRUE(
269 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
270 &url_fetcher, &error));
271 EXPECT_EQ("invalid_grant", error);