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, and then can be used in exactly the same way as the real | |
8 // GaiaAuthenticator. | |
9 | |
10 #include "chrome/browser/sync/test/engine/mock_gaia_authenticator.h" | |
11 | |
12 using std::string; | |
13 | |
14 namespace browser_sync { | |
15 | |
16 MockGaiaAuthenticator::MockGaiaAuthenticator(const char* user_agent, | |
17 const char* service_id, | |
18 const char* gaia_url) : | |
19 should_save_credentials_(false), current_user_("") { | |
20 // This discards user_agent, service_id, gaia_url since the mock object | |
21 // doesn't care about them. | |
22 } | |
23 | |
24 MockGaiaAuthenticator::~MockGaiaAuthenticator() {} | |
25 | |
26 // Add a mock user to internal list of users. | |
27 void MockGaiaAuthenticator::AddMockUser(MockUser mock_user) { | |
28 mock_credentials_[mock_user.email] = mock_user; | |
29 } | |
30 | |
31 // A convenience method to add a mock user to internal list of users. | |
32 void MockGaiaAuthenticator::AddMockUser(string email, string passwd, | |
33 string auth_token, string lsid, | |
34 string sid, | |
35 enum gaia::AuthenticationError | |
36 auth_error, string error_url, | |
37 string captcha_token, | |
38 string captcha_url) { | |
39 MockUser mock_user; | |
40 mock_user.email = email; | |
41 mock_user.passwd = passwd; | |
42 mock_user.auth_token = auth_token; | |
43 mock_user.lsid = lsid; | |
44 mock_user.sid = sid; | |
45 mock_user.auth_error = auth_error; | |
46 mock_user.error_url = error_url; | |
47 mock_user.captcha_token = captcha_token; | |
48 mock_user.captcha_url = captcha_url; | |
49 AddMockUser(mock_user); | |
50 } | |
51 | |
52 // A convenience method to add a mock user to internal list of users. | |
53 void MockGaiaAuthenticator::AddMockUser(string email, string passwd, | |
54 string auth_token, string lsid, | |
55 string sid, | |
56 enum gaia::AuthenticationError | |
57 auth_error) { | |
58 MockUser mock_user; | |
59 mock_user.email = email; | |
60 mock_user.passwd = passwd; | |
61 mock_user.auth_token = auth_token; | |
62 mock_user.lsid = lsid; | |
63 mock_user.sid = sid; | |
64 mock_user.auth_error = auth_error; | |
65 AddMockUser(mock_user); | |
66 } | |
67 | |
68 void MockGaiaAuthenticator::RemoveMockUser(const char* email) { | |
69 mock_credentials_.erase(email); | |
70 } | |
71 | |
72 void MockGaiaAuthenticator::RemoveAllMockUsers() { | |
73 mock_credentials_.clear(); | |
74 } | |
75 | |
76 bool MockGaiaAuthenticator::Authenticate() { | |
77 if (!should_save_credentials_) { | |
78 ResetCredentials(); | |
79 return false; | |
80 } | |
81 return Authenticate(mock_credentials_[current_user_].email.c_str(), | |
82 mock_credentials_[current_user_].passwd.c_str(), true); | |
83 } | |
84 | |
85 bool MockGaiaAuthenticator::Authenticate(const char* email, | |
86 const char* passwd, | |
87 bool should_save_credentials) { | |
88 // Simply assign value to field; the value is read by the accessors when | |
89 // reading. | |
90 should_save_credentials_ = should_save_credentials; | |
91 | |
92 // Check if we already know about this mock user. | |
93 if (mock_credentials_.find(email) == mock_credentials_.end()) { | |
94 current_user_ = ""; | |
95 return false; | |
96 } | |
97 | |
98 // If found, keep the current logged-in user available for token requests. | |
99 current_user_ = email; | |
100 | |
101 // Finding a user does not necessarily imply that the user was logged in OK. | |
102 // Therefore also check if the AuthenticationError is None. | |
103 return (mock_credentials_[current_user_].auth_error == gaia::None); | |
104 } | |
105 | |
106 // Remove any stored knowledge about the currently logged-in user, but keep | |
107 // details of mock users. | |
108 void MockGaiaAuthenticator::ResetCredentials() { | |
109 current_user_ = ""; | |
110 } | |
111 | |
112 std::string MockGaiaAuthenticator::email() { | |
113 return (current_user_.empty() || !should_save_credentials_) ? "" : | |
114 mock_credentials_[current_user_].email; | |
115 } | |
116 | |
117 std::string MockGaiaAuthenticator::auth_token() { | |
118 return (current_user_.empty()) ? "" : | |
119 mock_credentials_[current_user_].auth_token; | |
120 } | |
121 | |
122 std::string MockGaiaAuthenticator::sid() { | |
123 return (current_user_.empty()) ? "" : | |
124 mock_credentials_[current_user_].sid; | |
125 } | |
126 | |
127 std::string MockGaiaAuthenticator::lsid() { | |
128 return (current_user_.empty()) ? "" : | |
129 mock_credentials_[current_user_].lsid; | |
130 } | |
131 | |
132 gaia::AuthenticationError MockGaiaAuthenticator::auth_error() { | |
133 return (current_user_.empty()) ? gaia::CredentialsNotSet : | |
134 mock_credentials_[current_user_].auth_error; | |
135 } | |
136 | |
137 std::string MockGaiaAuthenticator::auth_error_url() { | |
138 return (current_user_.empty()) ? "" : | |
139 mock_credentials_[current_user_].error_url; | |
140 } | |
141 | |
142 std::string MockGaiaAuthenticator::captcha_token() { | |
143 return (current_user_.empty()) ? "" : | |
144 mock_credentials_[current_user_].captcha_token; | |
145 } | |
146 | |
147 std::string MockGaiaAuthenticator::captcha_url() { | |
148 return (current_user_.empty()) ? "" : | |
149 mock_credentials_[current_user_].captcha_url; | |
150 } | |
151 | |
152 } // namespace browser_sync | |
OLD | NEW |