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 | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 9 | #include <base/values.h> |
Alex Vakulenko | cf0b33a | 2014-08-20 15:02:10 -0700 | [diff] [blame] | 10 | #include <chromeos/bind_lambda.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 11 | #include <chromeos/http/http_request.h> |
| 12 | #include <chromeos/http/http_transport_fake.h> |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 13 | #include <chromeos/key_value_store.h> |
Alex Vakulenko | 3aeea1c | 2014-08-20 16:33:12 -0700 | [diff] [blame] | 14 | #include <chromeos/mime_utils.h> |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
| 16 | |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 17 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 18 | #include "buffet/commands/unittest_utils.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 19 | #include "buffet/device_registration_storage_keys.h" |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 20 | #include "buffet/states/mock_state_change_queue_interface.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 21 | #include "buffet/states/state_manager.h" |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 22 | #include "buffet/storage_impls.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 23 | |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 24 | namespace buffet { |
| 25 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 26 | using chromeos::http::request_header::kAuthorization; |
| 27 | using chromeos::http::fake::ServerRequest; |
| 28 | using chromeos::http::fake::ServerResponse; |
| 29 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 30 | namespace { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 31 | |
| 32 | namespace test_data { |
| 33 | |
| 34 | const char kServiceURL[] = "http://gcd.server.com/"; |
| 35 | const char kOAuthURL[] = "http://oauth.server.com/"; |
| 36 | const char kApiKey[] = "GOadRdTf9FERf0k4w6EFOof56fUJ3kFDdFL3d7f"; |
| 37 | const char kClientId[] = "123543821385-sfjkjshdkjhfk234sdfsdfkskd" |
| 38 | "fkjh7f.apps.googleusercontent.com"; |
| 39 | const char kClientSecret[] = "5sdGdGlfolGlrFKfdFlgP6FG"; |
| 40 | const char kDeviceId[] = "4a7ea2d1-b331-1e1f-b206-e863c7635196"; |
| 41 | const char kClaimTicketId[] = "RTcUE"; |
| 42 | const char kAccessToken[] = "ya29.1.AADtN_V-dLUM-sVZ0qVjG9Dxm5NgdS9J" |
| 43 | "Mx_JLUqhC9bED_YFjzHZtYt65ZzXCS35NMAeaVZ" |
| 44 | "Dei530-w0yE2urpQ"; |
| 45 | const char kRefreshToken[] = "1/zQmxR6PKNvhcxf9SjXUrCjcmCrcqRKXctc6cp" |
| 46 | "1nI-GQ"; |
| 47 | const char kRobotAccountAuthCode[] = "4/Mf_ujEhPejVhOq-OxW9F5cSOnWzx." |
| 48 | "YgciVjTYGscRshQV0ieZDAqiTIjMigI"; |
| 49 | const char kRobotAccountEmail[] = "6ed0b3f54f9bd619b942f4ad2441c252@" |
| 50 | "clouddevices.gserviceaccount.com"; |
| 51 | const char kUserAccountAuthCode[] = "2/sd_GD1TGFKpJOLJ34-0g5fK0fflp.GlT" |
| 52 | "I0F5g7hNtFgj5HFGOf8FlGK9eflO"; |
| 53 | const char kUserAccessToken[] = "sd56.4.FGDjG_F-gFGF-dFG6gGOG9Dxm5NgdS9" |
| 54 | "JMx_JLUqhC9bED_YFjLKjlkjLKJlkjLKjlKJea" |
| 55 | "VZDei530-w0yE2urpQ"; |
| 56 | const char kUserRefreshToken[] = "1/zQLKjlKJlkLkLKjLkjLKjLkjLjLkjl0ftc6" |
| 57 | "cp1nI-GQ"; |
| 58 | |
| 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) { |
| 63 | data->SetString(storage_keys::kRefreshToken, test_data::kRefreshToken); |
| 64 | data->SetString(storage_keys::kDeviceId, test_data::kDeviceId); |
| 65 | data->SetString(storage_keys::kRobotAccount, test_data::kRobotAccountEmail); |
| 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 | |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 165 | // This is a helper class that allows the unit tests to access private |
| 166 | // methods and data since TestHelper is declared as a friend to |
| 167 | // DeviceRegistrationInfo. |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 168 | class DeviceRegistrationInfo::TestHelper { |
| 169 | public: |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 170 | static bool Save(DeviceRegistrationInfo* info) { |
| 171 | return info->Save(); |
| 172 | } |
Nathan Bullock | 5e022a3 | 2015-04-08 15:13:07 -0400 | [diff] [blame] | 173 | |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 174 | static void PublishCommands(DeviceRegistrationInfo* info, |
| 175 | const base::ListValue& commands) { |
| 176 | return info->PublishCommands(commands); |
| 177 | } |
Nathan Bullock | 5e022a3 | 2015-04-08 15:13:07 -0400 | [diff] [blame] | 178 | |
| 179 | static bool CheckRegistration(DeviceRegistrationInfo* info, |
| 180 | chromeos::ErrorPtr* error) { |
| 181 | return info->CheckRegistration(error); |
| 182 | } |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | class DeviceRegistrationInfoTest : public ::testing::Test { |
| 186 | protected: |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 187 | void SetUp() override { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 188 | storage_ = std::make_shared<MemStorage>(); |
| 189 | storage_->Save(&data_); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 190 | transport_ = std::make_shared<chromeos::http::fake::Transport>(); |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 191 | command_manager_ = std::make_shared<CommandManager>(); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 192 | state_manager_ = std::make_shared<StateManager>(&mock_state_change_queue_); |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 193 | chromeos::KeyValueStore config_store; |
| 194 | config_store.SetString("client_id", test_data::kClientId); |
| 195 | config_store.SetString("client_secret", test_data::kClientSecret); |
| 196 | config_store.SetString("api_key", test_data::kApiKey); |
| 197 | config_store.SetString("device_kind", "vendor"); |
Alex Vakulenko | 468f7f3 | 2015-04-08 10:42:45 -0700 | [diff] [blame] | 198 | config_store.SetString("name", "Coffee Pot"); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 199 | config_store.SetString("description", "Easy to clean"); |
| 200 | config_store.SetString("location", "Kitchen"); |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 201 | config_store.SetString("anonymous_access_role", "viewer"); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 202 | config_store.SetString("model_id", "AAAAA"); |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 203 | config_store.SetString("oauth_url", test_data::kOAuthURL); |
| 204 | config_store.SetString("service_url", test_data::kServiceURL); |
| 205 | std::unique_ptr<BuffetConfig> config{new BuffetConfig}; |
| 206 | config->Load(config_store); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 207 | dev_reg_ = std::unique_ptr<DeviceRegistrationInfo>( |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 208 | new DeviceRegistrationInfo(command_manager_, state_manager_, |
Christopher Wiley | 583d64b | 2015-03-24 14:30:17 -0700 | [diff] [blame] | 209 | std::move(config), |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 210 | transport_, storage_, |
Christopher Wiley | d732bd0 | 2015-04-07 11:11:18 -0700 | [diff] [blame] | 211 | true, |
Vitaly Buka | fa94706 | 2015-04-17 00:41:31 -0700 | [diff] [blame] | 212 | nullptr)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 215 | base::DictionaryValue data_; |
| 216 | std::shared_ptr<MemStorage> storage_; |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 217 | std::shared_ptr<chromeos::http::fake::Transport> transport_; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 218 | std::unique_ptr<DeviceRegistrationInfo> dev_reg_; |
| 219 | std::shared_ptr<CommandManager> command_manager_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 220 | testing::NiceMock<MockStateChangeQueueInterface> mock_state_change_queue_; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 221 | std::shared_ptr<StateManager> state_manager_; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | //////////////////////////////////////////////////////////////////////////////// |
| 225 | TEST_F(DeviceRegistrationInfoTest, GetServiceURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 226 | EXPECT_TRUE(dev_reg_->Load()); |
| 227 | EXPECT_EQ(test_data::kServiceURL, dev_reg_->GetServiceURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 228 | std::string url = test_data::kServiceURL; |
| 229 | url += "registrationTickets"; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 230 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets")); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 231 | url += "?key="; |
| 232 | url += test_data::kApiKey; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 233 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 234 | {"key", test_data::kApiKey} |
| 235 | })); |
| 236 | url += "&restart=true"; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 237 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 238 | {"key", test_data::kApiKey}, |
| 239 | {"restart", "true"}, |
| 240 | })); |
| 241 | } |
| 242 | |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 243 | TEST_F(DeviceRegistrationInfoTest, VerifySave) { |
| 244 | base::DictionaryValue data; |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 245 | data.SetString(storage_keys::kRefreshToken, "d"); |
| 246 | data.SetString(storage_keys::kDeviceId, "e"); |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 247 | data.SetString(storage_keys::kRobotAccount, "h"); |
Alex Vakulenko | 468f7f3 | 2015-04-08 10:42:45 -0700 | [diff] [blame] | 248 | data.SetString(storage_keys::kName, "k"); |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 249 | data.SetString(storage_keys::kDescription, "l"); |
| 250 | data.SetString(storage_keys::kLocation, "m"); |
Vitaly Buka | 2c1029f | 2015-04-30 22:47:18 -0700 | [diff] [blame] | 251 | data.SetString(storage_keys::kAnonymousAccessRole, "user"); |
Nathan Bullock | 02ca28b | 2015-02-11 16:22:16 -0500 | [diff] [blame] | 252 | |
| 253 | storage_->Save(&data); |
| 254 | |
| 255 | // This test isn't really trying to test Load, it is just the easiest |
| 256 | // way to initialize the properties in dev_reg_. |
| 257 | EXPECT_TRUE(dev_reg_->Load()); |
| 258 | |
| 259 | // Clear the storage to get a clean test. |
| 260 | base::DictionaryValue empty; |
| 261 | storage_->Save(&empty); |
| 262 | EXPECT_TRUE(DeviceRegistrationInfo::TestHelper::Save(dev_reg_.get())); |
| 263 | EXPECT_TRUE(storage_->Load()->Equals(&data)); |
| 264 | } |
| 265 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 266 | TEST_F(DeviceRegistrationInfoTest, GetOAuthURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 267 | EXPECT_TRUE(dev_reg_->Load()); |
| 268 | EXPECT_EQ(test_data::kOAuthURL, dev_reg_->GetOAuthURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 269 | std::string url = test_data::kOAuthURL; |
| 270 | url += "auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fclouddevices&"; |
| 271 | url += "redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&"; |
| 272 | url += "response_type=code&"; |
| 273 | url += "client_id="; |
| 274 | url += test_data::kClientId; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 275 | EXPECT_EQ(url, dev_reg_->GetOAuthURL("auth", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 276 | {"scope", "https://www.googleapis.com/auth/clouddevices"}, |
| 277 | {"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"}, |
| 278 | {"response_type", "code"}, |
| 279 | {"client_id", test_data::kClientId} |
| 280 | })); |
| 281 | } |
| 282 | |
| 283 | TEST_F(DeviceRegistrationInfoTest, CheckRegistration) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 284 | EXPECT_TRUE(dev_reg_->Load()); |
Nathan Bullock | 5e022a3 | 2015-04-08 15:13:07 -0400 | [diff] [blame] | 285 | EXPECT_FALSE(DeviceRegistrationInfo::TestHelper::CheckRegistration( |
| 286 | dev_reg_.get(), nullptr)); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 287 | EXPECT_EQ(0, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 288 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 289 | SetDefaultDeviceRegistration(&data_); |
| 290 | storage_->Save(&data_); |
| 291 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 292 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 293 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 294 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 295 | base::Bind(OAuth2Handler)); |
| 296 | transport_->ResetRequestCount(); |
Nathan Bullock | 5e022a3 | 2015-04-08 15:13:07 -0400 | [diff] [blame] | 297 | EXPECT_TRUE(DeviceRegistrationInfo::TestHelper::CheckRegistration( |
| 298 | dev_reg_.get(), nullptr)); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 299 | EXPECT_EQ(1, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 302 | TEST_F(DeviceRegistrationInfoTest, CheckAuthenticationFailure) { |
| 303 | SetDefaultDeviceRegistration(&data_); |
| 304 | storage_->Save(&data_); |
| 305 | EXPECT_TRUE(dev_reg_->Load()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 306 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 307 | |
| 308 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 309 | chromeos::http::request_type::kPost, |
| 310 | base::Bind(OAuth2HandlerFail)); |
| 311 | transport_->ResetRequestCount(); |
| 312 | chromeos::ErrorPtr error; |
Nathan Bullock | 5e022a3 | 2015-04-08 15:13:07 -0400 | [diff] [blame] | 313 | EXPECT_FALSE(DeviceRegistrationInfo::TestHelper::CheckRegistration( |
| 314 | dev_reg_.get(), &error)); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 315 | EXPECT_EQ(1, transport_->GetRequestCount()); |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 316 | EXPECT_TRUE(error->HasError(kErrorDomainOAuth2, "unable_to_authenticate")); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 317 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | TEST_F(DeviceRegistrationInfoTest, CheckDeregistration) { |
| 321 | SetDefaultDeviceRegistration(&data_); |
| 322 | storage_->Save(&data_); |
| 323 | EXPECT_TRUE(dev_reg_->Load()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 324 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 325 | |
| 326 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 327 | chromeos::http::request_type::kPost, |
| 328 | base::Bind(OAuth2HandlerDeregister)); |
| 329 | transport_->ResetRequestCount(); |
| 330 | chromeos::ErrorPtr error; |
Nathan Bullock | 5e022a3 | 2015-04-08 15:13:07 -0400 | [diff] [blame] | 331 | EXPECT_FALSE(DeviceRegistrationInfo::TestHelper::CheckRegistration( |
| 332 | dev_reg_.get(), &error)); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 333 | EXPECT_EQ(1, transport_->GetRequestCount()); |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 334 | EXPECT_TRUE(error->HasError(kErrorDomainOAuth2, "invalid_grant")); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 335 | EXPECT_EQ(RegistrationStatus::kInvalidCredentials, |
| 336 | dev_reg_->GetRegistrationStatus()); |
| 337 | } |
| 338 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 339 | TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 340 | SetDefaultDeviceRegistration(&data_); |
| 341 | storage_->Save(&data_); |
| 342 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 343 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 344 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 345 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 346 | base::Bind(OAuth2Handler)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 347 | transport_->AddHandler(dev_reg_->GetDeviceURL(), |
| 348 | chromeos::http::request_type::kGet, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 349 | base::Bind(DeviceInfoHandler)); |
| 350 | transport_->ResetRequestCount(); |
| 351 | auto device_info = dev_reg_->GetDeviceInfo(nullptr); |
| 352 | EXPECT_EQ(2, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 353 | EXPECT_NE(nullptr, device_info.get()); |
| 354 | base::DictionaryValue* dict = nullptr; |
| 355 | EXPECT_TRUE(device_info->GetAsDictionary(&dict)); |
| 356 | std::string id; |
| 357 | EXPECT_TRUE(dict->GetString("id", &id)); |
| 358 | EXPECT_EQ(test_data::kDeviceId, id); |
| 359 | } |
| 360 | |
| 361 | TEST_F(DeviceRegistrationInfoTest, GetDeviceId) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 362 | SetDefaultDeviceRegistration(&data_); |
| 363 | storage_->Save(&data_); |
| 364 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 365 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 366 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 367 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 368 | base::Bind(OAuth2Handler)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 369 | transport_->AddHandler(dev_reg_->GetDeviceURL(), |
| 370 | chromeos::http::request_type::kGet, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 371 | base::Bind(DeviceInfoHandler)); |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 372 | EXPECT_EQ(test_data::kDeviceId, dev_reg_->GetDeviceId()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 375 | TEST_F(DeviceRegistrationInfoTest, RegisterDevice) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 376 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 377 | |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 378 | auto update_ticket = [](const ServerRequest& request, |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 379 | ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 380 | EXPECT_EQ(test_data::kApiKey, request.GetFormField("key")); |
| 381 | auto json = request.GetDataAsJson(); |
| 382 | EXPECT_NE(nullptr, json.get()); |
| 383 | std::string value; |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 384 | EXPECT_TRUE(json->GetString("id", &value)); |
| 385 | EXPECT_EQ(test_data::kClaimTicketId, value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 386 | EXPECT_TRUE(json->GetString("deviceDraft.channel.supportedType", &value)); |
| 387 | EXPECT_EQ("xmpp", value); |
| 388 | EXPECT_TRUE(json->GetString("oauthClientId", &value)); |
| 389 | EXPECT_EQ(test_data::kClientId, value); |
| 390 | EXPECT_TRUE(json->GetString("deviceDraft.deviceKind", &value)); |
| 391 | EXPECT_EQ("vendor", value); |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 392 | EXPECT_TRUE(json->GetString("deviceDraft.description", &value)); |
| 393 | EXPECT_EQ("Easy to clean", value); |
| 394 | EXPECT_TRUE(json->GetString("deviceDraft.location", &value)); |
| 395 | EXPECT_EQ("Kitchen", value); |
Vitaly Buka | 6522ab6 | 2015-02-19 10:26:31 -0800 | [diff] [blame] | 396 | EXPECT_TRUE(json->GetString("deviceDraft.modelManifestId", &value)); |
Vitaly Buka | 867b088 | 2015-04-16 10:03:26 -0700 | [diff] [blame] | 397 | EXPECT_EQ("AAAAA", value); |
Alex Vakulenko | 468f7f3 | 2015-04-08 10:42:45 -0700 | [diff] [blame] | 398 | EXPECT_TRUE(json->GetString("deviceDraft.name", &value)); |
Vitaly Buka | d3eb19c | 2014-11-21 13:39:43 -0800 | [diff] [blame] | 399 | EXPECT_EQ("Coffee Pot", value); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 400 | base::DictionaryValue* commandDefs = nullptr; |
| 401 | EXPECT_TRUE(json->GetDictionary("deviceDraft.commandDefs", &commandDefs)); |
| 402 | EXPECT_FALSE(commandDefs->empty()); |
Vitaly Buka | 7c82d29 | 2015-05-03 18:08:12 -0700 | [diff] [blame] | 403 | |
| 404 | auto expected = R"({ |
| 405 | 'base': { |
| 406 | 'reboot': { |
| 407 | 'parameters': { |
| 408 | 'delay': { |
| 409 | 'minimum': 10, |
| 410 | 'type': 'integer' |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | }, |
| 415 | 'robot': { |
| 416 | '_jump': { |
| 417 | 'parameters': { |
| 418 | '_height': { |
| 419 | 'type': 'integer' |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | })"; |
| 425 | EXPECT_JSON_EQ(expected, *commandDefs); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 426 | |
| 427 | base::DictionaryValue json_resp; |
| 428 | json_resp.SetString("id", test_data::kClaimTicketId); |
| 429 | json_resp.SetString("kind", "clouddevices#registrationTicket"); |
| 430 | json_resp.SetString("oauthClientId", test_data::kClientId); |
| 431 | base::DictionaryValue* device_draft = nullptr; |
| 432 | EXPECT_TRUE(json->GetDictionary("deviceDraft", &device_draft)); |
| 433 | device_draft = device_draft->DeepCopy(); |
| 434 | device_draft->SetString("id", test_data::kDeviceId); |
| 435 | device_draft->SetString("kind", "clouddevices#device"); |
| 436 | json_resp.Set("deviceDraft", device_draft); |
| 437 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 438 | response->ReplyJson(chromeos::http::status_code::Ok, &json_resp); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 439 | }; |
| 440 | |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 441 | auto json_base = 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': 'integer'}, |
| 445 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 446 | }, |
| 447 | 'shutdown': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 448 | 'parameters': {}, |
| 449 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | })"); |
| 453 | EXPECT_TRUE(command_manager_->LoadBaseCommands(*json_base, nullptr)); |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 454 | auto json_cmds = unittests::CreateDictionaryValue(R"({ |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 455 | 'base': { |
| 456 | 'reboot': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 457 | 'parameters': {'delay': {'minimum': 10}}, |
| 458 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 459 | } |
| 460 | }, |
| 461 | 'robot': { |
| 462 | '_jump': { |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 463 | 'parameters': {'_height': 'integer'}, |
| 464 | 'results': {} |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | })"); |
| 468 | EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, "", nullptr)); |
| 469 | |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 470 | transport_->AddHandler(dev_reg_->GetServiceURL( |
| 471 | std::string("registrationTickets/") + test_data::kClaimTicketId), |
| 472 | chromeos::http::request_type::kPatch, |
| 473 | base::Bind(update_ticket)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 474 | std::string ticket_url = |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 475 | dev_reg_->GetServiceURL("registrationTickets/" + |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 476 | std::string(test_data::kClaimTicketId)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 477 | transport_->AddHandler(ticket_url + "/finalize", |
| 478 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 479 | base::Bind(FinalizeTicketHandler)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 480 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 481 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 482 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 483 | base::Bind(OAuth2Handler)); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 484 | storage_->reset_save_count(); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 485 | |
| 486 | std::map<std::string, std::string> params; |
| 487 | params["ticket_id"] = test_data::kClaimTicketId; |
| 488 | std::string device_id = dev_reg_->RegisterDevice(params, nullptr); |
| 489 | |
| 490 | EXPECT_EQ(test_data::kDeviceId, device_id); |
Vitaly Buka | cf5248c | 2015-04-30 18:49:08 -0700 | [diff] [blame] | 491 | EXPECT_GT(storage_->save_count(), 1); // The state must have been saved. |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 492 | EXPECT_EQ(3, transport_->GetRequestCount()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 493 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 494 | |
| 495 | // Validate the device info saved to storage... |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 496 | auto storage_data = storage_->Load(); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 497 | base::DictionaryValue* dict = nullptr; |
| 498 | EXPECT_TRUE(storage_data->GetAsDictionary(&dict)); |
| 499 | std::string value; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 500 | EXPECT_TRUE(dict->GetString(storage_keys::kDeviceId, &value)); |
| 501 | EXPECT_EQ(test_data::kDeviceId, value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 502 | EXPECT_TRUE(dict->GetString(storage_keys::kRefreshToken, &value)); |
| 503 | EXPECT_EQ(test_data::kRefreshToken, value); |
| 504 | EXPECT_TRUE(dict->GetString(storage_keys::kRobotAccount, &value)); |
| 505 | EXPECT_EQ(test_data::kRobotAccountEmail, value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 508 | TEST_F(DeviceRegistrationInfoTest, OOBRegistrationStatus) { |
| 509 | // After we've been initialized, we should be either offline or unregistered, |
| 510 | // depending on whether or not we've found credentials. |
| 511 | EXPECT_TRUE(dev_reg_->Load()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 512 | EXPECT_EQ(RegistrationStatus::kUnconfigured, |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 513 | dev_reg_->GetRegistrationStatus()); |
| 514 | // Put some credentials into our state, make sure we call that offline. |
| 515 | SetDefaultDeviceRegistration(&data_); |
| 516 | storage_->Save(&data_); |
| 517 | EXPECT_TRUE(dev_reg_->Load()); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 518 | EXPECT_EQ(RegistrationStatus::kConnecting, dev_reg_->GetRegistrationStatus()); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 521 | TEST_F(DeviceRegistrationInfoTest, UpdateCommand) { |
| 522 | EXPECT_TRUE(dev_reg_->Load()); |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 523 | auto json_cmds = unittests::CreateDictionaryValue(R"({ |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 524 | 'robot': { |
| 525 | '_jump': { |
| 526 | 'parameters': {'_height': 'integer'}, |
| 527 | 'results': {'status': 'string'} |
| 528 | } |
| 529 | } |
| 530 | })"); |
| 531 | EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, "", nullptr)); |
| 532 | |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 533 | const std::string command_url = dev_reg_->GetServiceURL("commands/1234"); |
| 534 | |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 535 | auto commands_json = unittests::CreateValue(R"([{ |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 536 | 'name':'robot._jump', |
| 537 | 'id':'1234', |
| 538 | 'parameters': {'_height': 100} |
| 539 | }])"); |
| 540 | ASSERT_NE(nullptr, commands_json.get()); |
| 541 | const base::ListValue* command_list = nullptr; |
| 542 | ASSERT_TRUE(commands_json->GetAsList(&command_list)); |
| 543 | DeviceRegistrationInfo::TestHelper::PublishCommands(dev_reg_.get(), |
| 544 | *command_list); |
| 545 | auto command = command_manager_->FindCommand("1234"); |
| 546 | ASSERT_NE(nullptr, command); |
| 547 | StringPropType string_type; |
| 548 | native_types::Object results{ |
| 549 | {"status", string_type.CreateValue(std::string{"Ok"}, nullptr)} |
| 550 | }; |
| 551 | |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 552 | // UpdateCommand when setting command results. |
| 553 | auto update_command_results = [](const ServerRequest& request, |
| 554 | ServerResponse* response) { |
| 555 | EXPECT_EQ(R"({"results":{"status":"Ok"}})", |
| 556 | request.GetDataAsNormalizedJsonString()); |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 557 | response->ReplyJson(chromeos::http::status_code::Ok, |
| 558 | chromeos::http::FormFieldList{}); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 559 | }; |
| 560 | |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 561 | transport_->AddHandler(command_url, |
| 562 | chromeos::http::request_type::kPatch, |
| 563 | base::Bind(update_command_results)); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 564 | |
| 565 | command->SetResults(results); |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 566 | |
| 567 | // UpdateCommand when setting command progress. |
| 568 | int count = 0; // This will be called twice... |
| 569 | auto update_command_progress = [&count](const ServerRequest& request, |
| 570 | ServerResponse* response) { |
| 571 | if (count++ == 0) { |
| 572 | EXPECT_EQ(R"({"state":"inProgress"})", |
| 573 | request.GetDataAsNormalizedJsonString()); |
| 574 | } else { |
| 575 | EXPECT_EQ(R"({"progress":{"progress":18}})", |
| 576 | request.GetDataAsNormalizedJsonString()); |
| 577 | } |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 578 | response->ReplyJson(chromeos::http::status_code::Ok, |
| 579 | chromeos::http::FormFieldList{}); |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 580 | }; |
| 581 | |
| 582 | transport_->AddHandler(command_url, |
| 583 | chromeos::http::request_type::kPatch, |
| 584 | base::Bind(update_command_progress)); |
| 585 | |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 586 | native_types::Object progress{ |
| 587 | {"progress", unittests::make_int_prop_value(18)}}; |
| 588 | command->SetProgress(progress); |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 589 | |
| 590 | // UpdateCommand when changing command status. |
| 591 | auto update_command_state = [](const ServerRequest& request, |
| 592 | ServerResponse* response) { |
| 593 | EXPECT_EQ(R"({"state":"cancelled"})", |
| 594 | request.GetDataAsNormalizedJsonString()); |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 595 | response->ReplyJson(chromeos::http::status_code::Ok, |
| 596 | chromeos::http::FormFieldList{}); |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | transport_->AddHandler(command_url, |
| 600 | chromeos::http::request_type::kPatch, |
| 601 | base::Bind(update_command_state)); |
| 602 | |
| 603 | command->Cancel(); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 607 | } // namespace buffet |