Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/device_registration_info.h" |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 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> |
| 10 | #include <gtest/gtest.h> |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 11 | #include <weave/provider/test/fake_task_runner.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 12 | #include <weave/provider/test/mock_config_store.h> |
| 13 | #include <weave/provider/test/mock_http_client.h> |
Alex Vakulenko | 8a05beb | 2015-11-24 17:13:20 -0800 | [diff] [blame^] | 14 | #include <weave/test/unittest_utils.h> |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 15 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 16 | #include "src/bind_lambda.h" |
| 17 | #include "src/commands/command_manager.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 18 | #include "src/http_constants.h" |
| 19 | #include "src/states/mock_state_change_queue_interface.h" |
| 20 | #include "src/states/state_manager.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 21 | |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 22 | using testing::_; |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 23 | using testing::AtLeast; |
| 24 | using testing::Invoke; |
| 25 | using testing::InvokeWithoutArgs; |
| 26 | using testing::Mock; |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 27 | using testing::Return; |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 28 | using testing::ReturnRef; |
| 29 | using testing::ReturnRefOfCopy; |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 30 | using testing::SaveArg; |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 31 | using testing::StrictMock; |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 32 | using testing::WithArgs; |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 33 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 34 | namespace weave { |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 35 | |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 36 | using test::CreateDictionaryValue; |
| 37 | using test::CreateValue; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 38 | using provider::test::MockHttpClient; |
| 39 | using provider::test::MockHttpClientResponse; |
| 40 | using provider::HttpClient; |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 41 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 42 | namespace { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 43 | |
| 44 | namespace test_data { |
| 45 | |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 46 | const char kServiceURL[] = "http://gcd.server.com/"; |
| 47 | const char kOAuthURL[] = "http://oauth.server.com/"; |
| 48 | const char kApiKey[] = "GOadRdTf9FERf0k4w6EFOof56fUJ3kFDdFL3d7f"; |
| 49 | const char kClientId[] = |
| 50 | "123543821385-sfjkjshdkjhfk234sdfsdfkskd" |
| 51 | "fkjh7f.apps.googleusercontent.com"; |
| 52 | const char kClientSecret[] = "5sdGdGlfolGlrFKfdFlgP6FG"; |
| 53 | const char kDeviceId[] = "4a7ea2d1-b331-1e1f-b206-e863c7635196"; |
| 54 | const char kClaimTicketId[] = "RTcUE"; |
| 55 | const char kAccessToken[] = |
| 56 | "ya29.1.AADtN_V-dLUM-sVZ0qVjG9Dxm5NgdS9J" |
| 57 | "Mx_JLUqhC9bED_YFjzHZtYt65ZzXCS35NMAeaVZ" |
| 58 | "Dei530-w0yE2urpQ"; |
| 59 | const char kRefreshToken[] = |
| 60 | "1/zQmxR6PKNvhcxf9SjXUrCjcmCrcqRKXctc6cp" |
| 61 | "1nI-GQ"; |
| 62 | const char kRobotAccountAuthCode[] = |
| 63 | "4/Mf_ujEhPejVhOq-OxW9F5cSOnWzx." |
| 64 | "YgciVjTYGscRshQV0ieZDAqiTIjMigI"; |
| 65 | const char kRobotAccountEmail[] = |
| 66 | "6ed0b3f54f9bd619b942f4ad2441c252@" |
| 67 | "clouddevices.gserviceaccount.com"; |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 68 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 69 | } // namespace test_data |
| 70 | |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 71 | std::string GetFormField(const std::string& data, const std::string& name) { |
| 72 | EXPECT_FALSE(data.empty()); |
Vitaly Buka | 7d55639 | 2015-08-13 20:06:48 -0700 | [diff] [blame] | 73 | for (const auto& i : WebParamsDecode(data)) { |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 74 | if (i.first == name) |
| 75 | return i.second; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 76 | } |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 77 | return {}; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 80 | std::unique_ptr<HttpClient::Response> ReplyWithJson(int status_code, |
| 81 | const base::Value& json) { |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 82 | std::string text; |
| 83 | base::JSONWriter::WriteWithOptions( |
| 84 | json, base::JSONWriter::OPTIONS_PRETTY_PRINT, &text); |
| 85 | |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 86 | std::unique_ptr<MockHttpClientResponse> response{ |
| 87 | new StrictMock<MockHttpClientResponse>}; |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 88 | EXPECT_CALL(*response, GetStatusCode()) |
| 89 | .Times(AtLeast(1)) |
| 90 | .WillRepeatedly(Return(status_code)); |
| 91 | EXPECT_CALL(*response, GetContentType()) |
| 92 | .Times(AtLeast(1)) |
Vitaly Buka | 815b603 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 93 | .WillRepeatedly(Return(http::kJsonUtf8)); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 94 | EXPECT_CALL(*response, GetData()) |
| 95 | .Times(AtLeast(1)) |
Vitaly Buka | 4774df2 | 2015-10-09 12:36:22 -0700 | [diff] [blame] | 96 | .WillRepeatedly(Return(text)); |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 97 | return std::move(response); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 100 | std::pair<std::string, std::string> GetAuthHeader() { |
Vitaly Buka | 815b603 | 2015-08-06 11:06:25 -0700 | [diff] [blame] | 101 | return {http::kAuthorization, |
| 102 | std::string("Bearer ") + test_data::kAccessToken}; |
| 103 | } |
| 104 | |
| 105 | std::pair<std::string, std::string> GetJsonHeader() { |
| 106 | return {http::kContentType, http::kJsonUtf8}; |
| 107 | } |
| 108 | |
| 109 | std::pair<std::string, std::string> GetFormHeader() { |
| 110 | return {http::kContentType, http::kWwwFormUrlEncoded}; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | } // anonymous namespace |
| 114 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 115 | class DeviceRegistrationInfoTest : public ::testing::Test { |
| 116 | protected: |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 117 | void SetUp() override { |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 118 | EXPECT_CALL(mock_state_change_queue_, GetLastStateChangeId()) |
| 119 | .WillRepeatedly(Return(0)); |
| 120 | EXPECT_CALL(mock_state_change_queue_, MockAddOnStateUpdatedCallback(_)) |
| 121 | .WillRepeatedly(Return(nullptr)); |
| 122 | |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 123 | command_manager_ = std::make_shared<CommandManager>(); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 124 | state_manager_ = std::make_shared<StateManager>(&mock_state_change_queue_); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 125 | |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 126 | std::unique_ptr<Config> config{new Config{&config_store_}}; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 127 | config_ = config.get(); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 128 | dev_reg_.reset(new DeviceRegistrationInfo{command_manager_, state_manager_, |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 129 | std::move(config), &task_runner_, |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 130 | &http_client_, nullptr}); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 131 | |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 132 | ReloadDefaults(); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 135 | void ReloadDefaults() { |
| 136 | EXPECT_CALL(config_store_, LoadDefaults(_)) |
| 137 | .WillOnce(Invoke([](Settings* settings) { |
| 138 | settings->client_id = test_data::kClientId; |
| 139 | settings->client_secret = test_data::kClientSecret; |
| 140 | settings->api_key = test_data::kApiKey; |
Vitaly Buka | a05eadb | 2015-09-29 16:38:24 -0700 | [diff] [blame] | 141 | settings->oem_name = "Coffee Pot Maker"; |
| 142 | settings->model_name = "Pot v1"; |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 143 | settings->name = "Coffee Pot"; |
| 144 | settings->description = "Easy to clean"; |
| 145 | settings->location = "Kitchen"; |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 146 | settings->local_anonymous_access_role = AuthScope::kViewer; |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 147 | settings->model_id = "AAAAA"; |
| 148 | settings->oauth_url = test_data::kOAuthURL; |
| 149 | settings->service_url = test_data::kServiceURL; |
| 150 | return true; |
| 151 | })); |
| 152 | config_->Load(); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 153 | dev_reg_->Start(); |
| 154 | } |
| 155 | |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 156 | void ReloadSettings() { |
| 157 | base::DictionaryValue dict; |
| 158 | dict.SetString("refresh_token", test_data::kRefreshToken); |
Johan Euphrosine | 312c2f5 | 2015-09-29 00:04:29 -0700 | [diff] [blame] | 159 | dict.SetString("cloud_id", test_data::kDeviceId); |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 160 | dict.SetString("robot_account", test_data::kRobotAccountEmail); |
| 161 | std::string json_string; |
| 162 | base::JSONWriter::WriteWithOptions( |
| 163 | dict, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_string); |
| 164 | EXPECT_CALL(config_store_, LoadSettings()).WillOnce(Return(json_string)); |
| 165 | ReloadDefaults(); |
| 166 | } |
| 167 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 168 | void PublishCommands(const base::ListValue& commands) { |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 169 | dev_reg_->PublishCommands(commands, nullptr); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 172 | bool RefreshAccessToken(ErrorPtr* error) const { |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 173 | bool succeeded = false; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 174 | auto callback = [&succeeded, &error](ErrorPtr in_error) { |
| 175 | if (error) { |
Vitaly Buka | f7f52d4 | 2015-10-10 22:43:55 -0700 | [diff] [blame] | 176 | *error = std::move(in_error); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 177 | return; |
| 178 | } |
| 179 | succeeded = true; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 180 | }; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 181 | dev_reg_->RefreshAccessToken(base::Bind(callback)); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 182 | return succeeded; |
| 183 | } |
| 184 | |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 185 | void SetAccessToken() { dev_reg_->access_token_ = test_data::kAccessToken; } |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 186 | |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 187 | GcdState GetGcdState() const { return dev_reg_->GetGcdState(); } |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 188 | |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 189 | bool HaveRegistrationCredentials() const { |
| 190 | return dev_reg_->HaveRegistrationCredentials(); |
| 191 | } |
| 192 | |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 193 | provider::test::FakeTaskRunner task_runner_; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 194 | provider::test::MockConfigStore config_store_; |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 195 | StrictMock<MockHttpClient> http_client_; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 196 | base::DictionaryValue data_; |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 197 | Config* config_{nullptr}; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 198 | std::unique_ptr<DeviceRegistrationInfo> dev_reg_; |
| 199 | std::shared_ptr<CommandManager> command_manager_; |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 200 | StrictMock<MockStateChangeQueueInterface> mock_state_change_queue_; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 201 | std::shared_ptr<StateManager> state_manager_; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 204 | TEST_F(DeviceRegistrationInfoTest, GetServiceURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 205 | EXPECT_EQ(test_data::kServiceURL, dev_reg_->GetServiceURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 206 | std::string url = test_data::kServiceURL; |
| 207 | url += "registrationTickets"; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 208 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets")); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 209 | url += "?key="; |
| 210 | url += test_data::kApiKey; |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 211 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", |
| 212 | {{"key", test_data::kApiKey}})); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 213 | url += "&restart=true"; |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 214 | EXPECT_EQ(url, dev_reg_->GetServiceURL( |
| 215 | "registrationTickets", |
| 216 | { |
| 217 | {"key", test_data::kApiKey}, {"restart", "true"}, |
| 218 | })); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | TEST_F(DeviceRegistrationInfoTest, GetOAuthURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 222 | EXPECT_EQ(test_data::kOAuthURL, dev_reg_->GetOAuthURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 223 | std::string url = test_data::kOAuthURL; |
Vitaly Buka | 88272d5 | 2015-11-17 17:04:01 -0800 | [diff] [blame] | 224 | url += "auth?redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&"; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 225 | url += "response_type=code&"; |
| 226 | url += "client_id="; |
| 227 | url += test_data::kClientId; |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 228 | EXPECT_EQ(url, dev_reg_->GetOAuthURL( |
Vitaly Buka | c27390d | 2015-11-19 14:42:35 -0800 | [diff] [blame] | 229 | "auth", {{"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"}, |
| 230 | {"response_type", "code"}, |
| 231 | {"client_id", test_data::kClientId}})); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 234 | TEST_F(DeviceRegistrationInfoTest, HaveRegistrationCredentials) { |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 235 | EXPECT_FALSE(HaveRegistrationCredentials()); |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 236 | ReloadSettings(); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 237 | |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 238 | EXPECT_CALL( |
| 239 | http_client_, |
| 240 | SendRequest(HttpClient::Method::kPost, dev_reg_->GetOAuthURL("token"), |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 241 | HttpClient::Headers{GetFormHeader()}, _, _)) |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 242 | .WillOnce(WithArgs<3, 4>(Invoke([]( |
| 243 | const std::string& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 244 | const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 245 | EXPECT_EQ("refresh_token", GetFormField(data, "grant_type")); |
| 246 | EXPECT_EQ(test_data::kRefreshToken, |
| 247 | GetFormField(data, "refresh_token")); |
| 248 | EXPECT_EQ(test_data::kClientId, GetFormField(data, "client_id")); |
| 249 | EXPECT_EQ(test_data::kClientSecret, |
| 250 | GetFormField(data, "client_secret")); |
| 251 | |
| 252 | base::DictionaryValue json; |
| 253 | json.SetString("access_token", test_data::kAccessToken); |
| 254 | json.SetInteger("expires_in", 3600); |
| 255 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 256 | callback.Run(ReplyWithJson(200, json), nullptr); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 257 | }))); |
| 258 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 259 | EXPECT_TRUE(RefreshAccessToken(nullptr)); |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 260 | EXPECT_TRUE(HaveRegistrationCredentials()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 263 | TEST_F(DeviceRegistrationInfoTest, CheckAuthenticationFailure) { |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 264 | ReloadSettings(); |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 265 | EXPECT_EQ(GcdState::kConnecting, GetGcdState()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 266 | |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 267 | EXPECT_CALL( |
| 268 | http_client_, |
| 269 | SendRequest(HttpClient::Method::kPost, dev_reg_->GetOAuthURL("token"), |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 270 | HttpClient::Headers{GetFormHeader()}, _, _)) |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 271 | .WillOnce(WithArgs<3, 4>(Invoke([]( |
| 272 | const std::string& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 273 | const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 274 | EXPECT_EQ("refresh_token", GetFormField(data, "grant_type")); |
| 275 | EXPECT_EQ(test_data::kRefreshToken, |
| 276 | GetFormField(data, "refresh_token")); |
| 277 | EXPECT_EQ(test_data::kClientId, GetFormField(data, "client_id")); |
| 278 | EXPECT_EQ(test_data::kClientSecret, |
| 279 | GetFormField(data, "client_secret")); |
| 280 | |
| 281 | base::DictionaryValue json; |
| 282 | json.SetString("error", "unable_to_authenticate"); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 283 | callback.Run(ReplyWithJson(400, json), nullptr); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 284 | }))); |
| 285 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 286 | ErrorPtr error; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 287 | EXPECT_FALSE(RefreshAccessToken(&error)); |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 288 | EXPECT_TRUE(error->HasError(kErrorDomainOAuth2, "unable_to_authenticate")); |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 289 | EXPECT_EQ(GcdState::kConnecting, GetGcdState()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | TEST_F(DeviceRegistrationInfoTest, CheckDeregistration) { |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 293 | ReloadSettings(); |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 294 | EXPECT_EQ(GcdState::kConnecting, GetGcdState()); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 295 | |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 296 | EXPECT_CALL( |
| 297 | http_client_, |
| 298 | SendRequest(HttpClient::Method::kPost, dev_reg_->GetOAuthURL("token"), |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 299 | HttpClient::Headers{GetFormHeader()}, _, _)) |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 300 | .WillOnce(WithArgs<3, 4>(Invoke([]( |
| 301 | const std::string& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 302 | const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 303 | EXPECT_EQ("refresh_token", GetFormField(data, "grant_type")); |
| 304 | EXPECT_EQ(test_data::kRefreshToken, |
| 305 | GetFormField(data, "refresh_token")); |
| 306 | EXPECT_EQ(test_data::kClientId, GetFormField(data, "client_id")); |
| 307 | EXPECT_EQ(test_data::kClientSecret, |
| 308 | GetFormField(data, "client_secret")); |
| 309 | |
| 310 | base::DictionaryValue json; |
| 311 | json.SetString("error", "invalid_grant"); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 312 | callback.Run(ReplyWithJson(400, json), nullptr); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 313 | }))); |
| 314 | |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 315 | ErrorPtr error; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 316 | EXPECT_FALSE(RefreshAccessToken(&error)); |
Vitaly Buka | 32005de | 2015-05-01 12:33:31 -0700 | [diff] [blame] | 317 | EXPECT_TRUE(error->HasError(kErrorDomainOAuth2, "invalid_grant")); |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 318 | EXPECT_EQ(GcdState::kInvalidCredentials, GetGcdState()); |
Vitaly Buka | 672634b | 2015-11-20 09:49:30 -0800 | [diff] [blame] | 319 | EXPECT_EQ(test_data::kDeviceId, dev_reg_->GetSettings().cloud_id); |
Nathan Bullock | 24d189f | 2015-02-26 13:09:18 -0500 | [diff] [blame] | 320 | } |
| 321 | |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 322 | TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) { |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 323 | ReloadSettings(); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 324 | SetAccessToken(); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 325 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 326 | EXPECT_CALL( |
| 327 | http_client_, |
| 328 | SendRequest(HttpClient::Method::kGet, dev_reg_->GetDeviceURL(), |
| 329 | HttpClient::Headers{GetAuthHeader(), GetJsonHeader()}, _, _)) |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 330 | .WillOnce(WithArgs<3, 4>( |
| 331 | Invoke([](const std::string& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 332 | const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 333 | base::DictionaryValue json; |
| 334 | json.SetString("channel.supportedType", "xmpp"); |
| 335 | json.SetString("deviceKind", "vendor"); |
| 336 | json.SetString("id", test_data::kDeviceId); |
Vitaly Buka | 88272d5 | 2015-11-17 17:04:01 -0800 | [diff] [blame] | 337 | json.SetString("kind", "weave#device"); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 338 | callback.Run(ReplyWithJson(200, json), nullptr); |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 339 | }))); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 340 | |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 341 | bool succeeded = false; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 342 | auto callback = [&succeeded, this](const base::DictionaryValue& info, |
| 343 | ErrorPtr error) { |
| 344 | EXPECT_FALSE(error); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 345 | std::string id; |
| 346 | EXPECT_TRUE(info.GetString("id", &id)); |
| 347 | EXPECT_EQ(test_data::kDeviceId, id); |
| 348 | succeeded = true; |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 349 | }; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 350 | dev_reg_->GetDeviceInfo(base::Bind(callback)); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 351 | EXPECT_TRUE(succeeded); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 354 | TEST_F(DeviceRegistrationInfoTest, RegisterDevice) { |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 355 | auto json_cmds = CreateDictionaryValue(R"({ |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 356 | 'base': { |
| 357 | 'reboot': { |
Alex Vakulenko | 7e894da | 2015-11-23 11:47:49 -0800 | [diff] [blame] | 358 | 'parameters': {'delay': {'minimum': 10, 'type': 'integer'}}, |
| 359 | 'minimalRole': 'user' |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 360 | } |
| 361 | }, |
| 362 | 'robot': { |
| 363 | '_jump': { |
Alex Vakulenko | 7e894da | 2015-11-23 11:47:49 -0800 | [diff] [blame] | 364 | 'parameters': {'_height': {'type': 'integer'}}, |
| 365 | 'minimalRole': 'user' |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | })"); |
Vitaly Buka | 453c4dd | 2015-10-04 18:01:50 -0700 | [diff] [blame] | 369 | EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, nullptr)); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 370 | |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 371 | std::string ticket_url = dev_reg_->GetServiceURL("registrationTickets/") + |
| 372 | test_data::kClaimTicketId; |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 373 | EXPECT_CALL(http_client_, |
| 374 | SendRequest(HttpClient::Method::kPatch, |
| 375 | ticket_url + "?key=" + test_data::kApiKey, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 376 | HttpClient::Headers{GetJsonHeader()}, _, _)) |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 377 | .WillOnce(WithArgs<3, 4>(Invoke([]( |
| 378 | const std::string& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 379 | const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 380 | auto json = test::CreateDictionaryValue(data); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 381 | EXPECT_NE(nullptr, json.get()); |
| 382 | std::string value; |
| 383 | EXPECT_TRUE(json->GetString("id", &value)); |
| 384 | EXPECT_EQ(test_data::kClaimTicketId, value); |
| 385 | EXPECT_TRUE( |
| 386 | json->GetString("deviceDraft.channel.supportedType", &value)); |
| 387 | EXPECT_EQ("pull", value); |
| 388 | EXPECT_TRUE(json->GetString("oauthClientId", &value)); |
| 389 | EXPECT_EQ(test_data::kClientId, value); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 390 | EXPECT_TRUE(json->GetString("deviceDraft.description", &value)); |
| 391 | EXPECT_EQ("Easy to clean", value); |
| 392 | EXPECT_TRUE(json->GetString("deviceDraft.location", &value)); |
| 393 | EXPECT_EQ("Kitchen", value); |
| 394 | EXPECT_TRUE(json->GetString("deviceDraft.modelManifestId", &value)); |
| 395 | EXPECT_EQ("AAAAA", value); |
| 396 | EXPECT_TRUE(json->GetString("deviceDraft.name", &value)); |
| 397 | EXPECT_EQ("Coffee Pot", value); |
| 398 | base::DictionaryValue* commandDefs = nullptr; |
| 399 | EXPECT_TRUE( |
| 400 | json->GetDictionary("deviceDraft.commandDefs", &commandDefs)); |
| 401 | EXPECT_FALSE(commandDefs->empty()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 402 | |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 403 | auto expected = R"({ |
| 404 | 'base': { |
| 405 | 'reboot': { |
| 406 | 'parameters': { |
| 407 | 'delay': { |
| 408 | 'minimum': 10, |
| 409 | 'type': 'integer' |
| 410 | } |
| 411 | }, |
| 412 | 'minimalRole': 'user' |
| 413 | } |
| 414 | }, |
| 415 | 'robot': { |
| 416 | '_jump': { |
| 417 | 'parameters': { |
| 418 | '_height': { |
| 419 | 'type': 'integer' |
| 420 | } |
| 421 | }, |
| 422 | 'minimalRole': 'user' |
| 423 | } |
| 424 | } |
| 425 | })"; |
| 426 | EXPECT_JSON_EQ(expected, *commandDefs); |
| 427 | |
| 428 | base::DictionaryValue json_resp; |
| 429 | json_resp.SetString("id", test_data::kClaimTicketId); |
Vitaly Buka | 88272d5 | 2015-11-17 17:04:01 -0800 | [diff] [blame] | 430 | json_resp.SetString("kind", "weave#registrationTicket"); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 431 | json_resp.SetString("oauthClientId", test_data::kClientId); |
| 432 | base::DictionaryValue* device_draft = nullptr; |
| 433 | EXPECT_TRUE(json->GetDictionary("deviceDraft", &device_draft)); |
| 434 | device_draft = device_draft->DeepCopy(); |
| 435 | device_draft->SetString("id", test_data::kDeviceId); |
Vitaly Buka | 88272d5 | 2015-11-17 17:04:01 -0800 | [diff] [blame] | 436 | device_draft->SetString("kind", "weave#device"); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 437 | json_resp.Set("deviceDraft", device_draft); |
| 438 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 439 | callback.Run(ReplyWithJson(200, json_resp), nullptr); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 440 | }))); |
| 441 | |
| 442 | EXPECT_CALL(http_client_, |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 443 | SendRequest(HttpClient::Method::kPost, |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 444 | ticket_url + "/finalize?key=" + test_data::kApiKey, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 445 | HttpClient::Headers{}, _, _)) |
| 446 | .WillOnce(WithArgs<4>( |
| 447 | Invoke([](const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 448 | base::DictionaryValue json; |
| 449 | json.SetString("id", test_data::kClaimTicketId); |
Vitaly Buka | 88272d5 | 2015-11-17 17:04:01 -0800 | [diff] [blame] | 450 | json.SetString("kind", "weave#registrationTicket"); |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 451 | json.SetString("oauthClientId", test_data::kClientId); |
| 452 | json.SetString("userEmail", "user@email.com"); |
| 453 | json.SetString("deviceDraft.id", test_data::kDeviceId); |
Vitaly Buka | 88272d5 | 2015-11-17 17:04:01 -0800 | [diff] [blame] | 454 | json.SetString("deviceDraft.kind", "weave#device"); |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 455 | json.SetString("deviceDraft.channel.supportedType", "xmpp"); |
| 456 | json.SetString("robotAccountEmail", test_data::kRobotAccountEmail); |
| 457 | json.SetString("robotAccountAuthorizationCode", |
| 458 | test_data::kRobotAccountAuthCode); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 459 | callback.Run(ReplyWithJson(200, json), nullptr); |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 460 | }))); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 461 | |
Vitaly Buka | 1a42e14 | 2015-10-10 18:15:15 -0700 | [diff] [blame] | 462 | EXPECT_CALL( |
| 463 | http_client_, |
| 464 | SendRequest(HttpClient::Method::kPost, dev_reg_->GetOAuthURL("token"), |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 465 | HttpClient::Headers{GetFormHeader()}, _, _)) |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 466 | .WillOnce(WithArgs<3, 4>(Invoke([]( |
| 467 | const std::string& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 468 | const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 469 | EXPECT_EQ("authorization_code", GetFormField(data, "grant_type")); |
| 470 | EXPECT_EQ(test_data::kRobotAccountAuthCode, GetFormField(data, "code")); |
| 471 | EXPECT_EQ(test_data::kClientId, GetFormField(data, "client_id")); |
| 472 | EXPECT_EQ(test_data::kClientSecret, |
| 473 | GetFormField(data, "client_secret")); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 474 | EXPECT_EQ("oob", GetFormField(data, "redirect_uri")); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 475 | |
| 476 | base::DictionaryValue json; |
| 477 | json.SetString("access_token", test_data::kAccessToken); |
| 478 | json.SetString("token_type", "Bearer"); |
| 479 | json.SetString("refresh_token", test_data::kRefreshToken); |
| 480 | json.SetInteger("expires_in", 3600); |
| 481 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 482 | callback.Run(ReplyWithJson(200, json), nullptr); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 483 | }))); |
| 484 | |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 485 | bool done = false; |
| 486 | dev_reg_->RegisterDevice( |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 487 | test_data::kClaimTicketId, base::Bind([this, &done](ErrorPtr error) { |
| 488 | EXPECT_FALSE(error); |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 489 | done = true; |
| 490 | task_runner_.Break(); |
| 491 | EXPECT_EQ(GcdState::kConnecting, GetGcdState()); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 492 | |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 493 | // Validate the device info saved to storage... |
| 494 | EXPECT_EQ(test_data::kDeviceId, dev_reg_->GetSettings().cloud_id); |
| 495 | EXPECT_EQ(test_data::kRefreshToken, |
| 496 | dev_reg_->GetSettings().refresh_token); |
| 497 | EXPECT_EQ(test_data::kRobotAccountEmail, |
| 498 | dev_reg_->GetSettings().robot_account); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 499 | })); |
Vitaly Buka | 12870bd | 2015-10-08 23:49:39 -0700 | [diff] [blame] | 500 | task_runner_.Run(); |
| 501 | EXPECT_TRUE(done); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 504 | TEST_F(DeviceRegistrationInfoTest, OOBRegistrationStatus) { |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 505 | // After we've been initialized, we should be either offline or |
| 506 | // unregistered, depending on whether or not we've found credentials. |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 507 | EXPECT_EQ(GcdState::kUnconfigured, GetGcdState()); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 508 | // Put some credentials into our state, make sure we call that offline. |
Vitaly Buka | c11a17d | 2015-08-15 10:36:10 -0700 | [diff] [blame] | 509 | ReloadSettings(); |
Vitaly Buka | c3c6dab | 2015-10-01 19:41:02 -0700 | [diff] [blame] | 510 | EXPECT_EQ(GcdState::kConnecting, GetGcdState()); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 513 | class DeviceRegistrationInfoUpdateCommandTest |
| 514 | : public DeviceRegistrationInfoTest { |
| 515 | protected: |
| 516 | void SetUp() override { |
| 517 | DeviceRegistrationInfoTest::SetUp(); |
Alex Vakulenko | 266b2b1 | 2015-06-19 11:01:42 -0700 | [diff] [blame] | 518 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 519 | ReloadSettings(); |
| 520 | SetAccessToken(); |
| 521 | |
| 522 | auto json_cmds = CreateDictionaryValue(R"({ |
| 523 | 'robot': { |
| 524 | '_jump': { |
| 525 | 'parameters': {'_height': 'integer'}, |
| 526 | 'progress': {'progress': 'integer'}, |
| 527 | 'results': {'status': 'string'}, |
| 528 | 'minimalRole': 'user' |
| 529 | } |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 530 | } |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 531 | })"); |
| 532 | EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, nullptr)); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 533 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 534 | command_url_ = dev_reg_->GetServiceURL("commands/1234"); |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 535 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 536 | auto commands_json = CreateValue(R"([{ |
| 537 | 'name':'robot._jump', |
| 538 | 'id':'1234', |
| 539 | 'parameters': {'_height': 100}, |
| 540 | 'minimalRole': 'user' |
| 541 | }])"); |
| 542 | ASSERT_NE(nullptr, commands_json.get()); |
| 543 | const base::ListValue* command_list = nullptr; |
| 544 | ASSERT_TRUE(commands_json->GetAsList(&command_list)); |
| 545 | PublishCommands(*command_list); |
| 546 | command_ = command_manager_->FindCommand("1234"); |
| 547 | ASSERT_NE(nullptr, command_); |
| 548 | } |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 549 | |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 550 | void TearDown() override { |
| 551 | task_runner_.RunOnce(); |
| 552 | DeviceRegistrationInfoTest::TearDown(); |
| 553 | } |
| 554 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 555 | Command* command_{nullptr}; |
| 556 | std::string command_url_; |
| 557 | }; |
| 558 | |
| 559 | TEST_F(DeviceRegistrationInfoUpdateCommandTest, SetProgress) { |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 560 | EXPECT_CALL( |
| 561 | http_client_, |
| 562 | SendRequest(HttpClient::Method::kPatch, command_url_, |
| 563 | HttpClient::Headers{GetAuthHeader(), GetJsonHeader()}, _, _)) |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 564 | .WillOnce(WithArgs<3, 4>(Invoke([]( |
| 565 | const std::string& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 566 | const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 567 | EXPECT_JSON_EQ((R"({"state":"inProgress","progress":{"progress":18}})"), |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 568 | *CreateDictionaryValue(data)); |
| 569 | base::DictionaryValue json; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 570 | callback.Run(ReplyWithJson(200, json), nullptr); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 571 | }))); |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 572 | EXPECT_TRUE(command_->SetProgress(*CreateDictionaryValue("{'progress':18}"), |
| 573 | nullptr)); |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 574 | } |
Alex Vakulenko | 808e2d8 | 2015-04-08 15:45:56 -0700 | [diff] [blame] | 575 | |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 576 | TEST_F(DeviceRegistrationInfoUpdateCommandTest, Complete) { |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 577 | EXPECT_CALL( |
| 578 | http_client_, |
| 579 | SendRequest(HttpClient::Method::kPatch, command_url_, |
| 580 | HttpClient::Headers{GetAuthHeader(), GetJsonHeader()}, _, _)) |
Vitaly Buka | f0a3b22 | 2015-10-16 01:49:36 -0700 | [diff] [blame] | 581 | .WillOnce(WithArgs<3, 4>( |
| 582 | Invoke([](const std::string& data, |
| 583 | const HttpClient::SendRequestCallback& callback) { |
| 584 | EXPECT_JSON_EQ(R"({"state":"done", "results":{"status":"Ok"}})", |
| 585 | *CreateDictionaryValue(data)); |
| 586 | base::DictionaryValue json; |
| 587 | callback.Run(ReplyWithJson(200, json), nullptr); |
| 588 | }))); |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 589 | EXPECT_TRUE( |
| 590 | command_->Complete(*CreateDictionaryValue("{'status': 'Ok'}"), nullptr)); |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | TEST_F(DeviceRegistrationInfoUpdateCommandTest, Cancel) { |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 594 | EXPECT_CALL( |
| 595 | http_client_, |
| 596 | SendRequest(HttpClient::Method::kPatch, command_url_, |
| 597 | HttpClient::Headers{GetAuthHeader(), GetJsonHeader()}, _, _)) |
Vitaly Buka | 1728037 | 2015-10-10 17:11:47 -0700 | [diff] [blame] | 598 | .WillOnce(WithArgs<3, 4>(Invoke([]( |
| 599 | const std::string& data, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 600 | const HttpClient::SendRequestCallback& callback) { |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 601 | EXPECT_JSON_EQ(R"({"state":"cancelled"})", |
| 602 | *CreateDictionaryValue(data)); |
| 603 | base::DictionaryValue json; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 604 | callback.Run(ReplyWithJson(200, json), nullptr); |
Vitaly Buka | 1020618 | 2015-08-05 11:17:43 -0700 | [diff] [blame] | 605 | }))); |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 606 | EXPECT_TRUE(command_->Cancel(nullptr)); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 607 | } |
| 608 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 609 | } // namespace weave |