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