15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/bind_helpers.h"
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
89ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/stl_util.h"
1068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "chrome/browser/browser_process.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/policy/cloud/test_request_interceptor.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/policy/test/local_policy_test_server.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/in_process_browser_test.h"
14a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/cloud_policy_constants.h"
15a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/device_management_service.h"
16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/policy/core/common/cloud/mock_device_management_service.h"
1768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/upload_bytes_element_reader.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/base/upload_data_stream.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_fetcher.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request.h"
2268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "net/url_request/url_request_context_getter.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_test_job.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)using content::BrowserThread;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::DoAll;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::Invoke;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::InvokeWithoutArgs;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using testing::_;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace em = enterprise_management;
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace policy {
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Parses the DeviceManagementRequest in |request_data| and writes a serialized
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// DeviceManagementResponse to |response_data|.
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ConstructResponse(const char* request_data,
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       uint64 request_data_length,
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       std::string* response_data) {
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  em::DeviceManagementRequest request;
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(request.ParseFromArray(request_data, request_data_length));
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  em::DeviceManagementResponse response;
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (request.has_register_request()) {
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    response.mutable_register_response()->set_device_management_token(
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        "fake_token");
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else if (request.has_service_api_access_request()) {
51c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    response.mutable_service_api_access_response()->set_auth_code(
52c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        "fake_auth_code");
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else if (request.has_unregister_request()) {
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    response.mutable_unregister_response();
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else if (request.has_policy_request()) {
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    response.mutable_policy_response()->add_response();
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else if (request.has_auto_enrollment_request()) {
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    response.mutable_auto_enrollment_response();
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FAIL() << "Failed to parse request.";
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ASSERT_TRUE(response.SerializeToString(response_data));
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// JobCallback for the interceptor.
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)net::URLRequestJob* ResponseJob(
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    net::URLRequest* request,
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    net::NetworkDelegate* network_delegate) {
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const net::UploadDataStream* upload = request->get_upload();
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (upload != NULL &&
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      upload->element_readers().size() == 1 &&
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      upload->element_readers()[0]->AsBytesReader()) {
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::string response_data;
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const net::UploadBytesElementReader* bytes_reader =
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        upload->element_readers()[0]->AsBytesReader();
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ConstructResponse(bytes_reader->bytes(),
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      bytes_reader->length(),
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      &response_data);
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return new net::URLRequestTestJob(
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        request,
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        network_delegate,
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        net::URLRequestTestJob::test_headers(),
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        response_data,
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        true);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return NULL;
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DeviceManagementServiceIntegrationTest
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : public InProcessBrowserTest,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      public testing::WithParamInterface<
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          std::string (DeviceManagementServiceIntegrationTest::*)(void)> {
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
97b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  MOCK_METHOD3(OnJobDone, void(DeviceManagementStatus, int,
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               const em::DeviceManagementResponse&));
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string InitCannedResponse() {
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    interceptor_.reset(new TestRequestInterceptor(
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        "localhost",
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return "http://localhost";
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string InitTestServer() {
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    StartTestServer();
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return test_server_->GetServiceURL().spec();
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void RecordAuthCode(DeviceManagementStatus status,
113b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                      int net_error,
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                      const em::DeviceManagementResponse& response) {
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    robot_auth_code_ = response.service_api_access_response().auth_code();
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ExpectRequest() {
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (interceptor_)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      interceptor_->PushJobCallback(base::Bind(&ResponseJob));
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void PerformRegistration() {
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ExpectRequest();
126b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS, _, _))
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        .WillOnce(
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            DoAll(Invoke(this,
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         &DeviceManagementServiceIntegrationTest::RecordToken),
13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                  InvokeWithoutArgs(base::MessageLoop::current(),
13190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                    &base::MessageLoop::Quit)));
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    scoped_ptr<DeviceManagementRequestJob> job(
133a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        service_->CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION,
134a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                            g_browser_process->system_request_context()));
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    job->SetGaiaToken("gaia_auth_token");
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    job->SetOAuthToken("oauth_token");
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    job->SetClientID("testid");
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    job->GetRequest()->mutable_register_request();
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    job->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone,
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          base::Unretained(this)));
14190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    base::MessageLoop::current()->Run();
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SetUpOnMainThread() OVERRIDE {
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string service_url((this->*(GetParam()))());
14668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    service_.reset(new DeviceManagementService(
1474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        scoped_ptr<DeviceManagementService::Configuration>(
148a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)            new MockDeviceManagementServiceConfiguration(service_url))));
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    service_->ScheduleInitialization(0);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void TearDownOnMainThread() OVERRIDE {
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    service_.reset();
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    test_server_.reset();
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    interceptor_.reset();
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void StartTestServer() {
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    test_server_.reset(
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        new LocalPolicyTestServer("device_management_service_browsertest"));
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ASSERT_TRUE(test_server_->Start());
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RecordToken(DeviceManagementStatus status,
165b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                   int net_error,
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const em::DeviceManagementResponse& response) {
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    token_ = response.register_response().device_management_token();
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string token_;
171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string robot_auth_code_;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<DeviceManagementService> service_;
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<LocalPolicyTestServer> test_server_;
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<TestRequestInterceptor> interceptor_;
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, Registration) {
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PerformRegistration();
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_FALSE(token_.empty());
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
182c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest,
183c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       ApiAuthCodeFetch) {
184c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  PerformRegistration();
185c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ExpectRequest();
187b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS, _, _))
188c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      .WillOnce(
189c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          DoAll(Invoke(this,
190c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       &DeviceManagementServiceIntegrationTest::RecordAuthCode),
19190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                InvokeWithoutArgs(base::MessageLoop::current(),
19290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                  &base::MessageLoop::Quit)));
193c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  scoped_ptr<DeviceManagementRequestJob> job(service_->CreateJob(
194a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DeviceManagementRequestJob::TYPE_API_AUTH_CODE_FETCH,
195a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      g_browser_process->system_request_context()));
196c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  job->SetDMToken(token_);
197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  job->SetClientID("testid");
198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  em::DeviceServiceApiAccessRequest* request =
199c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      job->GetRequest()->mutable_service_api_access_request();
200c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  request->add_auth_scope("authScope4Test");
201c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  request->set_oauth2_client_id("oauth2ClientId4Test");
202c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  job->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone,
203c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                        base::Unretained(this)));
20490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->Run();
205c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  ASSERT_EQ("fake_auth_code", robot_auth_code_);
206c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
207c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, PolicyFetch) {
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PerformRegistration();
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExpectRequest();
212b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS, _, _))
21390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      .WillOnce(InvokeWithoutArgs(base::MessageLoop::current(),
21490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                  &base::MessageLoop::Quit));
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<DeviceManagementRequestJob> job(
216a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      service_->CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH,
217a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                          g_browser_process->system_request_context()));
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->SetDMToken(token_);
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->SetUserAffiliation(USER_AFFILIATION_NONE);
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->SetClientID("testid");
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  em::DevicePolicyRequest* request =
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      job->GetRequest()->mutable_policy_request();
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  request->add_request()->set_policy_type(dm_protocol::kChromeUserPolicyType);
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone,
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        base::Unretained(this)));
22690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->Run();
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, Unregistration) {
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PerformRegistration();
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExpectRequest();
233b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS, _, _))
23490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      .WillOnce(InvokeWithoutArgs(base::MessageLoop::current(),
23590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                  &base::MessageLoop::Quit));
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<DeviceManagementRequestJob> job(
237a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      service_->CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION,
238a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                          g_browser_process->system_request_context()));
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->SetDMToken(token_);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->SetClientID("testid");
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->GetRequest()->mutable_unregister_request();
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone,
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        base::Unretained(this)));
24490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->Run();
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest, AutoEnrollment) {
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ExpectRequest();
249b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS, _, _))
25090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      .WillOnce(InvokeWithoutArgs(base::MessageLoop::current(),
25190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                  &base::MessageLoop::Quit));
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<DeviceManagementRequestJob> job(
253a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      service_->CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT,
254a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                          g_browser_process->system_request_context()));
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->SetClientID("testid");
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->GetRequest()->mutable_auto_enrollment_request()->set_remainder(0);
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->GetRequest()->mutable_auto_enrollment_request()->set_modulus(1);
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  job->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone,
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                        base::Unretained(this)));
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::MessageLoop::current()->Run();
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)INSTANTIATE_TEST_CASE_P(
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DeviceManagementServiceIntegrationTestInstance,
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DeviceManagementServiceIntegrationTest,
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    testing::Values(&DeviceManagementServiceIntegrationTest::InitCannedResponse,
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    &DeviceManagementServiceIntegrationTest::InitTestServer));
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace policy
270