Commit c6123f52 authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

Use IdentityTestEnvironment APIs when applicable in //c/b/signin/dice_response_handler_unittest.cc

CL is a (driven-by) clean up pass on dice_response_handler_unittests.cc,
where pair calls to AccountTrackerService::SeedAccountInfo and
IdentityTestEnvironment::SetRefreshTokenForAccount are replaced by calls
to the wrapper IdentityTestEnvironment::MakeAccountAvailable.

CL also replaces from the tests the knowledge about the specific
refresh token values (eg "refresh_token_for_bleh"), by checks that
verify whether the IdentityManager holds a persistent error state or not.

BUG=890790

Change-Id: I74215310276023a387854d840a96763545699dfe
Reviewed-on: https://chromium-review.googlesource.com/c/1393303Reviewed-by: default avatarDavid Roger <droger@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Cr-Commit-Position: refs/heads/master@{#619930}
parent 6d16ec71
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "google_apis/gaia/fake_oauth2_token_service_delegate.h" #include "google_apis/gaia/fake_oauth2_token_service_delegate.h"
#include "services/identity/public/cpp/identity_test_environment.h" #include "services/identity/public/cpp/identity_test_environment.h"
#include "services/identity/public/cpp/identity_test_utils.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -44,10 +45,7 @@ using signin::DiceResponseParams; ...@@ -44,10 +45,7 @@ using signin::DiceResponseParams;
namespace { namespace {
const char kAuthorizationCode[] = "authorization_code"; const char kAuthorizationCode[] = "authorization_code";
const char kEmail[] = "email"; const char kEmail[] = "test@email.com";
const char kGaiaID[] = "gaia_id_for_email"; // This matches the gaia_id value
// to be used by
// IdentityTestEnvironment.
const int kSessionIndex = 42; const int kSessionIndex = 42;
// TestSigninClient implementation that intercepts the GaiaAuthConsumer and // TestSigninClient implementation that intercepts the GaiaAuthConsumer and
...@@ -166,7 +164,7 @@ class DiceResponseHandlerTest : public testing::Test, ...@@ -166,7 +164,7 @@ class DiceResponseHandlerTest : public testing::Test,
DiceResponseParams dice_params; DiceResponseParams dice_params;
dice_params.user_intention = action; dice_params.user_intention = action;
DiceResponseParams::AccountInfo account_info; DiceResponseParams::AccountInfo account_info;
account_info.gaia_id = kGaiaID; account_info.gaia_id = identity::GetTestGaiaIdForEmail(kEmail);
account_info.email = kEmail; account_info.email = kEmail;
account_info.session_index = kSessionIndex; account_info.session_index = kSessionIndex;
switch (action) { switch (action) {
...@@ -193,12 +191,6 @@ class DiceResponseHandlerTest : public testing::Test, ...@@ -193,12 +191,6 @@ class DiceResponseHandlerTest : public testing::Test,
return dice_params; return dice_params;
} }
std::string GetRefreshToken(const std::string& account_id) {
return static_cast<FakeOAuth2TokenServiceDelegate*>(
token_service_.GetDelegate())
->GetRefreshToken(account_id);
}
// AccountReconcilor::Observer: // AccountReconcilor::Observer:
void OnBlockReconcile() override { ++reconcilor_blocked_count_; } void OnBlockReconcile() override { ++reconcilor_blocked_count_; }
void OnUnblockReconcile() override { ++reconcilor_unblocked_count_; } void OnUnblockReconcile() override { ++reconcilor_unblocked_count_; }
...@@ -479,21 +471,19 @@ TEST_F(DiceResponseHandlerTest, Timeout) { ...@@ -479,21 +471,19 @@ TEST_F(DiceResponseHandlerTest, Timeout) {
TEST_F(DiceResponseHandlerTest, SignoutMainAccount) { TEST_F(DiceResponseHandlerTest, SignoutMainAccount) {
InitializeDiceResponseHandler(signin::AccountConsistencyMethod::kDice); InitializeDiceResponseHandler(signin::AccountConsistencyMethod::kDice);
const char kSecondaryGaiaID[] = "secondary_account";
const char kSecondaryEmail[] = "other@gmail.com"; const char kSecondaryEmail[] = "other@gmail.com";
DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNOUT); DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNOUT);
const auto& dice_account_info = dice_params.signout_info->account_infos[0]; const auto& dice_account_info = dice_params.signout_info->account_infos[0];
std::string secondary_account_id = account_tracker_service_.SeedAccountInfo(
kSecondaryGaiaID, kSecondaryEmail);
// User is signed in to Chrome, and has some refresh token for a secondary // User is signed in to Chrome, and has some refresh token for a secondary
// account. // account.
AccountInfo account_info = AccountInfo account_info =
identity_test_env_.MakePrimaryAccountAvailable(dice_account_info.email); identity_test_env_.MakePrimaryAccountAvailable(dice_account_info.email);
identity_test_env_.SetRefreshTokenForAccount(secondary_account_id); AccountInfo secondary_account_info =
identity_test_env_.MakeAccountAvailable(kSecondaryEmail);
EXPECT_TRUE( EXPECT_TRUE(
identity_manager()->HasAccountWithRefreshToken(account_info.account_id)); identity_manager()->HasAccountWithRefreshToken(account_info.account_id));
EXPECT_TRUE( EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(
identity_manager()->HasAccountWithRefreshToken(secondary_account_id)); secondary_account_info.account_id));
EXPECT_TRUE(identity_manager()->HasPrimaryAccount()); EXPECT_TRUE(identity_manager()->HasPrimaryAccount());
// Receive signout response for the main account. // Receive signout response for the main account.
dice_response_handler_->ProcessDiceHeader( dice_response_handler_->ProcessDiceHeader(
...@@ -503,12 +493,22 @@ TEST_F(DiceResponseHandlerTest, SignoutMainAccount) { ...@@ -503,12 +493,22 @@ TEST_F(DiceResponseHandlerTest, SignoutMainAccount) {
// secondary account is untouched. // secondary account is untouched.
EXPECT_TRUE( EXPECT_TRUE(
identity_manager()->HasAccountWithRefreshToken(account_info.account_id)); identity_manager()->HasAccountWithRefreshToken(account_info.account_id));
EXPECT_EQ(MutableProfileOAuth2TokenServiceDelegate::kInvalidRefreshToken,
GetRefreshToken(account_info.account_id));
EXPECT_TRUE( EXPECT_TRUE(
identity_manager()->HasAccountWithRefreshToken(secondary_account_id)); identity_manager()->HasAccountWithRefreshTokenInPersistentErrorState(
EXPECT_EQ(std::string("refresh_token_for_") + secondary_account_id, account_info.account_id));
GetRefreshToken(secondary_account_id)); auto error = identity_manager()->GetErrorStateOfRefreshTokenForAccount(
account_info.account_id);
EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, error.state());
EXPECT_EQ(GoogleServiceAuthError::InvalidGaiaCredentialsReason::
CREDENTIALS_REJECTED_BY_CLIENT,
error.GetInvalidGaiaCredentialsReason());
EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(
secondary_account_info.account_id));
EXPECT_FALSE(
identity_manager()->HasAccountWithRefreshTokenInPersistentErrorState(
secondary_account_info.account_id));
EXPECT_TRUE(identity_manager()->HasPrimaryAccount()); EXPECT_TRUE(identity_manager()->HasPrimaryAccount());
// Check that the reconcilor was not blocked. // Check that the reconcilor was not blocked.
EXPECT_EQ(0, reconcilor_blocked_count_); EXPECT_EQ(0, reconcilor_blocked_count_);
...@@ -518,24 +518,22 @@ TEST_F(DiceResponseHandlerTest, SignoutMainAccount) { ...@@ -518,24 +518,22 @@ TEST_F(DiceResponseHandlerTest, SignoutMainAccount) {
TEST_F(DiceResponseHandlerTest, MigrationSignout) { TEST_F(DiceResponseHandlerTest, MigrationSignout) {
InitializeDiceResponseHandler( InitializeDiceResponseHandler(
signin::AccountConsistencyMethod::kDiceMigration); signin::AccountConsistencyMethod::kDiceMigration);
const char kSecondaryGaiaID[] = "secondary_account";
const char kSecondaryEmail[] = "other@gmail.com"; const char kSecondaryEmail[] = "other@gmail.com";
DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNOUT); DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNOUT);
dice_params.signout_info->account_infos.emplace_back(kSecondaryGaiaID, dice_params.signout_info->account_infos.emplace_back(
kSecondaryEmail, 1); identity::GetTestGaiaIdForEmail(kSecondaryEmail), kSecondaryEmail, 1);
const auto& main_account_info = dice_params.signout_info->account_infos[0]; const auto& main_account_info = dice_params.signout_info->account_infos[0];
std::string secondary_account_id = account_tracker_service_.SeedAccountInfo(
kSecondaryGaiaID, kSecondaryEmail);
// User is signed in to Chrome, and has some refresh token for a secondary // User is signed in to Chrome, and has some refresh token for a secondary
// account. // account.
AccountInfo account_info = AccountInfo account_info =
identity_test_env_.MakePrimaryAccountAvailable(main_account_info.email); identity_test_env_.MakePrimaryAccountAvailable(main_account_info.email);
identity_test_env_.SetRefreshTokenForAccount(secondary_account_id); AccountInfo secondary_account_info =
identity_test_env_.MakeAccountAvailable(kSecondaryEmail);
EXPECT_TRUE( EXPECT_TRUE(
identity_manager()->HasAccountWithRefreshToken(account_info.account_id)); identity_manager()->HasAccountWithRefreshToken(account_info.account_id));
EXPECT_TRUE( EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(
identity_manager()->HasAccountWithRefreshToken(secondary_account_id)); secondary_account_info.account_id));
EXPECT_TRUE(identity_manager()->HasPrimaryAccount()); EXPECT_TRUE(identity_manager()->HasPrimaryAccount());
// Receive signout response for all accounts. // Receive signout response for all accounts.
...@@ -545,8 +543,8 @@ TEST_F(DiceResponseHandlerTest, MigrationSignout) { ...@@ -545,8 +543,8 @@ TEST_F(DiceResponseHandlerTest, MigrationSignout) {
// User is not signed out from Chrome, only the secondary token is deleted. // User is not signed out from Chrome, only the secondary token is deleted.
EXPECT_TRUE( EXPECT_TRUE(
identity_manager()->HasAccountWithRefreshToken(account_info.account_id)); identity_manager()->HasAccountWithRefreshToken(account_info.account_id));
EXPECT_FALSE( EXPECT_FALSE(identity_manager()->HasAccountWithRefreshToken(
identity_manager()->HasAccountWithRefreshToken(secondary_account_id)); secondary_account_info.account_id));
EXPECT_TRUE(identity_manager()->HasPrimaryAccount()); EXPECT_TRUE(identity_manager()->HasPrimaryAccount());
// Check that the reconcilor was not blocked. // Check that the reconcilor was not blocked.
EXPECT_EQ(0, reconcilor_blocked_count_); EXPECT_EQ(0, reconcilor_blocked_count_);
...@@ -557,15 +555,16 @@ TEST_F(DiceResponseHandlerTest, SignoutSecondaryAccount) { ...@@ -557,15 +555,16 @@ TEST_F(DiceResponseHandlerTest, SignoutSecondaryAccount) {
InitializeDiceResponseHandler(signin::AccountConsistencyMethod::kDice); InitializeDiceResponseHandler(signin::AccountConsistencyMethod::kDice);
const char kMainEmail[] = "main@gmail.com"; const char kMainEmail[] = "main@gmail.com";
DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNOUT); DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNOUT);
const auto& account_info = dice_params.signout_info->account_infos[0]; const auto& secondary_dice_account_info =
std::string account_id = account_tracker_service_.SeedAccountInfo( dice_params.signout_info->account_infos[0];
account_info.gaia_id, account_info.email);
// User is signed in to Chrome, and has some refresh token for a secondary // User is signed in to Chrome, and has some refresh token for a secondary
// account. // account.
auto main_account_info = AccountInfo main_account_info =
identity_test_env_.MakePrimaryAccountAvailable(kMainEmail); identity_test_env_.MakePrimaryAccountAvailable(kMainEmail);
identity_test_env_.SetRefreshTokenForAccount(account_id); AccountInfo secondary_account_info = identity_test_env_.MakeAccountAvailable(
EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(account_id)); secondary_dice_account_info.email);
EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(
secondary_account_info.account_id));
EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken( EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(
main_account_info.account_id)); main_account_info.account_id));
EXPECT_TRUE(identity_manager()->HasPrimaryAccount()); EXPECT_TRUE(identity_manager()->HasPrimaryAccount());
...@@ -575,7 +574,8 @@ TEST_F(DiceResponseHandlerTest, SignoutSecondaryAccount) { ...@@ -575,7 +574,8 @@ TEST_F(DiceResponseHandlerTest, SignoutSecondaryAccount) {
// Only the token corresponding the the Dice parameter has been removed, and // Only the token corresponding the the Dice parameter has been removed, and
// the user is still signed in. // the user is still signed in.
EXPECT_FALSE(identity_manager()->HasAccountWithRefreshToken(account_id)); EXPECT_FALSE(identity_manager()->HasAccountWithRefreshToken(
secondary_account_info.account_id));
EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken( EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(
main_account_info.account_id)); main_account_info.account_id));
EXPECT_TRUE(identity_manager()->HasPrimaryAccount()); EXPECT_TRUE(identity_manager()->HasPrimaryAccount());
...@@ -583,29 +583,28 @@ TEST_F(DiceResponseHandlerTest, SignoutSecondaryAccount) { ...@@ -583,29 +583,28 @@ TEST_F(DiceResponseHandlerTest, SignoutSecondaryAccount) {
TEST_F(DiceResponseHandlerTest, SignoutWebOnly) { TEST_F(DiceResponseHandlerTest, SignoutWebOnly) {
InitializeDiceResponseHandler(signin::AccountConsistencyMethod::kDice); InitializeDiceResponseHandler(signin::AccountConsistencyMethod::kDice);
const char kSecondaryGaiaID[] = "secondary_account";
const char kSecondaryEmail[] = "other@gmail.com"; const char kSecondaryEmail[] = "other@gmail.com";
DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNOUT); DiceResponseParams dice_params = MakeDiceParams(DiceAction::SIGNOUT);
const auto& account_info = dice_params.signout_info->account_infos[0]; const auto& dice_account_info = dice_params.signout_info->account_infos[0];
std::string account_id = account_tracker_service_.SeedAccountInfo(
account_info.gaia_id, account_info.email);
std::string secondary_account_id = account_tracker_service_.SeedAccountInfo(
kSecondaryGaiaID, kSecondaryEmail);
// User is NOT signed in to Chrome, and has some refresh tokens for two // User is NOT signed in to Chrome, and has some refresh tokens for two
// accounts. // accounts.
identity_test_env_.SetRefreshTokenForAccount(account_id); AccountInfo account_info =
identity_test_env_.SetRefreshTokenForAccount(secondary_account_id); identity_test_env_.MakeAccountAvailable(dice_account_info.email);
EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(account_id)); AccountInfo secondary_account_info =
identity_test_env_.MakeAccountAvailable(kSecondaryEmail);
EXPECT_TRUE( EXPECT_TRUE(
identity_manager()->HasAccountWithRefreshToken(secondary_account_id)); identity_manager()->HasAccountWithRefreshToken(account_info.account_id));
EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(
secondary_account_info.account_id));
EXPECT_FALSE(identity_manager()->HasPrimaryAccount()); EXPECT_FALSE(identity_manager()->HasPrimaryAccount());
// Receive signout response. // Receive signout response.
dice_response_handler_->ProcessDiceHeader( dice_response_handler_->ProcessDiceHeader(
dice_params, std::make_unique<TestProcessDiceHeaderDelegate>(this)); dice_params, std::make_unique<TestProcessDiceHeaderDelegate>(this));
// Only the token corresponding the the Dice parameter has been removed. // Only the token corresponding the the Dice parameter has been removed.
EXPECT_FALSE(identity_manager()->HasAccountWithRefreshToken(account_id)); EXPECT_FALSE(
EXPECT_TRUE( identity_manager()->HasAccountWithRefreshToken(account_info.account_id));
identity_manager()->HasAccountWithRefreshToken(secondary_account_id)); EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(
secondary_account_info.account_id));
EXPECT_FALSE(identity_manager()->HasPrimaryAccount()); EXPECT_FALSE(identity_manager()->HasPrimaryAccount());
} }
...@@ -639,8 +638,15 @@ TEST_F(DiceResponseHandlerTest, SigninSignoutSameAccount) { ...@@ -639,8 +638,15 @@ TEST_F(DiceResponseHandlerTest, SigninSignoutSameAccount) {
0u, dice_response_handler_->GetPendingDiceTokenFetchersCountForTesting()); 0u, dice_response_handler_->GetPendingDiceTokenFetchersCountForTesting());
EXPECT_TRUE( EXPECT_TRUE(
identity_manager()->HasAccountWithRefreshToken(account_info.account_id)); identity_manager()->HasAccountWithRefreshToken(account_info.account_id));
EXPECT_EQ(MutableProfileOAuth2TokenServiceDelegate::kInvalidRefreshToken, EXPECT_TRUE(
GetRefreshToken(account_info.account_id)); identity_manager()->HasAccountWithRefreshTokenInPersistentErrorState(
account_info.account_id));
auto error = identity_manager()->GetErrorStateOfRefreshTokenForAccount(
account_info.account_id);
EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, error.state());
EXPECT_EQ(GoogleServiceAuthError::InvalidGaiaCredentialsReason::
CREDENTIALS_REJECTED_BY_CLIENT,
error.GetInvalidGaiaCredentialsReason());
} }
// Checks that signin in progress is not canceled by a signout for a different // Checks that signin in progress is not canceled by a signout for a different
...@@ -692,7 +698,9 @@ TEST_F(DiceResponseHandlerTest, SigninSignoutDifferentAccount) { ...@@ -692,7 +698,9 @@ TEST_F(DiceResponseHandlerTest, SigninSignoutDifferentAccount) {
// Check that the right token is available. // Check that the right token is available.
EXPECT_FALSE(identity_manager()->HasAccountWithRefreshToken(account_id_1)); EXPECT_FALSE(identity_manager()->HasAccountWithRefreshToken(account_id_1));
EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(account_id_2)); EXPECT_TRUE(identity_manager()->HasAccountWithRefreshToken(account_id_2));
EXPECT_EQ("refresh_token", GetRefreshToken(account_id_2)); EXPECT_FALSE(
identity_manager()->HasAccountWithRefreshTokenInPersistentErrorState(
account_id_2));
} }
// Tests that the DiceResponseHandler is created for a normal profile but not // Tests that the DiceResponseHandler is created for a normal profile but not
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment