oauth2_api_call_flow_unittest.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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 OAuth2MintTokenFlow.
6
7#include <string>
8#include <vector>
9
10#include "base/memory/scoped_ptr.h"
11#include "base/message_loop/message_loop.h"
12#include "base/time/time.h"
13#include "google_apis/gaia/gaia_urls.h"
14#include "google_apis/gaia/google_service_auth_error.h"
15#include "google_apis/gaia/oauth2_access_token_consumer.h"
16#include "google_apis/gaia/oauth2_access_token_fetcher.h"
17#include "google_apis/gaia/oauth2_api_call_flow.h"
18#include "net/http/http_request_headers.h"
19#include "net/http/http_status_code.h"
20#include "net/url_request/test_url_fetcher_factory.h"
21#include "net/url_request/url_fetcher.h"
22#include "net/url_request/url_fetcher_delegate.h"
23#include "net/url_request/url_fetcher_factory.h"
24#include "net/url_request/url_request.h"
25#include "net/url_request/url_request_status.h"
26#include "net/url_request/url_request_test_util.h"
27#include "testing/gmock/include/gmock/gmock.h"
28#include "testing/gtest/include/gtest/gtest.h"
29
30using net::HttpRequestHeaders;
31using net::ScopedURLFetcherFactory;
32using net::TestURLFetcher;
33using net::URLFetcher;
34using net::URLFetcherDelegate;
35using net::URLFetcherFactory;
36using net::URLRequestStatus;
37using testing::_;
38using testing::Return;
39
40namespace {
41
42static std::string CreateBody() {
43  return "some body";
44}
45
46static GURL CreateApiUrl() {
47  return GURL("https://www.googleapis.com/someapi");
48}
49
50static std::vector<std::string> CreateTestScopes() {
51  std::vector<std::string> scopes;
52  scopes.push_back("scope1");
53  scopes.push_back("scope2");
54  return scopes;
55}
56
57class MockUrlFetcherFactory : public ScopedURLFetcherFactory,
58                              public URLFetcherFactory {
59 public:
60  MockUrlFetcherFactory()
61      : ScopedURLFetcherFactory(this) {
62  }
63  virtual ~MockUrlFetcherFactory() {}
64
65  MOCK_METHOD4(
66      CreateURLFetcher,
67      URLFetcher* (int id,
68                   const GURL& url,
69                   URLFetcher::RequestType request_type,
70                   URLFetcherDelegate* d));
71};
72
73class MockAccessTokenFetcher : public OAuth2AccessTokenFetcher {
74 public:
75  MockAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer,
76                         net::URLRequestContextGetter* getter)
77      : OAuth2AccessTokenFetcher(consumer, getter) {}
78  ~MockAccessTokenFetcher() {}
79
80  MOCK_METHOD4(Start,
81               void (const std::string& client_id,
82                     const std::string& client_secret,
83                     const std::string& refresh_token,
84                     const std::vector<std::string>& scopes));
85};
86
87class MockApiCallFlow : public OAuth2ApiCallFlow {
88 public:
89  MockApiCallFlow(net::URLRequestContextGetter* context,
90                  const std::string& refresh_token,
91                  const std::string& access_token,
92                  const std::vector<std::string>& scopes)
93      : OAuth2ApiCallFlow(context, refresh_token, access_token, scopes) {}
94  ~MockApiCallFlow() {}
95
96  MOCK_METHOD0(CreateApiCallUrl, GURL ());
97  MOCK_METHOD0(CreateApiCallBody, std::string ());
98  MOCK_METHOD1(ProcessApiCallSuccess,
99      void (const URLFetcher* source));
100  MOCK_METHOD1(ProcessApiCallFailure,
101      void (const URLFetcher* source));
102  MOCK_METHOD1(ProcessNewAccessToken,
103      void (const std::string& access_token));
104  MOCK_METHOD1(ProcessMintAccessTokenFailure,
105      void (const GoogleServiceAuthError& error));
106  MOCK_METHOD0(CreateAccessTokenFetcher, OAuth2AccessTokenFetcher* ());
107};
108
109}  // namespace
110
111class OAuth2ApiCallFlowTest : public testing::Test {
112 public:
113  OAuth2ApiCallFlowTest() {}
114  virtual ~OAuth2ApiCallFlowTest() {}
115
116 protected:
117  void SetupAccessTokenFetcher(
118      const std::string& rt, const std::vector<std::string>& scopes) {
119    EXPECT_CALL(*access_token_fetcher_,
120        Start(GaiaUrls::GetInstance()->oauth2_chrome_client_id(),
121              GaiaUrls::GetInstance()->oauth2_chrome_client_secret(),
122              rt, scopes))
123        .Times(1);
124    EXPECT_CALL(*flow_, CreateAccessTokenFetcher())
125        .WillOnce(Return(access_token_fetcher_.release()));
126  }
127
128  TestURLFetcher* CreateURLFetcher(
129      const GURL& url, bool fetch_succeeds,
130      int response_code, const std::string& body) {
131    TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, flow_.get());
132    URLRequestStatus::Status status =
133        fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED;
134    url_fetcher->set_status(URLRequestStatus(status, 0));
135
136    if (response_code != 0)
137      url_fetcher->set_response_code(response_code);
138
139    if (!body.empty())
140      url_fetcher->SetResponseString(body);
141
142    return url_fetcher;
143  }
144
145  void CreateFlow(const std::string& refresh_token,
146                  const std::string& access_token,
147                  const std::vector<std::string>& scopes) {
148    scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
149        new net::TestURLRequestContextGetter(
150            message_loop_.message_loop_proxy());
151    flow_.reset(new MockApiCallFlow(
152        request_context_getter, refresh_token, access_token, scopes));
153    access_token_fetcher_.reset(
154        new MockAccessTokenFetcher(flow_.get(), request_context_getter));
155  }
156
157  TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) {
158    std::string body(CreateBody());
159    GURL url(CreateApiUrl());
160    EXPECT_CALL(*flow_, CreateApiCallBody()).WillOnce(Return(body));
161    EXPECT_CALL(*flow_, CreateApiCallUrl()).WillOnce(Return(url));
162    TestURLFetcher* url_fetcher =
163        CreateURLFetcher(url, succeeds, status, std::string());
164    EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _))
165        .WillOnce(Return(url_fetcher));
166    return url_fetcher;
167  }
168
169  MockUrlFetcherFactory factory_;
170  scoped_ptr<MockApiCallFlow> flow_;
171  scoped_ptr<MockAccessTokenFetcher> access_token_fetcher_;
172  base::MessageLoop message_loop_;
173};
174
175TEST_F(OAuth2ApiCallFlowTest, FirstApiCallSucceeds) {
176  std::string rt = "refresh_token";
177  std::string at = "access_token";
178  std::vector<std::string> scopes(CreateTestScopes());
179
180  CreateFlow(rt, at, scopes);
181  TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK);
182  EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher));
183  flow_->Start();
184  flow_->OnURLFetchComplete(url_fetcher);
185}
186
187TEST_F(OAuth2ApiCallFlowTest, SecondApiCallSucceeds) {
188  std::string rt = "refresh_token";
189  std::string at = "access_token";
190  std::vector<std::string> scopes(CreateTestScopes());
191
192  CreateFlow(rt, at, scopes);
193  TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED);
194  flow_->Start();
195  SetupAccessTokenFetcher(rt, scopes);
196  flow_->OnURLFetchComplete(url_fetcher1);
197  TestURLFetcher* url_fetcher2 = SetupApiCall(true, net::HTTP_OK);
198  EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher2));
199  flow_->OnGetTokenSuccess(
200      at,
201      base::Time::Now() + base::TimeDelta::FromMinutes(3600));
202  flow_->OnURLFetchComplete(url_fetcher2);
203}
204
205TEST_F(OAuth2ApiCallFlowTest, SecondApiCallFails) {
206  std::string rt = "refresh_token";
207  std::string at = "access_token";
208  std::vector<std::string> scopes(CreateTestScopes());
209
210  CreateFlow(rt, at, scopes);
211  TestURLFetcher* url_fetcher1 = SetupApiCall(true, net::HTTP_UNAUTHORIZED);
212  flow_->Start();
213  SetupAccessTokenFetcher(rt, scopes);
214  flow_->OnURLFetchComplete(url_fetcher1);
215  TestURLFetcher* url_fetcher2 = SetupApiCall(false, net::HTTP_UNAUTHORIZED);
216  EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher2));
217  flow_->OnGetTokenSuccess(
218      at,
219      base::Time::Now() + base::TimeDelta::FromMinutes(3600));
220  flow_->OnURLFetchComplete(url_fetcher2);
221}
222
223TEST_F(OAuth2ApiCallFlowTest, NewTokenGenerationFails) {
224  std::string rt = "refresh_token";
225  std::string at = "access_token";
226  std::vector<std::string> scopes(CreateTestScopes());
227
228  CreateFlow(rt, at, scopes);
229  TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_UNAUTHORIZED);
230  flow_->Start();
231  SetupAccessTokenFetcher(rt, scopes);
232  flow_->OnURLFetchComplete(url_fetcher);
233  GoogleServiceAuthError error(
234      GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
235  EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error));
236  flow_->OnGetTokenFailure(error);
237}
238
239TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenFirstApiCallSucceeds) {
240  std::string rt = "refresh_token";
241  std::string at = "access_token";
242  std::vector<std::string> scopes(CreateTestScopes());
243
244  CreateFlow(rt, std::string(), scopes);
245  SetupAccessTokenFetcher(rt, scopes);
246  TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK);
247  EXPECT_CALL(*flow_, ProcessApiCallSuccess(url_fetcher));
248  flow_->Start();
249  flow_->OnGetTokenSuccess(
250      at,
251      base::Time::Now() + base::TimeDelta::FromMinutes(3600));
252  flow_->OnURLFetchComplete(url_fetcher);
253}
254
255TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenApiCallFails) {
256  std::string rt = "refresh_token";
257  std::string at = "access_token";
258  std::vector<std::string> scopes(CreateTestScopes());
259
260  CreateFlow(rt, std::string(), scopes);
261  SetupAccessTokenFetcher(rt, scopes);
262  TestURLFetcher* url_fetcher = SetupApiCall(false, net::HTTP_BAD_GATEWAY);
263  EXPECT_CALL(*flow_, ProcessApiCallFailure(url_fetcher));
264  flow_->Start();
265  flow_->OnGetTokenSuccess(
266      at,
267      base::Time::Now() + base::TimeDelta::FromMinutes(3600));
268  flow_->OnURLFetchComplete(url_fetcher);
269}
270
271TEST_F(OAuth2ApiCallFlowTest, EmptyAccessTokenNewTokenGenerationFails) {
272  std::string rt = "refresh_token";
273  std::string at = "access_token";
274  std::vector<std::string> scopes(CreateTestScopes());
275
276  CreateFlow(rt, std::string(), scopes);
277  SetupAccessTokenFetcher(rt, scopes);
278  GoogleServiceAuthError error(
279      GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
280  EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error));
281  flow_->Start();
282  flow_->OnGetTokenFailure(error);
283}
284
285TEST_F(OAuth2ApiCallFlowTest, CreateURLFetcher) {
286  std::string rt = "refresh_token";
287  std::string at = "access_token";
288  std::vector<std::string> scopes(CreateTestScopes());
289  std::string body = CreateBody();
290  GURL url(CreateApiUrl());
291
292  CreateFlow(rt, at, scopes);
293  scoped_ptr<TestURLFetcher> url_fetcher(SetupApiCall(true, net::HTTP_OK));
294  flow_->CreateURLFetcher();
295  HttpRequestHeaders headers;
296  url_fetcher->GetExtraRequestHeaders(&headers);
297  std::string auth_header;
298  EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header));
299  EXPECT_EQ("Bearer access_token", auth_header);
300  EXPECT_EQ(url, url_fetcher->GetOriginalURL());
301  EXPECT_EQ(body, url_fetcher->upload_data());
302}
303