Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -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/base_api_handler.h" |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 6 | |
| 7 | #include <base/strings/string_number_conversions.h> |
| 8 | #include <base/values.h> |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 9 | #include <gtest/gtest.h> |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 10 | #include <weave/provider/test/mock_config_store.h> |
| 11 | #include <weave/provider/test/mock_http_client.h> |
Vitaly Buka | 266e88b | 2015-10-06 11:33:00 -0700 | [diff] [blame] | 12 | #include <weave/test/mock_device.h> |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 13 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 14 | #include "src/commands/command_manager.h" |
| 15 | #include "src/commands/unittest_utils.h" |
| 16 | #include "src/config.h" |
| 17 | #include "src/device_registration_info.h" |
| 18 | #include "src/states/mock_state_change_queue_interface.h" |
| 19 | #include "src/states/state_manager.h" |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 20 | |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 21 | using testing::_; |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 22 | using testing::AnyOf; |
| 23 | using testing::Eq; |
Vitaly Buka | 9acf8aa | 2015-08-16 01:55:41 -0700 | [diff] [blame] | 24 | using testing::Invoke; |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 25 | using testing::Return; |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 26 | using testing::ReturnRef; |
Vitaly Buka | 9acf8aa | 2015-08-16 01:55:41 -0700 | [diff] [blame] | 27 | using testing::StrictMock; |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 28 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 29 | namespace weave { |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 30 | |
| 31 | class BaseApiHandlerTest : public ::testing::Test { |
| 32 | protected: |
| 33 | void SetUp() override { |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 34 | EXPECT_CALL(mock_state_change_queue_, NotifyPropertiesUpdated(_, _)) |
| 35 | .WillRepeatedly(Return(true)); |
| 36 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 37 | command_manager_ = std::make_shared<CommandManager>(); |
Vitaly Buka | 3110deb | 2015-10-06 19:54:09 -0700 | [diff] [blame] | 38 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 39 | state_manager_ = std::make_shared<StateManager>(&mock_state_change_queue_); |
Vitaly Buka | 266e88b | 2015-10-06 11:33:00 -0700 | [diff] [blame] | 40 | |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 41 | EXPECT_CALL(device_, AddStateDefinitionsFromJson(_)) |
| 42 | .WillRepeatedly(Invoke([this](const std::string& json) { |
| 43 | EXPECT_TRUE( |
| 44 | state_manager_->LoadStateDefinitionFromJson(json, nullptr)); |
| 45 | })); |
Vitaly Buka | 266e88b | 2015-10-06 11:33:00 -0700 | [diff] [blame] | 46 | EXPECT_CALL(device_, SetStateProperties(_, _)) |
| 47 | .WillRepeatedly( |
| 48 | Invoke(state_manager_.get(), &StateManager::SetProperties)); |
Vitaly Buka | 3110deb | 2015-10-06 19:54:09 -0700 | [diff] [blame] | 49 | EXPECT_CALL(device_, AddCommandDefinitionsFromJson(_)) |
| 50 | .WillRepeatedly(Invoke([this](const std::string& json) { |
| 51 | EXPECT_TRUE(command_manager_->LoadCommands(json, nullptr)); |
| 52 | })); |
| 53 | |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 54 | EXPECT_CALL(device_, AddCommandHandler(AnyOf("base.updateBaseConfiguration", |
| 55 | "base.updateDeviceInfo"), |
| 56 | _)) |
| 57 | .WillRepeatedly( |
| 58 | Invoke(command_manager_.get(), &CommandManager::AddCommandHandler)); |
Vitaly Buka | 266e88b | 2015-10-06 11:33:00 -0700 | [diff] [blame] | 59 | |
Vitaly Buka | 9acf8aa | 2015-08-16 01:55:41 -0700 | [diff] [blame] | 60 | std::unique_ptr<Config> config{new Config{&config_store_}}; |
| 61 | config->Load(); |
| 62 | dev_reg_.reset(new DeviceRegistrationInfo(command_manager_, state_manager_, |
Vitaly Buka | 41a90d6 | 2015-09-29 16:58:39 -0700 | [diff] [blame] | 63 | std::move(config), nullptr, |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 64 | &http_client_, nullptr)); |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 65 | |
| 66 | EXPECT_CALL(device_, GetSettings()) |
| 67 | .WillRepeatedly(ReturnRef(dev_reg_->GetSettings())); |
| 68 | |
Vitaly Buka | 266e88b | 2015-10-06 11:33:00 -0700 | [diff] [blame] | 69 | handler_.reset(new BaseApiHandler{dev_reg_.get(), &device_}); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 72 | void AddCommand(const std::string& command) { |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 73 | auto command_instance = CommandInstance::FromJson( |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 74 | test::CreateDictionaryValue(command.c_str()).get(), |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 75 | Command::Origin::kLocal, command_manager_->GetCommandDictionary(), |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 76 | nullptr, nullptr); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 77 | EXPECT_TRUE(!!command_instance); |
| 78 | |
| 79 | std::string id{base::IntToString(++command_id_)}; |
| 80 | command_instance->SetID(id); |
| 81 | command_manager_->AddCommand(std::move(command_instance)); |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 82 | EXPECT_EQ(Command::State::kDone, |
| 83 | command_manager_->FindCommand(id)->GetState()); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 86 | std::unique_ptr<base::DictionaryValue> GetBaseState() { |
| 87 | auto state = state_manager_->GetState(); |
| 88 | std::set<std::string> result; |
| 89 | for (base::DictionaryValue::Iterator it{*state}; !it.IsAtEnd(); |
| 90 | it.Advance()) { |
| 91 | if (it.key() != "base") |
| 92 | state->Remove(it.key(), nullptr); |
| 93 | } |
| 94 | return state; |
| 95 | } |
| 96 | |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 97 | provider::test::MockConfigStore config_store_; |
| 98 | StrictMock<provider::test::MockHttpClient> http_client_; |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 99 | std::unique_ptr<DeviceRegistrationInfo> dev_reg_; |
| 100 | std::shared_ptr<CommandManager> command_manager_; |
Vitaly Buka | 6b61e57 | 2015-08-05 23:16:13 -0700 | [diff] [blame] | 101 | testing::StrictMock<MockStateChangeQueueInterface> mock_state_change_queue_; |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 102 | std::shared_ptr<StateManager> state_manager_; |
| 103 | std::unique_ptr<BaseApiHandler> handler_; |
Vitaly Buka | 266e88b | 2015-10-06 11:33:00 -0700 | [diff] [blame] | 104 | StrictMock<test::MockDevice> device_; |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 105 | int command_id_{0}; |
| 106 | }; |
| 107 | |
Vitaly Buka | 80d6653 | 2015-10-27 23:06:11 -0700 | [diff] [blame] | 108 | TEST_F(BaseApiHandlerTest, Initialization) { |
| 109 | auto command_defs = |
| 110 | command_manager_->GetCommandDictionary().GetCommandsAsJson( |
| 111 | [](const CommandDefinition* def) { return true; }, true, nullptr); |
| 112 | |
| 113 | auto expected = R"({ |
| 114 | "base": { |
| 115 | "updateBaseConfiguration": { |
| 116 | "minimalRole": "manager", |
| 117 | "parameters": { |
| 118 | "localAnonymousAccessMaxRole": { |
| 119 | "enum": [ "none", "viewer", "user" ], |
| 120 | "type": "string" |
| 121 | }, |
| 122 | "localDiscoveryEnabled": { |
| 123 | "type": "boolean" |
| 124 | }, |
| 125 | "localPairingEnabled": { |
| 126 | "type": "boolean" |
| 127 | } |
| 128 | } |
| 129 | }, |
| 130 | "updateDeviceInfo": { |
| 131 | "minimalRole": "manager", |
| 132 | "parameters": { |
| 133 | "description": { |
| 134 | "type": "string" |
| 135 | }, |
| 136 | "location": { |
| 137 | "type": "string" |
| 138 | }, |
| 139 | "name": { |
| 140 | "type": "string" |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | })"; |
| 146 | EXPECT_JSON_EQ(expected, *command_defs); |
| 147 | } |
| 148 | |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 149 | TEST_F(BaseApiHandlerTest, UpdateBaseConfiguration) { |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 150 | const Settings& settings = dev_reg_->GetSettings(); |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 151 | |
| 152 | AddCommand(R"({ |
| 153 | 'name' : 'base.updateBaseConfiguration', |
| 154 | 'parameters': { |
| 155 | 'localDiscoveryEnabled': false, |
| 156 | 'localAnonymousAccessMaxRole': 'none', |
| 157 | 'localPairingEnabled': false |
| 158 | } |
| 159 | })"); |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 160 | EXPECT_EQ(AuthScope::kNone, settings.local_anonymous_access_role); |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 161 | EXPECT_FALSE(settings.local_discovery_enabled); |
| 162 | EXPECT_FALSE(settings.local_pairing_enabled); |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 163 | |
| 164 | auto expected = R"({ |
| 165 | 'base': { |
Vitaly Buka | a05eadb | 2015-09-29 16:38:24 -0700 | [diff] [blame] | 166 | 'firmwareVersion': 'TEST_FIRMWARE', |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 167 | 'localAnonymousAccessMaxRole': 'none', |
| 168 | 'localDiscoveryEnabled': false, |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 169 | 'localPairingEnabled': false |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 170 | } |
| 171 | })"; |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 172 | EXPECT_JSON_EQ(expected, *GetBaseState()); |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 173 | |
| 174 | AddCommand(R"({ |
| 175 | 'name' : 'base.updateBaseConfiguration', |
| 176 | 'parameters': { |
| 177 | 'localDiscoveryEnabled': true, |
| 178 | 'localAnonymousAccessMaxRole': 'user', |
| 179 | 'localPairingEnabled': true |
| 180 | } |
| 181 | })"); |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 182 | EXPECT_EQ(AuthScope::kUser, settings.local_anonymous_access_role); |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 183 | EXPECT_TRUE(settings.local_discovery_enabled); |
| 184 | EXPECT_TRUE(settings.local_pairing_enabled); |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 185 | expected = R"({ |
| 186 | 'base': { |
Vitaly Buka | a05eadb | 2015-09-29 16:38:24 -0700 | [diff] [blame] | 187 | 'firmwareVersion': 'TEST_FIRMWARE', |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 188 | 'localAnonymousAccessMaxRole': 'user', |
| 189 | 'localDiscoveryEnabled': true, |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 190 | 'localPairingEnabled': true |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 191 | } |
| 192 | })"; |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 193 | EXPECT_JSON_EQ(expected, *GetBaseState()); |
Vitaly Buka | 4c808b2 | 2015-08-02 13:44:52 -0700 | [diff] [blame] | 194 | |
| 195 | { |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 196 | Config::Transaction change{dev_reg_->GetMutableConfig()}; |
Vitaly Buka | b624bc4 | 2015-09-29 19:13:55 -0700 | [diff] [blame] | 197 | change.set_local_anonymous_access_role(AuthScope::kViewer); |
Vitaly Buka | 4c808b2 | 2015-08-02 13:44:52 -0700 | [diff] [blame] | 198 | } |
| 199 | expected = R"({ |
| 200 | 'base': { |
Vitaly Buka | a05eadb | 2015-09-29 16:38:24 -0700 | [diff] [blame] | 201 | 'firmwareVersion': 'TEST_FIRMWARE', |
Vitaly Buka | 4c808b2 | 2015-08-02 13:44:52 -0700 | [diff] [blame] | 202 | 'localAnonymousAccessMaxRole': 'viewer', |
| 203 | 'localDiscoveryEnabled': true, |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 204 | 'localPairingEnabled': true |
Vitaly Buka | 4c808b2 | 2015-08-02 13:44:52 -0700 | [diff] [blame] | 205 | } |
| 206 | })"; |
Vitaly Buka | 68af387 | 2015-10-27 16:19:00 -0700 | [diff] [blame] | 207 | EXPECT_JSON_EQ(expected, *GetBaseState()); |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 210 | TEST_F(BaseApiHandlerTest, UpdateDeviceInfo) { |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 211 | AddCommand(R"({ |
| 212 | 'name' : 'base.updateDeviceInfo', |
| 213 | 'parameters': { |
| 214 | 'name': 'testName', |
| 215 | 'description': 'testDescription', |
| 216 | 'location': 'testLocation' |
| 217 | } |
| 218 | })"); |
| 219 | |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 220 | const Settings& config = dev_reg_->GetSettings(); |
| 221 | EXPECT_EQ("testName", config.name); |
| 222 | EXPECT_EQ("testDescription", config.description); |
| 223 | EXPECT_EQ("testLocation", config.location); |
Alex Vakulenko | 7e8df46 | 2015-07-07 10:59:20 -0700 | [diff] [blame] | 224 | |
| 225 | AddCommand(R"({ |
| 226 | 'name' : 'base.updateDeviceInfo', |
| 227 | 'parameters': { |
| 228 | 'location': 'newLocation' |
| 229 | } |
| 230 | })"); |
| 231 | |
Vitaly Buka | 5cf12a3 | 2015-09-15 21:25:48 -0700 | [diff] [blame] | 232 | EXPECT_EQ("testName", config.name); |
| 233 | EXPECT_EQ("testDescription", config.description); |
| 234 | EXPECT_EQ("newLocation", config.location); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 237 | } // namespace weave |