Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: chrome/browser/policy/device_management_service_unittest.cc

Issue 5153002: Use a service to create device management backends. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/device_management_backend_impl.h" 5 #include "chrome/browser/policy/device_management_service.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/string_split.h" 8 #include "base/string_split.h"
9 #include "chrome/browser/browser_thread.h" 9 #include "chrome/browser/browser_thread.h"
10 #include "chrome/browser/policy/device_management_backend_mock.h" 10 #include "chrome/browser/policy/device_management_backend_mock.h"
11 #include "chrome/common/net/test_url_fetcher_factory.h" 11 #include "chrome/common/net/test_url_fetcher_factory.h"
12 #include "chrome/test/test_url_request_context_getter.h"
12 #include "net/base/escape.h" 13 #include "net/base/escape.h"
13 #include "net/url_request/url_request_status.h" 14 #include "net/url_request/url_request_status.h"
15 #include "net/url_request/url_request_unittest.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
16 18
17 using testing::_; 19 using testing::_;
18 20
19 namespace policy { 21 namespace policy {
20 22
21 namespace { 23 namespace {
22 24
23 const char kServiceURL[] = "https://example.com/management_service"; 25 const char kServiceURL[] = "https://example.com/management_service";
24 26
25 // Encoded error response messages for testing the error code paths. 27 // Encoded error response messages for testing the error code paths.
26 const char kResponseEmpty[] = "\x08\x00"; 28 const char kResponseEmpty[] = "\x08\x00";
27 const char kResponseErrorManagementNotSupported[] = "\x08\x01"; 29 const char kResponseErrorManagementNotSupported[] = "\x08\x01";
28 const char kResponseErrorDeviceNotFound[] = "\x08\x02"; 30 const char kResponseErrorDeviceNotFound[] = "\x08\x02";
29 const char kResponseErrorManagementTokenInvalid[] = "\x08\x03"; 31 const char kResponseErrorManagementTokenInvalid[] = "\x08\x03";
30 const char kResponseErrorActivationPending[] = "\x08\x04"; 32 const char kResponseErrorActivationPending[] = "\x08\x04";
31 33
32 #define PROTO_STRING(name) (std::string(name, arraysize(name) - 1)) 34 #define PROTO_STRING(name) (std::string(name, arraysize(name) - 1))
33 35
34 } // namespace 36 } // namespace
35 37
36 // Unit tests for the google apps policy backend. The pattern here is each test 38 // Unit tests for the device management policy backend. The tests are run
37 // case triggeres a request and installs a mock delegate. The test will run and 39 // against a TestURLFetcherFactory that is used to short-circuit the request
38 // the default action installed on the test delegate will quit the loop. 40 // without calling into the actual network stack.
39 template<typename TESTBASE> 41 template<typename TESTBASE>
40 class DeviceManagementBackendImplTestBase : public TESTBASE { 42 class DeviceManagementServiceTestBase : public TESTBASE {
41 protected: 43 protected:
42 DeviceManagementBackendImplTestBase() 44 DeviceManagementServiceTestBase()
43 : io_thread_(BrowserThread::IO, &loop_), 45 : request_context_(new TestURLRequestContextGetter()),
44 service_(kServiceURL) {} 46 io_thread_(BrowserThread::IO, &loop_) {
47 ResetBackend();
48 backend_->Initialize(request_context_.get());
49 }
45 50
46 virtual void SetUp() { 51 virtual void SetUp() {
47 URLFetcher::set_factory(&factory_); 52 URLFetcher::set_factory(&factory_);
48 } 53 }
49 54
50 virtual void TearDown() { 55 virtual void TearDown() {
51 URLFetcher::set_factory(NULL); 56 URLFetcher::set_factory(NULL);
57 service_.reset();
58 backend_.reset();
52 loop_.RunAllPending(); 59 loop_.RunAllPending();
53 } 60 }
54 61
62 void ResetBackend() {
63 service_.reset();
64 backend_.reset(new DeviceManagementService(kServiceURL));
65 service_.reset(backend_->CreateBackend());
66 }
67
68 TestURLFetcherFactory factory_;
69 scoped_refptr<TestURLRequestContextGetter> request_context_;
70 scoped_ptr<DeviceManagementService> backend_;
71 scoped_ptr<DeviceManagementBackend> service_;
72
73 private:
55 MessageLoopForUI loop_; 74 MessageLoopForUI loop_;
56 BrowserThread io_thread_; 75 BrowserThread io_thread_;
57 TestURLFetcherFactory factory_;
58 DeviceManagementBackendImpl service_;
59 }; 76 };
60 77
61 struct FailedRequestParams { 78 struct FailedRequestParams {
62 FailedRequestParams(DeviceManagementBackend::ErrorCode expected_error, 79 FailedRequestParams(DeviceManagementBackend::ErrorCode expected_error,
63 URLRequestStatus::Status request_status, 80 URLRequestStatus::Status request_status,
64 int http_status, 81 int http_status,
65 const std::string& response) 82 const std::string& response)
66 : expected_error_(expected_error), 83 : expected_error_(expected_error),
67 request_status_(request_status, 0), 84 request_status_(request_status, 0),
68 http_status_(http_status), 85 http_status_(http_status),
69 response_(response) {} 86 response_(response) {}
70 87
71 DeviceManagementBackend::ErrorCode expected_error_; 88 DeviceManagementBackend::ErrorCode expected_error_;
72 URLRequestStatus request_status_; 89 URLRequestStatus request_status_;
73 int http_status_; 90 int http_status_;
74 std::string response_; 91 std::string response_;
75 }; 92 };
76 93
77 // A parameterized test case for erroneous response situations, they're mostly 94 // A parameterized test case for erroneous response situations, they're mostly
78 // the same for all kinds of requests. 95 // the same for all kinds of requests.
79 class DeviceManagementBackendImplFailedRequestTest 96 class DeviceManagementServiceFailedRequestTest
80 : public DeviceManagementBackendImplTestBase< 97 : public DeviceManagementServiceTestBase<
81 testing::TestWithParam<FailedRequestParams> > { 98 testing::TestWithParam<FailedRequestParams> > {
82 }; 99 };
83 100
84 TEST_P(DeviceManagementBackendImplFailedRequestTest, RegisterRequest) { 101 TEST_P(DeviceManagementServiceFailedRequestTest, RegisterRequest) {
85 DeviceRegisterResponseDelegateMock mock; 102 DeviceRegisterResponseDelegateMock mock;
86 EXPECT_CALL(mock, OnError(GetParam().expected_error_)); 103 EXPECT_CALL(mock, OnError(GetParam().expected_error_));
87 em::DeviceRegisterRequest request; 104 em::DeviceRegisterRequest request;
88 service_.ProcessRegisterRequest("token", "device id", request, &mock); 105 service_->ProcessRegisterRequest("token", "device id", request, &mock);
89 TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 106 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
90 ASSERT_TRUE(fetcher); 107 ASSERT_TRUE(fetcher);
91 108
92 fetcher->delegate()->OnURLFetchComplete(fetcher, 109 fetcher->delegate()->OnURLFetchComplete(fetcher,
93 GURL(kServiceURL), 110 GURL(kServiceURL),
94 GetParam().request_status_, 111 GetParam().request_status_,
95 GetParam().http_status_, 112 GetParam().http_status_,
96 ResponseCookies(), 113 ResponseCookies(),
97 GetParam().response_); 114 GetParam().response_);
98 } 115 }
99 116
100 TEST_P(DeviceManagementBackendImplFailedRequestTest, UnregisterRequest) { 117 TEST_P(DeviceManagementServiceFailedRequestTest, UnregisterRequest) {
101 DeviceUnregisterResponseDelegateMock mock; 118 DeviceUnregisterResponseDelegateMock mock;
102 EXPECT_CALL(mock, OnError(GetParam().expected_error_)); 119 EXPECT_CALL(mock, OnError(GetParam().expected_error_));
103 em::DeviceUnregisterRequest request; 120 em::DeviceUnregisterRequest request;
104 service_.ProcessUnregisterRequest("token", request, &mock); 121 service_->ProcessUnregisterRequest("token", request, &mock);
105 TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 122 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
106 ASSERT_TRUE(fetcher); 123 ASSERT_TRUE(fetcher);
107 124
108 fetcher->delegate()->OnURLFetchComplete(fetcher, 125 fetcher->delegate()->OnURLFetchComplete(fetcher,
109 GURL(kServiceURL), 126 GURL(kServiceURL),
110 GetParam().request_status_, 127 GetParam().request_status_,
111 GetParam().http_status_, 128 GetParam().http_status_,
112 ResponseCookies(), 129 ResponseCookies(),
113 GetParam().response_); 130 GetParam().response_);
114 } 131 }
115 132
116 TEST_P(DeviceManagementBackendImplFailedRequestTest, PolicyRequest) { 133 TEST_P(DeviceManagementServiceFailedRequestTest, PolicyRequest) {
117 DevicePolicyResponseDelegateMock mock; 134 DevicePolicyResponseDelegateMock mock;
118 EXPECT_CALL(mock, OnError(GetParam().expected_error_)); 135 EXPECT_CALL(mock, OnError(GetParam().expected_error_));
119 em::DevicePolicyRequest request; 136 em::DevicePolicyRequest request;
120 request.set_policy_scope("Chrome"); 137 request.set_policy_scope("Chrome");
121 em::DevicePolicySettingRequest* setting_request = 138 em::DevicePolicySettingRequest* setting_request =
122 request.add_setting_request(); 139 request.add_setting_request();
123 setting_request->set_key("policy"); 140 setting_request->set_key("policy");
124 service_.ProcessPolicyRequest("token", request, &mock); 141 service_->ProcessPolicyRequest("token", request, &mock);
125 TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 142 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
126 ASSERT_TRUE(fetcher); 143 ASSERT_TRUE(fetcher);
127 144
128 fetcher->delegate()->OnURLFetchComplete(fetcher, 145 fetcher->delegate()->OnURLFetchComplete(fetcher,
129 GURL(kServiceURL), 146 GURL(kServiceURL),
130 GetParam().request_status_, 147 GetParam().request_status_,
131 GetParam().http_status_, 148 GetParam().http_status_,
132 ResponseCookies(), 149 ResponseCookies(),
133 GetParam().response_); 150 GetParam().response_);
134 } 151 }
135 152
136 INSTANTIATE_TEST_CASE_P( 153 INSTANTIATE_TEST_CASE_P(
137 DeviceManagementBackendImplFailedRequestTestInstance, 154 DeviceManagementServiceFailedRequestTestInstance,
138 DeviceManagementBackendImplFailedRequestTest, 155 DeviceManagementServiceFailedRequestTest,
139 testing::Values( 156 testing::Values(
140 FailedRequestParams( 157 FailedRequestParams(
141 DeviceManagementBackend::kErrorRequestFailed, 158 DeviceManagementBackend::kErrorRequestFailed,
142 URLRequestStatus::FAILED, 159 URLRequestStatus::FAILED,
143 200, 160 200,
144 PROTO_STRING(kResponseEmpty)), 161 PROTO_STRING(kResponseEmpty)),
145 FailedRequestParams( 162 FailedRequestParams(
146 DeviceManagementBackend::kErrorHttpStatus, 163 DeviceManagementBackend::kErrorHttpStatus,
147 URLRequestStatus::SUCCESS, 164 URLRequestStatus::SUCCESS,
148 500, 165 500,
(...skipping 17 matching lines...) Expand all
166 DeviceManagementBackend::kErrorServiceManagementTokenInvalid, 183 DeviceManagementBackend::kErrorServiceManagementTokenInvalid,
167 URLRequestStatus::SUCCESS, 184 URLRequestStatus::SUCCESS,
168 200, 185 200,
169 PROTO_STRING(kResponseErrorManagementTokenInvalid)), 186 PROTO_STRING(kResponseErrorManagementTokenInvalid)),
170 FailedRequestParams( 187 FailedRequestParams(
171 DeviceManagementBackend::kErrorServiceActivationPending, 188 DeviceManagementBackend::kErrorServiceActivationPending,
172 URLRequestStatus::SUCCESS, 189 URLRequestStatus::SUCCESS,
173 200, 190 200,
174 PROTO_STRING(kResponseErrorActivationPending)))); 191 PROTO_STRING(kResponseErrorActivationPending))));
175 192
176 class DeviceManagementBackendImplTest 193 class DeviceManagementServiceTest
177 : public DeviceManagementBackendImplTestBase<testing::Test> { 194 : public DeviceManagementServiceTestBase<testing::Test> {
178 }; 195 };
179 196
180 MATCHER_P(MessageEquals, reference, "") { 197 MATCHER_P(MessageEquals, reference, "") {
181 std::string reference_data; 198 std::string reference_data;
182 std::string arg_data; 199 std::string arg_data;
183 return arg.SerializeToString(&arg_data) && 200 return arg.SerializeToString(&arg_data) &&
184 reference.SerializeToString(&reference_data) && 201 reference.SerializeToString(&reference_data) &&
185 arg_data == reference_data; 202 arg_data == reference_data;
186 } 203 }
187 204
(...skipping 30 matching lines...) Expand all
218 } 235 }
219 } 236 }
220 return found; 237 return found;
221 } 238 }
222 239
223 private: 240 private:
224 typedef std::vector<std::pair<std::string, std::string> > ParamMap; 241 typedef std::vector<std::pair<std::string, std::string> > ParamMap;
225 ParamMap params_; 242 ParamMap params_;
226 }; 243 };
227 244
228 TEST_F(DeviceManagementBackendImplTest, RegisterRequest) { 245 TEST_F(DeviceManagementServiceTest, RegisterRequest) {
229 DeviceRegisterResponseDelegateMock mock; 246 DeviceRegisterResponseDelegateMock mock;
230 em::DeviceRegisterResponse expected_response; 247 em::DeviceRegisterResponse expected_response;
231 expected_response.set_device_management_token("mtoken"); 248 expected_response.set_device_management_token("mtoken");
232 EXPECT_CALL(mock, HandleRegisterResponse(MessageEquals(expected_response))); 249 EXPECT_CALL(mock, HandleRegisterResponse(MessageEquals(expected_response)));
233 em::DeviceRegisterRequest request; 250 em::DeviceRegisterRequest request;
234 service_.ProcessRegisterRequest("token", "device id", request, &mock); 251 service_->ProcessRegisterRequest("token", "device id", request, &mock);
235 TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 252 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
236 ASSERT_TRUE(fetcher); 253 ASSERT_TRUE(fetcher);
237 254
238 // Check the data the fetcher received. 255 // Check the data the fetcher received.
239 const GURL& request_url(fetcher->original_url()); 256 const GURL& request_url(fetcher->original_url());
240 const GURL service_url(kServiceURL); 257 const GURL service_url(kServiceURL);
241 EXPECT_EQ(service_url.scheme(), request_url.scheme()); 258 EXPECT_EQ(service_url.scheme(), request_url.scheme());
242 EXPECT_EQ(service_url.host(), request_url.host()); 259 EXPECT_EQ(service_url.host(), request_url.host());
243 EXPECT_EQ(service_url.port(), request_url.port()); 260 EXPECT_EQ(service_url.port(), request_url.port());
244 EXPECT_EQ(service_url.path(), request_url.path()); 261 EXPECT_EQ(service_url.path(), request_url.path());
(...skipping 16 matching lines...) Expand all
261 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); 278 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data));
262 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); 279 URLRequestStatus status(URLRequestStatus::SUCCESS, 0);
263 fetcher->delegate()->OnURLFetchComplete(fetcher, 280 fetcher->delegate()->OnURLFetchComplete(fetcher,
264 GURL(kServiceURL), 281 GURL(kServiceURL),
265 status, 282 status,
266 200, 283 200,
267 ResponseCookies(), 284 ResponseCookies(),
268 response_data); 285 response_data);
269 } 286 }
270 287
271 TEST_F(DeviceManagementBackendImplTest, UnregisterRequest) { 288 TEST_F(DeviceManagementServiceTest, UnregisterRequest) {
272 DeviceUnregisterResponseDelegateMock mock; 289 DeviceUnregisterResponseDelegateMock mock;
273 em::DeviceUnregisterResponse expected_response; 290 em::DeviceUnregisterResponse expected_response;
274 EXPECT_CALL(mock, HandleUnregisterResponse(MessageEquals(expected_response))); 291 EXPECT_CALL(mock, HandleUnregisterResponse(MessageEquals(expected_response)));
275 em::DeviceUnregisterRequest request; 292 em::DeviceUnregisterRequest request;
276 service_.ProcessUnregisterRequest("dmtokenvalue", request, &mock); 293 service_->ProcessUnregisterRequest("dmtokenvalue", request, &mock);
277 TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 294 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
278 ASSERT_TRUE(fetcher); 295 ASSERT_TRUE(fetcher);
279 296
280 // Check the data the fetcher received. 297 // Check the data the fetcher received.
281 const GURL& request_url(fetcher->original_url()); 298 const GURL& request_url(fetcher->original_url());
282 const GURL service_url(kServiceURL); 299 const GURL service_url(kServiceURL);
283 EXPECT_EQ(service_url.scheme(), request_url.scheme()); 300 EXPECT_EQ(service_url.scheme(), request_url.scheme());
284 EXPECT_EQ(service_url.host(), request_url.host()); 301 EXPECT_EQ(service_url.host(), request_url.host());
285 EXPECT_EQ(service_url.port(), request_url.port()); 302 EXPECT_EQ(service_url.port(), request_url.port());
286 EXPECT_EQ(service_url.path(), request_url.path()); 303 EXPECT_EQ(service_url.path(), request_url.path());
(...skipping 16 matching lines...) Expand all
303 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); 320 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data));
304 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); 321 URLRequestStatus status(URLRequestStatus::SUCCESS, 0);
305 fetcher->delegate()->OnURLFetchComplete(fetcher, 322 fetcher->delegate()->OnURLFetchComplete(fetcher,
306 GURL(kServiceURL), 323 GURL(kServiceURL),
307 status, 324 status,
308 200, 325 200,
309 ResponseCookies(), 326 ResponseCookies(),
310 response_data); 327 response_data);
311 } 328 }
312 329
313 TEST_F(DeviceManagementBackendImplTest, PolicyRequest) { 330 TEST_F(DeviceManagementServiceTest, PolicyRequest) {
314 DevicePolicyResponseDelegateMock mock; 331 DevicePolicyResponseDelegateMock mock;
315 em::DevicePolicyResponse expected_response; 332 em::DevicePolicyResponse expected_response;
316 em::DevicePolicySetting* policy_setting = expected_response.add_setting(); 333 em::DevicePolicySetting* policy_setting = expected_response.add_setting();
317 policy_setting->set_policy_key("policy"); 334 policy_setting->set_policy_key("policy");
318 policy_setting->set_watermark("fresh"); 335 policy_setting->set_watermark("fresh");
319 em::GenericSetting* policy_value = policy_setting->mutable_policy_value(); 336 em::GenericSetting* policy_value = policy_setting->mutable_policy_value();
320 em::GenericNamedValue* named_value = policy_value->add_named_value(); 337 em::GenericNamedValue* named_value = policy_value->add_named_value();
321 named_value->set_name("HomepageLocation"); 338 named_value->set_name("HomepageLocation");
322 named_value->mutable_value()->set_value_type( 339 named_value->mutable_value()->set_value_type(
323 em::GenericValue::VALUE_TYPE_STRING); 340 em::GenericValue::VALUE_TYPE_STRING);
324 named_value->mutable_value()->set_string_value("http://www.chromium.org"); 341 named_value->mutable_value()->set_string_value("http://www.chromium.org");
325 named_value = policy_value->add_named_value(); 342 named_value = policy_value->add_named_value();
326 named_value->set_name("HomepageIsNewTabPage"); 343 named_value->set_name("HomepageIsNewTabPage");
327 named_value->mutable_value()->set_value_type( 344 named_value->mutable_value()->set_value_type(
328 em::GenericValue::VALUE_TYPE_BOOL); 345 em::GenericValue::VALUE_TYPE_BOOL);
329 named_value->mutable_value()->set_bool_value(false); 346 named_value->mutable_value()->set_bool_value(false);
330 EXPECT_CALL(mock, HandlePolicyResponse(MessageEquals(expected_response))); 347 EXPECT_CALL(mock, HandlePolicyResponse(MessageEquals(expected_response)));
331 348
332 em::DevicePolicyRequest request; 349 em::DevicePolicyRequest request;
333 request.set_policy_scope("chromium"); 350 request.set_policy_scope("chromium");
334 em::DevicePolicySettingRequest* setting_request = 351 em::DevicePolicySettingRequest* setting_request =
335 request.add_setting_request(); 352 request.add_setting_request();
336 setting_request->set_key("policy"); 353 setting_request->set_key("policy");
337 setting_request->set_watermark("stale"); 354 setting_request->set_watermark("stale");
338 service_.ProcessPolicyRequest("dmtokenvalue", request, &mock); 355 service_->ProcessPolicyRequest("dmtokenvalue", request, &mock);
339 TestURLFetcher* fetcher = factory_.GetFetcherByID(0); 356 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
340 ASSERT_TRUE(fetcher); 357 ASSERT_TRUE(fetcher);
341 358
342 // Check the data the fetcher received. 359 // Check the data the fetcher received.
343 const GURL& request_url(fetcher->original_url()); 360 const GURL& request_url(fetcher->original_url());
344 const GURL service_url(kServiceURL); 361 const GURL service_url(kServiceURL);
345 EXPECT_EQ(service_url.scheme(), request_url.scheme()); 362 EXPECT_EQ(service_url.scheme(), request_url.scheme());
346 EXPECT_EQ(service_url.host(), request_url.host()); 363 EXPECT_EQ(service_url.host(), request_url.host());
347 EXPECT_EQ(service_url.port(), request_url.port()); 364 EXPECT_EQ(service_url.port(), request_url.port());
348 EXPECT_EQ(service_url.path(), request_url.path()); 365 EXPECT_EQ(service_url.path(), request_url.path());
(...skipping 16 matching lines...) Expand all
365 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data)); 382 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data));
366 URLRequestStatus status(URLRequestStatus::SUCCESS, 0); 383 URLRequestStatus status(URLRequestStatus::SUCCESS, 0);
367 fetcher->delegate()->OnURLFetchComplete(fetcher, 384 fetcher->delegate()->OnURLFetchComplete(fetcher,
368 GURL(kServiceURL), 385 GURL(kServiceURL),
369 status, 386 status,
370 200, 387 200,
371 ResponseCookies(), 388 ResponseCookies(),
372 response_data); 389 response_data);
373 } 390 }
374 391
392 TEST_F(DeviceManagementServiceTest, CancelRegisterRequest) {
393 DeviceRegisterResponseDelegateMock mock;
394 EXPECT_CALL(mock, HandleRegisterResponse(_)).Times(0);
395 em::DeviceRegisterRequest request;
396 service_->ProcessRegisterRequest("token", "device id", request, &mock);
397 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
398 ASSERT_TRUE(fetcher);
399
400 // There shouldn't be any callbacks.
401 service_.reset();
402 }
403
404 TEST_F(DeviceManagementServiceTest, CancelUnregisterRequest) {
405 DeviceUnregisterResponseDelegateMock mock;
406 EXPECT_CALL(mock, HandleUnregisterResponse(_)).Times(0);
407 em::DeviceUnregisterRequest request;
408 service_->ProcessUnregisterRequest("dmtokenvalue", request, &mock);
409 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
410 ASSERT_TRUE(fetcher);
411
412 // There shouldn't be any callbacks.
413 service_.reset();
414 }
415
416 TEST_F(DeviceManagementServiceTest, CancelPolicyRequest) {
417 DevicePolicyResponseDelegateMock mock;
418 EXPECT_CALL(mock, HandlePolicyResponse(_)).Times(0);
419 em::DevicePolicyRequest request;
420 request.set_policy_scope("chromium");
421 em::DevicePolicySettingRequest* setting_request =
422 request.add_setting_request();
423 setting_request->set_key("policy");
424 setting_request->set_watermark("stale");
425 service_->ProcessPolicyRequest("dmtokenvalue", request, &mock);
426 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
427 ASSERT_TRUE(fetcher);
428
429 // There shouldn't be any callbacks.
430 service_.reset();
431 }
432
433 TEST_F(DeviceManagementServiceTest, JobQueueing) {
434 // Start with a non-initialized backend.
435 ResetBackend();
436
437 // Make a request. We should not see any fetchers being created.
438 DeviceRegisterResponseDelegateMock mock;
439 em::DeviceRegisterResponse expected_response;
440 expected_response.set_device_management_token("mtoken");
441 EXPECT_CALL(mock, HandleRegisterResponse(MessageEquals(expected_response)));
442 em::DeviceRegisterRequest request;
443 service_->ProcessRegisterRequest("token", "device id", request, &mock);
444 TestURLFetcher* fetcher = factory_.GetFetcherByID(0);
445 ASSERT_FALSE(fetcher);
446
447 // Now initialize the backend. That should start the job.
448 backend_->Initialize(request_context_.get());
449 fetcher = factory_.GetFetcherByID(0);
450 ASSERT_TRUE(fetcher);
451 factory_.RemoveFetcherFromMap(0);
452
453 // Check that the request is processed as expected.
454 std::string response_data;
455 em::DeviceManagementResponse response_wrapper;
456 response_wrapper.set_error(em::DeviceManagementResponse::SUCCESS);
457 response_wrapper.mutable_register_response()->CopyFrom(expected_response);
458 ASSERT_TRUE(response_wrapper.SerializeToString(&response_data));
459 URLRequestStatus status(URLRequestStatus::SUCCESS, 0);
460 fetcher->delegate()->OnURLFetchComplete(fetcher,
461 GURL(kServiceURL),
462 status,
463 200,
464 ResponseCookies(),
465 response_data);
466 }
467
375 } // namespace policy 468 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698