| Index: blimp/client/core/authenticator_unittest.cc
|
| diff --git a/blimp/client/core/authenticator_unittest.cc b/blimp/client/core/authenticator_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..239f1d4c68687da2334ee62a36238c86e83f7aef
|
| --- /dev/null
|
| +++ b/blimp/client/core/authenticator_unittest.cc
|
| @@ -0,0 +1,147 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "blimp/client/core/authenticator.h"
|
| +
|
| +#include "base/macros.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "google_apis/gaia/fake_identity_provider.h"
|
| +#include "google_apis/gaia/fake_oauth2_token_service.h"
|
| +#include "google_apis/gaia/google_service_auth_error.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace blimp {
|
| +namespace client {
|
| +namespace {
|
| +
|
| +class AuthenticatorTest : public testing::Test {
|
| + public:
|
| + class MockAuthenticator : public Authenticator {
|
| + public:
|
| + explicit MockAuthenticator(
|
| + std::unique_ptr<IdentityProvider> identity_provider)
|
| + : Authenticator(std::move(identity_provider)),
|
| + success_(0),
|
| + fail_(0),
|
| + refresh_(0) {}
|
| + ~MockAuthenticator() override {};
|
| +
|
| + void OnGetTokenSuccess(
|
| + const OAuth2TokenService::Request* request,
|
| + const std::string& access_token,
|
| + const base::Time& expiration_time) override {
|
| + Authenticator::OnGetTokenSuccess(request, access_token, expiration_time);
|
| + success_++;
|
| + token_ = access_token;
|
| + }
|
| +
|
| + void OnGetTokenFailure(
|
| + const OAuth2TokenService::Request* request,
|
| + const GoogleServiceAuthError& error) override {
|
| + Authenticator::OnGetTokenFailure(request, error);
|
| + fail_++;
|
| + token_ = std::string();
|
| + }
|
| +
|
| + void OnRefreshTokenAvailable(const std::string& account_id) override {
|
| + Authenticator::OnRefreshTokenAvailable(account_id);
|
| + refresh_++;
|
| + }
|
| +
|
| + int Succeeded() { return success_; }
|
| + int Failed() { return fail_; }
|
| + int Refreshed() { return refresh_; }
|
| + const std::string& Token() { return token_; }
|
| +
|
| + private:
|
| + std::string token_;
|
| + int success_;
|
| + int fail_;
|
| + int refresh_;
|
| + };
|
| +
|
| + AuthenticatorTest() {}
|
| + ~AuthenticatorTest() override {}
|
| +
|
| + void SetUp() override {
|
| + }
|
| +
|
| + void TearDown() override {
|
| + }
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(AuthenticatorTest);
|
| +};
|
| +
|
| +TEST_F(AuthenticatorTest, TestConnect) {
|
| + std::unique_ptr<FakeOAuth2TokenService> token_service =
|
| + base::MakeUnique<FakeOAuth2TokenService>();
|
| + FakeIdentityProvider* id_provider = new FakeIdentityProvider(
|
| + token_service.get());
|
| + std::unique_ptr<IdentityProvider> id_ptr =
|
| + base::WrapUnique<IdentityProvider>((IdentityProvider*)id_provider);
|
| +
|
| + MockAuthenticator auth(std::move(id_ptr));
|
| +
|
| + // Connect when user is not signed in. Nothing happens.
|
| + id_provider->LogOut();
|
| + auth.Connect();
|
| + DCHECK_EQ(auth.Succeeded(), 0);
|
| + DCHECK_EQ(auth.Failed(), 0);
|
| + DCHECK_EQ(auth.Refreshed(), 0);
|
| + DCHECK_EQ(auth.Token(), std::string());
|
| +
|
| + FakeOAuth2TokenServiceDelegate* mock_delegate = token_service
|
| + ->GetFakeOAuth2TokenServiceDelegate();
|
| +
|
| + // Connect when user signed in, but no refresh token, refresh token observer
|
| + // should be added.
|
| + std::string account = "mock_account";
|
| + id_provider->LogIn(account);
|
| + mock_delegate->RevokeCredentials(account);
|
| + auth.Connect();
|
| + DCHECK_EQ(auth.Succeeded(), 0);
|
| + DCHECK_EQ(auth.Failed(), 0);
|
| + DCHECK_EQ(auth.Refreshed(), 0);
|
| +
|
| + // Issue refresh token, listener should be triggered, and request should be
|
| + // sent.
|
| + mock_delegate->UpdateCredentials(account, "mock_refresh_token");
|
| + DCHECK_EQ(auth.Succeeded(), 0);
|
| + DCHECK_EQ(auth.Failed(), 0);
|
| + DCHECK_EQ(auth.Refreshed(), 1);
|
| +
|
| + // Fire access token success, first request should be fulfilled.
|
| + base::Time time;
|
| + std::string mock_access_token = "mock_access_token";
|
| + token_service->IssueAllTokensForAccount(account, mock_access_token, time);
|
| + DCHECK_EQ(auth.Succeeded(), 1);
|
| + DCHECK_EQ(auth.Failed(), 0);
|
| + DCHECK_EQ(auth.Token(), mock_access_token);
|
| +
|
| + // Connect again and fire access token failed.
|
| + GoogleServiceAuthError error(GoogleServiceAuthError::State::REQUEST_CANCELED);
|
| + auth.Connect();
|
| + token_service->IssueErrorForAllPendingRequestsForAccount(account, error);
|
| + DCHECK_EQ(auth.Succeeded(), 1);
|
| + DCHECK_EQ(auth.Failed(), 1);
|
| + DCHECK_EQ(auth.Token(), std::string());
|
| +
|
| + // Refresh token listener should have been removed.
|
| + mock_delegate->UpdateCredentials(account, "mock_refresh_token");
|
| + DCHECK_EQ(auth.Refreshed(), 1);
|
| +
|
| + // Direct connect with refresh token, and no listener should be added.
|
| + auth.Connect();
|
| + token_service->IssueAllTokensForAccount(account, mock_access_token, time);
|
| + DCHECK_EQ(auth.Succeeded(), 2);
|
| + DCHECK_EQ(auth.Token(), mock_access_token);
|
| + mock_delegate->UpdateCredentials(account, "mock_refresh_token");
|
| + DCHECK_EQ(auth.Refreshed(), 1);
|
| +}
|
| +
|
| +} // namespace
|
| +} // namespace client
|
| +} // namespace blimp
|
|
|