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