1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// A complete set of unit tests for OAuth2AccessTokenFetcherImpl.
#include <string>
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "google_apis/gaia/gaia_urls.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "google_apis/gaia/oauth2_access_token_consumer.h"
#include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
#include "net/http/http_status_code.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_fetcher_factory.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_status.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using net::ResponseCookies;
using net::ScopedURLFetcherFactory;
using net::TestURLFetcher;
using net::URLFetcher;
using net::URLFetcherDelegate;
using net::URLFetcherFactory;
using net::URLRequestStatus;
using testing::_;
using testing::Return;
namespace {
typedef std::vector<std::string> ScopeList;
static const char kValidTokenResponse[] =
"{"
" \"access_token\": \"at1\","
" \"expires_in\": 3600,"
" \"token_type\": \"Bearer\""
"}";
static const char kTokenResponseNoAccessToken[] =
"{"
" \"expires_in\": 3600,"
" \"token_type\": \"Bearer\""
"}";
static const char kValidFailureTokenResponse[] =
"{"
" \"error\": \"invalid_grant\""
"}";
class MockUrlFetcherFactory : public ScopedURLFetcherFactory,
public URLFetcherFactory {
public:
MockUrlFetcherFactory() : ScopedURLFetcherFactory(this) {}
virtual ~MockUrlFetcherFactory() {}
MOCK_METHOD4(CreateURLFetcher,
URLFetcher*(int id,
const GURL& url,
URLFetcher::RequestType request_type,
URLFetcherDelegate* d));
};
class MockOAuth2AccessTokenConsumer : public OAuth2AccessTokenConsumer {
public:
MockOAuth2AccessTokenConsumer() {}
~MockOAuth2AccessTokenConsumer() {}
MOCK_METHOD2(OnGetTokenSuccess,
void(const std::string& access_token,
const base::Time& expiration_time));
MOCK_METHOD1(OnGetTokenFailure, void(const GoogleServiceAuthError& error));
};
} // namespace
class OAuth2AccessTokenFetcherImplTest : public testing::Test {
public:
OAuth2AccessTokenFetcherImplTest()
: request_context_getter_(new net::TestURLRequestContextGetter(
base::MessageLoopProxy::current())),
fetcher_(&consumer_, request_context_getter_, "refresh_token") {
base::RunLoop().RunUntilIdle();
}
virtual ~OAuth2AccessTokenFetcherImplTest() {}
virtual TestURLFetcher* SetupGetAccessToken(bool fetch_succeeds,
int response_code,
const std::string& body) {
GURL url(GaiaUrls::GetInstance()->oauth2_token_url());
TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &fetcher_);
URLRequestStatus::Status status =
fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED;
url_fetcher->set_status(URLRequestStatus(status, 0));
if (response_code != 0)
url_fetcher->set_response_code(response_code);
if (!body.empty())
url_fetcher->SetResponseString(body);
EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _))
.WillOnce(Return(url_fetcher));
return url_fetcher;
}
protected:
base::MessageLoop message_loop_;
MockUrlFetcherFactory factory_;
MockOAuth2AccessTokenConsumer consumer_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
OAuth2AccessTokenFetcherImpl fetcher_;
};
// These four tests time out, see http://crbug.com/113446.
TEST_F(OAuth2AccessTokenFetcherImplTest,
DISABLED_GetAccessTokenRequestFailure) {
TestURLFetcher* url_fetcher = SetupGetAccessToken(false, 0, std::string());
EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1);
fetcher_.Start("client_id", "client_secret", ScopeList());
fetcher_.OnURLFetchComplete(url_fetcher);
}
TEST_F(OAuth2AccessTokenFetcherImplTest,
DISABLED_GetAccessTokenResponseCodeFailure) {
TestURLFetcher* url_fetcher =
SetupGetAccessToken(true, net::HTTP_FORBIDDEN, std::string());
EXPECT_CALL(consumer_, OnGetTokenFailure(_)).Times(1);
fetcher_.Start("client_id", "client_secret", ScopeList());
fetcher_.OnURLFetchComplete(url_fetcher);
}
TEST_F(OAuth2AccessTokenFetcherImplTest, DISABLED_Success) {
TestURLFetcher* url_fetcher =
SetupGetAccessToken(true, net::HTTP_OK, kValidTokenResponse);
EXPECT_CALL(consumer_, OnGetTokenSuccess("at1", _)).Times(1);
fetcher_.Start("client_id", "client_secret", ScopeList());
fetcher_.OnURLFetchComplete(url_fetcher);
}
TEST_F(OAuth2AccessTokenFetcherImplTest, DISABLED_MakeGetAccessTokenBody) {
{ // No scope.
std::string body =
"client_id=cid1&"
"client_secret=cs1&"
"grant_type=refresh_token&"
"refresh_token=rt1";
EXPECT_EQ(body,
OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
"cid1", "cs1", "rt1", ScopeList()));
}
{ // One scope.
std::string body =
"client_id=cid1&"
"client_secret=cs1&"
"grant_type=refresh_token&"
"refresh_token=rt1&"
"scope=https://www.googleapis.com/foo";
ScopeList scopes;
scopes.push_back("https://www.googleapis.com/foo");
EXPECT_EQ(body,
OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
"cid1", "cs1", "rt1", scopes));
}
{ // Multiple scopes.
std::string body =
"client_id=cid1&"
"client_secret=cs1&"
"grant_type=refresh_token&"
"refresh_token=rt1&"
"scope=https://www.googleapis.com/foo+"
"https://www.googleapis.com/bar+"
"https://www.googleapis.com/baz";
ScopeList scopes;
scopes.push_back("https://www.googleapis.com/foo");
scopes.push_back("https://www.googleapis.com/bar");
scopes.push_back("https://www.googleapis.com/baz");
EXPECT_EQ(body,
OAuth2AccessTokenFetcherImpl::MakeGetAccessTokenBody(
"cid1", "cs1", "rt1", scopes));
}
}
// http://crbug.com/114215
#if defined(OS_WIN)
#define MAYBE_ParseGetAccessTokenResponse DISABLED_ParseGetAccessTokenResponse
#else
#define MAYBE_ParseGetAccessTokenResponse ParseGetAccessTokenResponse
#endif // defined(OS_WIN)
TEST_F(OAuth2AccessTokenFetcherImplTest, MAYBE_ParseGetAccessTokenResponse) {
{ // No body.
TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
std::string at;
int expires_in;
EXPECT_FALSE(
OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
&url_fetcher, &at, &expires_in));
EXPECT_TRUE(at.empty());
}
{ // Bad json.
TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
url_fetcher.SetResponseString("foo");
std::string at;
int expires_in;
EXPECT_FALSE(
OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
&url_fetcher, &at, &expires_in));
EXPECT_TRUE(at.empty());
}
{ // Valid json: access token missing.
TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
url_fetcher.SetResponseString(kTokenResponseNoAccessToken);
std::string at;
int expires_in;
EXPECT_FALSE(
OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
&url_fetcher, &at, &expires_in));
EXPECT_TRUE(at.empty());
}
{ // Valid json: all good.
TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
url_fetcher.SetResponseString(kValidTokenResponse);
std::string at;
int expires_in;
EXPECT_TRUE(
OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenSuccessResponse(
&url_fetcher, &at, &expires_in));
EXPECT_EQ("at1", at);
EXPECT_EQ(3600, expires_in);
}
{ // Valid json: invalid error response.
TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
url_fetcher.SetResponseString(kTokenResponseNoAccessToken);
std::string error;
EXPECT_FALSE(
OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
&url_fetcher, &error));
EXPECT_TRUE(error.empty());
}
{ // Valid json: error response.
TestURLFetcher url_fetcher(0, GURL("www.google.com"), NULL);
url_fetcher.SetResponseString(kValidFailureTokenResponse);
std::string error;
EXPECT_TRUE(
OAuth2AccessTokenFetcherImpl::ParseGetAccessTokenFailureResponse(
&url_fetcher, &error));
EXPECT_EQ("invalid_grant", error);
}
}
|