Commit 5bcdd1ed authored by Abhijeet Kandalkar's avatar Abhijeet Kandalkar Committed by Commit Bot

Expand IdentityManager::DiagnosticsObserver with OnAccessTokenRequestCompleted()

As one of the servicification support, this CL makes that
IdentityManager::DiagnosticsObserver can handle
OAuth2TokenService::DiagnosticsObserver::OnAccessTokenRequestCompleted
callback.

The test case observe access token fetch request error when account has
no refresh token, when access token request succeeded with no error,
when access token request fails after revoking refresh token.

Bug: 931246
Change-Id: I2d8d1d497c9a8f740e9cc0614141dbd9dc4571e2
Reviewed-on: https://chromium-review.googlesource.com/c/1474958
Commit-Queue: Abhijeet Kandalkar <abhijeet@igalia.com>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#634518}
parent be921f6b
...@@ -134,6 +134,12 @@ void FakeProfileOAuth2TokenService::CompleteRequests( ...@@ -134,6 +134,12 @@ void FakeProfileOAuth2TokenService::CompleteRequests(
bool scope_matches = all_scopes || it->scopes == scope; bool scope_matches = all_scopes || it->scopes == scope;
bool account_matches = account_id.empty() || account_id == it->account_id; bool account_matches = account_id.empty() || account_id == it->account_id;
if (account_matches && scope_matches) { if (account_matches && scope_matches) {
for (auto& diagnostic_observer : GetDiagnicsObservers()) {
diagnostic_observer.OnFetchAccessTokenComplete(
account_id, it->request->GetConsumerId(), scope, error,
base::Time());
}
it->request->InformConsumer( it->request->InformConsumer(
error, OAuth2AccessTokenConsumer::TokenResponse( error, OAuth2AccessTokenConsumer::TokenResponse(
token_response.access_token, token_response.access_token,
...@@ -189,7 +195,7 @@ void FakeProfileOAuth2TokenService::FetchOAuth2Token( ...@@ -189,7 +195,7 @@ void FakeProfileOAuth2TokenService::FetchOAuth2Token(
FROM_HERE, FROM_HERE,
base::BindOnce(&FakeProfileOAuth2TokenService::CompleteRequests, base::BindOnce(&FakeProfileOAuth2TokenService::CompleteRequests,
weak_ptr_factory_.GetWeakPtr(), account_id, weak_ptr_factory_.GetWeakPtr(), account_id,
/*all_scoped=*/true, ScopeSet(), /*all_scoped=*/true, scopes,
GoogleServiceAuthError::AuthErrorNone(), GoogleServiceAuthError::AuthErrorNone(),
OAuth2AccessTokenConsumer::TokenResponse( OAuth2AccessTokenConsumer::TokenResponse(
"access_token", base::Time::Max(), std::string()))); "access_token", base::Time::Max(), std::string())));
......
...@@ -488,6 +488,15 @@ void IdentityManager::OnAccessTokenRequested( ...@@ -488,6 +488,15 @@ void IdentityManager::OnAccessTokenRequested(
} }
} }
void IdentityManager::OnFetchAccessTokenComplete(const std::string& account_id,
const std::string& consumer_id,
const ScopeSet& scopes,
GoogleServiceAuthError error,
base::Time expiration_time) {
for (auto& observer : diagnostics_observer_list_)
observer.OnAccessTokenRequestCompleted(account_id, error, scopes);
}
void IdentityManager::OnAccessTokenRemoved(const std::string& account_id, void IdentityManager::OnAccessTokenRemoved(const std::string& account_id,
const ScopeSet& scopes) { const ScopeSet& scopes) {
for (auto& observer : diagnostics_observer_list_) for (auto& observer : diagnostics_observer_list_)
......
...@@ -169,6 +169,12 @@ class IdentityManager : public SigninManagerBase::Observer, ...@@ -169,6 +169,12 @@ class IdentityManager : public SigninManagerBase::Observer,
const std::string& consumer_id, const std::string& consumer_id,
const identity::ScopeSet& scopes) {} const identity::ScopeSet& scopes) {}
// Called when an access token request is completed. Contains diagnostic
// information about the access token request.
virtual void OnAccessTokenRequestCompleted(const std::string& account_id,
GoogleServiceAuthError error,
const ScopeSet& scopes) {}
// Called when an access token was removed. // Called when an access token was removed.
virtual void OnAccessTokenRemovedFromCache(const std::string& account_id, virtual void OnAccessTokenRemovedFromCache(const std::string& account_id,
const ScopeSet& scopes) {} const ScopeSet& scopes) {}
...@@ -532,6 +538,11 @@ class IdentityManager : public SigninManagerBase::Observer, ...@@ -532,6 +538,11 @@ class IdentityManager : public SigninManagerBase::Observer,
const std::string& account_id, const std::string& account_id,
const std::string& consumer_id, const std::string& consumer_id,
const OAuth2TokenService::ScopeSet& scopes) override; const OAuth2TokenService::ScopeSet& scopes) override;
void OnFetchAccessTokenComplete(const std::string& account_id,
const std::string& consumer_id,
const ScopeSet& scopes,
GoogleServiceAuthError error,
base::Time expiration_time) override;
void OnAccessTokenRemoved(const std::string& account_id, void OnAccessTokenRemoved(const std::string& account_id,
const ScopeSet& scopes) override; const ScopeSet& scopes) override;
void OnRefreshTokenAvailableFromSource(const std::string& account_id, void OnRefreshTokenAvailableFromSource(const std::string& account_id,
......
...@@ -230,6 +230,11 @@ class TestIdentityManagerDiagnosticsObserver ...@@ -230,6 +230,11 @@ class TestIdentityManagerDiagnosticsObserver
on_access_token_requested_callback_ = std::move(callback); on_access_token_requested_callback_ = std::move(callback);
} }
void set_on_access_token_request_completed_callback(
base::OnceClosure callback) {
on_access_token_request_completed_callback_ = std::move(callback);
}
const std::string& token_requestor_account_id() { const std::string& token_requestor_account_id() {
return token_requestor_account_id_; return token_requestor_account_id_;
} }
...@@ -245,6 +250,15 @@ class TestIdentityManagerDiagnosticsObserver ...@@ -245,6 +250,15 @@ class TestIdentityManagerDiagnosticsObserver
const identity::ScopeSet& token_remover_scopes() { const identity::ScopeSet& token_remover_scopes() {
return token_remover_scopes_; return token_remover_scopes_;
} }
const std::string& on_access_token_request_completed_account_id() {
return access_token_request_completed_account_id_;
}
const identity::ScopeSet& on_access_token_request_completed_scopes() {
return access_token_request_completed_scopes_;
}
const GoogleServiceAuthError& on_access_token_request_completed_error() {
return access_token_request_completed_error_;
}
private: private:
// IdentityManager::DiagnosticsObserver: // IdentityManager::DiagnosticsObserver:
...@@ -265,13 +279,28 @@ class TestIdentityManagerDiagnosticsObserver ...@@ -265,13 +279,28 @@ class TestIdentityManagerDiagnosticsObserver
token_remover_scopes_ = scopes; token_remover_scopes_ = scopes;
} }
void OnAccessTokenRequestCompleted(const std::string& account_id,
GoogleServiceAuthError error,
const ScopeSet& scopes) override {
access_token_request_completed_account_id_ = account_id;
access_token_request_completed_scopes_ = scopes;
access_token_request_completed_error_ = error;
if (on_access_token_request_completed_callback_)
std::move(on_access_token_request_completed_callback_).Run();
}
IdentityManager* identity_manager_; IdentityManager* identity_manager_;
base::OnceClosure on_access_token_requested_callback_; base::OnceClosure on_access_token_requested_callback_;
base::OnceClosure on_access_token_request_completed_callback_;
std::string token_requestor_account_id_; std::string token_requestor_account_id_;
std::string token_requestor_consumer_id_; std::string token_requestor_consumer_id_;
std::string token_remover_account_id_; std::string token_remover_account_id_;
identity::ScopeSet token_requestor_scopes_; identity::ScopeSet token_requestor_scopes_;
identity::ScopeSet token_remover_scopes_; identity::ScopeSet token_remover_scopes_;
std::string access_token_request_completed_account_id_;
identity::ScopeSet access_token_request_completed_scopes_;
GoogleServiceAuthError access_token_request_completed_error_;
}; };
} // namespace } // namespace
...@@ -1282,6 +1311,92 @@ TEST_F(IdentityManagerTest, ObserveAccessTokenFetch) { ...@@ -1282,6 +1311,92 @@ TEST_F(IdentityManagerTest, ObserveAccessTokenFetch) {
identity_manager_diagnostics_observer()->token_requestor_scopes()); identity_manager_diagnostics_observer()->token_requestor_scopes());
} }
TEST_F(IdentityManagerTest,
ObserveAccessTokenRequestCompletionWithoutRefreshToken) {
base::RunLoop run_loop;
identity_manager_diagnostics_observer()
->set_on_access_token_request_completed_callback(run_loop.QuitClosure());
std::set<std::string> scopes{"scope"};
AccessTokenFetcher::TokenCallback callback = base::BindOnce(
[](GoogleServiceAuthError error, AccessTokenInfo access_token_info) {});
// Account has no refresh token.
std::unique_ptr<AccessTokenFetcher> token_fetcher =
identity_manager()->CreateAccessTokenFetcherForAccount(
identity_manager()->GetPrimaryAccountId(), "dummy_consumer", scopes,
std::move(callback), AccessTokenFetcher::Mode::kImmediate);
run_loop.Run();
EXPECT_TRUE(token_fetcher);
EXPECT_EQ(GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP),
identity_manager_diagnostics_observer()
->on_access_token_request_completed_error());
}
TEST_F(IdentityManagerTest,
ObserveAccessTokenRequestCompletionWithRefreshToken) {
base::RunLoop run_loop;
identity_manager_diagnostics_observer()
->set_on_access_token_request_completed_callback(run_loop.QuitClosure());
signin_manager()->SetAuthenticatedAccountInfo(kTestGaiaId, kTestEmail);
std::string account_id = signin_manager()->GetAuthenticatedAccountId();
token_service()->UpdateCredentials(account_id, "refresh_token");
token_service()->set_auto_post_fetch_response_on_message_loop(true);
std::set<std::string> scopes{"scope"};
AccessTokenFetcher::TokenCallback callback = base::BindOnce(
[](GoogleServiceAuthError error, AccessTokenInfo access_token_info) {});
// This should result in a request for an access token without an error.
std::unique_ptr<AccessTokenFetcher> token_fetcher =
identity_manager()->CreateAccessTokenFetcherForAccount(
identity_manager()->GetPrimaryAccountId(), "dummy_consumer", scopes,
std::move(callback), AccessTokenFetcher::Mode::kImmediate);
run_loop.Run();
EXPECT_TRUE(token_fetcher);
EXPECT_EQ(account_id, identity_manager_diagnostics_observer()
->on_access_token_request_completed_account_id());
EXPECT_EQ(scopes, identity_manager_diagnostics_observer()
->on_access_token_request_completed_scopes());
EXPECT_EQ(GoogleServiceAuthError(GoogleServiceAuthError::NONE),
identity_manager_diagnostics_observer()
->on_access_token_request_completed_error());
}
TEST_F(IdentityManagerTest,
ObserveAccessTokenRequestCompletionAfterRevokingRefreshToken) {
base::RunLoop run_loop;
identity_manager_diagnostics_observer()
->set_on_access_token_request_completed_callback(run_loop.QuitClosure());
account_tracker()->SeedAccountInfo(kTestGaiaId2, kTestEmail2);
std::string account_id2 =
account_tracker()->FindAccountInfoByGaiaId(kTestGaiaId2).account_id;
token_service()->UpdateCredentials(account_id2, "refresh_token");
std::set<std::string> scopes{"scope"};
AccessTokenFetcher::TokenCallback callback = base::BindOnce(
[](GoogleServiceAuthError error, AccessTokenInfo access_token_info) {});
// This should result in a request for an access token.
std::unique_ptr<AccessTokenFetcher> token_fetcher =
identity_manager()->CreateAccessTokenFetcherForAccount(
account_id2, "dummy_consumer 2", scopes, std::move(callback),
AccessTokenFetcher::Mode::kImmediate);
// Revoke the refresh token result cancelling access token request.
token_service()->RevokeCredentials(account_id2);
run_loop.Run();
EXPECT_TRUE(token_fetcher);
EXPECT_EQ(GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED),
identity_manager_diagnostics_observer()
->on_access_token_request_completed_error());
}
TEST_F(IdentityManagerTest, GetAccountsCookieMutator) { TEST_F(IdentityManagerTest, GetAccountsCookieMutator) {
AccountsCookieMutator* mutator = AccountsCookieMutator* mutator =
identity_manager()->GetAccountsCookieMutator(); identity_manager()->GetAccountsCookieMutator();
......
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