buffet: Switch to new registration workflow, user-initiated.

BUG=None
TEST=None

Change-Id: I181065640b8f887dcd3b530942b1d7531f49aedd
Reviewed-on: https://chromium-review.googlesource.com/219720
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Anton Muhin <antonm@chromium.org>
Tested-by: Anton Muhin <antonm@chromium.org>
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc
index d4b22a2..3e35cec 100644
--- a/buffet/device_registration_info.cc
+++ b/buffet/device_registration_info.cc
@@ -343,31 +343,16 @@
 std::string DeviceRegistrationInfo::StartRegistration(
     const std::map<std::string, std::string>& params,
     chromeos::ErrorPtr* error) {
+  GetParamValue(params, "ticket_id", &ticket_id_);
   GetParamValue(params, storage_keys::kClientId, &client_id_);
   GetParamValue(params, storage_keys::kClientSecret, &client_secret_);
   GetParamValue(params, storage_keys::kApiKey, &api_key_);
-  GetParamValue(params, storage_keys::kDeviceId, &device_id_);
   GetParamValue(params, storage_keys::kDeviceKind, &device_kind_);
   GetParamValue(params, storage_keys::kSystemName, &system_name_);
   GetParamValue(params, storage_keys::kDisplayName, &display_name_);
   GetParamValue(params, storage_keys::kOAuthURL, &oauth_url_);
   GetParamValue(params, storage_keys::kServiceURL, &service_url_);
 
-  if (!CheckParam(storage_keys::kClientId, client_id_, error))
-    return std::string();
-  if (!CheckParam(storage_keys::kClientSecret, client_secret_, error))
-    return std::string();
-  if (!CheckParam(storage_keys::kApiKey, api_key_, error))
-    return std::string();
-  if (!CheckParam(storage_keys::kDeviceKind, device_kind_, error))
-    return std::string();
-  if (!CheckParam(storage_keys::kSystemName, system_name_, error))
-    return std::string();
-  if (!CheckParam(storage_keys::kOAuthURL, oauth_url_, error))
-    return std::string();
-  if (!CheckParam(storage_keys::kServiceURL, service_url_, error))
-    return std::string();
-
   std::unique_ptr<base::DictionaryValue> commands =
       command_manager_->GetCommandDictionary().GetCommandsAsJson(true, error);
   if (!commands)
@@ -379,26 +364,27 @@
     return std::string();
 
   base::DictionaryValue req_json;
+  req_json.SetString("id", ticket_id_);
   req_json.SetString("oauthClientId", client_id_);
   req_json.SetString("deviceDraft.deviceKind", device_kind_);
   req_json.SetString("deviceDraft.systemName", system_name_);
-  req_json.SetString("deviceDraft.displayName", display_name_);
+  if (!display_name_.empty())
+    req_json.SetString("deviceDraft.displayName", display_name_);
   req_json.SetString("deviceDraft.channel.supportedType", "xmpp");
   req_json.Set("deviceDraft.commandDefs", commands.release());
   req_json.Set("deviceDraft.state", state.release());
 
-  std::string url = GetServiceURL("registrationTickets", {{"key", api_key_}});
+  int status_code{0};
+  auto url = GetServiceURL("registrationTickets/" + ticket_id_,
+                           {{"key", api_key_}});
   auto resp_json = chromeos::http::ParseJsonResponse(
-      chromeos::http::PostJson(url, &req_json, transport_, error).get(),
-      nullptr, error);
+      chromeos::http::PatchJson(url, &req_json, transport_, error).get(),
+      &status_code, error);
   if (!resp_json)
     return std::string();
 
-  if (!resp_json->GetString("id", &ticket_id_)) {
-    chromeos::Error::AddTo(error, kErrorDomainGCD, "unexpected_response",
-                           "Device ID missing");
+  if (status_code >= chromeos::http::status_code::BadRequest)
     return std::string();
-  }
 
   std::string auth_url = GetOAuthURL("auth", {
     {"scope", "https://www.googleapis.com/auth/clouddevices"},
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h
index 90e306f..117f891 100644
--- a/buffet/device_registration_info.h
+++ b/buffet/device_registration_info.h
@@ -120,17 +120,14 @@
   // Persistent data. Some of default values for testing purposes are used.
   // TODO(avakulenko): remove these default values in the future.
   // http://crbug.com/364692
-  std::string client_id_ =
-    "583509257718-lnmeofvjef3b1tm33sbjmckfnumfvn8j.apps.googleusercontent.com";
-  std::string client_secret_ = "6fzZwQhgnsHhvYYvvFdpv5SD";
-  std::string api_key_ = "AIzaSyAp7KVig5m9g4LWWKr79mTS8sXWfUU6w9g";
+  std::string client_id_ = "58855907228.apps.googleusercontent.com";
+  std::string client_secret_ = "eHSAREAHrIqPsHBxCE9zPPBi";
+  std::string api_key_ = "AIzaSyDSq46gG-AxUnC3zoqD9COIPrjolFsMfMA";
   std::string refresh_token_;
   std::string device_id_;
   std::string device_robot_account_;
   std::string oauth_url_ = "https://accounts.google.com/o/oauth2/";
-  std::string service_url_ =
-    "https://www-googleapis-staging.sandbox.google.com/"
-    "clouddevices/v1/";
+  std::string service_url_ = "https://www.googleapis.com/clouddevices/v1/";
 
   // Transient data
   std::string access_token_;
diff --git a/buffet/device_registration_info_unittest.cc b/buffet/device_registration_info_unittest.cc
index 50eb01a..d61f502 100644
--- a/buffet/device_registration_info_unittest.cc
+++ b/buffet/device_registration_info_unittest.cc
@@ -273,12 +273,14 @@
 TEST_F(DeviceRegistrationInfoTest, StartRegistration) {
   EXPECT_TRUE(dev_reg_->Load());
 
-  auto create_ticket = [](const ServerRequest& request,
+  auto update_ticket = [](const ServerRequest& request,
                           ServerResponse* response) {
     EXPECT_EQ(test_data::kApiKey, request.GetFormField("key"));
     auto json = request.GetDataAsJson();
     EXPECT_NE(nullptr, json.get());
     std::string value;
+    EXPECT_TRUE(json->GetString("id", &value));
+    EXPECT_EQ(test_data::kClaimTicketId, value);
     EXPECT_TRUE(json->GetString("deviceDraft.channel.supportedType", &value));
     EXPECT_EQ("xmpp", value);
     EXPECT_TRUE(json->GetString("oauthClientId", &value));
@@ -333,10 +335,12 @@
   })");
   EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, "", nullptr));
 
-  transport_->AddHandler(dev_reg_->GetServiceURL("registrationTickets"),
-                         chromeos::http::request_type::kPost,
-                         base::Bind(create_ticket));
+  transport_->AddHandler(dev_reg_->GetServiceURL(
+      std::string("registrationTickets/") + test_data::kClaimTicketId),
+      chromeos::http::request_type::kPatch,
+      base::Bind(update_ticket));
   std::map<std::string, std::string> params;
+  params["ticket_id"] = test_data::kClaimTicketId;
   std::string json_resp = dev_reg_->StartRegistration(params, nullptr);
   auto json = std::unique_ptr<base::Value>(base::JSONReader::Read(json_resp));
   EXPECT_NE(nullptr, json.get());