1// Copyright 2014 The Chromium 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 "chrome/browser/local_discovery/gcd_api_flow.h" 6 7#include "base/bind.h" 8#include "base/message_loop/message_loop.h" 9#include "base/values.h" 10#include "chrome/browser/local_discovery/gcd_api_flow_impl.h" 11#include "content/public/test/test_browser_thread.h" 12#include "google_apis/gaia/fake_oauth2_token_service.h" 13#include "google_apis/gaia/google_service_auth_error.h" 14#include "net/base/host_port_pair.h" 15#include "net/base/net_errors.h" 16#include "net/http/http_request_headers.h" 17#include "net/url_request/test_url_fetcher_factory.h" 18#include "net/url_request/url_request_test_util.h" 19#include "testing/gmock/include/gmock/gmock.h" 20#include "testing/gtest/include/gtest/gtest.h" 21 22using testing::_; 23using testing::Return; 24 25namespace local_discovery { 26 27namespace { 28 29const char kSampleConfirmResponse[] = "{}"; 30 31const char kFailedConfirmResponseBadJson[] = "[]"; 32 33const char kAccountId[] = "account_id"; 34 35class MockDelegate : public CloudPrintApiFlowRequest { 36 public: 37 MOCK_METHOD1(OnGCDAPIFlowError, void(GCDApiFlow::Status)); 38 MOCK_METHOD1(OnGCDAPIFlowComplete, void(const base::DictionaryValue&)); 39 40 MOCK_METHOD0(GetURL, GURL()); 41}; 42 43class GCDApiFlowTest : public testing::Test { 44 public: 45 GCDApiFlowTest() 46 : ui_thread_(content::BrowserThread::UI, &loop_), 47 request_context_(new net::TestURLRequestContextGetter( 48 base::MessageLoopProxy::current())), 49 account_id_(kAccountId) {} 50 51 virtual ~GCDApiFlowTest() {} 52 53 protected: 54 virtual void SetUp() OVERRIDE { 55 token_service_.set_request_context(request_context_.get()); 56 token_service_.AddAccount(account_id_); 57 ui_thread_.Stop(); // HACK: Fake being on the UI thread 58 59 scoped_ptr<MockDelegate> delegate(new MockDelegate); 60 mock_delegate_ = delegate.get(); 61 EXPECT_CALL(*mock_delegate_, GetURL()) 62 .WillRepeatedly(Return( 63 GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"))); 64 gcd_flow_.reset(new GCDApiFlowImpl( 65 request_context_.get(), &token_service_, account_id_)); 66 gcd_flow_->Start(delegate.PassAs<GCDApiFlow::Request>()); 67 } 68 base::MessageLoopForUI loop_; 69 content::TestBrowserThread ui_thread_; 70 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 71 net::TestURLFetcherFactory fetcher_factory_; 72 FakeOAuth2TokenService token_service_; 73 std::string account_id_; 74 scoped_ptr<GCDApiFlowImpl> gcd_flow_; 75 MockDelegate* mock_delegate_; 76}; 77 78TEST_F(GCDApiFlowTest, SuccessOAuth2) { 79 gcd_flow_->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 80 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 81 82 EXPECT_EQ(GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"), 83 fetcher->GetOriginalURL()); 84 85 net::HttpRequestHeaders headers; 86 fetcher->GetExtraRequestHeaders(&headers); 87 std::string oauth_header; 88 std::string proxy; 89 EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header)); 90 EXPECT_EQ("Bearer SomeToken", oauth_header); 91 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy)); 92 EXPECT_EQ("Chrome", proxy); 93 94 fetcher->SetResponseString(kSampleConfirmResponse); 95 fetcher->set_status( 96 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); 97 fetcher->set_response_code(200); 98 99 EXPECT_CALL(*mock_delegate_, OnGCDAPIFlowComplete(_)); 100 101 fetcher->delegate()->OnURLFetchComplete(fetcher); 102} 103 104TEST_F(GCDApiFlowTest, BadToken) { 105 EXPECT_CALL(*mock_delegate_, OnGCDAPIFlowError(GCDApiFlow::ERROR_TOKEN)); 106 gcd_flow_->OnGetTokenFailure( 107 NULL, GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP)); 108} 109 110TEST_F(GCDApiFlowTest, BadJson) { 111 gcd_flow_->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 112 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 113 114 EXPECT_EQ(GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"), 115 fetcher->GetOriginalURL()); 116 117 fetcher->SetResponseString(kFailedConfirmResponseBadJson); 118 fetcher->set_status( 119 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); 120 fetcher->set_response_code(200); 121 122 EXPECT_CALL(*mock_delegate_, 123 OnGCDAPIFlowError(GCDApiFlow::ERROR_MALFORMED_RESPONSE)); 124 125 fetcher->delegate()->OnURLFetchComplete(fetcher); 126} 127 128} // namespace 129 130} // namespace local_discovery 131