blob: 56e294ac1fb8904a81f037f129a9de3050987959 [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
Alex Deymof6cbe322014-11-10 19:55:35 -08005#include "buffet/device_registration_info.h"
6
Alex Vakulenko8e34d392014-04-29 11:02:56 -07007#include <base/json/json_reader.h>
8#include <base/values.h>
Alex Vakulenkocf0b33a2014-08-20 15:02:10 -07009#include <chromeos/bind_lambda.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070010#include <chromeos/http/http_request.h>
11#include <chromeos/http/http_transport_fake.h>
Anton Muhin332df192014-11-22 05:59:14 +040012#include <chromeos/key_value_store.h>
Alex Vakulenko3aeea1c2014-08-20 16:33:12 -070013#include <chromeos/mime_utils.h>
Alex Vakulenko8e34d392014-04-29 11:02:56 -070014#include <gtest/gtest.h>
15
Alex Vakulenko1f30a622014-07-23 11:13:15 -070016#include "buffet/commands/command_manager.h"
Alex Vakulenko45109442014-07-29 11:07:10 -070017#include "buffet/commands/unittest_utils.h"
Alex Vakulenko8e34d392014-04-29 11:02:56 -070018#include "buffet/device_registration_storage_keys.h"
Alex Vakulenko57123b22014-10-28 13:50:16 -070019#include "buffet/states/mock_state_change_queue_interface.h"
Alex Vakulenko07216fe2014-09-19 15:31:09 -070020#include "buffet/states/state_manager.h"
Christopher Wiley006e94e2014-05-02 13:44:48 -070021#include "buffet/storage_impls.h"
Alex Vakulenko8e34d392014-04-29 11:02:56 -070022
Alex Vakulenkocca20932014-08-20 17:35:12 -070023using chromeos::http::request_header::kAuthorization;
24using chromeos::http::fake::ServerRequest;
25using chromeos::http::fake::ServerResponse;
26
27namespace buffet {
Alex Vakulenko8e34d392014-04-29 11:02:56 -070028
29namespace {
Alex Vakulenko8e34d392014-04-29 11:02:56 -070030
31namespace test_data {
32
33const char kServiceURL[] = "http://gcd.server.com/";
34const char kOAuthURL[] = "http://oauth.server.com/";
35const char kApiKey[] = "GOadRdTf9FERf0k4w6EFOof56fUJ3kFDdFL3d7f";
36const char kClientId[] = "123543821385-sfjkjshdkjhfk234sdfsdfkskd"
37 "fkjh7f.apps.googleusercontent.com";
38const char kClientSecret[] = "5sdGdGlfolGlrFKfdFlgP6FG";
39const char kDeviceId[] = "4a7ea2d1-b331-1e1f-b206-e863c7635196";
40const char kClaimTicketId[] = "RTcUE";
41const char kAccessToken[] = "ya29.1.AADtN_V-dLUM-sVZ0qVjG9Dxm5NgdS9J"
42 "Mx_JLUqhC9bED_YFjzHZtYt65ZzXCS35NMAeaVZ"
43 "Dei530-w0yE2urpQ";
44const char kRefreshToken[] = "1/zQmxR6PKNvhcxf9SjXUrCjcmCrcqRKXctc6cp"
45 "1nI-GQ";
46const char kRobotAccountAuthCode[] = "4/Mf_ujEhPejVhOq-OxW9F5cSOnWzx."
47 "YgciVjTYGscRshQV0ieZDAqiTIjMigI";
48const char kRobotAccountEmail[] = "6ed0b3f54f9bd619b942f4ad2441c252@"
49 "clouddevices.gserviceaccount.com";
50const char kUserAccountAuthCode[] = "2/sd_GD1TGFKpJOLJ34-0g5fK0fflp.GlT"
51 "I0F5g7hNtFgj5HFGOf8FlGK9eflO";
52const char kUserAccessToken[] = "sd56.4.FGDjG_F-gFGF-dFG6gGOG9Dxm5NgdS9"
53 "JMx_JLUqhC9bED_YFjLKjlkjLKJlkjLKjlKJea"
54 "VZDei530-w0yE2urpQ";
55const char kUserRefreshToken[] = "1/zQLKjlKJlkLkLKjLkjLKjLkjLjLkjl0ftc6"
56 "cp1nI-GQ";
57
58} // namespace test_data
59
60// Fill in the storage with default environment information (URLs, etc).
61void InitDefaultStorage(base::DictionaryValue* data) {
62 data->SetString(storage_keys::kClientId, test_data::kClientId);
63 data->SetString(storage_keys::kClientSecret, test_data::kClientSecret);
64 data->SetString(storage_keys::kApiKey, test_data::kApiKey);
65 data->SetString(storage_keys::kRefreshToken, "");
66 data->SetString(storage_keys::kDeviceId, "");
67 data->SetString(storage_keys::kOAuthURL, test_data::kOAuthURL);
68 data->SetString(storage_keys::kServiceURL, test_data::kServiceURL);
69 data->SetString(storage_keys::kRobotAccount, "");
70}
71
72// Add the test device registration information.
73void SetDefaultDeviceRegistration(base::DictionaryValue* data) {
74 data->SetString(storage_keys::kRefreshToken, test_data::kRefreshToken);
75 data->SetString(storage_keys::kDeviceId, test_data::kDeviceId);
76 data->SetString(storage_keys::kRobotAccount, test_data::kRobotAccountEmail);
77}
78
Alex Vakulenkocca20932014-08-20 17:35:12 -070079void OAuth2Handler(const ServerRequest& request, ServerResponse* response) {
Alex Vakulenko8e34d392014-04-29 11:02:56 -070080 base::DictionaryValue json;
81 if (request.GetFormField("grant_type") == "refresh_token") {
82 // Refresh device access token.
83 EXPECT_EQ(test_data::kRefreshToken, request.GetFormField("refresh_token"));
84 EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id"));
85 EXPECT_EQ(test_data::kClientSecret, request.GetFormField("client_secret"));
86 json.SetString("access_token", test_data::kAccessToken);
87 } else if (request.GetFormField("grant_type") == "authorization_code") {
88 // Obtain access token.
89 std::string code = request.GetFormField("code");
90 if (code == test_data::kUserAccountAuthCode) {
91 // Get user access token.
92 EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id"));
93 EXPECT_EQ(test_data::kClientSecret,
94 request.GetFormField("client_secret"));
95 EXPECT_EQ("urn:ietf:wg:oauth:2.0:oob",
96 request.GetFormField("redirect_uri"));
97 json.SetString("access_token", test_data::kUserAccessToken);
98 json.SetString("token_type", "Bearer");
99 json.SetString("refresh_token", test_data::kUserRefreshToken);
100 } else if (code == test_data::kRobotAccountAuthCode) {
101 // Get device access token.
102 EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id"));
103 EXPECT_EQ(test_data::kClientSecret,
104 request.GetFormField("client_secret"));
105 EXPECT_EQ("oob", request.GetFormField("redirect_uri"));
106 EXPECT_EQ("https://www.googleapis.com/auth/clouddevices",
107 request.GetFormField("scope"));
108 json.SetString("access_token", test_data::kAccessToken);
109 json.SetString("token_type", "Bearer");
110 json.SetString("refresh_token", test_data::kRefreshToken);
111 } else {
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700112 FAIL() << "Unexpected authorization code";
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700113 }
114 } else {
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700115 FAIL() << "Unexpected grant type";
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700116 }
117 json.SetInteger("expires_in", 3600);
Alex Vakulenkocca20932014-08-20 17:35:12 -0700118 response->ReplyJson(chromeos::http::status_code::Ok, &json);
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700119}
120
Alex Vakulenkocca20932014-08-20 17:35:12 -0700121void DeviceInfoHandler(const ServerRequest& request, ServerResponse* response) {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700122 std::string auth = "Bearer ";
123 auth += test_data::kAccessToken;
Alex Vakulenkocca20932014-08-20 17:35:12 -0700124 EXPECT_EQ(auth,
125 request.GetHeader(chromeos::http::request_header::kAuthorization));
126 response->ReplyJson(chromeos::http::status_code::Ok, {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700127 {"channel.supportedType", "xmpp"},
128 {"deviceKind", "vendor"},
129 {"id", test_data::kDeviceId},
130 {"kind", "clouddevices#device"},
131 });
132}
133
Alex Vakulenkocca20932014-08-20 17:35:12 -0700134void FinalizeTicketHandler(const ServerRequest& request,
135 ServerResponse* response) {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700136 EXPECT_EQ(test_data::kApiKey, request.GetFormField("key"));
137 EXPECT_TRUE(request.GetData().empty());
138
Alex Vakulenkocca20932014-08-20 17:35:12 -0700139 response->ReplyJson(chromeos::http::status_code::Ok, {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700140 {"id", test_data::kClaimTicketId},
141 {"kind", "clouddevices#registrationTicket"},
142 {"oauthClientId", test_data::kClientId},
143 {"userEmail", "user@email.com"},
144 {"deviceDraft.id", test_data::kDeviceId},
145 {"deviceDraft.kind", "clouddevices#device"},
146 {"deviceDraft.channel.supportedType", "xmpp"},
147 {"robotAccountEmail", test_data::kRobotAccountEmail},
148 {"robotAccountAuthorizationCode", test_data::kRobotAccountAuthCode},
149 });
150}
151
152} // anonymous namespace
153
154// This is a helper class that allows the unit tests to set the private
155// member DeviceRegistrationInfo::ticket_id_, since TestHelper is declared
156// as a friend to DeviceRegistrationInfo.
157class DeviceRegistrationInfo::TestHelper {
158 public:
159 static void SetTestTicketId(DeviceRegistrationInfo* info) {
160 info->ticket_id_ = test_data::kClaimTicketId;
161 }
162};
163
164class DeviceRegistrationInfoTest : public ::testing::Test {
165 protected:
Alex Vakulenko5a9e7182014-08-11 15:59:58 -0700166 void SetUp() override {
Alex Vakulenko45109442014-07-29 11:07:10 -0700167 InitDefaultStorage(&data_);
168 storage_ = std::make_shared<MemStorage>();
169 storage_->Save(&data_);
Alex Vakulenkocca20932014-08-20 17:35:12 -0700170 transport_ = std::make_shared<chromeos::http::fake::Transport>();
Alex Vakulenko45109442014-07-29 11:07:10 -0700171 command_manager_ = std::make_shared<CommandManager>();
Alex Vakulenko57123b22014-10-28 13:50:16 -0700172 state_manager_ = std::make_shared<StateManager>(&mock_state_change_queue_);
Anton Muhin332df192014-11-22 05:59:14 +0400173 std::unique_ptr<chromeos::KeyValueStore> config_store{
174 new chromeos::KeyValueStore};
175 config_store->SetString("client_id", test_data::kClientId);
176 config_store->SetString("client_secret", test_data::kClientSecret);
177 config_store->SetString("api_key", test_data::kApiKey);
178 config_store->SetString("device_kind", "vendor");
179 config_store->SetString("name", "coffee_pot");
180 config_store->SetString("display_name", "Coffee Pot");
181 config_store->SetString("description", "Easy to clean");
182 config_store->SetString("location", "Kitchen");
183 config_store->SetString("oauth_url", test_data::kOAuthURL);
184 config_store->SetString("service_url", test_data::kServiceURL);
Alex Vakulenko45109442014-07-29 11:07:10 -0700185 dev_reg_ = std::unique_ptr<DeviceRegistrationInfo>(
Alex Vakulenko07216fe2014-09-19 15:31:09 -0700186 new DeviceRegistrationInfo(command_manager_, state_manager_,
Anton Muhin332df192014-11-22 05:59:14 +0400187 std::move(config_store),
Alex Vakulenko07216fe2014-09-19 15:31:09 -0700188 transport_, storage_));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700189 }
190
Alex Vakulenko45109442014-07-29 11:07:10 -0700191 base::DictionaryValue data_;
192 std::shared_ptr<MemStorage> storage_;
Alex Vakulenkocca20932014-08-20 17:35:12 -0700193 std::shared_ptr<chromeos::http::fake::Transport> transport_;
Alex Vakulenko45109442014-07-29 11:07:10 -0700194 std::unique_ptr<DeviceRegistrationInfo> dev_reg_;
195 std::shared_ptr<CommandManager> command_manager_;
Alex Vakulenko57123b22014-10-28 13:50:16 -0700196 testing::NiceMock<MockStateChangeQueueInterface> mock_state_change_queue_;
Alex Vakulenko07216fe2014-09-19 15:31:09 -0700197 std::shared_ptr<StateManager> state_manager_;
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700198};
199
200////////////////////////////////////////////////////////////////////////////////
201TEST_F(DeviceRegistrationInfoTest, GetServiceURL) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700202 EXPECT_TRUE(dev_reg_->Load());
203 EXPECT_EQ(test_data::kServiceURL, dev_reg_->GetServiceURL());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700204 std::string url = test_data::kServiceURL;
205 url += "registrationTickets";
Alex Vakulenko45109442014-07-29 11:07:10 -0700206 EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets"));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700207 url += "?key=";
208 url += test_data::kApiKey;
Alex Vakulenko45109442014-07-29 11:07:10 -0700209 EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700210 {"key", test_data::kApiKey}
211 }));
212 url += "&restart=true";
Alex Vakulenko45109442014-07-29 11:07:10 -0700213 EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700214 {"key", test_data::kApiKey},
215 {"restart", "true"},
216 }));
217}
218
219TEST_F(DeviceRegistrationInfoTest, GetOAuthURL) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700220 EXPECT_TRUE(dev_reg_->Load());
221 EXPECT_EQ(test_data::kOAuthURL, dev_reg_->GetOAuthURL());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700222 std::string url = test_data::kOAuthURL;
223 url += "auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fclouddevices&";
224 url += "redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&";
225 url += "response_type=code&";
226 url += "client_id=";
227 url += test_data::kClientId;
Alex Vakulenko45109442014-07-29 11:07:10 -0700228 EXPECT_EQ(url, dev_reg_->GetOAuthURL("auth", {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700229 {"scope", "https://www.googleapis.com/auth/clouddevices"},
230 {"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"},
231 {"response_type", "code"},
232 {"client_id", test_data::kClientId}
233 }));
234}
235
236TEST_F(DeviceRegistrationInfoTest, CheckRegistration) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700237 EXPECT_TRUE(dev_reg_->Load());
238 EXPECT_FALSE(dev_reg_->CheckRegistration(nullptr));
239 EXPECT_EQ(0, transport_->GetRequestCount());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700240
Alex Vakulenko45109442014-07-29 11:07:10 -0700241 SetDefaultDeviceRegistration(&data_);
242 storage_->Save(&data_);
243 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700244
Alex Vakulenkocca20932014-08-20 17:35:12 -0700245 transport_->AddHandler(dev_reg_->GetOAuthURL("token"),
246 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700247 base::Bind(OAuth2Handler));
248 transport_->ResetRequestCount();
249 EXPECT_TRUE(dev_reg_->CheckRegistration(nullptr));
250 EXPECT_EQ(1, transport_->GetRequestCount());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700251}
252
253TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700254 SetDefaultDeviceRegistration(&data_);
255 storage_->Save(&data_);
256 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700257
Alex Vakulenkocca20932014-08-20 17:35:12 -0700258 transport_->AddHandler(dev_reg_->GetOAuthURL("token"),
259 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700260 base::Bind(OAuth2Handler));
Alex Vakulenkocca20932014-08-20 17:35:12 -0700261 transport_->AddHandler(dev_reg_->GetDeviceURL(),
262 chromeos::http::request_type::kGet,
Alex Vakulenko45109442014-07-29 11:07:10 -0700263 base::Bind(DeviceInfoHandler));
264 transport_->ResetRequestCount();
265 auto device_info = dev_reg_->GetDeviceInfo(nullptr);
266 EXPECT_EQ(2, transport_->GetRequestCount());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700267 EXPECT_NE(nullptr, device_info.get());
268 base::DictionaryValue* dict = nullptr;
269 EXPECT_TRUE(device_info->GetAsDictionary(&dict));
270 std::string id;
271 EXPECT_TRUE(dict->GetString("id", &id));
272 EXPECT_EQ(test_data::kDeviceId, id);
273}
274
275TEST_F(DeviceRegistrationInfoTest, GetDeviceId) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700276 SetDefaultDeviceRegistration(&data_);
277 storage_->Save(&data_);
278 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700279
Alex Vakulenkocca20932014-08-20 17:35:12 -0700280 transport_->AddHandler(dev_reg_->GetOAuthURL("token"),
281 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700282 base::Bind(OAuth2Handler));
Alex Vakulenkocca20932014-08-20 17:35:12 -0700283 transport_->AddHandler(dev_reg_->GetDeviceURL(),
284 chromeos::http::request_type::kGet,
Alex Vakulenko45109442014-07-29 11:07:10 -0700285 base::Bind(DeviceInfoHandler));
286 std::string id = dev_reg_->GetDeviceId(nullptr);
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700287 EXPECT_EQ(test_data::kDeviceId, id);
288}
289
Anton Muhinbeb1c5b2014-10-16 18:59:57 +0400290TEST_F(DeviceRegistrationInfoTest, RegisterDevice) {
Alex Vakulenko45109442014-07-29 11:07:10 -0700291 EXPECT_TRUE(dev_reg_->Load());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700292
Anton Muhina4803142014-09-24 19:30:45 +0400293 auto update_ticket = [](const ServerRequest& request,
Alex Vakulenkocca20932014-08-20 17:35:12 -0700294 ServerResponse* response) {
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700295 EXPECT_EQ(test_data::kApiKey, request.GetFormField("key"));
296 auto json = request.GetDataAsJson();
297 EXPECT_NE(nullptr, json.get());
298 std::string value;
Anton Muhina4803142014-09-24 19:30:45 +0400299 EXPECT_TRUE(json->GetString("id", &value));
300 EXPECT_EQ(test_data::kClaimTicketId, value);
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700301 EXPECT_TRUE(json->GetString("deviceDraft.channel.supportedType", &value));
302 EXPECT_EQ("xmpp", value);
303 EXPECT_TRUE(json->GetString("oauthClientId", &value));
304 EXPECT_EQ(test_data::kClientId, value);
305 EXPECT_TRUE(json->GetString("deviceDraft.deviceKind", &value));
306 EXPECT_EQ("vendor", value);
Vitaly Bukad3eb19c2014-11-21 13:39:43 -0800307 EXPECT_TRUE(json->GetString("deviceDraft.description", &value));
308 EXPECT_EQ("Easy to clean", value);
309 EXPECT_TRUE(json->GetString("deviceDraft.location", &value));
310 EXPECT_EQ("Kitchen", value);
311 EXPECT_TRUE(json->GetString("deviceDraft.displayName", &value));
312 EXPECT_EQ("Coffee Pot", value);
Alex Vakulenko45109442014-07-29 11:07:10 -0700313 base::DictionaryValue* commandDefs = nullptr;
314 EXPECT_TRUE(json->GetDictionary("deviceDraft.commandDefs", &commandDefs));
315 EXPECT_FALSE(commandDefs->empty());
316 EXPECT_EQ("{'base':{'reboot':{'parameters':{"
317 "'delay':{'minimum':10,'type':'integer'}}}},"
318 "'robot':{'_jump':{'parameters':{"
319 "'_height':{'type':'integer'}}}}}",
320 buffet::unittests::ValueToString(commandDefs));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700321
322 base::DictionaryValue json_resp;
323 json_resp.SetString("id", test_data::kClaimTicketId);
324 json_resp.SetString("kind", "clouddevices#registrationTicket");
325 json_resp.SetString("oauthClientId", test_data::kClientId);
326 base::DictionaryValue* device_draft = nullptr;
327 EXPECT_TRUE(json->GetDictionary("deviceDraft", &device_draft));
328 device_draft = device_draft->DeepCopy();
329 device_draft->SetString("id", test_data::kDeviceId);
330 device_draft->SetString("kind", "clouddevices#device");
331 json_resp.Set("deviceDraft", device_draft);
332
Alex Vakulenkocca20932014-08-20 17:35:12 -0700333 response->ReplyJson(chromeos::http::status_code::Ok, &json_resp);
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700334 };
335
Alex Vakulenko45109442014-07-29 11:07:10 -0700336 auto json_base = buffet::unittests::CreateDictionaryValue(R"({
337 'base': {
338 'reboot': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400339 'parameters': {'delay': 'integer'},
340 'results': {}
Alex Vakulenko45109442014-07-29 11:07:10 -0700341 },
342 'shutdown': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400343 'parameters': {},
344 'results': {}
Alex Vakulenko45109442014-07-29 11:07:10 -0700345 }
346 }
347 })");
348 EXPECT_TRUE(command_manager_->LoadBaseCommands(*json_base, nullptr));
349 auto json_cmds = buffet::unittests::CreateDictionaryValue(R"({
350 'base': {
351 'reboot': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400352 'parameters': {'delay': {'minimum': 10}},
353 'results': {}
Alex Vakulenko45109442014-07-29 11:07:10 -0700354 }
355 },
356 'robot': {
357 '_jump': {
Anton Muhin71fb9d52014-11-21 22:22:39 +0400358 'parameters': {'_height': 'integer'},
359 'results': {}
Alex Vakulenko45109442014-07-29 11:07:10 -0700360 }
361 }
362 })");
363 EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, "", nullptr));
364
Anton Muhina4803142014-09-24 19:30:45 +0400365 transport_->AddHandler(dev_reg_->GetServiceURL(
366 std::string("registrationTickets/") + test_data::kClaimTicketId),
367 chromeos::http::request_type::kPatch,
368 base::Bind(update_ticket));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700369 std::string ticket_url =
Alex Vakulenko45109442014-07-29 11:07:10 -0700370 dev_reg_->GetServiceURL("registrationTickets/" +
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700371 std::string(test_data::kClaimTicketId));
Alex Vakulenkocca20932014-08-20 17:35:12 -0700372 transport_->AddHandler(ticket_url + "/finalize",
373 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700374 base::Bind(FinalizeTicketHandler));
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700375
Alex Vakulenkocca20932014-08-20 17:35:12 -0700376 transport_->AddHandler(dev_reg_->GetOAuthURL("token"),
377 chromeos::http::request_type::kPost,
Alex Vakulenko45109442014-07-29 11:07:10 -0700378 base::Bind(OAuth2Handler));
Alex Vakulenko45109442014-07-29 11:07:10 -0700379 storage_->reset_save_count();
380 DeviceRegistrationInfo::TestHelper::SetTestTicketId(dev_reg_.get());
Anton Muhinbeb1c5b2014-10-16 18:59:57 +0400381
382 std::map<std::string, std::string> params;
383 params["ticket_id"] = test_data::kClaimTicketId;
384 std::string device_id = dev_reg_->RegisterDevice(params, nullptr);
385
386 EXPECT_EQ(test_data::kDeviceId, device_id);
Alex Vakulenko45109442014-07-29 11:07:10 -0700387 EXPECT_EQ(1,
388 storage_->save_count()); // The device info must have been saved.
Anton Muhinbeb1c5b2014-10-16 18:59:57 +0400389 EXPECT_EQ(3, transport_->GetRequestCount());
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700390
391 // Validate the device info saved to storage...
Alex Vakulenko45109442014-07-29 11:07:10 -0700392 auto storage_data = storage_->Load();
Alex Vakulenko8e34d392014-04-29 11:02:56 -0700393 base::DictionaryValue* dict = nullptr;
394 EXPECT_TRUE(storage_data->GetAsDictionary(&dict));
395 std::string value;
396 EXPECT_TRUE(dict->GetString(storage_keys::kApiKey, &value));
397 EXPECT_EQ(test_data::kApiKey, value);
398 EXPECT_TRUE(dict->GetString(storage_keys::kClientId, &value));
399 EXPECT_EQ(test_data::kClientId, value);
400 EXPECT_TRUE(dict->GetString(storage_keys::kClientSecret, &value));
401 EXPECT_EQ(test_data::kClientSecret, value);
402 EXPECT_TRUE(dict->GetString(storage_keys::kDeviceId, &value));
403 EXPECT_EQ(test_data::kDeviceId, value);
404 EXPECT_TRUE(dict->GetString(storage_keys::kOAuthURL, &value));
405 EXPECT_EQ(test_data::kOAuthURL, value);
406 EXPECT_TRUE(dict->GetString(storage_keys::kRefreshToken, &value));
407 EXPECT_EQ(test_data::kRefreshToken, value);
408 EXPECT_TRUE(dict->GetString(storage_keys::kRobotAccount, &value));
409 EXPECT_EQ(test_data::kRobotAccountEmail, value);
410 EXPECT_TRUE(dict->GetString(storage_keys::kServiceURL, &value));
411 EXPECT_EQ(test_data::kServiceURL, value);
412}
413
Alex Vakulenkocca20932014-08-20 17:35:12 -0700414} // namespace buffet