OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "blimp/client/core/session/identity_source.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/callback.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "blimp/client/test/test_blimp_client_context_delegate.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace blimp { | |
18 namespace client { | |
19 namespace { | |
20 | |
21 class MockIdentitySource : public IdentitySource { | |
22 public: | |
23 explicit MockIdentitySource(BlimpClientContextDelegate* delegate) | |
24 : IdentitySource(delegate), | |
25 success_(0), | |
26 fail_(0), | |
27 refresh_(0), | |
28 token_callback_count_(0) {} | |
29 ~MockIdentitySource() override{}; | |
30 | |
31 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
32 const std::string& access_token, | |
33 const base::Time& expiration_time) override { | |
34 IdentitySource::OnGetTokenSuccess(request, access_token, expiration_time); | |
35 success_++; | |
36 token_ = access_token; | |
37 } | |
38 | |
39 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
40 const GoogleServiceAuthError& error) override { | |
41 IdentitySource::OnGetTokenFailure(request, error); | |
42 fail_++; | |
43 token_.clear(); | |
44 } | |
45 | |
46 void OnRefreshTokenAvailable(const std::string& account_id) override { | |
47 IdentitySource::OnRefreshTokenAvailable(account_id); | |
48 refresh_++; | |
49 } | |
50 | |
51 void MockTokenCall(const std::string& token) { | |
52 token_callback_count_++; | |
53 callback_token_ = token; | |
54 } | |
55 | |
56 IdentityProvider* GetIdentityProvider() { return identity_provider_.get(); } | |
57 int Succeeded() { return success_; } | |
58 int Failed() { return fail_; } | |
59 int Refreshed() { return refresh_; } | |
60 int TokenCallbackCount() { return token_callback_count_; } | |
61 // Return the token passed to TokenCallback. | |
62 const std::string& CallbackToken() { return callback_token_; } | |
63 // Return the token get from OAuth2TokenService. | |
64 const std::string& Token() { return token_; } | |
65 | |
66 private: | |
67 std::string token_; | |
68 int success_; | |
69 int fail_; | |
70 int refresh_; | |
71 | |
72 int token_callback_count_; | |
73 std::string callback_token_; | |
74 DISALLOW_COPY_AND_ASSIGN(MockIdentitySource); | |
75 }; | |
76 | |
77 class IdentitySourceTest : public testing::Test { | |
78 public: | |
79 IdentitySourceTest() = default; | |
80 ~IdentitySourceTest() override = default; | |
81 | |
82 private: | |
83 DISALLOW_COPY_AND_ASSIGN(IdentitySourceTest); | |
84 }; | |
85 | |
86 TEST_F(IdentitySourceTest, TestConnect) { | |
87 TestBlimpClientContextDelegate mock_blimp_delegate; | |
88 MockIdentitySource auth(&mock_blimp_delegate); | |
89 auth.SetTokenCallback( | |
90 base::Bind(&MockIdentitySource::MockTokenCall, base::Unretained(&auth))); | |
91 FakeIdentityProvider* id_provider = | |
92 static_cast<FakeIdentityProvider*>(auth.GetIdentityProvider()); | |
93 FakeOAuth2TokenService* token_service = mock_blimp_delegate.GetTokenService(); | |
94 | |
95 // Connect when user is not signed in. Nothing happens. | |
96 id_provider->LogOut(); | |
97 auth.Connect(); | |
98 DCHECK_EQ(auth.Succeeded(), 0); | |
99 DCHECK_EQ(auth.Failed(), 0); | |
100 DCHECK_EQ(auth.Refreshed(), 0); | |
101 DCHECK_EQ(auth.Token(), std::string()); | |
102 | |
103 FakeOAuth2TokenServiceDelegate* mock_token_service_delegate = | |
104 token_service->GetFakeOAuth2TokenServiceDelegate(); | |
105 | |
106 // Connect when user signed in, but no refresh token, refresh token observer | |
107 // should be added. | |
108 std::string account = "mock_account"; | |
109 id_provider->LogIn(account); | |
110 mock_token_service_delegate->RevokeCredentials(account); | |
111 // Mock duplicate connect calls in this test. | |
112 auth.Connect(); | |
113 auth.Connect(); | |
114 DCHECK_EQ(auth.Succeeded(), 0); | |
115 DCHECK_EQ(auth.Failed(), 0); | |
116 DCHECK_EQ(auth.Refreshed(), 0); | |
117 DCHECK_EQ(auth.TokenCallbackCount(), 0); | |
118 | |
119 // Issue refresh token, listener should be triggered, and request should be | |
120 // sent. | |
121 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token"); | |
122 DCHECK_EQ(auth.Succeeded(), 0); | |
David Trainor- moved to gerrit
2016/08/18 17:27:06
Can we add a way to reset the counts so it's easie
xingliu
2016/08/18 18:29:22
Done.
| |
123 DCHECK_EQ(auth.Failed(), 0); | |
124 DCHECK_EQ(auth.Refreshed(), 1); | |
125 DCHECK_EQ(auth.TokenCallbackCount(), 0); | |
126 | |
127 // Fire access token success, first request should be fulfilled. | |
128 base::Time time; | |
129 std::string mock_access_token = "mock_access_token"; | |
130 token_service->IssueAllTokensForAccount(account, mock_access_token, time); | |
131 DCHECK_EQ(auth.Succeeded(), 1); | |
132 DCHECK_EQ(auth.Failed(), 0); | |
133 DCHECK_EQ(auth.Token(), mock_access_token); | |
134 DCHECK_EQ(auth.TokenCallbackCount(), 1); | |
135 DCHECK_EQ(auth.CallbackToken(), mock_access_token); | |
136 | |
137 // Connect again and fire access token failed. | |
138 GoogleServiceAuthError error(GoogleServiceAuthError::State::REQUEST_CANCELED); | |
139 auth.Connect(); | |
140 auth.Connect(); | |
141 token_service->IssueErrorForAllPendingRequestsForAccount(account, error); | |
142 DCHECK_EQ(auth.Succeeded(), 1); | |
143 DCHECK_EQ(auth.Failed(), 1); | |
144 DCHECK_EQ(auth.Token(), std::string()); | |
145 DCHECK_EQ(auth.TokenCallbackCount(), 1); | |
146 | |
147 // Refresh token listener should have been removed. | |
148 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token"); | |
149 DCHECK_EQ(auth.Refreshed(), 1); | |
150 | |
151 // Direct connect with refresh token, and no listener should be | |
152 // added. | |
153 auth.Connect(); | |
154 auth.Connect(); | |
155 token_service->IssueAllTokensForAccount(account, mock_access_token, time); | |
156 DCHECK_EQ(auth.Succeeded(), 2); | |
157 DCHECK_EQ(auth.Token(), mock_access_token); | |
158 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token"); | |
159 DCHECK_EQ(auth.Refreshed(), 1); | |
160 DCHECK_EQ(auth.TokenCallbackCount(), 2); | |
161 DCHECK_EQ(auth.CallbackToken(), mock_access_token); | |
162 } | |
163 | |
164 } // namespace | |
165 } // namespace client | |
166 } // namespace blimp | |
OLD | NEW |