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

Side by Side Diff: chrome/browser/net/gaia/token_service_unittest.cc

Issue 3024002: Add IssueAuthToken support to the TokenService. (Closed)
Patch Set: Code review fixes Created 10 years, 4 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 (c) 2010 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 // This file defines a unit test for the profile's token service.
6
7 #include "chrome/browser/net/gaia/token_service.h"
8 #include "chrome/common/net/gaia/gaia_authenticator2_unittest.h"
9 #include "chrome/common/net/gaia/gaia_auth_consumer.h"
10 #include "chrome/common/net/gaia/gaia_constants.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "chrome/test/test_notification_tracker.h"
13
14 // TestNotificationTracker doesn't do a deep copy on the notification details.
15 // We have to in order to read it out, or we have a bad ptr, since the details
16 // are a reference on the stack.
17 class TokenAvailableTracker : public TestNotificationTracker {
18 public:
19 const TokenService::TokenAvailableDetails& get_last_token_details() {
20 return details_;
21 }
22
23 private:
24 virtual void Observe(NotificationType type,
25 const NotificationSource& source,
26 const NotificationDetails& details) {
27 TestNotificationTracker::Observe(type, source, details);
28 if (type == NotificationType::TOKEN_AVAILABLE) {
29 Details<const TokenService::TokenAvailableDetails> full = details;
30 details_ = *full.ptr();
31 }
32 }
33
34 TokenService::TokenAvailableDetails details_;
35 };
36
37 class TokenFailedTracker : public TestNotificationTracker {
38 public:
39 const TokenService::TokenRequestFailedDetails& get_last_token_details() {
40 return details_;
41 }
42
43 private:
44 virtual void Observe(NotificationType type,
45 const NotificationSource& source,
46 const NotificationDetails& details) {
47 TestNotificationTracker::Observe(type, source, details);
48 if (type == NotificationType::TOKEN_REQUEST_FAILED) {
49 Details<const TokenService::TokenRequestFailedDetails> full = details;
50 details_ = *full.ptr();
51 }
52 }
53
54 TokenService::TokenRequestFailedDetails details_;
55 };
56
57 class TokenServiceTest : public testing::Test {
58 public:
59 TokenServiceTest() {
60 credentials_.sid = "sid";
61 credentials_.lsid = "lsid";
62 credentials_.token = "token";
63 credentials_.data = "data";
64
65 success_tracker_.ListenFor(NotificationType::TOKEN_AVAILABLE,
66 Source<TokenService>(&service_));
67 failure_tracker_.ListenFor(NotificationType::TOKEN_REQUEST_FAILED,
68 Source<TokenService>(&service_));
69
70 service_.Initialize("test", profile_.GetRequestContext(), credentials_);
71 }
72
73 TokenService service_;
74 TokenAvailableTracker success_tracker_;
75 TokenFailedTracker failure_tracker_;
76 GaiaAuthConsumer::ClientLoginResult credentials_;
77
78 private:
79 TestingProfile profile_;
80 };
81
82 TEST_F(TokenServiceTest, SanityCheck) {
83 EXPECT_TRUE(service_.HasLsid());
84 EXPECT_EQ(service_.GetLsid(), "lsid");
85 EXPECT_FALSE(service_.HasTokenForService("nonexistant service"));
86 }
87
88 TEST_F(TokenServiceTest, NoToken) {
89 EXPECT_FALSE(service_.HasTokenForService("nonexistant service"));
90 EXPECT_EQ(service_.GetTokenForService("nonexistant service"), std::string());
91 }
92
93 TEST_F(TokenServiceTest, NotificationSuccess) {
94 EXPECT_EQ(0U, success_tracker_.size());
95 EXPECT_EQ(0U, failure_tracker_.size());
96 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token");
97 EXPECT_EQ(1U, success_tracker_.size());
98 EXPECT_EQ(0U, failure_tracker_.size());
99
100 TokenService::TokenAvailableDetails details =
101 success_tracker_.get_last_token_details();
102 EXPECT_EQ(details.service(), GaiaConstants::kSyncService);
103 EXPECT_EQ(details.token(), "token");
104 }
105
106 TEST_F(TokenServiceTest, NotificationFailed) {
107 EXPECT_EQ(0U, success_tracker_.size());
108 EXPECT_EQ(0U, failure_tracker_.size());
109 GaiaAuthConsumer::GaiaAuthError error;
110 error.code = GaiaAuthConsumer::REQUEST_CANCELED;
111 service_.OnIssueAuthTokenFailure(GaiaConstants::kSyncService, error);
112 EXPECT_EQ(0U, success_tracker_.size());
113 EXPECT_EQ(1U, failure_tracker_.size());
114
115 TokenService::TokenRequestFailedDetails details =
116 failure_tracker_.get_last_token_details();
117 EXPECT_EQ(details.service(), GaiaConstants::kSyncService);
118 EXPECT_TRUE(details.error() == error); // Struct has no print function
119 }
120
121 TEST_F(TokenServiceTest, OnTokenSuccessUpdate) {
122 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token");
123 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService));
124 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token");
125
126 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token2");
127 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService));
128 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token2");
129
130 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "");
131 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService));
132 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "");
133 }
134
135 TEST_F(TokenServiceTest, OnTokenSuccess) {
136 // Don't "start fetching", just go ahead and issue the callback.
137 service_.OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, "token");
138 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService));
139 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService));
140 // Gaia returns the entire result as the token so while this is a shared
141 // result with ClientLogin, it doesn't matter, we should still get it back.
142 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token");
143
144 // Check 2nd service
145 service_.OnIssueAuthTokenSuccess(GaiaConstants::kTalkService, "token2");
146 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kTalkService));
147 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), "token2");
148
149 // Didn't change
150 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), "token");
151 }
152
153 TEST_F(TokenServiceTest, FullIntegration) {
154 MockFactory factory;
155 std::string result = "SID=sid\nLSID=lsid\nAuth=auth\n";
156 factory.set_results(result);
157 URLFetcher::set_factory(&factory);
158 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kSyncService));
159 EXPECT_FALSE(service_.HasTokenForService(GaiaConstants::kTalkService));
160 service_.StartFetchingTokens();
161 URLFetcher::set_factory(NULL);
162
163 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kSyncService));
164 EXPECT_TRUE(service_.HasTokenForService(GaiaConstants::kTalkService));
165 // Gaia returns the entire result as the token so while this is a shared
166 // result with ClientLogin, it doesn't matter, we should still get it back.
167 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kSyncService), result);
168 EXPECT_EQ(service_.GetTokenForService(GaiaConstants::kTalkService), result);
169 }
OLDNEW
« no previous file with comments | « chrome/browser/net/gaia/token_service.cc ('k') | chrome/browser/sync/profile_sync_service_startup_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698