Roll src/third_party/WebKit 57aef96:a1089e6 (svn 201978:201979)
[chromium-blink-merge.git] / google_apis / gaia / oauth2_access_token_fetcher_impl_unittest.cc
blobf234b39d90742706835eaadf2c9a34cc4d63fe87
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 "base/thread_task_runner_handle.h"
12 #include "google_apis/gaia/gaia_urls.h"
13 #include "google_apis/gaia/google_service_auth_error.h"
14 #include "google_apis/gaia/oauth2_access_token_consumer.h"
15 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
16 #include "net/base/net_errors.h"
17 #include "net/http/http_status_code.h"
18 #include "net/url_request/test_url_fetcher_factory.h"
19 #include "net/url_request/url_fetcher.h"
20 #include "net/url_request/url_fetcher_delegate.h"
21 #include "net/url_request/url_fetcher_factory.h"
22 #include "net/url_request/url_request.h"
23 #include "net/url_request/url_request_status.h"
24 #include "net/url_request/url_request_test_util.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "url/gurl.h"
29 using net::ResponseCookies;
30 using net::ScopedURLFetcherFactory;
31 using net::TestURLFetcher;
32 using net::URLFetcher;
33 using net::URLFetcherDelegate;
34 using net::URLFetcherFactory;
35 using net::URLRequestStatus;
36 using testing::_;
37 using testing::Return;
39 namespace {
41 typedef std::vector<std::string> ScopeList;
43 static const char kValidTokenResponse[] =
44 "{"
45 " \"access_token\": \"at1\","
46 " \"expires_in\": 3600,"
47 " \"token_type\": \"Bearer\""
48 "}";
49 static const char kTokenResponseNoAccessToken[] =
50 "{"
51 " \"expires_in\": 3600,"
52 " \"token_type\": \"Bearer\""
53 "}";
55 static const char kValidFailureTokenResponse[] =
56 "{"
57 " \"error\": \"invalid_grant\""
58 "}";
60 class MockUrlFetcherFactory : public ScopedURLFetcherFactory,
61 public URLFetcherFactory {
62 public:
63 MockUrlFetcherFactory() : ScopedURLFetcherFactory(this) {}
64 virtual ~MockUrlFetcherFactory() {}
66 MOCK_METHOD4(CreateURLFetcherMock,
67 URLFetcher*(int id,
68 const GURL& url,
69 URLFetcher::RequestType request_type,
70 URLFetcherDelegate* d));
72 scoped_ptr<URLFetcher> CreateURLFetcher(int id,
73 const GURL& url,
74 URLFetcher::RequestType request_type,
75 URLFetcherDelegate* d) override {
76 return scoped_ptr<URLFetcher>(
77 CreateURLFetcherMock(id, url, request_type, d));
81 class MockOAuth2AccessTokenConsumer : public OAuth2AccessTokenConsumer {
82 public:
83 MockOAuth2AccessTokenConsumer() {}
84 ~MockOAuth2AccessTokenConsumer() {}
86 MOCK_METHOD2(OnGetTokenSuccess,
87 void(const std::string& access_token,
88 const base::Time& expiration_time));
89 MOCK_METHOD1(OnGetTokenFailure, void(const GoogleServiceAuthError& error));
92 } // namespace
94 class OAuth2AccessTokenFetcherImplTest : public testing::Test {
95 public:
96 OAuth2AccessTokenFetcherImplTest()
97 : request_context_getter_(new net::TestURLRequestContextGetter(
98 base::ThreadTaskRunnerHandle::Get())),
99 fetcher_(&consumer_, request_context_getter_.get(), "refresh_token") {
100 base::RunLoop().RunUntilIdle();
103 ~OAuth2AccessTokenFetcherImplTest() override {}
105 virtual TestURLFetcher* SetupGetAccessToken(bool fetch_succeeds,
106 int response_code,
107 const std::string& body) {
108 GURL url(GaiaUrls::GetInstance()->oauth2_token_url());
109 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &fetcher_);
110 net::Error error = fetch_succeeds ? net::OK : net::ERR_FAILED;
111 url_fetcher->set_status(URLRequestStatus::FromError(error));
113 if (response_code != 0)
114 url_fetcher->set_response_code(response_code);
116 if (!body.empty())
117 url_fetcher->SetResponseString(body);
119 EXPECT_CALL(factory_, CreateURLFetcherMock(_, url, _, _))
120 .WillOnce(Return(url_fetcher));
121 return url_fetcher;
124 protected:
125 base::MessageLoop message_loop_;
126 MockUrlFetcherFactory factory_;
127 MockOAuth2AccessTokenConsumer consumer_;
128 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
129 OAuth2AccessTokenFetcherImpl fetcher_;
132 // These four tests time out, see http://crbug.com/113446.
133 TEST_F(OAuth2AccessTokenFetcherImplTest,
134 DISABLED_GetAccessTokenRequestFailure) {
135 TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, std::string());
136 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1);
137 fetcher_.Start("client_id", "client_secret", ScopeList());
138 fetcher_.OnURLFetchComplete(url_fetcher);
141 TEST_F(OAuth2AccessTokenFetcherImplTest,
142 DISABLED_GetAccessTokenResponseCodeFailure) {
143 TestURLFetcher* url_fetcher =
144 SetupGetAccessToken(true, net::HTTP_FORBIDDEN, std::string());
145 EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1);
146 fetcher_.Start("client_id", "client_secret", ScopeList());
147 fetcher_.OnURLFetchComplete(url_fetcher);
150 TEST_F(OAuth2AccessTokenFetcherImplTest, DISABLED_Success) {
151 TestURLFetcher* url_fetcher =
152 SetupGetAccessToken(true, net::HTTP_OK, kValidTokenResponse);
153 EXPECT_CALL(consumer_, OnGetTokenSuccess("at1", _)).Times(1);
154 fetcher_.Start("client_id", "client_secret", ScopeList());
155 fetcher_.OnURLFetchComplete(url_fetcher);
158 TEST_F(OAuth2AccessTokenFetcherImplTest, DISABLED_MakeGetAccessTokenBody) {
159 { // No scope.
160 std::string body =
161 "client_id=cid1&"
162 "client_secret=cs1&"
163 "grant_type=refresh_token&"
164 "refresh_token=rt1";
165 EXPECT_EQ(body,
166 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
167 "cid1", "cs1", "rt1", ScopeList()));
170 { // One scope.
171 std::string body =
172 "client_id=cid1&"
173 "client_secret=cs1&"
174 "grant_type=refresh_token&"
175 "refresh_token=rt1&"
176 "scope=https://www.googleapis.com/foo";
177 ScopeList scopes;
178 scopes.push_back("https://www.googleapis.com/foo");
179 EXPECT_EQ(body,
180 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
181 "cid1", "cs1", "rt1", scopes));
184 { // Multiple scopes.
185 std::string body =
186 "client_id=cid1&"
187 "client_secret=cs1&"
188 "grant_type=refresh_token&"
189 "refresh_token=rt1&"
190 "scope=https://www.googleapis.com/foo+"
191 "https://www.googleapis.com/bar+"
192 "https://www.googleapis.com/baz";
193 ScopeList scopes;
194 scopes.push_back("https://www.googleapis.com/foo");
195 scopes.push_back("https://www.googleapis.com/bar");
196 scopes.push_back("https://www.googleapis.com/baz");
197 EXPECT_EQ(body,
198 OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
199 "cid1", "cs1", "rt1", scopes));
203 // http://crbug.com/114215
204 #if defined(OS_WIN)
205 #define MAYBE_ParseGetAccessTokenResponse DISABLED_ParseGetAccessTokenResponse
206 #else
207 #define MAYBE_ParseGetAccessTokenResponse ParseGetAccessTokenResponse
208 #endif // defined(OS_WIN)
209 TEST_F(OAuth2AccessTokenFetcherImplTest, MAYBE_ParseGetAccessTokenResponse) {
210 { // No body.
211 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
213 std::string at;
214 int expires_in;
215 EXPECT_FALSE(
216 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
217 &url_fetcher, &at, &expires_in));
218 EXPECT_TRUE(at.empty());
220 { // Bad json.
221 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
222 url_fetcher.SetResponseString("foo");
224 std::string at;
225 int expires_in;
226 EXPECT_FALSE(
227 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
228 &url_fetcher, &at, &expires_in));
229 EXPECT_TRUE(at.empty());
231 { // Valid json: access token missing.
232 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
233 url_fetcher.SetResponseString(kTokenResponseNoAccessToken);
235 std::string at;
236 int expires_in;
237 EXPECT_FALSE(
238 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
239 &url_fetcher, &at, &expires_in));
240 EXPECT_TRUE(at.empty());
242 { // Valid json: all good.
243 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
244 url_fetcher.SetResponseString(kValidTokenResponse);
246 std::string at;
247 int expires_in;
248 EXPECT_TRUE(
249 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
250 &url_fetcher, &at, &expires_in));
251 EXPECT_EQ("at1", at);
252 EXPECT_EQ(3600, expires_in);
254 { // Valid json: invalid error response.
255 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
256 url_fetcher.SetResponseString(kTokenResponseNoAccessToken);
258 std::string error;
259 EXPECT_FALSE(
260 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
261 &url_fetcher, &error));
262 EXPECT_TRUE(error.empty());
264 { // Valid json: error response.
265 TestURLFetcher url_fetcher(0, GURL("http://www.google.com"), NULL);
266 url_fetcher.SetResponseString(kValidFailureTokenResponse);
268 std::string error;
269 EXPECT_TRUE(
270 OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
271 &url_fetcher, &error));
272 EXPECT_EQ("invalid_grant", error);