blob: 27aeaa008ab1dac7229455082d58c697e78aada7 [file] [log] [blame]
Alex Vakulenko8e34d392014-04-29 11:02:56 -07001// Copyright 2014 The Chromium OS 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#include <base/json/json_reader.h>
6#include <base/values.h>
Alex Vakulenkocf0b33a2014-08-20 15:02:10 -07007#include <chromeos/bind_lambda.h>
Alex Vakulenkocca20932014-08-20 17:35:12 -07008#include <chromeos/http_request.h>
9#include <chromeos/http_transport_fake.h>
Alex Vakulenko3aeea1c2014-08-20 16:33:12 -070010#include <chromeos/mime_utils.h>
Alex Vakulenko8e34d392014-04-29 11:02:56 -070011#include <gtest/gtest.h>
12
Alex Vakulenko1f30a622014-07-23 11:13:15 -070013#include "buffet/commands/command_manager.h"
Alex Vakulenko45109442014-07-29 11:07:10 -070014#include "buffet/commands/unittest_utils.h"
Alex Vakulenko8e34d392014-04-29 11:02:56 -070015#include "buffet/device_registration_info.h"
16#include "buffet/device_registration_storage_keys.h"
Christopher Wiley006e94e2014-05-02 13:44:48 -070017#include "buffet/storage_impls.h"
Alex Vakulenko8e34d392014-04-29 11:02:56 -070018
Alex Vakulenkocca20932014-08-20 17:35:12 -070019using chromeos::http::request_header::kAuthorization;
20using chromeos::http::fake::ServerRequest;
21using chromeos::http::fake::ServerResponse;
22
23namespace buffet {
Alex Vakulenko8e34d392014-04-29 11:02:56 -070024
25namespace {
Alex Vakulenko8e34d392014-04-29 11:02:56 -070026
27namespace test_data {
28
29const char kServiceURL[] = "http://gcd.server.com/";
30const char kOAuthURL[] = "http://oauth.server.com/";
31const char kApiKey[] = "GOadRdTf9FERf0k4w6EFOof56fUJ3kFDdFL3d7f";
32const char kClientId[] = "123543821385-sfjkjshdkjhfk234sdfsdfkskd"
33 "fkjh7f.apps.googleusercontent.com";
34const char kClientSecret[] = "5sdGdGlfolGlrFKfdFlgP6FG";
35const char kDeviceId[] = "4a7ea2d1-b331-1e1f-b206-e863c7635196";
36const char kClaimTicketId[] = "RTcUE";
37const char kAccessToken[] = "ya29.1.AADtN_V-dLUM-sVZ0qVjG9Dxm5NgdS9J"
38 "Mx_JLUqhC9bED_YFjzHZtYt65ZzXCS35NMAeaVZ"
39 "Dei530-w0yE2urpQ";
40const char kRefreshToken[] = "1/zQmxR6PKNvhcxf9SjXUrCjcmCrcqRKXctc6cp"
41 "1nI-GQ";
42const char kRobotAccountAuthCode[] = "4/Mf_ujEhPejVhOq-OxW9F5cSOnWzx."
43 "YgciVjTYGscRshQV0ieZDAqiTIjMigI";
44const char kRobotAccountEmail[] = "6ed0b3f54f9bd619b942f4ad2441c252@"
45 "clouddevices.gserviceaccount.com";
46const char kUserAccountAuthCode[] = "2/sd_GD1TGFKpJOLJ34-0g5fK0fflp.GlT"
47 "I0F5g7hNtFgj5HFGOf8FlGK9eflO";
48const char kUserAccessToken[] = "sd56.4.FGDjG_F-gFGF-dFG6gGOG9Dxm5NgdS9"
49 "JMx_JLUqhC9bED_YFjLKjlkjLKJlkjLKjlKJea"
50 "VZDei530-w0yE2urpQ";
51const char kUserRefreshToken[] = "1/zQLKjlKJlkLkLKjLkjLKjLkjLjLkjl0ftc6"
52 "cp1nI-GQ";
53
54} // namespace test_data
55
56// Fill in the storage with default environment information (URLs, etc).
57void InitDefaultStorage(base::DictionaryValue* data) {
58 data->SetString(storage_keys::kClientId, test_data::kClientId);
59 data->SetString(storage_keys::kClientSecret, test_data::kClientSecret);
60 data->SetString(storage_keys::kApiKey, test_data::kApiKey);
61 data->SetString(storage_keys::kRefreshToken, "");
62 data->SetString(storage_keys::kDeviceId, "");
63 data->SetString(storage_keys::kOAuthURL, test_data::kOAuthURL);
64 data->SetString(storage_keys::kServiceURL, test_data::kServiceURL);
65 data->SetString(storage_keys::kRobotAccount, "");
66}
67
68// Add the test device registration information.
69void SetDefaultDeviceRegistration(base::DictionaryValue* data) {
70 data->SetString(storage_keys::kRefreshToken, test_data::kRefreshToken);
71 data->SetString(storage_keys::kDeviceId, test_data::kDeviceId);
72 data->SetString(storage_keys::kRobotAccount, test_data::kRobotAccountEmail);
73}
74
Alex Vakulenkocca20932014-08-20 17:35:12 -070075void OAuth2Handler(const ServerRequest& request, ServerResponse* response) {
Alex Vakulenko8e34d392014-04-29 11:02:56 -070076 base::DictionaryValue json;
77 if (request.GetFormField("grant_type") == "refresh_token") {
78 // Refresh device access token.
79 EXPECT_EQ(test_data::kRefreshToken, request.GetFormField("refresh_token"));
80 EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id"));
81 EXPECT_EQ(test_data::kClientSecret, request.GetFormField("client_secret"));
82 json.SetString("access_token", test_data::kAccessToken);
83 } else if (request.GetFormField("grant_type") == "authorization_code") {
84 // Obtain access token.
85 std::string code = request.GetFormField("code");
86 if (code == test_data::kUserAccountAuthCode) {
87 // Get user access token.
88 EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id"));
89 EXPECT_EQ(test_data::kClientSecret,
90 request.GetFormField("client_secret"));
91 EXPECT_EQ("urn:ietf:wg:oauth:2.0:oob",
92 request.GetFormField("redirect_uri"));
93 json.SetString("access_token", test_data::kUserAccessToken);
94 json.SetString("token_type", "Bearer");
95 json.SetString("refresh_token", test_data::kUserRefreshToken);
96 } else if (code == test_data::kRobotAccountAuthCode) {
97 // Get device access token.
98 EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id"));
99 EXPECT_EQ(test_data::kClientSecret,
100 request.GetFormField("client_secret"));
101 EXPECT_EQ("oob", request.GetFormField("redirect_uri"));
102 EXPECT_EQ("https://www.googleapis.com/auth/clouddevices",
103 request.GetFormField("scope"));
104 json.SetString("access_token", test_data::kAccessToken);
105 json.SetString("token_type", "Bearer");
106 json.SetString("refresh_token", test_data::kRefreshToken);
107 } else {
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700108 FAIL() << "Unexpected authorization code";
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700109 }
110 } else {
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700111 FAIL() << "Unexpected grant type";
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700112 }
113 json.SetInteger("expires_in", 3600);
Alex Vakulenkocca20932014-08-20 17:35:12 -0700114 response->ReplyJson(chromeos::http::status_code::Ok, &json);
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700115}
116
Alex Vakulenkocca20932014-08-20 17:35:12 -0700117void DeviceInfoHandler(const ServerRequest& request, ServerResponse* response) {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700118 std::string auth = "Bearer ";
119 auth += test_data::kAccessToken;
Alex Vakulenkocca20932014-08-20 17:35:12 -0700120 EXPECT_EQ(auth,
121 request.GetHeader(chromeos::http::request_header::kAuthorization));
122 response->ReplyJson(chromeos::http::status_code::Ok, {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700123 {"channel.supportedType", "xmpp"},
124 {"deviceKind", "vendor"},
125 {"id", test_data::kDeviceId},
126 {"kind", "clouddevices#device"},
127 });
128}
129
Alex Vakulenkocca20932014-08-20 17:35:12 -0700130void FinalizeTicketHandler(const ServerRequest& request,
131 ServerResponse* response) {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700132 EXPECT_EQ(test_data::kApiKey, request.GetFormField("key"));
133 EXPECT_TRUE(request.GetData().empty());
134
Alex Vakulenkocca20932014-08-20 17:35:12 -0700135 response->ReplyJson(chromeos::http::status_code::Ok, {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700136 {"id", test_data::kClaimTicketId},
137 {"kind", "clouddevices#registrationTicket"},
138 {"oauthClientId", test_data::kClientId},
139 {"userEmail", "user@email.com"},
140 {"deviceDraft.id", test_data::kDeviceId},
141 {"deviceDraft.kind", "clouddevices#device"},
142 {"deviceDraft.channel.supportedType", "xmpp"},
143 {"robotAccountEmail", test_data::kRobotAccountEmail},
144 {"robotAccountAuthorizationCode", test_data::kRobotAccountAuthCode},
145 });
146}
147
148} // anonymous namespace
149
150// This is a helper class that allows the unit tests to set the private
151// member DeviceRegistrationInfo::ticket_id_, since TestHelper is declared
152// as a friend to DeviceRegistrationInfo.
153class DeviceRegistrationInfo::TestHelper {
154 public:
155 static void SetTestTicketId(DeviceRegistrationInfo* info) {
156 info->ticket_id_ = test_data::kClaimTicketId;
157 }
158};
159
160class DeviceRegistrationInfoTest : public ::testing::Test {
161 protected:
Alex Vakulenko5a9e7182014-08-11 15:59:58 -0700162 void SetUp() override {
Alex Vakulenko45109442014-07-29 11:07:10 -0700163 InitDefaultStorage(&data_);
164 storage_ = std::make_shared<MemStorage>();
165 storage_->Save(&data_);
Alex Vakulenkocca20932014-08-20 17:35:12 -0700166 transport_ = std::make_shared<chromeos::http::fake::Transport>();
Alex Vakulenko45109442014-07-29 11:07:10 -0700167 command_manager_ = std::make_shared<CommandManager>();
168 dev_reg_ = std::unique_ptr<DeviceRegistrationInfo>(
169 new DeviceRegistrationInfo(command_manager_, transport_, storage_));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700170 }
171
Alex Vakulenko45109442014-07-29 11:07:10 -0700172 base::DictionaryValue data_;
173 std::shared_ptr<MemStorage> storage_;
Alex Vakulenkocca20932014-08-20 17:35:12 -0700174 std::shared_ptr<chromeos::http::fake::Transport> transport_;
Alex Vakulenko45109442014-07-29 11:07:10 -0700175 std::unique_ptr<DeviceRegistrationInfo> dev_reg_;
176 std::shared_ptr<CommandManager> command_manager_;
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700177};
178
179////////////////////////////////////////////////////////////////////////////////
180TEST_F(DeviceRegistrationInfoTest, GetServiceURL) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700181 EXPECT_TRUE(dev_reg_->Load());
182 EXPECT_EQ(test_data::kServiceURL, dev_reg_->GetServiceURL());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700183 std::string url = test_data::kServiceURL;
184 url += "registrationTickets";
Alex Vakulenko45109442014-07-29 11:07:10 -0700185 EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets"));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700186 url += "?key=";
187 url += test_data::kApiKey;
Alex Vakulenko45109442014-07-29 11:07:10 -0700188 EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700189 {"key", test_data::kApiKey}
190 }));
191 url += "&restart=true";
Alex Vakulenko45109442014-07-29 11:07:10 -0700192 EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700193 {"key", test_data::kApiKey},
194 {"restart", "true"},
195 }));
196}
197
198TEST_F(DeviceRegistrationInfoTest, GetOAuthURL) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700199 EXPECT_TRUE(dev_reg_->Load());
200 EXPECT_EQ(test_data::kOAuthURL, dev_reg_->GetOAuthURL());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700201 std::string url = test_data::kOAuthURL;
202 url += "auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fclouddevices&";
203 url += "redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&";
204 url += "response_type=code&";
205 url += "client_id=";
206 url += test_data::kClientId;
Alex Vakulenko45109442014-07-29 11:07:10 -0700207 EXPECT_EQ(url, dev_reg_->GetOAuthURL("auth", {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700208 {"scope", "https://www.googleapis.com/auth/clouddevices"},
209 {"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"},
210 {"response_type", "code"},
211 {"client_id", test_data::kClientId}
212 }));
213}
214
215TEST_F(DeviceRegistrationInfoTest, CheckRegistration) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700216 EXPECT_TRUE(dev_reg_->Load());
217 EXPECT_FALSE(dev_reg_->CheckRegistration(nullptr));
218 EXPECT_EQ(0, transport_->GetRequestCount());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700219
Alex Vakulenko45109442014-07-29 11:07:10 -0700220 SetDefaultDeviceRegistration(&data_);
221 storage_->Save(&data_);
222 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700223
Alex Vakulenkocca20932014-08-20 17:35:12 -0700224 transport_->AddHandler(dev_reg_->GetOAuthURL("token"),
225 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700226 base::Bind(OAuth2Handler));
227 transport_->ResetRequestCount();
228 EXPECT_TRUE(dev_reg_->CheckRegistration(nullptr));
229 EXPECT_EQ(1, transport_->GetRequestCount());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700230}
231
232TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700233 SetDefaultDeviceRegistration(&data_);
234 storage_->Save(&data_);
235 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700236
Alex Vakulenkocca20932014-08-20 17:35:12 -0700237 transport_->AddHandler(dev_reg_->GetOAuthURL("token"),
238 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700239 base::Bind(OAuth2Handler));
Alex Vakulenkocca20932014-08-20 17:35:12 -0700240 transport_->AddHandler(dev_reg_->GetDeviceURL(),
241 chromeos::http::request_type::kGet,
Alex Vakulenko45109442014-07-29 11:07:10 -0700242 base::Bind(DeviceInfoHandler));
243 transport_->ResetRequestCount();
244 auto device_info = dev_reg_->GetDeviceInfo(nullptr);
245 EXPECT_EQ(2, transport_->GetRequestCount());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700246 EXPECT_NE(nullptr, device_info.get());
247 base::DictionaryValue* dict = nullptr;
248 EXPECT_TRUE(device_info->GetAsDictionary(&dict));
249 std::string id;
250 EXPECT_TRUE(dict->GetString("id", &id));
251 EXPECT_EQ(test_data::kDeviceId, id);
252}
253
254TEST_F(DeviceRegistrationInfoTest, GetDeviceId) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700255 SetDefaultDeviceRegistration(&data_);
256 storage_->Save(&data_);
257 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700258
Alex Vakulenkocca20932014-08-20 17:35:12 -0700259 transport_->AddHandler(dev_reg_->GetOAuthURL("token"),
260 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700261 base::Bind(OAuth2Handler));
Alex Vakulenkocca20932014-08-20 17:35:12 -0700262 transport_->AddHandler(dev_reg_->GetDeviceURL(),
263 chromeos::http::request_type::kGet,
Alex Vakulenko45109442014-07-29 11:07:10 -0700264 base::Bind(DeviceInfoHandler));
265 std::string id = dev_reg_->GetDeviceId(nullptr);
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700266 EXPECT_EQ(test_data::kDeviceId, id);
267}
268
269TEST_F(DeviceRegistrationInfoTest, StartRegistration) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700270 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700271
Alex Vakulenkocca20932014-08-20 17:35:12 -0700272 auto create_ticket = [](const ServerRequest& request,
273 ServerResponse* response) {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700274 EXPECT_EQ(test_data::kApiKey, request.GetFormField("key"));
275 auto json = request.GetDataAsJson();
276 EXPECT_NE(nullptr, json.get());
277 std::string value;
278 EXPECT_TRUE(json->GetString("deviceDraft.channel.supportedType", &value));
279 EXPECT_EQ("xmpp", value);
280 EXPECT_TRUE(json->GetString("oauthClientId", &value));
281 EXPECT_EQ(test_data::kClientId, value);
282 EXPECT_TRUE(json->GetString("deviceDraft.deviceKind", &value));
283 EXPECT_EQ("vendor", value);
Alex Vakulenko45109442014-07-29 11:07:10 -0700284 base::DictionaryValue* commandDefs = nullptr;
285 EXPECT_TRUE(json->GetDictionary("deviceDraft.commandDefs", &commandDefs));
286 EXPECT_FALSE(commandDefs->empty());
287 EXPECT_EQ("{'base':{'reboot':{'parameters':{"
288 "'delay':{'minimum':10,'type':'integer'}}}},"
289 "'robot':{'_jump':{'parameters':{"
290 "'_height':{'type':'integer'}}}}}",
291 buffet::unittests::ValueToString(commandDefs));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700292
293 base::DictionaryValue json_resp;
294 json_resp.SetString("id", test_data::kClaimTicketId);
295 json_resp.SetString("kind", "clouddevices#registrationTicket");
296 json_resp.SetString("oauthClientId", test_data::kClientId);
297 base::DictionaryValue* device_draft = nullptr;
298 EXPECT_TRUE(json->GetDictionary("deviceDraft", &device_draft));
299 device_draft = device_draft->DeepCopy();
300 device_draft->SetString("id", test_data::kDeviceId);
301 device_draft->SetString("kind", "clouddevices#device");
302 json_resp.Set("deviceDraft", device_draft);
303
Alex Vakulenkocca20932014-08-20 17:35:12 -0700304 response->ReplyJson(chromeos::http::status_code::Ok, &json_resp);
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700305 };
306
Alex Vakulenko45109442014-07-29 11:07:10 -0700307 auto json_base = buffet::unittests::CreateDictionaryValue(R"({
308 'base': {
309 'reboot': {
310 'parameters': {'delay': 'integer'}
311 },
312 'shutdown': {
313 'parameters': {}
314 }
315 }
316 })");
317 EXPECT_TRUE(command_manager_->LoadBaseCommands(*json_base, nullptr));
318 auto json_cmds = buffet::unittests::CreateDictionaryValue(R"({
319 'base': {
320 'reboot': {
321 'parameters': {'delay': {'minimum': 10}}
322 }
323 },
324 'robot': {
325 '_jump': {
326 'parameters': {'_height': 'integer'}
327 }
328 }
329 })");
330 EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, "", nullptr));
331
332 transport_->AddHandler(dev_reg_->GetServiceURL("registrationTickets"),
Alex Vakulenkocca20932014-08-20 17:35:12 -0700333 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700334 base::Bind(create_ticket));
Alex Vakulenkoa9044342014-08-23 19:31:27 -0700335 std::map<std::string, std::string> params;
Alex Vakulenko45109442014-07-29 11:07:10 -0700336 std::string json_resp = dev_reg_->StartRegistration(params, nullptr);
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700337 auto json = std::unique_ptr<base::Value>(base::JSONReader::Read(json_resp));
338 EXPECT_NE(nullptr, json.get());
339 base::DictionaryValue* dict = nullptr;
340 EXPECT_TRUE(json->GetAsDictionary(&dict));
341 std::string value;
342 EXPECT_TRUE(dict->GetString("ticket_id", &value));
343 EXPECT_EQ(test_data::kClaimTicketId, value);
344}
345
346TEST_F(DeviceRegistrationInfoTest, FinishRegistration_NoAuth) {
347 // Test finalizing ticket with no user authorization token.
348 // This assumes that a client would patch in their email separately.
Alex Vakulenko45109442014-07-29 11:07:10 -0700349 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700350
351 // General ticket finalization handler.
352 std::string ticket_url =
Alex Vakulenko45109442014-07-29 11:07:10 -0700353 dev_reg_->GetServiceURL("registrationTickets/" +
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700354 std::string(test_data::kClaimTicketId));
Alex Vakulenkocca20932014-08-20 17:35:12 -0700355 transport_->AddHandler(ticket_url + "/finalize",
356 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700357 base::Bind(FinalizeTicketHandler));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700358
Alex Vakulenkocca20932014-08-20 17:35:12 -0700359 transport_->AddHandler(dev_reg_->GetOAuthURL("token"),
360 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700361 base::Bind(OAuth2Handler));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700362
Alex Vakulenko45109442014-07-29 11:07:10 -0700363 storage_->reset_save_count();
364 DeviceRegistrationInfo::TestHelper::SetTestTicketId(dev_reg_.get());
365 EXPECT_TRUE(dev_reg_->FinishRegistration("", nullptr));
366 EXPECT_EQ(1,
367 storage_->save_count()); // The device info must have been saved.
368 EXPECT_EQ(2, transport_->GetRequestCount());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700369
370 // Validate the device info saved to storage...
Alex Vakulenko45109442014-07-29 11:07:10 -0700371 auto storage_data = storage_->Load();
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700372 base::DictionaryValue* dict = nullptr;
373 EXPECT_TRUE(storage_data->GetAsDictionary(&dict));
374 std::string value;
375 EXPECT_TRUE(dict->GetString(storage_keys::kApiKey, &value));
376 EXPECT_EQ(test_data::kApiKey, value);
377 EXPECT_TRUE(dict->GetString(storage_keys::kClientId, &value));
378 EXPECT_EQ(test_data::kClientId, value);
379 EXPECT_TRUE(dict->GetString(storage_keys::kClientSecret, &value));
380 EXPECT_EQ(test_data::kClientSecret, value);
381 EXPECT_TRUE(dict->GetString(storage_keys::kDeviceId, &value));
382 EXPECT_EQ(test_data::kDeviceId, value);
383 EXPECT_TRUE(dict->GetString(storage_keys::kOAuthURL, &value));
384 EXPECT_EQ(test_data::kOAuthURL, value);
385 EXPECT_TRUE(dict->GetString(storage_keys::kRefreshToken, &value));
386 EXPECT_EQ(test_data::kRefreshToken, value);
387 EXPECT_TRUE(dict->GetString(storage_keys::kRobotAccount, &value));
388 EXPECT_EQ(test_data::kRobotAccountEmail, value);
389 EXPECT_TRUE(dict->GetString(storage_keys::kServiceURL, &value));
390 EXPECT_EQ(test_data::kServiceURL, value);
391}
392
393TEST_F(DeviceRegistrationInfoTest, FinishRegistration_Auth) {
394 // Test finalizing ticket with user authorization token.
Alex Vakulenko45109442014-07-29 11:07:10 -0700395 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700396
397 // General ticket finalization handler.
398 std::string ticket_url =
Alex Vakulenko45109442014-07-29 11:07:10 -0700399 dev_reg_->GetServiceURL("registrationTickets/" +
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700400 std::string(test_data::kClaimTicketId));
Alex Vakulenkocca20932014-08-20 17:35:12 -0700401 transport_->AddHandler(ticket_url + "/finalize",
402 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700403 base::Bind(FinalizeTicketHandler));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700404
Alex Vakulenkocca20932014-08-20 17:35:12 -0700405 transport_->AddHandler(dev_reg_->GetOAuthURL("token"),
406 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700407 base::Bind(OAuth2Handler));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700408
409 // Handle patching in the user email onto the device record.
Alex Vakulenkocca20932014-08-20 17:35:12 -0700410 auto email_patch_handler = [](const ServerRequest& request,
411 ServerResponse* response) {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700412 std::string auth_header = "Bearer ";
413 auth_header += test_data::kUserAccessToken;
414 EXPECT_EQ(auth_header,
Alex Vakulenkocca20932014-08-20 17:35:12 -0700415 request.GetHeader(chromeos::http::request_header::kAuthorization));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700416 auto json = request.GetDataAsJson();
417 EXPECT_NE(nullptr, json.get());
418 std::string value;
419 EXPECT_TRUE(json->GetString("userEmail", &value));
420 EXPECT_EQ("me", value);
421
Alex Vakulenkocca20932014-08-20 17:35:12 -0700422 response->ReplyJson(chromeos::http::status_code::Ok, {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700423 {"id", test_data::kClaimTicketId},
424 {"kind", "clouddevices#registrationTicket"},
425 {"oauthClientId", test_data::kClientId},
426 {"userEmail", "user@email.com"},
427 {"deviceDraft.id", test_data::kDeviceId},
428 {"deviceDraft.kind", "clouddevices#device"},
429 {"deviceDraft.channel.supportedType", "xmpp"},
430 });
431 };
Alex Vakulenkocca20932014-08-20 17:35:12 -0700432 transport_->AddHandler(ticket_url, chromeos::http::request_type::kPatch,
Alex Vakulenko45109442014-07-29 11:07:10 -0700433 base::Bind(email_patch_handler));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700434
Alex Vakulenko45109442014-07-29 11:07:10 -0700435 storage_->reset_save_count();
436 DeviceRegistrationInfo::TestHelper::SetTestTicketId(dev_reg_.get());
437 EXPECT_TRUE(dev_reg_->FinishRegistration(test_data::kUserAccountAuthCode,
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700438 nullptr));
Alex Vakulenko45109442014-07-29 11:07:10 -0700439 EXPECT_EQ(1,
440 storage_->save_count()); // The device info must have been saved.
441 EXPECT_EQ(4, transport_->GetRequestCount());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700442}
Alex Vakulenkocca20932014-08-20 17:35:12 -0700443
444} // namespace buffet