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 #include "chrome/browser/signin/ubertoken_fetcher.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | |
9 #include "chrome/browser/signin/fake_signin_manager.h" | |
10 #include "chrome/browser/signin/profile_oauth2_token_service.h" | |
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
12 #include "chrome/test/base/testing_profile.h" | |
13 #include "content/public/test/test_browser_thread_bundle.h" | |
14 #include "google_apis/gaia/gaia_constants.h" | |
15 #include "net/url_request/test_url_fetcher_factory.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace { | |
19 | |
20 const char kTestAccountId[] = "test@gmail.com"; | |
21 | |
22 class MockUbertokenConsumer : public UbertokenConsumer { | |
23 public: | |
24 MockUbertokenConsumer() | |
25 : nb_correct_token_(0), | |
26 last_error_(GoogleServiceAuthError::AuthErrorNone()), | |
27 nb_error_(0) { | |
28 } | |
29 virtual ~MockUbertokenConsumer() {} | |
30 | |
31 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE { | |
32 last_token_ = token; | |
33 ++ nb_correct_token_; | |
34 } | |
35 | |
36 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) | |
37 OVERRIDE { | |
38 last_error_ = error; | |
39 ++nb_error_; | |
40 } | |
41 | |
42 std::string last_token_; | |
43 int nb_correct_token_; | |
44 GoogleServiceAuthError last_error_; | |
45 int nb_error_; | |
46 }; | |
47 | |
48 } // namespace | |
49 | |
50 class UbertokenFetcherTest : public testing::Test { | |
51 public: | |
52 virtual void SetUp() OVERRIDE { | |
53 profile_ = CreateProfile(); | |
54 fetcher_.reset(new UbertokenFetcher(profile(), &consumer_)); | |
55 } | |
56 | |
57 scoped_ptr<TestingProfile> CreateProfile() { | |
58 TestingProfile::Builder builder; | |
59 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | |
60 &FakeProfileOAuth2TokenService::Build); | |
61 return builder.Build().Pass(); | |
62 } | |
63 | |
64 virtual void TearDown() OVERRIDE { | |
65 fetcher_.reset(); | |
66 } | |
67 | |
68 TestingProfile* profile() { return profile_.get(); } | |
69 | |
70 protected: | |
71 content::TestBrowserThreadBundle thread_bundle_; | |
72 scoped_ptr<TestingProfile> profile_; | |
73 net::TestURLFetcherFactory factory_; | |
74 MockUbertokenConsumer consumer_; | |
75 scoped_ptr<UbertokenFetcher> fetcher_; | |
76 }; | |
77 | |
78 TEST_F(UbertokenFetcherTest, Basic) { | |
79 } | |
80 | |
81 TEST_F(UbertokenFetcherTest, Success) { | |
82 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())-> | |
83 UpdateCredentials(kTestAccountId, "refreshToken"); | |
84 fetcher_->StartFetchingToken(kTestAccountId); | |
85 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time()); | |
86 fetcher_->OnUberAuthTokenSuccess("uberToken"); | |
87 EXPECT_EQ(0, consumer_.nb_error_); | |
88 EXPECT_EQ(1, consumer_.nb_correct_token_); | |
89 EXPECT_EQ("uberToken", consumer_.last_token_); | |
90 } | |
91 | |
92 TEST_F(UbertokenFetcherTest, NoRefreshToken) { | |
93 fetcher_->StartFetchingToken(kTestAccountId); | |
94 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP); | |
95 fetcher_->OnGetTokenFailure(NULL, error); | |
96 EXPECT_EQ(1, consumer_.nb_error_); | |
97 EXPECT_EQ(0, consumer_.nb_correct_token_); | |
98 } | |
99 | |
100 TEST_F(UbertokenFetcherTest, FailureToGetAccessToken) { | |
101 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP); | |
102 | |
103 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())-> | |
104 UpdateCredentials(kTestAccountId, "refreshToken"); | |
105 fetcher_->StartFetchingToken(kTestAccountId); | |
106 fetcher_->OnGetTokenFailure(NULL, error); | |
107 | |
108 EXPECT_EQ(1, consumer_.nb_error_); | |
109 EXPECT_EQ(0, consumer_.nb_correct_token_); | |
110 EXPECT_EQ("", consumer_.last_token_); | |
111 } | |
112 | |
113 TEST_F(UbertokenFetcherTest, FailureToGetUberToken) { | |
114 GoogleServiceAuthError error(GoogleServiceAuthError::USER_NOT_SIGNED_UP); | |
115 | |
116 ProfileOAuth2TokenServiceFactory::GetForProfile(profile())-> | |
117 UpdateCredentials(kTestAccountId, "refreshToken"); | |
118 fetcher_->StartFetchingToken(kTestAccountId); | |
119 fetcher_->OnGetTokenSuccess(NULL, "accessToken", base::Time()); | |
120 fetcher_->OnUberAuthTokenFailure(error); | |
121 | |
122 EXPECT_EQ(1, consumer_.nb_error_); | |
123 EXPECT_EQ(0, consumer_.nb_correct_token_); | |
124 EXPECT_EQ("", consumer_.last_token_); | |
125 } | |
OLD | NEW |