OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 // A complete set of unit tests for GaiaOAuthClient. | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/message_loop.h" | |
10 #include "base/string_number_conversions.h" | |
11 #include "base/string_util.h" | |
12 #include "chrome/common/net/gaia/gaia_oauth_client.h" | |
13 #include "chrome/test/base/testing_profile.h" | |
14 #include "googleurl/src/gurl.h" | |
15 #include "net/base/net_errors.h" | |
16 #include "net/http/http_status_code.h" | |
17 #include "net/url_request/test_url_fetcher_factory.h" | |
18 #include "net/url_request/url_fetcher_delegate.h" | |
19 #include "net/url_request/url_request_status.h" | |
20 #include "testing/gmock/include/gmock/gmock.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 using ::testing::_; | |
24 | |
25 namespace { | |
26 // Responds as though OAuth returned from the server. | |
27 class MockOAuthFetcher : public net::TestURLFetcher { | |
28 public: | |
29 MockOAuthFetcher(int response_code, | |
30 int max_failure_count, | |
31 const GURL& url, | |
32 const std::string& results, | |
33 net::URLFetcher::RequestType request_type, | |
34 net::URLFetcherDelegate* d) | |
35 : net::TestURLFetcher(0, url, d), | |
36 max_failure_count_(max_failure_count), | |
37 current_failure_count_(0) { | |
38 set_url(url); | |
39 set_response_code(response_code); | |
40 SetResponseString(results); | |
41 } | |
42 | |
43 virtual ~MockOAuthFetcher() { } | |
44 | |
45 virtual void Start() { | |
46 if ((GetResponseCode() != net::HTTP_OK) && (max_failure_count_ != -1) && | |
47 (current_failure_count_ == max_failure_count_)) { | |
48 set_response_code(net::HTTP_OK); | |
49 } | |
50 | |
51 net::URLRequestStatus::Status code = net::URLRequestStatus::SUCCESS; | |
52 if (GetResponseCode() != net::HTTP_OK) { | |
53 code = net::URLRequestStatus::FAILED; | |
54 current_failure_count_++; | |
55 } | |
56 set_status(net::URLRequestStatus(code, 0)); | |
57 | |
58 delegate()->OnURLFetchComplete(this); | |
59 } | |
60 | |
61 private: | |
62 int max_failure_count_; | |
63 int current_failure_count_; | |
64 DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcher); | |
65 }; | |
66 | |
67 class MockOAuthFetcherFactory : public net::URLFetcherFactory, | |
68 public net::ScopedURLFetcherFactory { | |
69 public: | |
70 MockOAuthFetcherFactory() | |
71 : net::ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
72 response_code_(net::HTTP_OK) { | |
73 } | |
74 ~MockOAuthFetcherFactory() {} | |
75 virtual net::URLFetcher* CreateURLFetcher( | |
76 int id, | |
77 const GURL& url, | |
78 net::URLFetcher::RequestType request_type, | |
79 net::URLFetcherDelegate* d) { | |
80 return new MockOAuthFetcher( | |
81 response_code_, | |
82 max_failure_count_, | |
83 url, | |
84 results_, | |
85 request_type, | |
86 d); | |
87 } | |
88 void set_response_code(int response_code) { | |
89 response_code_ = response_code; | |
90 } | |
91 void set_max_failure_count(int count) { | |
92 max_failure_count_ = count; | |
93 } | |
94 void set_results(const std::string& results) { | |
95 results_ = results; | |
96 } | |
97 private: | |
98 int response_code_; | |
99 int max_failure_count_; | |
100 std::string results_; | |
101 DISALLOW_COPY_AND_ASSIGN(MockOAuthFetcherFactory); | |
102 }; | |
103 | |
104 const std::string kTestAccessToken = "1/fFAGRNJru1FTz70BzhT3Zg"; | |
105 const std::string kTestRefreshToken = | |
106 "1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ"; | |
107 const int kTestExpiresIn = 3920; | |
108 | |
109 const std::string kDummyGetTokensResult = | |
110 "{\"access_token\":\"" + kTestAccessToken + "\"," | |
111 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "," | |
112 "\"refresh_token\":\"" + kTestRefreshToken + "\"}"; | |
113 | |
114 const std::string kDummyRefreshTokenResult = | |
115 "{\"access_token\":\"" + kTestAccessToken + "\"," | |
116 "\"expires_in\":" + base::IntToString(kTestExpiresIn) + "}"; | |
117 } | |
118 | |
119 namespace gaia { | |
120 | |
121 class GaiaOAuthClientTest : public testing::Test { | |
122 public: | |
123 GaiaOAuthClientTest() {} | |
124 | |
125 TestingProfile profile_; | |
126 protected: | |
127 MessageLoop message_loop_; | |
128 }; | |
129 | |
130 class MockGaiaOAuthClientDelegate : public gaia::GaiaOAuthClient::Delegate { | |
131 public: | |
132 MockGaiaOAuthClientDelegate() {} | |
133 ~MockGaiaOAuthClientDelegate() {} | |
134 | |
135 MOCK_METHOD3(OnGetTokensResponse, void(const std::string& refresh_token, | |
136 const std::string& access_token, int expires_in_seconds)); | |
137 MOCK_METHOD2(OnRefreshTokenResponse, void(const std::string& access_token, | |
138 int expires_in_seconds)); | |
139 MOCK_METHOD0(OnOAuthError, void()); | |
140 MOCK_METHOD1(OnNetworkError, void(int response_code)); | |
141 }; | |
142 | |
143 TEST_F(GaiaOAuthClientTest, NetworkFailure) { | |
144 int response_code = net::HTTP_INTERNAL_SERVER_ERROR; | |
145 | |
146 MockGaiaOAuthClientDelegate delegate; | |
147 EXPECT_CALL(delegate, OnNetworkError(response_code)) | |
148 .Times(1); | |
149 | |
150 TestingProfile profile; | |
151 | |
152 MockOAuthFetcherFactory factory; | |
153 factory.set_response_code(response_code); | |
154 factory.set_max_failure_count(4); | |
155 | |
156 OAuthClientInfo client_info; | |
157 client_info.client_id = "test_client_id"; | |
158 client_info.client_secret = "test_client_secret"; | |
159 GaiaOAuthClient auth(kGaiaOAuth2Url, | |
160 profile_.GetRequestContext()); | |
161 auth.GetTokensFromAuthCode(client_info, "auth_code", 2, &delegate); | |
162 } | |
163 | |
164 TEST_F(GaiaOAuthClientTest, NetworkFailureRecover) { | |
165 int response_code = net::HTTP_INTERNAL_SERVER_ERROR; | |
166 | |
167 MockGaiaOAuthClientDelegate delegate; | |
168 EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken, | |
169 kTestExpiresIn)).Times(1); | |
170 | |
171 TestingProfile profile; | |
172 | |
173 MockOAuthFetcherFactory factory; | |
174 factory.set_response_code(response_code); | |
175 factory.set_max_failure_count(4); | |
176 factory.set_results(kDummyGetTokensResult); | |
177 | |
178 OAuthClientInfo client_info; | |
179 client_info.client_id = "test_client_id"; | |
180 client_info.client_secret = "test_client_secret"; | |
181 GaiaOAuthClient auth(kGaiaOAuth2Url, | |
182 profile_.GetRequestContext()); | |
183 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); | |
184 } | |
185 | |
186 TEST_F(GaiaOAuthClientTest, OAuthFailure) { | |
187 int response_code = net::HTTP_BAD_REQUEST; | |
188 | |
189 MockGaiaOAuthClientDelegate delegate; | |
190 EXPECT_CALL(delegate, OnOAuthError()).Times(1); | |
191 | |
192 TestingProfile profile; | |
193 | |
194 MockOAuthFetcherFactory factory; | |
195 factory.set_response_code(response_code); | |
196 factory.set_max_failure_count(-1); | |
197 factory.set_results(kDummyGetTokensResult); | |
198 | |
199 OAuthClientInfo client_info; | |
200 client_info.client_id = "test_client_id"; | |
201 client_info.client_secret = "test_client_secret"; | |
202 GaiaOAuthClient auth(kGaiaOAuth2Url, | |
203 profile_.GetRequestContext()); | |
204 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); | |
205 } | |
206 | |
207 | |
208 TEST_F(GaiaOAuthClientTest, GetTokensSuccess) { | |
209 MockGaiaOAuthClientDelegate delegate; | |
210 EXPECT_CALL(delegate, OnGetTokensResponse(kTestRefreshToken, kTestAccessToken, | |
211 kTestExpiresIn)).Times(1); | |
212 | |
213 TestingProfile profile; | |
214 | |
215 MockOAuthFetcherFactory factory; | |
216 factory.set_results(kDummyGetTokensResult); | |
217 | |
218 OAuthClientInfo client_info; | |
219 client_info.client_id = "test_client_id"; | |
220 client_info.client_secret = "test_client_secret"; | |
221 GaiaOAuthClient auth(kGaiaOAuth2Url, | |
222 profile_.GetRequestContext()); | |
223 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); | |
224 } | |
225 | |
226 TEST_F(GaiaOAuthClientTest, RefreshTokenSuccess) { | |
227 MockGaiaOAuthClientDelegate delegate; | |
228 EXPECT_CALL(delegate, OnRefreshTokenResponse(kTestAccessToken, | |
229 kTestExpiresIn)).Times(1); | |
230 | |
231 TestingProfile profile; | |
232 | |
233 MockOAuthFetcherFactory factory; | |
234 factory.set_results(kDummyRefreshTokenResult); | |
235 | |
236 OAuthClientInfo client_info; | |
237 client_info.client_id = "test_client_id"; | |
238 client_info.client_secret = "test_client_secret"; | |
239 GaiaOAuthClient auth(kGaiaOAuth2Url, | |
240 profile_.GetRequestContext()); | |
241 auth.GetTokensFromAuthCode(client_info, "auth_code", -1, &delegate); | |
242 } | |
243 } // namespace gaia | |
OLD | NEW |