Chromium Code Reviews| 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 "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/gmock/include/gmock/gmock.h" | |
|
nyquist
2016/08/12 05:47:04
Is this used here?
xingliu
2016/08/12 17:58:27
Done.
| |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace blimp { | |
| 17 namespace client { | |
| 18 namespace { | |
| 19 | |
| 20 class IdentitySourceTest : public testing::Test { | |
| 21 public: | |
| 22 class MockIdentitySource : public IdentitySource { | |
|
nyquist
2016/08/12 05:47:04
Does this need to be an inner class, or could you
xingliu
2016/08/12 17:58:27
Done.
| |
| 23 public: | |
| 24 explicit MockIdentitySource( | |
| 25 std::unique_ptr<IdentityProvider> identity_provider) | |
| 26 : IdentitySource(std::move(identity_provider)), | |
| 27 success_(0), | |
| 28 fail_(0), | |
| 29 refresh_(0) {} | |
| 30 ~MockIdentitySource() override{}; | |
| 31 | |
| 32 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 33 const std::string& access_token, | |
| 34 const base::Time& expiration_time) override { | |
| 35 IdentitySource::OnGetTokenSuccess(request, access_token, expiration_time); | |
| 36 success_++; | |
| 37 token_ = access_token; | |
| 38 } | |
| 39 | |
| 40 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 41 const GoogleServiceAuthError& error) override { | |
| 42 IdentitySource::OnGetTokenFailure(request, error); | |
| 43 fail_++; | |
| 44 token_.clear(); | |
| 45 } | |
| 46 | |
| 47 void OnRefreshTokenAvailable(const std::string& account_id) override { | |
| 48 IdentitySource::OnRefreshTokenAvailable(account_id); | |
| 49 refresh_++; | |
| 50 } | |
| 51 | |
| 52 int Succeeded() { return success_; } | |
| 53 int Failed() { return fail_; } | |
| 54 int Refreshed() { return refresh_; } | |
| 55 const std::string& Token() { return token_; } | |
| 56 | |
| 57 private: | |
|
nyquist
2016/08/12 05:47:04
DISALLOW_COPY_AND_ASSIGN?
xingliu
2016/08/12 17:58:27
Done.
| |
| 58 std::string token_; | |
| 59 int success_; | |
| 60 int fail_; | |
| 61 int refresh_; | |
| 62 }; | |
| 63 | |
| 64 IdentitySourceTest() {} | |
|
nyquist
2016/08/12 05:47:04
= default here and below?
xingliu
2016/08/12 17:58:28
Done.
| |
| 65 ~IdentitySourceTest() override {} | |
| 66 | |
| 67 private: | |
| 68 DISALLOW_COPY_AND_ASSIGN(IdentitySourceTest); | |
| 69 }; | |
| 70 | |
| 71 TEST_F(IdentitySourceTest, TestConnect) { | |
| 72 std::unique_ptr<FakeOAuth2TokenService> token_service = | |
| 73 base::MakeUnique<FakeOAuth2TokenService>(); | |
| 74 FakeIdentityProvider* id_provider = | |
| 75 new FakeIdentityProvider(token_service.get()); | |
| 76 std::unique_ptr<IdentityProvider> id_ptr = | |
| 77 base::WrapUnique<IdentityProvider>((IdentityProvider*)id_provider); | |
| 78 | |
| 79 MockIdentitySource auth(std::move(id_ptr)); | |
| 80 TestBlimpClientContextDelegate mock_blimp_delegate; | |
| 81 auth.SetDelegate(&mock_blimp_delegate); | |
| 82 | |
| 83 // Connect when user is not signed in. Nothing happens. | |
| 84 id_provider->LogOut(); | |
| 85 auth.Connect(); | |
| 86 DCHECK_EQ(auth.Succeeded(), 0); | |
| 87 DCHECK_EQ(auth.Failed(), 0); | |
| 88 DCHECK_EQ(auth.Refreshed(), 0); | |
| 89 DCHECK_EQ(auth.Token(), std::string()); | |
| 90 | |
| 91 FakeOAuth2TokenServiceDelegate* mock_delegate = | |
|
nyquist
2016/08/12 05:47:04
Could you make this name a little bit more specifi
xingliu
2016/08/12 17:58:27
Done.
| |
| 92 token_service->GetFakeOAuth2TokenServiceDelegate(); | |
| 93 | |
| 94 // Connect when user signed in, but no refresh token, refresh token observer | |
| 95 // should be added. | |
| 96 std::string account = "mock_account"; | |
| 97 id_provider->LogIn(account); | |
| 98 mock_delegate->RevokeCredentials(account); | |
| 99 auth.Connect(); | |
| 100 DCHECK_EQ(auth.Succeeded(), 0); | |
| 101 DCHECK_EQ(auth.Failed(), 0); | |
| 102 DCHECK_EQ(auth.Refreshed(), 0); | |
| 103 | |
| 104 // Issue refresh token, listener should be triggered, and request should be | |
| 105 // sent. | |
| 106 mock_delegate->UpdateCredentials(account, "mock_refresh_token"); | |
| 107 DCHECK_EQ(auth.Succeeded(), 0); | |
| 108 DCHECK_EQ(auth.Failed(), 0); | |
| 109 DCHECK_EQ(auth.Refreshed(), 1); | |
| 110 | |
| 111 // Fire access token success, first request should be fulfilled. | |
| 112 base::Time time; | |
| 113 std::string mock_access_token = "mock_access_token"; | |
| 114 token_service->IssueAllTokensForAccount(account, mock_access_token, time); | |
| 115 DCHECK_EQ(auth.Succeeded(), 1); | |
| 116 DCHECK_EQ(auth.Failed(), 0); | |
| 117 DCHECK_EQ(auth.Token(), mock_access_token); | |
| 118 | |
| 119 // Connect again and fire access token failed. | |
| 120 GoogleServiceAuthError error(GoogleServiceAuthError::State::REQUEST_CANCELED); | |
| 121 auth.Connect(); | |
| 122 token_service->IssueErrorForAllPendingRequestsForAccount(account, error); | |
| 123 DCHECK_EQ(auth.Succeeded(), 1); | |
| 124 DCHECK_EQ(auth.Failed(), 1); | |
| 125 DCHECK_EQ(auth.Token(), std::string()); | |
| 126 | |
| 127 // Refresh token listener should have been removed. | |
| 128 mock_delegate->UpdateCredentials(account, "mock_refresh_token"); | |
| 129 DCHECK_EQ(auth.Refreshed(), 1); | |
| 130 | |
| 131 // Direct connect with refresh token, and no listener should be added. | |
| 132 auth.Connect(); | |
| 133 token_service->IssueAllTokensForAccount(account, mock_access_token, time); | |
| 134 DCHECK_EQ(auth.Succeeded(), 2); | |
| 135 DCHECK_EQ(auth.Token(), mock_access_token); | |
| 136 mock_delegate->UpdateCredentials(account, "mock_refresh_token"); | |
| 137 DCHECK_EQ(auth.Refreshed(), 1); | |
| 138 } | |
| 139 | |
| 140 } // namespace | |
| 141 } // namespace client | |
| 142 } // namespace blimp | |
| OLD | NEW |