Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / local_discovery / privet_confirm_api_flow_unittest.cc
blob97c2f041a15e2b249cc7c0cb86648e7411366fb1
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.
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"
19 using testing::NiceMock;
21 namespace local_discovery {
23 namespace {
25 const char kSampleConfirmResponse[] = "{"
26 " \"success\": true"
27 "}";
29 const char kFailedConfirmResponse[] = "{"
30 " \"success\": false"
31 "}";
33 const char kFailedConfirmResponseBadJson[] = "["
34 " \"success\""
35 "]";
37 const char kAccountId[] = "account_id";
39 class TestOAuth2TokenService : public OAuth2TokenService {
40 public:
41 explicit TestOAuth2TokenService(net::URLRequestContextGetter* request_context)
42 : request_context_(request_context) {
44 protected:
45 virtual std::string GetRefreshToken(const std::string& account_id) OVERRIDE {
46 return "SampleToken";
49 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE {
50 return request_context_.get();
53 private:
54 scoped_refptr<net::URLRequestContextGetter> request_context_;
57 class MockableConfirmCallback {
58 public:
59 MOCK_METHOD1(ConfirmCallback, void(CloudPrintBaseApiFlow::Status));
61 PrivetConfirmApiCallFlow::ResponseCallback callback() {
62 return base::Bind(&MockableConfirmCallback::ConfirmCallback,
63 base::Unretained(this));
67 class 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
79 virtual ~PrivetConfirmApiFlowTest() {
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_;
92 TEST_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();
101 confirm_flow.Start();
103 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
104 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
106 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
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);
117 fetcher->SetResponseString(kSampleConfirmResponse);
118 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
119 net::OK));
120 fetcher->set_response_code(200);
122 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS));
124 fetcher->delegate()->OnURLFetchComplete(fetcher);
127 TEST_F(PrivetConfirmApiFlowTest, SuccessCookies) {
128 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
130 "SomeToken",
131 GURL("http://SoMeUrL.com?token=tkn"),
132 callback_.callback());
134 confirm_flow.Start();
136 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
138 EXPECT_EQ(GURL("http://SoMeUrL.com?token=tkn&xsrf=SomeToken&user=1"),
139 fetcher->GetOriginalURL());
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);
147 fetcher->SetResponseString(kSampleConfirmResponse);
148 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
149 net::OK));
150 fetcher->set_response_code(200);
152 EXPECT_CALL(callback_, ConfirmCallback(CloudPrintBaseApiFlow::SUCCESS));
154 fetcher->delegate()->OnURLFetchComplete(fetcher);
157 TEST_F(PrivetConfirmApiFlowTest, BadToken) {
158 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
159 &token_service_,
160 account_id_,
161 GURL("http://SoMeUrL.com"),
162 callback_.callback());
164 confirm_flow.Start();
166 CloudPrintBaseApiFlow* cloudprint_flow =
167 confirm_flow.GetBaseApiFlowForTests();
169 EXPECT_CALL(callback_,
170 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_TOKEN));
171 cloudprint_flow->OnGetTokenFailure(NULL, GoogleServiceAuthError(
172 GoogleServiceAuthError::USER_NOT_SIGNED_UP));
175 TEST_F(PrivetConfirmApiFlowTest, ServerFailure) {
176 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
177 &token_service_,
178 account_id_,
179 GURL("http://SoMeUrL.com"),
180 callback_.callback());
182 confirm_flow.Start();
184 CloudPrintBaseApiFlow* cloudprint_flow =
185 confirm_flow.GetBaseApiFlowForTests();
187 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
188 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
190 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
192 fetcher->SetResponseString(kFailedConfirmResponse);
193 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
194 net::OK));
195 fetcher->set_response_code(200);
197 EXPECT_CALL(callback_,
198 ConfirmCallback(CloudPrintBaseApiFlow::ERROR_FROM_SERVER));
200 fetcher->delegate()->OnURLFetchComplete(fetcher);
203 TEST_F(PrivetConfirmApiFlowTest, BadJson) {
204 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
205 &token_service_,
206 account_id_,
207 GURL("http://SoMeUrL.com"),
208 callback_.callback());
210 confirm_flow.Start();
212 CloudPrintBaseApiFlow* cloudprint_flow =
213 confirm_flow.GetBaseApiFlowForTests();
215 cloudprint_flow->OnGetTokenSuccess(NULL, "SomeToken", base::Time());
216 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
218 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
220 fetcher->SetResponseString(kFailedConfirmResponseBadJson);
221 fetcher->set_status(net::URLRequestStatus(
222 net::URLRequestStatus::SUCCESS,
223 net::OK));
224 fetcher->set_response_code(200);
226 EXPECT_CALL(callback_, ConfirmCallback
227 (CloudPrintBaseApiFlow::ERROR_MALFORMED_RESPONSE));
229 fetcher->delegate()->OnURLFetchComplete(fetcher);
232 } // namespace
234 } // namespace local_discovery