privet_confirm_api_flow_unittest.cc revision 68043e1e95eeb07d5cae7aca370b26518b0867d6
1// Copyright 2013 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 "base/bind.h" 6#include "base/message_loop/message_loop.h" 7#include "chrome/browser/local_discovery/privet_confirm_api_flow.h" 8#include "chrome/browser/signin/fake_profile_oauth2_token_service.h" 9#include "content/public/test/test_browser_thread.h" 10#include "google_apis/gaia/google_service_auth_error.h" 11#include "net/base/host_port_pair.h" 12#include "net/base/net_errors.h" 13#include "net/http/http_request_headers.h" 14#include "net/url_request/test_url_fetcher_factory.h" 15#include "net/url_request/url_request_test_util.h" 16#include "testing/gmock/include/gmock/gmock.h" 17#include "testing/gtest/include/gtest/gtest.h" 18 19using testing::NiceMock; 20 21namespace local_discovery { 22 23namespace { 24 25const char kSampleConfirmResponse[] = "{" 26 " \"success\": true" 27 "}"; 28 29const char kFailedConfirmResponse[] = "{" 30 " \"success\": false" 31 "}"; 32 33const char kFailedConfirmResponseBadJson[] = "[" 34 " \"success\"" 35 "]"; 36 37const char kAccountId[] = "account_id"; 38 39class TestOAuth2TokenService : public OAuth2TokenService { 40 public: 41 explicit TestOAuth2TokenService(net::URLRequestContextGetter* request_context) 42 : request_context_(request_context) { 43 } 44 protected: 45 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE { 46 return "SampleToken"; 47 } 48 49 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE { 50 return request_context_.get(); 51 } 52 53 private: 54 scoped_refptr<net::URLRequestContextGetter> request_context_; 55}; 56 57class MockableConfirmCallback { 58 public: 59 MOCK_METHOD1(ConfirmCallback, void(CloudPrintBaseApiFlow::Status)); 60 61 PrivetConfirmApiCallFlow::ResponseCallback callback() { 62 return base::Bind(&MockableConfirmCallback::ConfirmCallback, 63 base::Unretained(this)); 64 } 65}; 66 67class PrivetConfirmApiFlowTest : public testing::Test { 68 public: 69 PrivetConfirmApiFlowTest() 70 : ui_thread_(content::BrowserThread::UI, 71 &loop_), 72 request_context_(new net::TestURLRequestContextGetter( 73 base::MessageLoopProxy::current())), 74 token_service_(request_context_.get()), 75 account_id_(kAccountId) { 76 ui_thread_.Stop(); // HACK: Fake being on the UI thread 77 } 78 79 virtual ~PrivetConfirmApiFlowTest() { 80 } 81 82 protected: 83 base::MessageLoopForUI loop_; 84 content::TestBrowserThread ui_thread_; 85 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 86 net::TestURLFetcherFactory fetcher_factory_; 87 TestOAuth2TokenService token_service_; 88 MockableConfirmCallback callback_; 89 std::string account_id_; 90}; 91 92TEST_F(PrivetConfirmApiFlowTest, SuccessOAuth2) { 93 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 94 &token_service_, 95 account_id_, 96 GURL("http://SoMeUrL.com"), 97 callback_.callback()); 98 CloudPrintBaseApiFlow* cloudprint_flow = 99 confirm_flow.GetBaseApiFlowForTests(); 100 101 confirm_flow.Start(); 102 103 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 104 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 105 106 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); 107 108 net::HttpRequestHeaders headers; 109 fetcher->GetExtraRequestHeaders(&headers); 110 std::string oauth_header; 111 std::string proxy; 112 EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header)); 113 EXPECT_EQ("Bearer SomeToken", oauth_header); 114 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy)); 115 EXPECT_EQ("Chrome", proxy); 116 117 fetcher->SetResponseString(kSampleConfirmResponse); 118 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 119 net::OK)); 120 fetcher->set_response_code(200); 121 122 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS)); 123 124 fetcher->delegate()->OnURLFetchComplete(fetcher); 125} 126 127TEST_F(PrivetConfirmApiFlowTest, SuccessCookies) { 128 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 129 1, 130 "SomeToken", 131 GURL("http://SoMeUrL.com?token=tkn"), 132 callback_.callback()); 133 134 confirm_flow.Start(); 135 136 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 137 138 EXPECT_EQ(GURL("http://SoMeUrL.com?token=tkn&xsrf=SomeToken&user=1"), 139 fetcher->GetOriginalURL()); 140 141 net::HttpRequestHeaders headers; 142 fetcher->GetExtraRequestHeaders(&headers); 143 std::string proxy; 144 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy)); 145 EXPECT_EQ("Chrome", proxy); 146 147 fetcher->SetResponseString(kSampleConfirmResponse); 148 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 149 net::OK)); 150 fetcher->set_response_code(200); 151 152 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS)); 153 154 fetcher->delegate()->OnURLFetchComplete(fetcher); 155} 156 157TEST_F(PrivetConfirmApiFlowTest, BadToken) { 158 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 159 &token_service_, 160 account_id_, 161 GURL("http://SoMeUrL.com"), 162 callback_.callback()); 163 164 confirm_flow.Start(); 165 166 CloudPrintBaseApiFlow* cloudprint_flow = 167 confirm_flow.GetBaseApiFlowForTests(); 168 169 EXPECT_CALL(callback_, 170 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_TOKEN)); 171 cloudprint_flow->OnGetTokenFailure(NULL, GoogleServiceAuthError( 172 GoogleServiceAuthError::USER_NOT_SIGNED_UP)); 173} 174 175TEST_F(PrivetConfirmApiFlowTest, ServerFailure) { 176 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 177 &token_service_, 178 account_id_, 179 GURL("http://SoMeUrL.com"), 180 callback_.callback()); 181 182 confirm_flow.Start(); 183 184 CloudPrintBaseApiFlow* cloudprint_flow = 185 confirm_flow.GetBaseApiFlowForTests(); 186 187 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 188 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 189 190 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); 191 192 fetcher->SetResponseString(kFailedConfirmResponse); 193 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 194 net::OK)); 195 fetcher->set_response_code(200); 196 197 EXPECT_CALL(callback_, 198 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_FROM_SERVER)); 199 200 fetcher->delegate()->OnURLFetchComplete(fetcher); 201} 202 203TEST_F(PrivetConfirmApiFlowTest, BadJson) { 204 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 205 &token_service_, 206 account_id_, 207 GURL("http://SoMeUrL.com"), 208 callback_.callback()); 209 210 confirm_flow.Start(); 211 212 CloudPrintBaseApiFlow* cloudprint_flow = 213 confirm_flow.GetBaseApiFlowForTests(); 214 215 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 216 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 217 218 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); 219 220 fetcher->SetResponseString(kFailedConfirmResponseBadJson); 221 fetcher->set_status(net::URLRequestStatus( 222 net::URLRequestStatus::SUCCESS, 223 net::OK)); 224 fetcher->set_response_code(200); 225 226 EXPECT_CALL(callback_, ConfirmCallback 227 (CloudPrintBaseApiFlow::ERROR_MALFORMED_RESPONSE)); 228 229 fetcher->delegate()->OnURLFetchComplete(fetcher); 230} 231 232} // namespace 233 234} // namespace local_discovery 235