Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <base/json/json_reader.h> |
| 6 | #include <base/values.h> |
Alex Vakulenko | cf0b33a | 2014-08-20 15:02:10 -0700 | [diff] [blame] | 7 | #include <chromeos/bind_lambda.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 8 | #include <chromeos/http/http_request.h> |
| 9 | #include <chromeos/http/http_transport_fake.h> |
Alex Vakulenko | 3aeea1c | 2014-08-20 16:33:12 -0700 | [diff] [blame] | 10 | #include <chromeos/mime_utils.h> |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 11 | #include <gtest/gtest.h> |
| 12 | |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 13 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 14 | #include "buffet/commands/unittest_utils.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 15 | #include "buffet/device_registration_info.h" |
| 16 | #include "buffet/device_registration_storage_keys.h" |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 17 | #include "buffet/states/mock_state_change_queue_interface.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 18 | #include "buffet/states/state_manager.h" |
Christopher Wiley | 006e94e | 2014-05-02 13:44:48 -0700 | [diff] [blame] | 19 | #include "buffet/storage_impls.h" |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 20 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 21 | using chromeos::http::request_header::kAuthorization; |
| 22 | using chromeos::http::fake::ServerRequest; |
| 23 | using chromeos::http::fake::ServerResponse; |
| 24 | |
| 25 | namespace buffet { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 26 | |
| 27 | namespace { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 28 | |
| 29 | namespace test_data { |
| 30 | |
| 31 | const char kServiceURL[] = "http://gcd.server.com/"; |
| 32 | const char kOAuthURL[] = "http://oauth.server.com/"; |
| 33 | const char kApiKey[] = "GOadRdTf9FERf0k4w6EFOof56fUJ3kFDdFL3d7f"; |
| 34 | const char kClientId[] = "123543821385-sfjkjshdkjhfk234sdfsdfkskd" |
| 35 | "fkjh7f.apps.googleusercontent.com"; |
| 36 | const char kClientSecret[] = "5sdGdGlfolGlrFKfdFlgP6FG"; |
| 37 | const char kDeviceId[] = "4a7ea2d1-b331-1e1f-b206-e863c7635196"; |
| 38 | const char kClaimTicketId[] = "RTcUE"; |
| 39 | const char kAccessToken[] = "ya29.1.AADtN_V-dLUM-sVZ0qVjG9Dxm5NgdS9J" |
| 40 | "Mx_JLUqhC9bED_YFjzHZtYt65ZzXCS35NMAeaVZ" |
| 41 | "Dei530-w0yE2urpQ"; |
| 42 | const char kRefreshToken[] = "1/zQmxR6PKNvhcxf9SjXUrCjcmCrcqRKXctc6cp" |
| 43 | "1nI-GQ"; |
| 44 | const char kRobotAccountAuthCode[] = "4/Mf_ujEhPejVhOq-OxW9F5cSOnWzx." |
| 45 | "YgciVjTYGscRshQV0ieZDAqiTIjMigI"; |
| 46 | const char kRobotAccountEmail[] = "6ed0b3f54f9bd619b942f4ad2441c252@" |
| 47 | "clouddevices.gserviceaccount.com"; |
| 48 | const char kUserAccountAuthCode[] = "2/sd_GD1TGFKpJOLJ34-0g5fK0fflp.GlT" |
| 49 | "I0F5g7hNtFgj5HFGOf8FlGK9eflO"; |
| 50 | const char kUserAccessToken[] = "sd56.4.FGDjG_F-gFGF-dFG6gGOG9Dxm5NgdS9" |
| 51 | "JMx_JLUqhC9bED_YFjLKjlkjLKJlkjLKjlKJea" |
| 52 | "VZDei530-w0yE2urpQ"; |
| 53 | const char kUserRefreshToken[] = "1/zQLKjlKJlkLkLKjLkjLKjLkjLjLkjl0ftc6" |
| 54 | "cp1nI-GQ"; |
| 55 | |
| 56 | } // namespace test_data |
| 57 | |
| 58 | // Fill in the storage with default environment information (URLs, etc). |
| 59 | void InitDefaultStorage(base::DictionaryValue* data) { |
| 60 | data->SetString(storage_keys::kClientId, test_data::kClientId); |
| 61 | data->SetString(storage_keys::kClientSecret, test_data::kClientSecret); |
| 62 | data->SetString(storage_keys::kApiKey, test_data::kApiKey); |
| 63 | data->SetString(storage_keys::kRefreshToken, ""); |
| 64 | data->SetString(storage_keys::kDeviceId, ""); |
| 65 | data->SetString(storage_keys::kOAuthURL, test_data::kOAuthURL); |
| 66 | data->SetString(storage_keys::kServiceURL, test_data::kServiceURL); |
| 67 | data->SetString(storage_keys::kRobotAccount, ""); |
| 68 | } |
| 69 | |
| 70 | // Add the test device registration information. |
| 71 | void SetDefaultDeviceRegistration(base::DictionaryValue* data) { |
| 72 | data->SetString(storage_keys::kRefreshToken, test_data::kRefreshToken); |
| 73 | data->SetString(storage_keys::kDeviceId, test_data::kDeviceId); |
| 74 | data->SetString(storage_keys::kRobotAccount, test_data::kRobotAccountEmail); |
| 75 | } |
| 76 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 77 | void OAuth2Handler(const ServerRequest& request, ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 78 | base::DictionaryValue json; |
| 79 | if (request.GetFormField("grant_type") == "refresh_token") { |
| 80 | // Refresh device access token. |
| 81 | EXPECT_EQ(test_data::kRefreshToken, request.GetFormField("refresh_token")); |
| 82 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 83 | EXPECT_EQ(test_data::kClientSecret, request.GetFormField("client_secret")); |
| 84 | json.SetString("access_token", test_data::kAccessToken); |
| 85 | } else if (request.GetFormField("grant_type") == "authorization_code") { |
| 86 | // Obtain access token. |
| 87 | std::string code = request.GetFormField("code"); |
| 88 | if (code == test_data::kUserAccountAuthCode) { |
| 89 | // Get user access token. |
| 90 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 91 | EXPECT_EQ(test_data::kClientSecret, |
| 92 | request.GetFormField("client_secret")); |
| 93 | EXPECT_EQ("urn:ietf:wg:oauth:2.0:oob", |
| 94 | request.GetFormField("redirect_uri")); |
| 95 | json.SetString("access_token", test_data::kUserAccessToken); |
| 96 | json.SetString("token_type", "Bearer"); |
| 97 | json.SetString("refresh_token", test_data::kUserRefreshToken); |
| 98 | } else if (code == test_data::kRobotAccountAuthCode) { |
| 99 | // Get device access token. |
| 100 | EXPECT_EQ(test_data::kClientId, request.GetFormField("client_id")); |
| 101 | EXPECT_EQ(test_data::kClientSecret, |
| 102 | request.GetFormField("client_secret")); |
| 103 | EXPECT_EQ("oob", request.GetFormField("redirect_uri")); |
| 104 | EXPECT_EQ("https://www.googleapis.com/auth/clouddevices", |
| 105 | request.GetFormField("scope")); |
| 106 | json.SetString("access_token", test_data::kAccessToken); |
| 107 | json.SetString("token_type", "Bearer"); |
| 108 | json.SetString("refresh_token", test_data::kRefreshToken); |
| 109 | } else { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 110 | FAIL() << "Unexpected authorization code"; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 111 | } |
| 112 | } else { |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 113 | FAIL() << "Unexpected grant type"; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 114 | } |
| 115 | json.SetInteger("expires_in", 3600); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 116 | response->ReplyJson(chromeos::http::status_code::Ok, &json); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 119 | void DeviceInfoHandler(const ServerRequest& request, ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 120 | std::string auth = "Bearer "; |
| 121 | auth += test_data::kAccessToken; |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 122 | EXPECT_EQ(auth, |
| 123 | request.GetHeader(chromeos::http::request_header::kAuthorization)); |
| 124 | response->ReplyJson(chromeos::http::status_code::Ok, { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 125 | {"channel.supportedType", "xmpp"}, |
| 126 | {"deviceKind", "vendor"}, |
| 127 | {"id", test_data::kDeviceId}, |
| 128 | {"kind", "clouddevices#device"}, |
| 129 | }); |
| 130 | } |
| 131 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 132 | void FinalizeTicketHandler(const ServerRequest& request, |
| 133 | ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 134 | EXPECT_EQ(test_data::kApiKey, request.GetFormField("key")); |
| 135 | EXPECT_TRUE(request.GetData().empty()); |
| 136 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 137 | response->ReplyJson(chromeos::http::status_code::Ok, { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 138 | {"id", test_data::kClaimTicketId}, |
| 139 | {"kind", "clouddevices#registrationTicket"}, |
| 140 | {"oauthClientId", test_data::kClientId}, |
| 141 | {"userEmail", "user@email.com"}, |
| 142 | {"deviceDraft.id", test_data::kDeviceId}, |
| 143 | {"deviceDraft.kind", "clouddevices#device"}, |
| 144 | {"deviceDraft.channel.supportedType", "xmpp"}, |
| 145 | {"robotAccountEmail", test_data::kRobotAccountEmail}, |
| 146 | {"robotAccountAuthorizationCode", test_data::kRobotAccountAuthCode}, |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | } // anonymous namespace |
| 151 | |
| 152 | // This is a helper class that allows the unit tests to set the private |
| 153 | // member DeviceRegistrationInfo::ticket_id_, since TestHelper is declared |
| 154 | // as a friend to DeviceRegistrationInfo. |
| 155 | class DeviceRegistrationInfo::TestHelper { |
| 156 | public: |
| 157 | static void SetTestTicketId(DeviceRegistrationInfo* info) { |
| 158 | info->ticket_id_ = test_data::kClaimTicketId; |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | class DeviceRegistrationInfoTest : public ::testing::Test { |
| 163 | protected: |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 164 | void SetUp() override { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 165 | InitDefaultStorage(&data_); |
| 166 | storage_ = std::make_shared<MemStorage>(); |
| 167 | storage_->Save(&data_); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 168 | transport_ = std::make_shared<chromeos::http::fake::Transport>(); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 169 | command_manager_ = std::make_shared<CommandManager>(); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 170 | state_manager_ = std::make_shared<StateManager>(&mock_state_change_queue_); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 171 | dev_reg_ = std::unique_ptr<DeviceRegistrationInfo>( |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 172 | new DeviceRegistrationInfo(command_manager_, state_manager_, |
| 173 | transport_, storage_)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 176 | base::DictionaryValue data_; |
| 177 | std::shared_ptr<MemStorage> storage_; |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 178 | std::shared_ptr<chromeos::http::fake::Transport> transport_; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 179 | std::unique_ptr<DeviceRegistrationInfo> dev_reg_; |
| 180 | std::shared_ptr<CommandManager> command_manager_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 181 | testing::NiceMock<MockStateChangeQueueInterface> mock_state_change_queue_; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 182 | std::shared_ptr<StateManager> state_manager_; |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | //////////////////////////////////////////////////////////////////////////////// |
| 186 | TEST_F(DeviceRegistrationInfoTest, GetServiceURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 187 | EXPECT_TRUE(dev_reg_->Load()); |
| 188 | EXPECT_EQ(test_data::kServiceURL, dev_reg_->GetServiceURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 189 | std::string url = test_data::kServiceURL; |
| 190 | url += "registrationTickets"; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 191 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets")); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 192 | url += "?key="; |
| 193 | url += test_data::kApiKey; |
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 | {"key", test_data::kApiKey} |
| 196 | })); |
| 197 | url += "&restart=true"; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 198 | EXPECT_EQ(url, dev_reg_->GetServiceURL("registrationTickets", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 199 | {"key", test_data::kApiKey}, |
| 200 | {"restart", "true"}, |
| 201 | })); |
| 202 | } |
| 203 | |
| 204 | TEST_F(DeviceRegistrationInfoTest, GetOAuthURL) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 205 | EXPECT_TRUE(dev_reg_->Load()); |
| 206 | EXPECT_EQ(test_data::kOAuthURL, dev_reg_->GetOAuthURL()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 207 | std::string url = test_data::kOAuthURL; |
| 208 | url += "auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fclouddevices&"; |
| 209 | url += "redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&"; |
| 210 | url += "response_type=code&"; |
| 211 | url += "client_id="; |
| 212 | url += test_data::kClientId; |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 213 | EXPECT_EQ(url, dev_reg_->GetOAuthURL("auth", { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 214 | {"scope", "https://www.googleapis.com/auth/clouddevices"}, |
| 215 | {"redirect_uri", "urn:ietf:wg:oauth:2.0:oob"}, |
| 216 | {"response_type", "code"}, |
| 217 | {"client_id", test_data::kClientId} |
| 218 | })); |
| 219 | } |
| 220 | |
| 221 | TEST_F(DeviceRegistrationInfoTest, CheckRegistration) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 222 | EXPECT_TRUE(dev_reg_->Load()); |
| 223 | EXPECT_FALSE(dev_reg_->CheckRegistration(nullptr)); |
| 224 | EXPECT_EQ(0, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 225 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 226 | SetDefaultDeviceRegistration(&data_); |
| 227 | storage_->Save(&data_); |
| 228 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 229 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 230 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 231 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 232 | base::Bind(OAuth2Handler)); |
| 233 | transport_->ResetRequestCount(); |
| 234 | EXPECT_TRUE(dev_reg_->CheckRegistration(nullptr)); |
| 235 | EXPECT_EQ(1, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | TEST_F(DeviceRegistrationInfoTest, GetDeviceInfo) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 239 | SetDefaultDeviceRegistration(&data_); |
| 240 | storage_->Save(&data_); |
| 241 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 242 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 243 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 244 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 245 | base::Bind(OAuth2Handler)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 246 | transport_->AddHandler(dev_reg_->GetDeviceURL(), |
| 247 | chromeos::http::request_type::kGet, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 248 | base::Bind(DeviceInfoHandler)); |
| 249 | transport_->ResetRequestCount(); |
| 250 | auto device_info = dev_reg_->GetDeviceInfo(nullptr); |
| 251 | EXPECT_EQ(2, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 252 | EXPECT_NE(nullptr, device_info.get()); |
| 253 | base::DictionaryValue* dict = nullptr; |
| 254 | EXPECT_TRUE(device_info->GetAsDictionary(&dict)); |
| 255 | std::string id; |
| 256 | EXPECT_TRUE(dict->GetString("id", &id)); |
| 257 | EXPECT_EQ(test_data::kDeviceId, id); |
| 258 | } |
| 259 | |
| 260 | TEST_F(DeviceRegistrationInfoTest, GetDeviceId) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 261 | SetDefaultDeviceRegistration(&data_); |
| 262 | storage_->Save(&data_); |
| 263 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 264 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 265 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 266 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 267 | base::Bind(OAuth2Handler)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 268 | transport_->AddHandler(dev_reg_->GetDeviceURL(), |
| 269 | chromeos::http::request_type::kGet, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 270 | base::Bind(DeviceInfoHandler)); |
| 271 | std::string id = dev_reg_->GetDeviceId(nullptr); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 272 | EXPECT_EQ(test_data::kDeviceId, id); |
| 273 | } |
| 274 | |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 275 | TEST_F(DeviceRegistrationInfoTest, RegisterDevice) { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 276 | EXPECT_TRUE(dev_reg_->Load()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 277 | |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 278 | auto update_ticket = [](const ServerRequest& request, |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 279 | ServerResponse* response) { |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 280 | EXPECT_EQ(test_data::kApiKey, request.GetFormField("key")); |
| 281 | auto json = request.GetDataAsJson(); |
| 282 | EXPECT_NE(nullptr, json.get()); |
| 283 | std::string value; |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 284 | EXPECT_TRUE(json->GetString("id", &value)); |
| 285 | EXPECT_EQ(test_data::kClaimTicketId, value); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 286 | EXPECT_TRUE(json->GetString("deviceDraft.channel.supportedType", &value)); |
| 287 | EXPECT_EQ("xmpp", value); |
| 288 | EXPECT_TRUE(json->GetString("oauthClientId", &value)); |
| 289 | EXPECT_EQ(test_data::kClientId, value); |
| 290 | EXPECT_TRUE(json->GetString("deviceDraft.deviceKind", &value)); |
| 291 | EXPECT_EQ("vendor", value); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 292 | base::DictionaryValue* commandDefs = nullptr; |
| 293 | EXPECT_TRUE(json->GetDictionary("deviceDraft.commandDefs", &commandDefs)); |
| 294 | EXPECT_FALSE(commandDefs->empty()); |
| 295 | EXPECT_EQ("{'base':{'reboot':{'parameters':{" |
| 296 | "'delay':{'minimum':10,'type':'integer'}}}}," |
| 297 | "'robot':{'_jump':{'parameters':{" |
| 298 | "'_height':{'type':'integer'}}}}}", |
| 299 | buffet::unittests::ValueToString(commandDefs)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 300 | |
| 301 | base::DictionaryValue json_resp; |
| 302 | json_resp.SetString("id", test_data::kClaimTicketId); |
| 303 | json_resp.SetString("kind", "clouddevices#registrationTicket"); |
| 304 | json_resp.SetString("oauthClientId", test_data::kClientId); |
| 305 | base::DictionaryValue* device_draft = nullptr; |
| 306 | EXPECT_TRUE(json->GetDictionary("deviceDraft", &device_draft)); |
| 307 | device_draft = device_draft->DeepCopy(); |
| 308 | device_draft->SetString("id", test_data::kDeviceId); |
| 309 | device_draft->SetString("kind", "clouddevices#device"); |
| 310 | json_resp.Set("deviceDraft", device_draft); |
| 311 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 312 | response->ReplyJson(chromeos::http::status_code::Ok, &json_resp); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 315 | auto json_base = buffet::unittests::CreateDictionaryValue(R"({ |
| 316 | 'base': { |
| 317 | 'reboot': { |
| 318 | 'parameters': {'delay': 'integer'} |
| 319 | }, |
| 320 | 'shutdown': { |
| 321 | 'parameters': {} |
| 322 | } |
| 323 | } |
| 324 | })"); |
| 325 | EXPECT_TRUE(command_manager_->LoadBaseCommands(*json_base, nullptr)); |
| 326 | auto json_cmds = buffet::unittests::CreateDictionaryValue(R"({ |
| 327 | 'base': { |
| 328 | 'reboot': { |
| 329 | 'parameters': {'delay': {'minimum': 10}} |
| 330 | } |
| 331 | }, |
| 332 | 'robot': { |
| 333 | '_jump': { |
| 334 | 'parameters': {'_height': 'integer'} |
| 335 | } |
| 336 | } |
| 337 | })"); |
| 338 | EXPECT_TRUE(command_manager_->LoadCommands(*json_cmds, "", nullptr)); |
| 339 | |
Anton Muhin | a480314 | 2014-09-24 19:30:45 +0400 | [diff] [blame] | 340 | transport_->AddHandler(dev_reg_->GetServiceURL( |
| 341 | std::string("registrationTickets/") + test_data::kClaimTicketId), |
| 342 | chromeos::http::request_type::kPatch, |
| 343 | base::Bind(update_ticket)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 344 | std::string ticket_url = |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 345 | dev_reg_->GetServiceURL("registrationTickets/" + |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 346 | std::string(test_data::kClaimTicketId)); |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 347 | transport_->AddHandler(ticket_url + "/finalize", |
| 348 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 349 | base::Bind(FinalizeTicketHandler)); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 350 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 351 | transport_->AddHandler(dev_reg_->GetOAuthURL("token"), |
| 352 | chromeos::http::request_type::kPost, |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 353 | base::Bind(OAuth2Handler)); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 354 | storage_->reset_save_count(); |
| 355 | DeviceRegistrationInfo::TestHelper::SetTestTicketId(dev_reg_.get()); |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 356 | |
| 357 | std::map<std::string, std::string> params; |
| 358 | params["ticket_id"] = test_data::kClaimTicketId; |
| 359 | std::string device_id = dev_reg_->RegisterDevice(params, nullptr); |
| 360 | |
| 361 | EXPECT_EQ(test_data::kDeviceId, device_id); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 362 | EXPECT_EQ(1, |
| 363 | storage_->save_count()); // The device info must have been saved. |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 364 | EXPECT_EQ(3, transport_->GetRequestCount()); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 365 | |
| 366 | // Validate the device info saved to storage... |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 367 | auto storage_data = storage_->Load(); |
Alex Vakulenko | 8e34d39 | 2014-04-29 11:02:56 -0700 | [diff] [blame] | 368 | base::DictionaryValue* dict = nullptr; |
| 369 | EXPECT_TRUE(storage_data->GetAsDictionary(&dict)); |
| 370 | std::string value; |
| 371 | EXPECT_TRUE(dict->GetString(storage_keys::kApiKey, &value)); |
| 372 | EXPECT_EQ(test_data::kApiKey, value); |
| 373 | EXPECT_TRUE(dict->GetString(storage_keys::kClientId, &value)); |
| 374 | EXPECT_EQ(test_data::kClientId, value); |
| 375 | EXPECT_TRUE(dict->GetString(storage_keys::kClientSecret, &value)); |
| 376 | EXPECT_EQ(test_data::kClientSecret, value); |
| 377 | EXPECT_TRUE(dict->GetString(storage_keys::kDeviceId, &value)); |
| 378 | EXPECT_EQ(test_data::kDeviceId, value); |
| 379 | EXPECT_TRUE(dict->GetString(storage_keys::kOAuthURL, &value)); |
| 380 | EXPECT_EQ(test_data::kOAuthURL, value); |
| 381 | EXPECT_TRUE(dict->GetString(storage_keys::kRefreshToken, &value)); |
| 382 | EXPECT_EQ(test_data::kRefreshToken, value); |
| 383 | EXPECT_TRUE(dict->GetString(storage_keys::kRobotAccount, &value)); |
| 384 | EXPECT_EQ(test_data::kRobotAccountEmail, value); |
| 385 | EXPECT_TRUE(dict->GetString(storage_keys::kServiceURL, &value)); |
| 386 | EXPECT_EQ(test_data::kServiceURL, value); |
| 387 | } |
| 388 | |
Alex Vakulenko | cca2093 | 2014-08-20 17:35:12 -0700 | [diff] [blame] | 389 | } // namespace buffet |