Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: chrome/browser/local_discovery/privet_confirm_api_flow_unittest.cc

Issue 19788004: Implement server-side confirm request for privet registration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/google_service_auth_error.h"
10 #include "net/base/host_port_pair.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_request_headers.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 using testing::NiceMock;
19
20 namespace local_discovery {
21
22 namespace {
23
24 const char kSampleConfirmResponse[] = "{"
25 " \"success\": true"
26 "}";
27
28 const char kFailedConfirmResponse[] = "{"
29 " \"success\": false"
30 "}";
31
32
33 const char kFailedConfirmResponseBadJson[] = "["
34 " \"success\""
35 "]";
36
37 class TestOAuth2TokenService : public OAuth2TokenService {
38 public:
39 explicit TestOAuth2TokenService(net::URLRequestContextGetter* context)
40 : OAuth2TokenService(context) {
41 }
42 protected:
43 virtual std::string GetRefreshToken() OVERRIDE {
44 return "SampleToken";
45 }
46 };
47
48 class MockableConfirmCallback {
49 public:
50 MOCK_METHOD1(ConfirmCallback, void(PrivetConfirmApiCallFlow::Status));
51
52 PrivetConfirmApiCallFlow::ResponseCallback callback() {
53 return base::Bind(&MockableConfirmCallback::ConfirmCallback,
54 base::Unretained(this));
55 }
56 };
57
58 class PrivetConfirmApiFlowTest : public testing::Test {
59 public:
60 PrivetConfirmApiFlowTest()
61 : ui_thread_(content::BrowserThread::UI,
62 &loop_),
63 request_context_(new net::TestURLRequestContextGetter(
64 base::MessageLoopProxy::current())),
65 token_service_(request_context_.get()) {
66 ui_thread_.Stop(); // HACK: Fake being on the UI thread
67 }
68
69 virtual ~PrivetConfirmApiFlowTest() {
70 }
71
72 protected:
73 base::MessageLoopForUI loop_;
74 content::TestBrowserThread ui_thread_;
75 scoped_refptr<net::TestURLRequestContextGetter> request_context_;
76 net::TestURLFetcherFactory fetcher_factory_;
77 TestOAuth2TokenService token_service_;
78 MockableConfirmCallback callback_;
79 };
80
81 TEST_F(PrivetConfirmApiFlowTest, Success) {
82 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
83 &token_service_,
84 GURL("http://SoMeUrL.com"),
85 callback_.callback());
86
87 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time());
88 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
89
90 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
91
92 net::HttpRequestHeaders headers;
93 fetcher->GetExtraRequestHeaders(&headers);
94 std::string oauth_header;
95 std::string proxy;
96 EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header));
97 EXPECT_EQ("Bearer SomeToken", oauth_header);
98 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy));
99 EXPECT_EQ("Chrome", proxy);
100
101 fetcher->SetResponseString(kSampleConfirmResponse);
102 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
103 net::OK));
104 fetcher->set_response_code(200);
105
106 EXPECT_CALL(callback_, ConfirmCallback(PrivetConfirmApiCallFlow::SUCCESS));
107
108 fetcher->delegate()->OnURLFetchComplete(fetcher);
109 }
110
111 TEST_F(PrivetConfirmApiFlowTest, BadToken) {
112 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
113 &token_service_,
114 GURL("http://SoMeUrL.com"),
115 callback_.callback());
116
117 EXPECT_CALL(callback_,
118 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_TOKEN));
119 confirm_flow.OnGetTokenFailure(NULL, GoogleServiceAuthError(
120 GoogleServiceAuthError::USER_NOT_SIGNED_UP));
121 }
122
123 TEST_F(PrivetConfirmApiFlowTest, ServerFailure) {
124 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
125 &token_service_,
126 GURL("http://SoMeUrL.com"),
127 callback_.callback());
128
129 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time());
130 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
131
132 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
133
134 fetcher->SetResponseString(kFailedConfirmResponse);
135 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS,
136 net::OK));
137 fetcher->set_response_code(200);
138
139 EXPECT_CALL(callback_,
140 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_FROM_SERVER));
141
142 fetcher->delegate()->OnURLFetchComplete(fetcher);
143 }
144
145 TEST_F(PrivetConfirmApiFlowTest, BadJson) {
146 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(),
147 &token_service_,
148 GURL("http://SoMeUrL.com"),
149 callback_.callback());
150
151 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time());
152 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
153
154 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL());
155
156 fetcher->SetResponseString(kFailedConfirmResponseBadJson);
157 fetcher->set_status(net::URLRequestStatus(
158 net::URLRequestStatus::SUCCESS,
159 net::OK));
160 fetcher->set_response_code(200);
161
162 EXPECT_CALL(callback_, ConfirmCallback
163 (PrivetConfirmApiCallFlow::ERROR_MALFORMED_RESPONSE));
164
165 fetcher->delegate()->OnURLFetchComplete(fetcher);
166 }
167
168 } // namespace
169
170 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698