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

Side by Side Diff: blimp/client/core/session/identity_source_unittest.cc

Issue 2204223005: Blimp OAuth2 token retreival on application start up. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unittests compiling. Created 4 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 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 "base/macros.h"
8 #include "base/memory/ptr_util.h"
9 #include "blimp/client/test/test_blimp_client_context_delegate.h"
10 #include "google_apis/gaia/fake_identity_provider.h"
11 #include "google_apis/gaia/fake_oauth2_token_service.h"
12 #include "google_apis/gaia/google_service_auth_error.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace blimp {
16 namespace client {
17 namespace {
18
19 class MockIdentitySource : public IdentitySource {
20 public:
21 explicit MockIdentitySource(
22 std::unique_ptr<IdentityProvider> identity_provider)
23 : IdentitySource(std::move(identity_provider)),
24 success_(0),
25 fail_(0),
26 refresh_(0) {}
27 ~MockIdentitySource() override{};
28
29 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
30 const std::string& access_token,
31 const base::Time& expiration_time) override {
32 IdentitySource::OnGetTokenSuccess(request, access_token, expiration_time);
33 success_++;
34 token_ = access_token;
35 }
36
37 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
38 const GoogleServiceAuthError& error) override {
39 IdentitySource::OnGetTokenFailure(request, error);
40 fail_++;
41 token_.clear();
42 }
43
44 void OnRefreshTokenAvailable(const std::string& account_id) override {
45 IdentitySource::OnRefreshTokenAvailable(account_id);
46 refresh_++;
47 }
48
49 int Succeeded() { return success_; }
50 int Failed() { return fail_; }
51 int Refreshed() { return refresh_; }
52 const std::string& Token() { return token_; }
53
54 private:
55 std::string token_;
56 int success_;
57 int fail_;
58 int refresh_;
59
60 DISALLOW_COPY_AND_ASSIGN(MockIdentitySource);
61 };
62
63 class IdentitySourceTest : public testing::Test {
64 public:
65 IdentitySourceTest() = default;
66 ~IdentitySourceTest() override = default;
67
68 private:
69 DISALLOW_COPY_AND_ASSIGN(IdentitySourceTest);
70 };
71
72 TEST_F(IdentitySourceTest, TestConnect) {
73 std::unique_ptr<FakeOAuth2TokenService> token_service =
74 base::MakeUnique<FakeOAuth2TokenService>();
75 FakeIdentityProvider* id_provider =
76 new FakeIdentityProvider(token_service.get());
77 std::unique_ptr<IdentityProvider> id_ptr =
78 base::WrapUnique<IdentityProvider>((IdentityProvider*)id_provider);
79
80 MockIdentitySource auth(std::move(id_ptr));
81 TestBlimpClientContextDelegate mock_blimp_delegate;
82 auth.SetDelegate(&mock_blimp_delegate);
83
84 // Connect when user is not signed in. Nothing happens.
85 IdentitySource::TokenCallback token_callback;
nyquist 2016/08/16 18:38:06 Should this test ensure that the callback is invok
xingliu 2016/08/17 00:32:24 Fixed, also move a couple of fake objects into Moc
86 id_provider->LogOut();
87 auth.Connect(token_callback);
88 DCHECK_EQ(auth.Succeeded(), 0);
89 DCHECK_EQ(auth.Failed(), 0);
90 DCHECK_EQ(auth.Refreshed(), 0);
91 DCHECK_EQ(auth.Token(), std::string());
92
93 FakeOAuth2TokenServiceDelegate* mock_token_service_delegate =
94 token_service->GetFakeOAuth2TokenServiceDelegate();
95
96 // Connect when user signed in, but no refresh token, refresh token observer
97 // should be added.
98 std::string account = "mock_account";
99 id_provider->LogIn(account);
100 mock_token_service_delegate->RevokeCredentials(account);
101 auth.Connect(token_callback);
102 DCHECK_EQ(auth.Succeeded(), 0);
103 DCHECK_EQ(auth.Failed(), 0);
104 DCHECK_EQ(auth.Refreshed(), 0);
105
106 // Issue refresh token, listener should be triggered, and request should be
107 // sent.
108 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token");
109 DCHECK_EQ(auth.Succeeded(), 0);
110 DCHECK_EQ(auth.Failed(), 0);
111 DCHECK_EQ(auth.Refreshed(), 1);
112
113 // Fire access token success, first request should be fulfilled.
114 base::Time time;
115 std::string mock_access_token = "mock_access_token";
116 token_service->IssueAllTokensForAccount(account, mock_access_token, time);
117 DCHECK_EQ(auth.Succeeded(), 1);
118 DCHECK_EQ(auth.Failed(), 0);
119 DCHECK_EQ(auth.Token(), mock_access_token);
120
121 // Connect again and fire access token failed.
122 GoogleServiceAuthError error(GoogleServiceAuthError::State::REQUEST_CANCELED);
123 auth.Connect(token_callback);
124 token_service->IssueErrorForAllPendingRequestsForAccount(account, error);
125 DCHECK_EQ(auth.Succeeded(), 1);
126 DCHECK_EQ(auth.Failed(), 1);
127 DCHECK_EQ(auth.Token(), std::string());
128
129 // Refresh token listener should have been removed.
130 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token");
131 DCHECK_EQ(auth.Refreshed(), 1);
132
133 // Direct connect with refresh token_callbacktoken, and no listener should be
134 // added.
135 auth.Connect(token_callback);
136 token_service->IssueAllTokensForAccount(account, mock_access_token, time);
137 DCHECK_EQ(auth.Succeeded(), 2);
138 DCHECK_EQ(auth.Token(), mock_access_token);
139 mock_token_service_delegate->UpdateCredentials(account, "mock_refresh_token");
140 DCHECK_EQ(auth.Refreshed(), 1);
141 }
142
143 } // namespace
144 } // namespace client
145 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698