OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 // Use MockGaiaAuthenticator to test your application by faking a login session. |
| 6 // This mock object should be initialized with the response you expect it to |
| 7 // return for multiple users, and then can be used in exactly the same way |
| 8 // as the real GaiaAuthenticator. |
| 9 // |
| 10 // Sample usage: |
| 11 // MockGaiaAuthenticator mock_gaia_auth("User-Agent", SYNC_SERVICE_NAME, |
| 12 // "any random string"); |
| 13 // mock_gaia_auth.AddMockUser("email", "password", "authtoken", "lsid", "sid", |
| 14 // AuthenticationError); |
| 15 // mock_gaia_auth.AddMockUser("email2", "password2", "authtoken2", "lsid2", |
| 16 // "sid2", AuthenticationError, error_url, |
| 17 // "captcha_token", "captcha_url"); |
| 18 // if (gaia_auth.Authenticate("email", "passwd")) { |
| 19 // // Do something with: gaia_auth.auth_token(), or gaia_auth.sid(), |
| 20 // // or gaia_auth.lsid() |
| 21 // } |
| 22 |
| 23 #ifndef CHROME_TEST_SYNC_ENGINE_MOCK_GAIA_AUTHENTICATOR_H_ |
| 24 #define CHROME_TEST_SYNC_ENGINE_MOCK_GAIA_AUTHENTICATOR_H_ |
| 25 |
| 26 #include <map> |
| 27 #include <string> |
| 28 |
| 29 #include "base/port.h" |
| 30 |
| 31 #include "base/basictypes.h" |
| 32 #include "chrome/browser/sync/engine/net/gaia_authenticator.h" |
| 33 |
| 34 namespace browser_sync { |
| 35 |
| 36 // A struct used internally for storing a user's credentials. You can either |
| 37 // create one yourself, or use the convenience methods to have the |
| 38 // MockGaiaAuthenticator create one for you. |
| 39 typedef struct { |
| 40 std::string email; |
| 41 std::string passwd; |
| 42 std::string auth_token; |
| 43 std::string sid; |
| 44 std::string lsid; |
| 45 AuthenticationError auth_error; |
| 46 std::string captcha_token; |
| 47 std::string captcha_url; |
| 48 std::string error_url; |
| 49 } MockUser; |
| 50 |
| 51 // MockGaiaAuthenticator can be used to fake Gaia authentication without |
| 52 // actually making a network connection. For details about the methods shared |
| 53 // with GaiaAuthenticator, see GaiaAuthenticator in gaia_auth.h. Only methods |
| 54 // that are unique to MockGaiaAuthenticator are documented in this file. |
| 55 class MockGaiaAuthenticator { |
| 56 public: |
| 57 MockGaiaAuthenticator(const char* user_agent, const char* service_id, |
| 58 const char* gaia_url); |
| 59 |
| 60 // Add a mock user; takes a struct. You can populate any or all fields when |
| 61 // adding a user. The email field is required, all others optional. |
| 62 void AddMockUser(MockUser mock_user); |
| 63 |
| 64 // A convenience method that makes it easy to create new mock users in a |
| 65 // single method call. Includes all parameters. |
| 66 void AddMockUser(std::string email, std::string passwd, |
| 67 std::string auth_token, |
| 68 std::string lsid, std::string sid, |
| 69 AuthenticationError auth_error, |
| 70 std::string error_url, std::string captcha_token, |
| 71 std::string captcha_url); |
| 72 |
| 73 // A convenience method that makes it easy to create new mock users in a |
| 74 // single method call. Includes only the most common parameters. See overload |
| 75 // if you want to pass all parameters. |
| 76 void AddMockUser(std::string email, std::string passwd, |
| 77 std::string auth_token, |
| 78 std::string lsid, std::string sid, |
| 79 enum AuthenticationError auth_error); |
| 80 |
| 81 // Removes a mock user from the current list of added users. |
| 82 void RemoveMockUser(const char* email); |
| 83 |
| 84 // Removes all mock users from the current list of added users. |
| 85 void RemoveAllMockUsers(); |
| 86 |
| 87 // See GaiaAuthenticator::Authenticate() |
| 88 bool Authenticate(); |
| 89 |
| 90 // See GaiaAuthenticator::Authenticate(...) |
| 91 bool Authenticate(const char* user_name, const char* password, |
| 92 bool should_save_credentials = false); |
| 93 |
| 94 // See GaiaAuthenticator::Authenticate(...) |
| 95 void ResetCredentials(); |
| 96 |
| 97 // Accessors follow. |
| 98 std::string email() { |
| 99 return (current_user_.length() == 0 || !should_save_credentials_) ? "" : |
| 100 mock_credentials_[current_user_].email; |
| 101 } |
| 102 |
| 103 std::string auth_token() { |
| 104 return (current_user_.length() == 0) ? "" : |
| 105 mock_credentials_[current_user_].auth_token; |
| 106 } |
| 107 |
| 108 std::string sid() { |
| 109 return (current_user_.length() == 0) ? "" : |
| 110 mock_credentials_[current_user_].sid; |
| 111 } |
| 112 |
| 113 std::string lsid() { |
| 114 return (current_user_.length() == 0) ? "" : |
| 115 mock_credentials_[current_user_].lsid; |
| 116 } |
| 117 |
| 118 AuthenticationError auth_error() { |
| 119 return (current_user_.length() == 0) ? CredentialsNotSet : |
| 120 mock_credentials_[current_user_].auth_error; |
| 121 } |
| 122 |
| 123 std::string auth_error_url() { |
| 124 return (current_user_.length() == 0) ? "" : |
| 125 mock_credentials_[current_user_].error_url; |
| 126 } |
| 127 |
| 128 std::string captcha_token() { |
| 129 return (current_user_.length() == 0) ? "" : |
| 130 mock_credentials_[current_user_].captcha_token; |
| 131 } |
| 132 |
| 133 std::string captcha_url() { |
| 134 return (current_user_.length() == 0) ? "" : |
| 135 mock_credentials_[current_user_].captcha_url; |
| 136 } |
| 137 |
| 138 private: |
| 139 bool should_save_credentials_; |
| 140 std::map<std::string, MockUser> mock_credentials_; |
| 141 std::string current_user_; |
| 142 }; |
| 143 } // namespace browser_sync |
| 144 |
| 145 #endif // CHROME_TEST_SYNC_ENGINE_MOCK_GAIA_AUTHENTICATOR_H_ |
OLD | NEW |