Revert of Merge duplicate code in invalidation helper. (patchset #2 id:40001 of https...
[chromium-blink-merge.git] / google_apis / gaia / oauth2_token_service_request_unittest.cc
blobd514f93bb7fb3cf9d83dde0ecf9a89bcf48644f9
1 // Copyright 2014 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 "google_apis/gaia/oauth2_token_service_request.h"
7 #include <set>
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/threading/thread.h"
14 #include "google_apis/gaia/fake_oauth2_token_service.h"
15 #include "google_apis/gaia/google_service_auth_error.h"
16 #include "google_apis/gaia/oauth2_token_service.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace {
21 const char kAccessToken[] = "access_token";
22 const char kAccountId[] = "test_user@gmail.com";
23 const char kScope[] = "SCOPE";
25 class TestingOAuth2TokenServiceConsumer : public OAuth2TokenService::Consumer {
26 public:
27 TestingOAuth2TokenServiceConsumer();
28 ~TestingOAuth2TokenServiceConsumer() override;
30 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
31 const std::string& access_token,
32 const base::Time& expiration_time) override;
33 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
34 const GoogleServiceAuthError& error) override;
36 int num_get_token_success_;
37 int num_get_token_failure_;
38 std::string last_token_;
39 GoogleServiceAuthError last_error_;
42 TestingOAuth2TokenServiceConsumer::TestingOAuth2TokenServiceConsumer()
43 : OAuth2TokenService::Consumer("test"),
44 num_get_token_success_(0),
45 num_get_token_failure_(0),
46 last_error_(GoogleServiceAuthError::AuthErrorNone()) {
49 TestingOAuth2TokenServiceConsumer::~TestingOAuth2TokenServiceConsumer() {
52 void TestingOAuth2TokenServiceConsumer::OnGetTokenSuccess(
53 const OAuth2TokenService::Request* request,
54 const std::string& token,
55 const base::Time& expiration_date) {
56 last_token_ = token;
57 ++num_get_token_success_;
60 void TestingOAuth2TokenServiceConsumer::OnGetTokenFailure(
61 const OAuth2TokenService::Request* request,
62 const GoogleServiceAuthError& error) {
63 last_error_ = error;
64 ++num_get_token_failure_;
67 // A mock implementation of an OAuth2TokenService.
69 // Use SetResponse to vary the response to token requests.
70 class MockOAuth2TokenService : public FakeOAuth2TokenService {
71 public:
72 MockOAuth2TokenService();
73 ~MockOAuth2TokenService() override;
75 void SetResponse(const GoogleServiceAuthError& error,
76 const std::string& access_token,
77 const base::Time& expiration);
79 int num_invalidate_token() const { return num_invalidate_token_; }
81 const std::string& last_token_invalidated() const {
82 return last_token_invalidated_;
85 protected:
86 void FetchOAuth2Token(RequestImpl* request,
87 const std::string& account_id,
88 net::URLRequestContextGetter* getter,
89 const std::string& client_id,
90 const std::string& client_secret,
91 const ScopeSet& scopes) override;
93 void InvalidateAccessTokenImpl(const std::string& account_id,
94 const std::string& client_id,
95 const ScopeSet& scopes,
96 const std::string& access_token) override;
98 private:
99 GoogleServiceAuthError response_error_;
100 std::string response_access_token_;
101 base::Time response_expiration_;
102 int num_invalidate_token_;
103 std::string last_token_invalidated_;
106 MockOAuth2TokenService::MockOAuth2TokenService()
107 : response_error_(GoogleServiceAuthError::AuthErrorNone()),
108 response_access_token_(kAccessToken),
109 response_expiration_(base::Time::Max()),
110 num_invalidate_token_(0) {
113 MockOAuth2TokenService::~MockOAuth2TokenService() {
116 void MockOAuth2TokenService::SetResponse(const GoogleServiceAuthError& error,
117 const std::string& access_token,
118 const base::Time& expiration) {
119 response_error_ = error;
120 response_access_token_ = access_token;
121 response_expiration_ = expiration;
124 void MockOAuth2TokenService::FetchOAuth2Token(
125 RequestImpl* request,
126 const std::string& account_id,
127 net::URLRequestContextGetter* getter,
128 const std::string& client_id,
129 const std::string& client_secret,
130 const ScopeSet& scopes) {
131 base::MessageLoop::current()->PostTask(
132 FROM_HERE,
133 base::Bind(&OAuth2TokenService::RequestImpl::InformConsumer,
134 request->AsWeakPtr(),
135 response_error_,
136 response_access_token_,
137 response_expiration_));
140 void MockOAuth2TokenService::InvalidateAccessTokenImpl(
141 const std::string& account_id,
142 const std::string& client_id,
143 const ScopeSet& scopes,
144 const std::string& access_token) {
145 ++num_invalidate_token_;
146 last_token_invalidated_ = access_token;
149 class OAuth2TokenServiceRequestTest : public testing::Test {
150 public:
151 void SetUp() override;
152 void TearDown() override;
154 protected:
155 class Provider : public OAuth2TokenServiceRequest::TokenServiceProvider {
156 public:
157 Provider(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
158 OAuth2TokenService* token_service);
160 scoped_refptr<base::SingleThreadTaskRunner> GetTokenServiceTaskRunner()
161 override;
162 OAuth2TokenService* GetTokenService() override;
164 private:
165 ~Provider() override;
167 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
168 OAuth2TokenService* token_service_;
171 base::MessageLoop ui_loop_;
172 OAuth2TokenService::ScopeSet scopes_;
173 scoped_ptr<MockOAuth2TokenService> oauth2_service_;
174 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider> provider_;
175 TestingOAuth2TokenServiceConsumer consumer_;
178 void OAuth2TokenServiceRequestTest::SetUp() {
179 scopes_.insert(kScope);
180 oauth2_service_.reset(new MockOAuth2TokenService);
181 oauth2_service_->AddAccount(kAccountId);
182 provider_ =
183 new Provider(base::ThreadTaskRunnerHandle::Get(), oauth2_service_.get());
186 void OAuth2TokenServiceRequestTest::TearDown() {
187 // Run the loop to execute any pending tasks that may free resources.
188 ui_loop_.RunUntilIdle();
191 OAuth2TokenServiceRequestTest::Provider::Provider(
192 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
193 OAuth2TokenService* token_service)
194 : task_runner_(task_runner), token_service_(token_service) {
197 scoped_refptr<base::SingleThreadTaskRunner>
198 OAuth2TokenServiceRequestTest::Provider::GetTokenServiceTaskRunner() {
199 return task_runner_;
202 OAuth2TokenService* OAuth2TokenServiceRequestTest::Provider::GetTokenService() {
203 return token_service_;
206 OAuth2TokenServiceRequestTest::Provider::~Provider() {
209 TEST_F(OAuth2TokenServiceRequestTest, CreateAndStart_Failure) {
210 oauth2_service_->SetResponse(
211 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE),
212 std::string(),
213 base::Time());
214 scoped_ptr<OAuth2TokenServiceRequest> request(
215 OAuth2TokenServiceRequest::CreateAndStart(
216 provider_.get(), kAccountId, scopes_, &consumer_));
217 ui_loop_.RunUntilIdle();
218 EXPECT_EQ(0, consumer_.num_get_token_success_);
219 EXPECT_EQ(1, consumer_.num_get_token_failure_);
220 EXPECT_EQ(GoogleServiceAuthError::SERVICE_UNAVAILABLE,
221 consumer_.last_error_.state());
222 EXPECT_EQ(0, oauth2_service_->num_invalidate_token());
225 TEST_F(OAuth2TokenServiceRequestTest, CreateAndStart_Success) {
226 scoped_ptr<OAuth2TokenServiceRequest> request(
227 OAuth2TokenServiceRequest::CreateAndStart(
228 provider_.get(), kAccountId, scopes_, &consumer_));
229 ui_loop_.RunUntilIdle();
230 EXPECT_EQ(1, consumer_.num_get_token_success_);
231 EXPECT_EQ(0, consumer_.num_get_token_failure_);
232 EXPECT_EQ(kAccessToken, consumer_.last_token_);
233 EXPECT_EQ(0, oauth2_service_->num_invalidate_token());
236 TEST_F(OAuth2TokenServiceRequestTest,
237 CreateAndStart_DestroyRequestBeforeCompletes) {
238 scoped_ptr<OAuth2TokenServiceRequest> request(
239 OAuth2TokenServiceRequest::CreateAndStart(
240 provider_.get(), kAccountId, scopes_, &consumer_));
241 request.reset();
242 ui_loop_.RunUntilIdle();
243 EXPECT_EQ(0, consumer_.num_get_token_success_);
244 EXPECT_EQ(0, consumer_.num_get_token_failure_);
245 EXPECT_EQ(0, oauth2_service_->num_invalidate_token());
248 TEST_F(OAuth2TokenServiceRequestTest,
249 CreateAndStart_DestroyRequestAfterCompletes) {
250 scoped_ptr<OAuth2TokenServiceRequest> request(
251 OAuth2TokenServiceRequest::CreateAndStart(
252 provider_.get(), kAccountId, scopes_, &consumer_));
253 ui_loop_.RunUntilIdle();
254 request.reset();
255 EXPECT_EQ(1, consumer_.num_get_token_success_);
256 EXPECT_EQ(0, consumer_.num_get_token_failure_);
257 EXPECT_EQ(kAccessToken, consumer_.last_token_);
258 EXPECT_EQ(0, oauth2_service_->num_invalidate_token());
261 TEST_F(OAuth2TokenServiceRequestTest, InvalidateToken) {
262 OAuth2TokenServiceRequest::InvalidateToken(
263 provider_.get(), kAccountId, scopes_, kAccessToken);
264 ui_loop_.RunUntilIdle();
265 EXPECT_EQ(0, consumer_.num_get_token_success_);
266 EXPECT_EQ(0, consumer_.num_get_token_failure_);
267 EXPECT_EQ(kAccessToken, oauth2_service_->last_token_invalidated());
268 EXPECT_EQ(1, oauth2_service_->num_invalidate_token());
271 } // namespace