forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoauth2_mint_token_flow_unittest.cc
365 lines (335 loc) · 11.8 KB
/
oauth2_mint_token_flow_unittest.cc
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (c) 2012 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 OAuth2MintTokenFlow.
#include "google_apis/gaia/oauth2_mint_token_flow.h"
#include <memory>
#include <string>
#include <vector>
#include "base/json/json_reader.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "google_apis/gaia/oauth2_access_token_fetcher.h"
#include "net/base/net_errors.h"
#include "services/network/public/cpp/resource_response.h"
#include "services/network/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::StrictMock;
namespace {
static const char kValidTokenResponse[] =
"{"
" \"token\": \"at1\","
" \"issueAdvice\": \"Auto\","
" \"expiresIn\": \"3600\""
"}";
static const char kTokenResponseNoAccessToken[] =
"{"
" \"issueAdvice\": \"Auto\""
"}";
static const char kValidIssueAdviceResponse[] =
"{"
" \"issueAdvice\": \"consent\","
" \"consent\": {"
" \"oauthClient\": {"
" \"name\": \"Test app\","
" \"iconUri\": \"\","
" \"developerEmail\": \"munjal@chromium.org\""
" },"
" \"scopes\": ["
" {"
" \"description\": \"Manage your calendars\","
" \"detail\": \"\nView and manage your calendars\n\""
" },"
" {"
" \"description\": \"Manage your documents\","
" \"detail\": \"\nView your documents\nUpload new documents\n\""
" }"
" ]"
" }"
"}";
static const char kIssueAdviceResponseNoDescription[] =
"{"
" \"issueAdvice\": \"consent\","
" \"consent\": {"
" \"oauthClient\": {"
" \"name\": \"Test app\","
" \"iconUri\": \"\","
" \"developerEmail\": \"munjal@chromium.org\""
" },"
" \"scopes\": ["
" {"
" \"description\": \"Manage your calendars\","
" \"detail\": \"\nView and manage your calendars\n\""
" },"
" {"
" \"detail\": \"\nView your documents\nUpload new documents\n\""
" }"
" ]"
" }"
"}";
static const char kIssueAdviceResponseNoDetail[] =
"{"
" \"issueAdvice\": \"consent\","
" \"consent\": {"
" \"oauthClient\": {"
" \"name\": \"Test app\","
" \"iconUri\": \"\","
" \"developerEmail\": \"munjal@chromium.org\""
" },"
" \"scopes\": ["
" {"
" \"description\": \"Manage your calendars\","
" \"detail\": \"\nView and manage your calendars\n\""
" },"
" {"
" \"description\": \"Manage your documents\""
" }"
" ]"
" }"
"}";
std::vector<std::string> CreateTestScopes() {
std::vector<std::string> scopes;
scopes.push_back("http://scope1");
scopes.push_back("http://scope2");
return scopes;
}
static IssueAdviceInfo CreateIssueAdvice() {
IssueAdviceInfo ia;
IssueAdviceInfoEntry e1;
e1.description = base::ASCIIToUTF16("Manage your calendars");
e1.details.push_back(base::ASCIIToUTF16("View and manage your calendars"));
ia.push_back(e1);
IssueAdviceInfoEntry e2;
e2.description = base::ASCIIToUTF16("Manage your documents");
e2.details.push_back(base::ASCIIToUTF16("View your documents"));
e2.details.push_back(base::ASCIIToUTF16("Upload new documents"));
ia.push_back(e2);
return ia;
}
class MockDelegate : public OAuth2MintTokenFlow::Delegate {
public:
MockDelegate() {}
~MockDelegate() override {}
MOCK_METHOD2(OnMintTokenSuccess, void(const std::string& access_token,
int time_to_live));
MOCK_METHOD1(OnIssueAdviceSuccess,
void (const IssueAdviceInfo& issue_advice));
MOCK_METHOD1(OnMintTokenFailure,
void(const GoogleServiceAuthError& error));
};
class MockMintTokenFlow : public OAuth2MintTokenFlow {
public:
explicit MockMintTokenFlow(MockDelegate* delegate,
const OAuth2MintTokenFlow::Parameters& parameters)
: OAuth2MintTokenFlow(delegate, parameters) {}
~MockMintTokenFlow() override {}
MOCK_METHOD0(CreateAccessTokenFetcher, OAuth2AccessTokenFetcher*());
};
} // namespace
class OAuth2MintTokenFlowTest : public testing::Test {
public:
OAuth2MintTokenFlowTest() {}
~OAuth2MintTokenFlowTest() override {}
protected:
void CreateFlow(OAuth2MintTokenFlow::Mode mode) {
return CreateFlow(&delegate_, mode, "");
}
void CreateFlowWithDeviceId(const std::string& device_id) {
return CreateFlow(&delegate_, OAuth2MintTokenFlow::MODE_ISSUE_ADVICE,
device_id);
}
void CreateFlow(MockDelegate* delegate,
OAuth2MintTokenFlow::Mode mode,
const std::string& device_id) {
std::string ext_id = "ext1";
std::string client_id = "client1";
std::vector<std::string> scopes(CreateTestScopes());
flow_.reset(new MockMintTokenFlow(
delegate, OAuth2MintTokenFlow::Parameters(ext_id, client_id, scopes,
device_id, mode)));
}
// Helper to parse the given string to DictionaryValue.
static base::DictionaryValue* ParseJson(const std::string& str) {
std::unique_ptr<base::Value> value = base::JSONReader::ReadDeprecated(str);
EXPECT_TRUE(value.get());
EXPECT_EQ(base::Value::Type::DICTIONARY, value->type());
return static_cast<base::DictionaryValue*>(value.release());
}
std::unique_ptr<MockMintTokenFlow> flow_;
StrictMock<MockDelegate> delegate_;
};
TEST_F(OAuth2MintTokenFlowTest, CreateApiCallBody) {
{ // Issue advice mode.
CreateFlow(OAuth2MintTokenFlow::MODE_ISSUE_ADVICE);
std::string body = flow_->CreateApiCallBody();
std::string expected_body(
"force=false"
"&response_type=none"
"&scope=http://scope1+http://scope2"
"&client_id=client1"
"&origin=ext1");
EXPECT_EQ(expected_body, body);
}
{ // Record grant mode.
CreateFlow(OAuth2MintTokenFlow::MODE_RECORD_GRANT);
std::string body = flow_->CreateApiCallBody();
std::string expected_body(
"force=true"
"&response_type=none"
"&scope=http://scope1+http://scope2"
"&client_id=client1"
"&origin=ext1");
EXPECT_EQ(expected_body, body);
}
{ // Mint token no force mode.
CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
std::string body = flow_->CreateApiCallBody();
std::string expected_body(
"force=false"
"&response_type=token"
"&scope=http://scope1+http://scope2"
"&client_id=client1"
"&origin=ext1");
EXPECT_EQ(expected_body, body);
}
{ // Mint token force mode.
CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_FORCE);
std::string body = flow_->CreateApiCallBody();
std::string expected_body(
"force=true"
"&response_type=token"
"&scope=http://scope1+http://scope2"
"&client_id=client1"
"&origin=ext1");
EXPECT_EQ(expected_body, body);
}
{ // Mint token with device_id.
CreateFlowWithDeviceId("device_id1");
std::string body = flow_->CreateApiCallBody();
std::string expected_body(
"force=false"
"&response_type=none"
"&scope=http://scope1+http://scope2"
"&client_id=client1"
"&origin=ext1"
"&device_id=device_id1"
"&device_type=chrome"
"&lib_ver=extension");
EXPECT_EQ(expected_body, body);
}
}
TEST_F(OAuth2MintTokenFlowTest, ParseMintTokenResponse) {
{ // Access token missing.
std::unique_ptr<base::DictionaryValue> json(
ParseJson(kTokenResponseNoAccessToken));
std::string at;
int ttl;
EXPECT_FALSE(OAuth2MintTokenFlow::ParseMintTokenResponse(json.get(), &at,
&ttl));
EXPECT_TRUE(at.empty());
}
{ // All good.
std::unique_ptr<base::DictionaryValue> json(ParseJson(kValidTokenResponse));
std::string at;
int ttl;
EXPECT_TRUE(OAuth2MintTokenFlow::ParseMintTokenResponse(json.get(), &at,
&ttl));
EXPECT_EQ("at1", at);
EXPECT_EQ(3600, ttl);
}
}
TEST_F(OAuth2MintTokenFlowTest, ParseIssueAdviceResponse) {
{ // Description missing.
std::unique_ptr<base::DictionaryValue> json(
ParseJson(kIssueAdviceResponseNoDescription));
IssueAdviceInfo ia;
EXPECT_FALSE(OAuth2MintTokenFlow::ParseIssueAdviceResponse(
json.get(), &ia));
EXPECT_TRUE(ia.empty());
}
{ // Detail missing.
std::unique_ptr<base::DictionaryValue> json(
ParseJson(kIssueAdviceResponseNoDetail));
IssueAdviceInfo ia;
EXPECT_FALSE(OAuth2MintTokenFlow::ParseIssueAdviceResponse(
json.get(), &ia));
EXPECT_TRUE(ia.empty());
}
{ // All good.
std::unique_ptr<base::DictionaryValue> json(
ParseJson(kValidIssueAdviceResponse));
IssueAdviceInfo ia;
EXPECT_TRUE(OAuth2MintTokenFlow::ParseIssueAdviceResponse(
json.get(), &ia));
IssueAdviceInfo ia_expected(CreateIssueAdvice());
EXPECT_EQ(ia_expected, ia);
}
}
TEST_F(OAuth2MintTokenFlowTest, ProcessApiCallSuccess) {
network::ResourceResponseHead head_200 =
network::CreateResourceResponseHead(net::HTTP_OK);
{ // No body.
CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
EXPECT_CALL(delegate_, OnMintTokenFailure(_));
flow_->ProcessApiCallSuccess(&head_200, nullptr);
}
{ // Bad json.
CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
EXPECT_CALL(delegate_, OnMintTokenFailure(_));
flow_->ProcessApiCallSuccess(&head_200,
std::make_unique<std::string>("foo"));
}
{ // Valid json: no access token.
CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
EXPECT_CALL(delegate_, OnMintTokenFailure(_));
flow_->ProcessApiCallSuccess(
&head_200, std::make_unique<std::string>(kTokenResponseNoAccessToken));
}
{ // Valid json: good token response.
CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
EXPECT_CALL(delegate_, OnMintTokenSuccess("at1", 3600));
flow_->ProcessApiCallSuccess(
&head_200, std::make_unique<std::string>(kValidTokenResponse));
}
{ // Valid json: no description.
CreateFlow(OAuth2MintTokenFlow::MODE_ISSUE_ADVICE);
EXPECT_CALL(delegate_, OnMintTokenFailure(_));
flow_->ProcessApiCallSuccess(
&head_200,
std::make_unique<std::string>(kIssueAdviceResponseNoDescription));
}
{ // Valid json: no detail.
CreateFlow(OAuth2MintTokenFlow::MODE_ISSUE_ADVICE);
EXPECT_CALL(delegate_, OnMintTokenFailure(_));
flow_->ProcessApiCallSuccess(
&head_200, std::make_unique<std::string>(kIssueAdviceResponseNoDetail));
}
{ // Valid json: good issue advice response.
CreateFlow(OAuth2MintTokenFlow::MODE_ISSUE_ADVICE);
IssueAdviceInfo ia(CreateIssueAdvice());
EXPECT_CALL(delegate_, OnIssueAdviceSuccess(ia));
flow_->ProcessApiCallSuccess(
&head_200, std::make_unique<std::string>(kValidIssueAdviceResponse));
}
}
TEST_F(OAuth2MintTokenFlowTest, ProcessApiCallFailure) {
network::ResourceResponseHead head;
{ // Null delegate should work fine.
CreateFlow(NULL, OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE, "");
flow_->ProcessApiCallFailure(net::ERR_FAILED, &head, nullptr);
}
{ // Non-null delegate.
CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
EXPECT_CALL(delegate_, OnMintTokenFailure(_));
flow_->ProcessApiCallFailure(net::ERR_FAILED, &head, nullptr);
}
{ // Null head might happen.
CreateFlow(OAuth2MintTokenFlow::MODE_MINT_TOKEN_NO_FORCE);
EXPECT_CALL(delegate_, OnMintTokenFailure(_));
flow_->ProcessApiCallFailure(net::ERR_FAILED, nullptr, nullptr);
}
}