fake_oauth2_access_token_manager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 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. #ifndef GOOGLE_APIS_GAIA_FAKE_OAUTH2_ACCESS_TOKEN_MANAGER_H_
  5. #define GOOGLE_APIS_GAIA_FAKE_OAUTH2_ACCESS_TOKEN_MANAGER_H_
  6. #include "base/compiler_specific.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "google_apis/gaia/oauth2_access_token_manager.h"
  9. namespace network {
  10. class SharedURLLoaderFactory;
  11. }
  12. // Helper class to simplify writing unittests that depend on an instance of
  13. // OAuth2AccessTokenManager.
  14. class FakeOAuth2AccessTokenManager : public OAuth2AccessTokenManager {
  15. public:
  16. struct PendingRequest {
  17. PendingRequest();
  18. PendingRequest(const PendingRequest& other);
  19. ~PendingRequest();
  20. CoreAccountId account_id;
  21. std::string client_id;
  22. std::string client_secret;
  23. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory;
  24. OAuth2AccessTokenManager::ScopeSet scopes;
  25. base::WeakPtr<OAuth2AccessTokenManager::RequestImpl> request;
  26. };
  27. explicit FakeOAuth2AccessTokenManager(
  28. OAuth2AccessTokenManager::Delegate* delegate);
  29. FakeOAuth2AccessTokenManager(const FakeOAuth2AccessTokenManager&) = delete;
  30. FakeOAuth2AccessTokenManager& operator=(const FakeOAuth2AccessTokenManager&) =
  31. delete;
  32. ~FakeOAuth2AccessTokenManager() override;
  33. // Gets a list of active requests (can be used by tests to validate that the
  34. // correct request has been issued).
  35. std::vector<PendingRequest> GetPendingRequests();
  36. // Helper routines to issue tokens for pending requests.
  37. void IssueAllTokensForAccount(const CoreAccountId& account_id,
  38. const std::string& access_token,
  39. const base::Time& expiration);
  40. // Helper routines to issue token for pending requests based on TokenResponse.
  41. void IssueAllTokensForAccount(
  42. const CoreAccountId& account_id,
  43. const OAuth2AccessTokenConsumer::TokenResponse& token_response);
  44. void IssueErrorForAllPendingRequestsForAccount(
  45. const CoreAccountId& account_id,
  46. const GoogleServiceAuthError& error);
  47. void IssueTokenForScope(const OAuth2AccessTokenManager::ScopeSet& scopes,
  48. const std::string& access_token,
  49. const base::Time& expiration);
  50. void IssueTokenForScope(
  51. const OAuth2AccessTokenManager::ScopeSet& scopes,
  52. const OAuth2AccessTokenConsumer::TokenResponse& token_response);
  53. void IssueErrorForScope(const OAuth2AccessTokenManager::ScopeSet& scopes,
  54. const GoogleServiceAuthError& error);
  55. void IssueTokenForAllPendingRequests(const std::string& access_token,
  56. const base::Time& expiration);
  57. void IssueTokenForAllPendingRequests(
  58. const OAuth2AccessTokenConsumer::TokenResponse& token_response);
  59. void IssueErrorForAllPendingRequests(const GoogleServiceAuthError& error);
  60. void set_auto_post_fetch_response_on_message_loop(bool auto_post_response) {
  61. auto_post_fetch_response_on_message_loop_ = auto_post_response;
  62. }
  63. // OAuth2AccessTokenManager overrides.
  64. void CancelAllRequests() override;
  65. void CancelRequestsForAccount(const CoreAccountId& account_id) override;
  66. void FetchOAuth2Token(
  67. OAuth2AccessTokenManager::RequestImpl* request,
  68. const CoreAccountId& account_id,
  69. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  70. const std::string& client_id,
  71. const std::string& client_secret,
  72. const std::string& consumer_name,
  73. const OAuth2AccessTokenManager::ScopeSet& scopes) override;
  74. void InvalidateAccessTokenImpl(
  75. const CoreAccountId& account_id,
  76. const std::string& client_id,
  77. const OAuth2AccessTokenManager::ScopeSet& scopes,
  78. const std::string& access_token) override;
  79. private:
  80. // Helper function to complete pending requests - if |all_scopes| is true,
  81. // then all pending requests are completed, otherwise, only those requests
  82. // matching |scopes| are completed. If |account_id| is empty, then pending
  83. // requests for all accounts are completed, otherwise only requests for the
  84. // given account.
  85. void CompleteRequests(
  86. const CoreAccountId& account_id,
  87. bool all_scopes,
  88. const OAuth2AccessTokenManager::ScopeSet& scopes,
  89. const GoogleServiceAuthError& error,
  90. const OAuth2AccessTokenConsumer::TokenResponse& token_response);
  91. std::vector<PendingRequest> pending_requests_;
  92. // If true, then this fake manager will post responses to
  93. // |FetchOAuth2Token| on the current run loop. There is no need to call
  94. // |IssueTokenForScope| in this case.
  95. bool auto_post_fetch_response_on_message_loop_;
  96. base::WeakPtrFactory<FakeOAuth2AccessTokenManager> weak_ptr_factory_{this};
  97. };
  98. #endif // GOOGLE_APIS_GAIA_FAKE_OAUTH2_ACCESS_TOKEN_MANAGER_H_