Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 1 | // 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 Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "buffet/device_registration_info.h" |
| 6 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 7 | #include <base/json/json_reader.h> |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 8 | #include <base/json/json_writer.h> |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 9 | #include <base/message_loop/message_loop.h> |
| 10 | #include <base/run_loop.h> |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 11 | #include <base/values.h> |
Alex Vakulenko | cf0b33a | 2014-08-20 15:02:10 -0700 | [diff] [blame] | 12 | #include <chromeos/bind_lambda.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 13 | #include <chromeos/http/http_request.h> |
| 14 | #include <chromeos/http/http_transport_fake.h> |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 15 | #include <chromeos/key_value_store.h> |
Alex Vakulenko | 3aeea1c | 2014-08-20 16:33:12 -0700 | [diff] [blame] | 16 | #include <chromeos/mime_utils.h> |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
| 18 | |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 19 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 20 | #include "buffet/commands/unittest_utils.h" |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 21 | #include "buffet/states/mock_state_change_queue_interface.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 22 | #include "buffet/states/state_manager.h" |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 23 | #include "buffet/storage_impls.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 24 | |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 25 | namespace buffet { |
| 26 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 27 | using chromeos::http::request_header::kAuthorization; |
| 28 | using chromeos::http::fake::ServerRequest; |
| 29 | using chromeos::http::fake::ServerResponse; |
| 30 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 31 | namespace { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 32 | |
| 33 | namespace test_data { |
| 34 | |
| 35 | const char kServiceURL[] = "http://gcd.server.com/"; |
| 36 | const char kOAuthURL[] = "http://oauth.server.com/"; |
| 37 | const char kApiKey[] = "GOadRdTf9FERf0k4w6EFOof56fUJ3kFDdFL3d7f"; |
| 38 | const char kClientId[] = "123543821385-sfjkjshdkjhfk234sdfsdfkskd" |
| 39 | "fkjh7f.apps.googleusercontent.com"; |
| 40 | const char kClientSecret[] = "5sdGdGlfolGlrFKfdFlgP6FG"; |
| 41 | const char kDeviceId[] = "4a7ea2d1-b331-1e1f-b206-e863c7635196"; |
| 42 | const char kClaimTicketId[] = "RTcUE"; |
| 43 | const char kAccessToken[] = "ya29.1.AADtN_V-dLUM-sVZ0qVjG9Dxm5NgdS9J" |
| 44 | "Mx_JLUqhC9bED_YFjzHZtYt65ZzXCS35NMAeaVZ" |
| 45 | "Dei530-w0yE2urpQ"; |
| 46 | const char kRefreshToken[] = "1/zQmxR6PKNvhcxf9SjXUrCjcmCrcqRKXctc6cp" |
| 47 | "1nI-GQ"; |
| 48 | const char kRobotAccountAuthCode[] = "4/Mf_ujEhPejVhOq-OxW9F5cSOnWzx." |
| 49 | "YgciVjTYGscRshQV0ieZDAqiTIjMigI"; |
| 50 | const char kRobotAccountEmail[] = "6ed0b3f54f9bd619b942f4ad2441c252@" |
| 51 | "clouddevices.gserviceaccount.com"; |
| 52 | const char kUserAccountAuthCode[] = "2/sd_GD1TGFKpJOLJ34-0g5fK0fflp.GlT" |
| 53 | "I0F5g7hNtFgj5HFGOf8FlGK9eflO"; |
| 54 | const char kUserAccessToken[] = "sd56.4.FGDjG_F-gFGF-dFG6gGOG9Dxm5NgdS9" |
| 55 | "JMx_JLUqhC9bED_YFjLKjlkjLKJlkjLKjlKJea" |
| 56 | "VZDei530-w0yE2urpQ"; |
| 57 | const char kUserRefreshToken[] = "1/zQLKjlKJlkLkLKjLkjLKjLkjLjLkjl0ftc6" |
| 58 | "cp1nI-GQ"; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 59 | } // namespace test_data |
| 60 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 61 | // Add the test device registration information. |
| 62 | void SetDefaultDeviceRegistration(base::DictionaryValue* data) { |
Vitaly Buka | cad2e33 | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 63 | data->SetString("refresh_token", test_data::kRefreshToken); |
| 64 | data->SetString("device_id", test_data::kDeviceId); |
| 65 | data->SetString("robot_account", test_data::kRobotAccountEmail); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 68 | void OAuth2Handler(const ServerRequest& request, ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 69 | base::DictionaryValue json; |
| 70 | if (request.GetFormField("grant_type") == "refresh_token") { |
| 71 | // Refresh device access token. |
| 72 | EXPECT_EQ(test_data::kRefreshToken, request.GetFormField("refresh_token")); |
| 73 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 74 | EXPECT_EQ(test_data::kClientSecret, request.GetFormField("client_secret")); |
| 75 | json.SetString("access_token", test_data::kAccessToken); |
| 76 | } else if (request.GetFormField("grant_type") == "authorization_code") { |
| 77 | // Obtain access token. |
| 78 | std::string code = request.GetFormField("code"); |
| 79 | if (code == test_data::kUserAccountAuthCode) { |
| 80 | // Get user access token. |
| 81 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 82 | EXPECT_EQ(test_data::kClientSecret, |
| 83 | request.GetFormField("client_secret")); |
| 84 | EXPECT_EQ("urn:ietf:wg:oauth:2.0:oob", |
| 85 | request.GetFormField("redirect_uri")); |
| 86 | json.SetString("access_token", test_data::kUserAccessToken); |
| 87 | json.SetString("token_type", "Bearer"); |
| 88 | json.SetString("refresh_token", test_data::kUserRefreshToken); |
| 89 | } else if (code == test_data::kRobotAccountAuthCode) { |
| 90 | // Get device access token. |
| 91 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 92 | EXPECT_EQ(test_data::kClientSecret, |
| 93 | request.GetFormField("client_secret")); |
| 94 | EXPECT_EQ("oob", request.GetFormField("redirect_uri")); |
| 95 | EXPECT_EQ("https://www.googleapis.com/auth/clouddevices", |
| 96 | request.GetFormField("scope")); |
| 97 | json.SetString("access_token", test_data::kAccessToken); |
| 98 | json.SetString("token_type", "Bearer"); |
| 99 | json.SetString("refresh_token", test_data::kRefreshToken); |
| 100 | } else { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 101 | FAIL() << "Unexpected authorization code"; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 102 | } |
| 103 | } else { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 104 | FAIL() << "Unexpected grant type"; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 105 | } |
| 106 | json.SetInteger("expires_in", 3600); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 107 | response->ReplyJson(chromeos::http::status_code::Ok, &json); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 110 | void OAuth2HandlerFail(const ServerRequest& request, |
| 111 | ServerResponse* response) { |
| 112 | base::DictionaryValue json; |
| 113 | EXPECT_EQ("refresh_token", request.GetFormField("grant_type")); |
| 114 | EXPECT_EQ(test_data::kRefreshToken, request.GetFormField("refresh_token")); |
| 115 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 116 | EXPECT_EQ(test_data::kClientSecret, request.GetFormField("client_secret")); |
| 117 | json.SetString("error", "unable_to_authenticate"); |
| 118 | response->ReplyJson(chromeos::http::status_code::BadRequest, &json); |
| 119 | } |
| 120 | |
| 121 | void OAuth2HandlerDeregister(const ServerRequest& request, |
| 122 | ServerResponse* response) { |
| 123 | base::DictionaryValue json; |
| 124 | EXPECT_EQ("refresh_token", request.GetFormField("grant_type")); |
| 125 | EXPECT_EQ(test_data::kRefreshToken, request.GetFormField("refresh_token")); |
| 126 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 127 | EXPECT_EQ(test_data::kClientSecret, request.GetFormField("client_secret")); |
| 128 | json.SetString("error", "invalid_grant"); |
| 129 | response->ReplyJson(chromeos::http::status_code::BadRequest, &json); |
| 130 | } |
| 131 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 132 | void DeviceInfoHandler(const ServerRequest& request, ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 133 | std::string auth = "Bearer "; |
| 134 | auth += test_data::kAccessToken; |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 135 | EXPECT_EQ(auth, |
| 136 | request.GetHeader(chromeos::http::request_header::kAuthorization)); |
| 137 | response->ReplyJson(chromeos::http::status_code::Ok, { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 138 | {"channel.supportedType", "xmpp"}, |
| 139 | {"deviceKind", "vendor"}, |
| 140 | {"id", test_data::kDeviceId}, |
| 141 | {"kind", "clouddevices#device"}, |
| 142 | }); |
| 143 | } |
| 144 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 145 | void FinalizeTicketHandler(const ServerRequest& request, |
| 146 | ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 147 | EXPECT_EQ(test_data::kApiKey, request.GetFormField("key")); |
| 148 | EXPECT_TRUE(request.GetData().empty()); |
| 149 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 150 | response->ReplyJson(chromeos::http::status_code::Ok, { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 151 | {"id", test_data::kClaimTicketId}, |
| 152 | {"kind", "clouddevices#registrationTicket"}, |
| 153 | {"oauthClientId", test_data::kClientId}, |
| 154 | {"userEmail", "user@email.com"}, |
| 155 | {"deviceDraft.id", test_data::kDeviceId}, |
| 156 | {"deviceDraft.kind", "clouddevices#device"}, |
| 157 | {"deviceDraft.channel.supportedType", "xmpp"}, |
| 158 | {"robotAccountEmail", test_data::kRobotAccountEmail}, |
| 159 | {"robotAccountAuthorizationCode", test_data::kRobotAccountAuthCode}, |
| 160 | }); |
| 161 | } |
| 162 | |
| 163 | } // anonymous namespace |
| 164 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 165 | class DeviceRegistrationInfoTest : public ::testing::Test { |
| 166 | protected: |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 167 | void SetUp() override { |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 168 | std::unique_ptr<StorageInterface> storage{new MemStorage}; |
| 169 | storage_ = storage.get(); |
| 170 | storage->Save(data_); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 171 | transport_ = std::make_shared<chromeos::http::fake::Transport>(); |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 172 | command_manager_ = std::make_shared<CommandManager>(); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 173 | state_manager_ = std::make_shared<StateManager>(&mock_state_change_queue_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 174 | |
| 175 | std::unique_ptr<BuffetConfig> config{new BuffetConfig{std::move(storage)}}; |
| 176 | config_ = config.get(); |
Vitaly Buka | 63cc3d2 | 2015-06-23 20:11:36 -0700 | [diff] [blame] | 177 | dev_reg_.reset(new DeviceRegistrationInfo{command_manager_, state_manager_, |
| 178 | std::move(config), transport_, |
| 179 | true, nullptr}); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 180 | |
| 181 | ReloadConfig(); |
| 182 | } |
| 183 | |
| 184 | void ReloadConfig() { |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 185 | chromeos::KeyValueStore config_store; |
| 186 | config_store.SetString("client_id", test_data::kClientId); |
| 187 | config_store.SetString("client_secret", test_data::kClientSecret); |
| 188 | config_store.SetString("api_key", test_data::kApiKey); |
| 189 | config_store.SetString("device_kind", "vendor"); |
Alex Vakulenko | 468f7f3 | 2015-04-08 10:42:45 -0700 | [diff] [blame] | 190 | config_store.SetString("name", "Coffee Pot"); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 191 | config_store.SetString("description", "Easy to clean"); |
| 192 | config_store.SetString("location", "Kitchen"); |
Vitaly Buka | 4a3a9a2 | 2015-05-13 16:06:01 -0700 | [diff] [blame] | 193 | config_store.SetString("local_anonymous_access_role", "viewer"); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 194 | config_store.SetString("model_id", "AAAAA"); |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 195 | config_store.SetString("oauth_url", test_data::kOAuthURL); |
| 196 | config_store.SetString("service_url", test_data::kServiceURL); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 197 | config_->Load(config_store); |
| 198 | dev_reg_->Start(); |
| 199 | } |
| 200 | |
| 201 | void PublishCommands(const base::ListValue& commands) { |
| 202 | return dev_reg_->PublishCommands(commands); |
| 203 | } |
| 204 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 205 | bool RefreshAccessToken(chromeos::ErrorPtr* error) const { |
| 206 | base::MessageLoopForIO message_loop; |
| 207 | base::RunLoop run_loop; |
| 208 | |
| 209 | bool succeeded = false; |
| 210 | auto on_success = [&run_loop, &succeeded]() { |
| 211 | succeeded = true; |
| 212 | run_loop.Quit(); |
| 213 | }; |
| 214 | auto on_failure = [&run_loop, &error](const chromeos::Error* in_error) { |
| 215 | if (error) |
| 216 | *error = in_error->Clone(); |
| 217 | run_loop.Quit(); |
| 218 | }; |
| 219 | dev_reg_->RefreshAccessToken(base::Bind(on_success), |
| 220 | base::Bind(on_failure)); |
| 221 | run_loop.Run(); |
| 222 | return succeeded; |
| 223 | } |
| 224 | |
| 225 | void SetAccessToken() { |
| 226 | dev_reg_->access_token_ = test_data::kAccessToken; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | RegistrationStatus GetRegistrationStatus() const { |
| 230 | return dev_reg_->registration_status_; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 233 | base::DictionaryValue data_; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 234 | StorageInterface* storage_{nullptr}; |
| 235 | BuffetConfig* config_{nullptr}; |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 236 | std::shared_ptr<chromeos::http::fake::Transport> transport_; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 237 | std::unique_ptr<DeviceRegistrationInfo> dev_reg_; |
| 238 | std::shared_ptr<CommandManager> command_manager_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 239 | testing::NiceMock<MockStateChangeQueueInterface> mock_state_change_queue_; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 240 | std::shared_ptr<StateManager> state_manager_; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | //////////////////////////////////////////////////////////////////////////////// |
| 244 | TEST_F(DeviceRegistrationInfoTest, GetServiceURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 245 | EXPECT_EQ(test_data::kServiceURL, dev_reg_->GetServiceURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 246 | std::string url = test_data::kServiceURL; |
| 247 | url += "registrationTickets"; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 248 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets")); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 249 | url += "?key="; |
| 250 | url += test_data::kApiKey; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 251 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 252 | {"key", test_data::kApiKey} |
| 253 | })); |
| 254 | url += "&restart=true"; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 255 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 256 | {"key", test_data::kApiKey}, |
| 257 | {"restart", "true"}, |
| 258 | })); |
| 259 | } |
| 260 | |
| 261 | TEST_F(DeviceRegistrationInfoTest, GetOAuthURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 262 | EXPECT_EQ(test_data::kOAuthURL, dev_reg_->GetOAuthURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 263 | std::string url = test_data::kOAuthURL; |
| 264 | url += "auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fclouddevices&"; |
| 265 | url += "redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&"; |
| 266 | url += "response_type=code&"; |
| 267 | url += "client_id="; |
| 268 | url += test_data::kClientId; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 269 | EXPECT_EQ(url, dev_reg_->GetOAuthURL("auth", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 270 | {"scope", "https://www.googleapis.com/auth/clouddevices"}, |
| 271 | {"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"}, |
| 272 | {"response_type", "code"}, |
| 273 | {"client_id", test_data::kClientId} |
| 274 | })); |
| 275 | } |
| 276 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 277 | TEST_F(DeviceRegistrationInfoTest, HaveRegistrationCredentials) { |
| 278 | EXPECT_FALSE(dev_reg_->HaveRegistrationCredentials(nullptr)); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 279 | EXPECT_EQ(0, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 280 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 281 | SetDefaultDeviceRegistration(&data_); |
Vitaly Buka | 5636322 | 2015-05-13 15:09:21 -0700 | [diff] [blame] | 282 | storage_->Save(data_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 283 | ReloadConfig(); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 284 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 285 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 286 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 287 | base::Bind(OAuth2Handler)); |
| 288 | transport_->ResetRequestCount(); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 289 | EXPECT_TRUE(RefreshAccessToken(nullptr)); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 290 | EXPECT_EQ(1, transport_->GetRequestCount()); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 291 | EXPECT_TRUE(dev_reg_->HaveRegistrationCredentials(nullptr)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 294 | TEST_F(DeviceRegistrationInfoTest, CheckAuthenticationFailure) { |
| 295 | SetDefaultDeviceRegistration(&data_); |
Vitaly Buka | 5636322 | 2015-05-13 15:09:21 -0700 | [diff] [blame] | 296 | storage_->Save(data_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 297 | ReloadConfig(); |
| 298 | EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 299 | |
| 300 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 301 | chromeos::http::request_type::kPost, |
| 302 | base::Bind(OAuth2HandlerFail)); |
| 303 | transport_->ResetRequestCount(); |
| 304 | chromeos::ErrorPtr error; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 305 | EXPECT_FALSE(RefreshAccessToken(&error)); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 306 | EXPECT_EQ(1, transport_->GetRequestCount()); |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 307 | EXPECT_TRUE(error->HasError(kErrorDomainOAuth2, "unable_to_authenticate")); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 308 | EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST_F(DeviceRegistrationInfoTest, CheckDeregistration) { |
| 312 | SetDefaultDeviceRegistration(&data_); |
Vitaly Buka | 5636322 | 2015-05-13 15:09:21 -0700 | [diff] [blame] | 313 | storage_->Save(data_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 314 | ReloadConfig(); |
| 315 | EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 316 | |
| 317 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 318 | chromeos::http::request_type::kPost, |
| 319 | base::Bind(OAuth2HandlerDeregister)); |
| 320 | transport_->ResetRequestCount(); |
| 321 | chromeos::ErrorPtr error; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 322 | EXPECT_FALSE(RefreshAccessToken(&error)); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 323 | EXPECT_EQ(1, transport_->GetRequestCount()); |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 324 | EXPECT_TRUE(error->HasError(kErrorDomainOAuth2, "invalid_grant")); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 325 | EXPECT_EQ(RegistrationStatus::kInvalidCredentials, GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 328 | TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 329 | SetDefaultDeviceRegistration(&data_); |
Vitaly Buka | 5636322 | 2015-05-13 15:09:21 -0700 | [diff] [blame] | 330 | storage_->Save(data_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 331 | ReloadConfig(); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 332 | SetAccessToken(); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 333 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 334 | transport_->AddHandler(dev_reg_->GetDeviceURL(), |
| 335 | chromeos::http::request_type::kGet, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 336 | base::Bind(DeviceInfoHandler)); |
| 337 | transport_->ResetRequestCount(); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 338 | base::MessageLoopForIO message_loop; |
| 339 | base::RunLoop run_loop; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 340 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 341 | bool succeeded = false; |
| 342 | auto on_success = |
| 343 | [&run_loop, &succeeded, this](const base::DictionaryValue& info) { |
| 344 | EXPECT_EQ(1, transport_->GetRequestCount()); |
| 345 | std::string id; |
| 346 | EXPECT_TRUE(info.GetString("id", &id)); |
| 347 | EXPECT_EQ(test_data::kDeviceId, id); |
| 348 | succeeded = true; |
| 349 | run_loop.Quit(); |
| 350 | }; |
| 351 | auto on_failure = [&run_loop](const chromeos::Error* error) { |
| 352 | run_loop.Quit(); |
| 353 | FAIL() << "Should not be called"; |
| 354 | }; |
| 355 | dev_reg_->GetDeviceInfo(base::Bind(on_success), base::Bind(on_failure)); |
| 356 | run_loop.Run(); |
| 357 | EXPECT_TRUE(succeeded); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 360 | TEST_F(DeviceRegistrationInfoTest, RegisterDevice) { |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 361 | auto update_ticket = [](const ServerRequest& request, |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 362 | ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 363 | EXPECT_EQ(test_data::kApiKey, request.GetFormField("key")); |
| 364 | auto json = request.GetDataAsJson(); |
| 365 | EXPECT_NE(nullptr, json.get()); |
| 366 | std::string value; |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 367 | EXPECT_TRUE(json->GetString("id", &value)); |
| 368 | EXPECT_EQ(test_data::kClaimTicketId, value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 369 | EXPECT_TRUE(json->GetString("deviceDraft.channel.supportedType", &value)); |
Alex Vakulenko | d05725f | 2015-05-27 15:48:19 -0700 | [diff] [blame] | 370 | EXPECT_EQ("pull", value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 371 | EXPECT_TRUE(json->GetString("oauthClientId", &value)); |
| 372 | EXPECT_EQ(test_data::kClientId, value); |
| 373 | EXPECT_TRUE(json->GetString("deviceDraft.deviceKind", &value)); |
| 374 | EXPECT_EQ("vendor", value); |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 375 | EXPECT_TRUE(json->GetString("deviceDraft.description", &value)); |
| 376 | EXPECT_EQ("Easy to clean", value); |
| 377 | EXPECT_TRUE(json->GetString("deviceDraft.location", &value)); |
| 378 | EXPECT_EQ("Kitchen", value); |
Vitaly Buka | 6522ab6 | 2015-02-19 10:26:31 -0800 | [diff] [blame] | 379 | EXPECT_TRUE(json->GetString("deviceDraft.modelManifestId", &value)); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 380 | EXPECT_EQ("AAAAA", value); |
Alex Vakulenko | 468f7f3 | 2015-04-08 10:42:45 -0700 | [diff] [blame] | 381 | EXPECT_TRUE(json->GetString("deviceDraft.name", &value)); |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 382 | EXPECT_EQ("Coffee Pot", value); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 383 | base::DictionaryValue* commandDefs = nullptr; |
| 384 | EXPECT_TRUE(json->GetDictionary("deviceDraft.commandDefs", &commandDefs)); |
| 385 | EXPECT_FALSE(commandDefs->empty()); |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 386 | |
| 387 | auto expected = R"({ |
| 388 | 'base': { |
| 389 | 'reboot': { |
| 390 | 'parameters': { |
| 391 | 'delay': { |
| 392 | 'minimum': 10, |
| 393 | 'type': 'integer' |
| 394 | } |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 395 | }, |
| 396 | 'minimalRole': 'user' |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 397 | } |
| 398 | }, |
| 399 | 'robot': { |
| 400 | '_jump': { |
| 401 | 'parameters': { |
| 402 | '_height': { |
| 403 | 'type': 'integer' |
| 404 | } |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 405 | }, |
| 406 | 'minimalRole': 'user' |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | })"; |
| 410 | EXPECT_JSON_EQ(expected, *commandDefs); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 411 | |
| 412 | base::DictionaryValue json_resp; |
| 413 | json_resp.SetString("id", test_data::kClaimTicketId); |
| 414 | json_resp.SetString("kind", "clouddevices#registrationTicket"); |
| 415 | json_resp.SetString("oauthClientId", test_data::kClientId); |
| 416 | base::DictionaryValue* device_draft = nullptr; |
| 417 | EXPECT_TRUE(json->GetDictionary("deviceDraft", &device_draft)); |
| 418 | device_draft = device_draft->DeepCopy(); |
| 419 | device_draft->SetString("id", test_data::kDeviceId); |
| 420 | device_draft->SetString("kind", "clouddevices#device"); |
| 421 | json_resp.Set("deviceDraft", device_draft); |
| 422 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 423 | response->ReplyJson(chromeos::http::status_code::Ok, &json_resp); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 426 | auto json_base = unittests::CreateDictionaryValue(R"({ |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 427 | 'base': { |
| 428 | 'reboot': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 429 | 'parameters': {'delay': 'integer'}, |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 430 | 'minimalRole': 'user', |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 431 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 432 | }, |
| 433 | 'shutdown': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 434 | 'parameters': {}, |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 435 | 'minimalRole': 'user', |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 436 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | })"); |
| 440 | EXPECT_TRUE(command_manager_->LoadBaseCommands(*json_base, nullptr)); |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 441 | auto json_cmds = unittests::CreateDictionaryValue(R"({ |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 442 | 'base': { |
| 443 | 'reboot': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 444 | 'parameters': {'delay': {'minimum': 10}}, |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 445 | 'minimalRole': 'user', |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 446 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 447 | } |
| 448 | }, |
| 449 | 'robot': { |
| 450 | '_jump': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 451 | 'parameters': {'_height': 'integer'}, |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 452 | 'minimalRole': 'user', |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 453 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | })"); |
| 457 | EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, "", nullptr)); |
| 458 | |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 459 | transport_->AddHandler(dev_reg_->GetServiceURL( |
| 460 | std::string("registrationTickets/") + test_data::kClaimTicketId), |
| 461 | chromeos::http::request_type::kPatch, |
| 462 | base::Bind(update_ticket)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 463 | std::string ticket_url = |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 464 | dev_reg_->GetServiceURL("registrationTickets/" + |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 465 | std::string(test_data::kClaimTicketId)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 466 | transport_->AddHandler(ticket_url + "/finalize", |
| 467 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 468 | base::Bind(FinalizeTicketHandler)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 469 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 470 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 471 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 472 | base::Bind(OAuth2Handler)); |
Vitaly Buka | cad2e33 | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 473 | std::string device_id = |
| 474 | dev_reg_->RegisterDevice(test_data::kClaimTicketId, nullptr); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 475 | |
| 476 | EXPECT_EQ(test_data::kDeviceId, device_id); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 477 | EXPECT_EQ(3, transport_->GetRequestCount()); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 478 | EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 479 | |
| 480 | // Validate the device info saved to storage... |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 481 | auto storage_data = storage_->Load(); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 482 | base::DictionaryValue* dict = nullptr; |
| 483 | EXPECT_TRUE(storage_data->GetAsDictionary(&dict)); |
| 484 | std::string value; |
Vitaly Buka | cad2e33 | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 485 | EXPECT_TRUE(dict->GetString("device_id", &value)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 486 | EXPECT_EQ(test_data::kDeviceId, value); |
Vitaly Buka | cad2e33 | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 487 | EXPECT_TRUE(dict->GetString("refresh_token", &value)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 488 | EXPECT_EQ(test_data::kRefreshToken, value); |
Vitaly Buka | cad2e33 | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 489 | EXPECT_TRUE(dict->GetString("robot_account", &value)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 490 | EXPECT_EQ(test_data::kRobotAccountEmail, value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 493 | TEST_F(DeviceRegistrationInfoTest, OOBRegistrationStatus) { |
| 494 | // After we've been initialized, we should be either offline or unregistered, |
| 495 | // depending on whether or not we've found credentials. |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 496 | EXPECT_EQ(RegistrationStatus::kUnconfigured, GetRegistrationStatus()); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 497 | // Put some credentials into our state, make sure we call that offline. |
| 498 | SetDefaultDeviceRegistration(&data_); |
Vitaly Buka | 5636322 | 2015-05-13 15:09:21 -0700 | [diff] [blame] | 499 | storage_->Save(data_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 500 | ReloadConfig(); |
| 501 | EXPECT_EQ(RegistrationStatus::kConnecting, GetRegistrationStatus()); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 504 | TEST_F(DeviceRegistrationInfoTest, UpdateCommand) { |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 505 | SetDefaultDeviceRegistration(&data_); |
| 506 | storage_->Save(data_); |
| 507 | ReloadConfig(); |
| 508 | SetAccessToken(); |
| 509 | |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 510 | auto json_cmds = unittests::CreateDictionaryValue(R"({ |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 511 | 'robot': { |
| 512 | '_jump': { |
| 513 | 'parameters': {'_height': 'integer'}, |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 514 | 'results': {'status': 'string'}, |
| 515 | 'minimalRole': 'user' |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | })"); |
| 519 | EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, "", nullptr)); |
| 520 | |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 521 | const std::string command_url = dev_reg_->GetServiceURL("commands/1234"); |
| 522 | |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 523 | auto commands_json = unittests::CreateValue(R"([{ |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 524 | 'name':'robot._jump', |
| 525 | 'id':'1234', |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 526 | 'parameters': {'_height': 100}, |
| 527 | 'minimalRole': 'user' |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 528 | }])"); |
| 529 | ASSERT_NE(nullptr, commands_json.get()); |
| 530 | const base::ListValue* command_list = nullptr; |
| 531 | ASSERT_TRUE(commands_json->GetAsList(&command_list)); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 532 | PublishCommands(*command_list); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 533 | auto command = command_manager_->FindCommand("1234"); |
| 534 | ASSERT_NE(nullptr, command); |
| 535 | StringPropType string_type; |
| 536 | native_types::Object results{ |
| 537 | {"status", string_type.CreateValue(std::string{"Ok"}, nullptr)} |
| 538 | }; |
| 539 | |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 540 | // UpdateCommand when setting command results. |
| 541 | auto update_command_results = [](const ServerRequest& request, |
| 542 | ServerResponse* response) { |
| 543 | EXPECT_EQ(R"({"results":{"status":"Ok"}})", |
| 544 | request.GetDataAsNormalizedJsonString()); |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 545 | response->ReplyJson(chromeos::http::status_code::Ok, |
| 546 | chromeos::http::FormFieldList{}); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 547 | }; |
| 548 | |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 549 | transport_->AddHandler(command_url, |
| 550 | chromeos::http::request_type::kPatch, |
| 551 | base::Bind(update_command_results)); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 552 | |
| 553 | command->SetResults(results); |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 554 | |
| 555 | // UpdateCommand when setting command progress. |
| 556 | int count = 0; // This will be called twice... |
| 557 | auto update_command_progress = [&count](const ServerRequest& request, |
| 558 | ServerResponse* response) { |
| 559 | if (count++ == 0) { |
| 560 | EXPECT_EQ(R"({"state":"inProgress"})", |
| 561 | request.GetDataAsNormalizedJsonString()); |
| 562 | } else { |
| 563 | EXPECT_EQ(R"({"progress":{"progress":18}})", |
| 564 | request.GetDataAsNormalizedJsonString()); |
| 565 | } |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 566 | response->ReplyJson(chromeos::http::status_code::Ok, |
| 567 | chromeos::http::FormFieldList{}); |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 568 | }; |
| 569 | |
| 570 | transport_->AddHandler(command_url, |
| 571 | chromeos::http::request_type::kPatch, |
| 572 | base::Bind(update_command_progress)); |
| 573 | |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 574 | native_types::Object progress{ |
| 575 | {"progress", unittests::make_int_prop_value(18)}}; |
| 576 | command->SetProgress(progress); |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 577 | |
| 578 | // UpdateCommand when changing command status. |
| 579 | auto update_command_state = [](const ServerRequest& request, |
| 580 | ServerResponse* response) { |
| 581 | EXPECT_EQ(R"({"state":"cancelled"})", |
| 582 | request.GetDataAsNormalizedJsonString()); |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 583 | response->ReplyJson(chromeos::http::status_code::Ok, |
| 584 | chromeos::http::FormFieldList{}); |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 585 | }; |
| 586 | |
| 587 | transport_->AddHandler(command_url, |
| 588 | chromeos::http::request_type::kPatch, |
| 589 | base::Bind(update_command_state)); |
| 590 | |
| 591 | command->Cancel(); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 595 | } // namespace buffet |