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