Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import logging | 6 import logging |
| 7 import os | 7 import os |
| 8 import pickle | 8 import pickle |
| 9 | 9 |
| 10 import autofill_dataset_converter | 10 import autofill_dataset_converter |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 'ADDRESS_HOME_ZIP': 'my_zip', | 83 'ADDRESS_HOME_ZIP': 'my_zip', |
| 84 'ADDRESS_HOME_COUNTRY': 'United States'} | 84 'ADDRESS_HOME_COUNTRY': 'United States'} |
| 85 # Add some invalid fields. | 85 # Add some invalid fields. |
| 86 with_invalid = without_invalid.copy() | 86 with_invalid = without_invalid.copy() |
| 87 with_invalid['PHONE_HOME_WHOLE_NUMBER'] = 'Invalid_Phone_Number' | 87 with_invalid['PHONE_HOME_WHOLE_NUMBER'] = 'Invalid_Phone_Number' |
| 88 with_invalid['PHONE_FAX_WHOLE_NUMBER'] = 'Invalid_Fax_Number' | 88 with_invalid['PHONE_FAX_WHOLE_NUMBER'] = 'Invalid_Fax_Number' |
| 89 self.FillAutofillProfile(profiles=[with_invalid]) | 89 self.FillAutofillProfile(profiles=[with_invalid]) |
| 90 self.assertEqual([without_invalid], | 90 self.assertEqual([without_invalid], |
| 91 self.GetAutofillProfile()['profiles']) | 91 self.GetAutofillProfile()['profiles']) |
| 92 | 92 |
| 93 def testFilterIncompleteAddresses(self): | 93 def testAutofillPrefsStringSavedAsIs(self): |
| 94 """Test Autofill filters out profile with incomplete address info.""" | 94 """Test invalid credit card numbers typed in prefs should be saved as-is.""" |
| 95 credit_card = {'CREDIT_CARD_NUMBER': 'Not_0123-5Checked'} | |
| 96 self.FillAutofillProfile(credit_cards=[credit_card]) | |
| 97 self.assertEqual([credit_card], | |
| 98 self.GetAutofillProfile()['credit_cards'], | |
| 99 msg='Credit card number in prefs not saved as-is.') | |
| 100 | |
| 101 def _LuhnCreditCardNumberValidator(self, number): | |
|
dennis_jeffrey
2011/04/15 23:41:55
Based on what I read online (http://www.merriampar
dyu1
2011/04/16 00:11:06
Done.
| |
| 102 """Validates whether a number is valid or invalid using the Luhn test. | |
| 103 | |
| 104 Validation steps: | |
| 105 1. Reverse the order of the digits in the number. | |
| 106 2. Take the first, third, and every other odd digit and sum them up to | |
| 107 form sum s1. | |
| 108 3. Take the second, fourth, and every even digit, multiply each digit by | |
| 109 two and sum the digits if the answer is greater than nine. | |
| 110 4. Sum the partial sums of the even digits to form s2. | |
| 111 5. If s1 + s2 ends in zero then the number is valid. | |
| 112 | |
| 113 Args: | |
| 114 number: the credit card number being validated as a string. | |
|
Nirnimesh
2011/04/15 22:15:46
nit: put a , after validated
dennis_jeffrey
2011/04/15 22:51:19
Clarify that this function assumes the string cont
dyu1
2011/04/16 00:11:06
Done.
dyu1
2011/04/16 00:11:06
I added a line to filter the numbers so it removed
| |
| 115 | |
| 116 Return: | |
| 117 boolean whether the credit card number is true or false. | |
|
Nirnimesh
2011/04/15 22:15:46
s/is true or false/is valid or not/
dyu1
2011/04/16 00:11:06
Done.
| |
| 118 """ | |
| 119 reverse = [int(ch) for ch in str(number)][::-1] | |
| 120 # The divmod of the function splits a number into two digits, ready for | |
| 121 # summing. | |
| 122 return ((sum(reverse[0::2]) + sum(sum(divmod(d*2, 10)) | |
| 123 for d in reverse[1::2])) % 10 == 0) | |
|
dennis_jeffrey
2011/04/15 22:51:19
As discussed offline, the implementation here migh
dyu1
2011/04/16 00:11:06
Done.
| |
| 124 | |
| 125 def testInvalidCreditCardNumberIsNotAggregated(self): | |
| 126 """Test credit card info with an invalid number is not aggregated. | |
|
dennis_jeffrey
2011/04/15 22:51:19
Delete 1 of the extra spaces within "invalid numb
dyu1
2011/04/16 00:11:06
Done.
| |
| 127 | |
| 128 When filling out a form with an invalid credit card number (one that | |
|
dennis_jeffrey
2011/04/15 22:51:19
Delete 1 of the extra spaces within "card number"
dyu1
2011/04/16 00:11:06
Done.
| |
| 129 does not pass the Luhn test) the credit card info should not be saved into | |
| 130 Autofill preferences. | |
| 131 """ | |
| 132 invalid_cc_info = {'CREDIT_CARD_NAME': 'Bob Smith', | |
| 133 'CREDIT_CARD_NUMBER': '4408 0412 3456 7890', | |
| 134 'CREDIT_CARD_EXP_MONTH': '12', | |
| 135 'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2014'} | |
| 136 | |
| 137 # Strip the white spaces from the number. | |
| 138 cc_number = invalid_cc_info['CREDIT_CARD_NUMBER'].replace(' ', '') | |
| 139 self.assertFalse(self._LuhnCreditCardNumberValidator(cc_number), | |
| 140 msg='This test requires an invalid credit card number.') | |
|
dennis_jeffrey
2011/04/15 22:51:19
Indent by 1 more space.
dyu1
2011/04/16 00:11:06
Done.
| |
| 141 url = self.GetHttpURLForDataPath( | |
| 142 os.path.join('autofill', 'autofill_creditcard_form.html')) | |
| 143 self.NavigateToURL(url) | |
| 144 for key, value in invalid_cc_info.iteritems(): | |
| 145 script = ('document.getElementById("%s").value = "%s"; ' | |
| 146 'window.domAutomationController.send("done");') % (key, value) | |
| 147 self.ExecuteJavascript(script, 0, 0) | |
| 148 js_code = """ | |
| 149 document.getElementById("cc_submit").submit(); | |
| 150 window.addEventListener("unload", function() { | |
| 151 window.domAutomationController.send("done"); | |
| 152 }); | |
| 153 """ | |
| 154 self.ExecuteJavascript(js_code, 0, 0) | |
| 155 # Wait until the form is submitted and the page completes loading. | |
| 156 self.WaitUntil( | |
| 157 lambda: self.GetDOMValue('document.readyState'), | |
| 158 expect_retval='complete') | |
| 159 cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] | |
| 160 self.assertFalse( | |
| 161 cc_infobar, msg='Save credit card infobar offered to save CC info.') | |
| 162 | |
| 163 def testWhitespacesAndSeparatorCharsStrippedForValidCCNums(self): | |
| 164 """Test whitespaces and separator chars are stripped for valid CC numbers. | |
| 165 | |
| 166 The credit card number used in this test passes the Luhn test. | |
|
dennis_jeffrey
2011/04/15 22:51:19
"number" --> "numbers"
"passes" --> "pass"
dyu1
2011/04/16 00:11:06
Done.
| |
| 167 For reference: http://www.merriampark.com/anatomycc.htm | |
| 168 """ | |
| 169 credit_card_info = [{'CREDIT_CARD_NAME': 'Bob Smith', | |
| 170 'CREDIT_CARD_NUMBER': '4408 0412 3456 7893', | |
| 171 'CREDIT_CARD_EXP_MONTH': '12', | |
| 172 'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2014'}, | |
| 173 {'CREDIT_CARD_NAME': 'Jane Doe', | |
| 174 'CREDIT_CARD_NUMBER': '4417-1234-5678-9113', | |
| 175 'CREDIT_CARD_EXP_MONTH': '10', | |
| 176 'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2013'}] | |
| 177 | |
|
dennis_jeffrey
2011/04/15 22:51:19
(optional) In the previous test above, you assert
dyu1
2011/04/16 00:11:06
Done.
| |
| 178 url = self.GetHttpURLForDataPath( | |
| 179 os.path.join('autofill', 'autofill_creditcard_form.html')) | |
| 180 for cc_info in credit_card_info: | |
| 181 self.NavigateToURL(url) | |
| 182 for key, value in cc_info.iteritems(): | |
| 183 script = ('document.getElementById("%s").value = "%s"; ' | |
| 184 'window.domAutomationController.send("done");') % (key, value) | |
| 185 self.ExecuteJavascript(script, 0, 0) | |
| 186 js_code = """ | |
| 187 document.getElementById("cc_submit").submit(); | |
| 188 window.addEventListener("unload", function() { | |
| 189 window.domAutomationController.send("done"); | |
| 190 }); | |
| 191 """ | |
| 192 self.ExecuteJavascript(js_code, 0, 0) | |
| 193 # Wait until form is submitted and page completes loading. | |
| 194 self.WaitUntil( | |
| 195 lambda: self.GetDOMValue('document.readyState'), | |
| 196 expect_retval='complete') | |
| 197 self.PerformActionOnInfobar('accept', infobar_index=0) | |
| 198 | |
| 199 # Verify the filled in credit card number against the aggregated number. | |
|
dennis_jeffrey
2011/04/15 22:51:19
"filled in" --> "filled-in"
dyu1
2011/04/16 00:11:06
Done.
| |
| 200 aggregated_cc_1 = ( | |
| 201 self.GetAutofillProfile()['credit_cards'][0]['CREDIT_CARD_NUMBER']) | |
| 202 aggregated_cc_2 = ( | |
| 203 self.GetAutofillProfile()['credit_cards'][1]['CREDIT_CARD_NUMBER']) | |
| 204 self.assertFalse((' ' in aggregated_cc_1 or ' ' in aggregated_cc_2 or '-' in | |
|
dennis_jeffrey
2011/04/15 22:51:19
Recommend moving:
'-' in
to the next line.
dyu1
2011/04/16 00:11:06
Done.
| |
| 205 aggregated_cc_1 or '-' in aggregated_cc_2), | |
|
dennis_jeffrey
2011/04/15 22:51:19
Indent line by 1 more space.
dyu1
2011/04/16 00:11:06
Done.
| |
| 206 msg='Whitespaces or separator chars not stripped.') | |
|
dennis_jeffrey
2011/04/15 22:51:19
Indent line by 1 more space.
dyu1
2011/04/16 00:11:06
Done.
| |
| 207 | |
| 208 def testProfilesNotAggregatedWithNoAddress(self): | |
| 209 """Test Autofill does not aggregate profiles with no address info.""" | |
| 95 profile = {'NAME_FIRST': 'Bob', | 210 profile = {'NAME_FIRST': 'Bob', |
| 96 'NAME_LAST': 'Smith', | 211 'NAME_LAST': 'Smith', |
| 97 'EMAIL_ADDRESS': 'bsmith@example.com', | 212 'EMAIL_ADDRESS': 'bsmith@example.com', |
| 98 'COMPANY_NAME': 'Company X', | 213 'COMPANY_NAME': 'Company X', |
| 99 'PHONE_HOME_WHOLE_NUMBER': '650-123-4567',} | 214 'PHONE_HOME_WHOLE_NUMBER': '650-123-4567',} |
| 100 url = self.GetHttpURLForDataPath( | 215 url = self.GetHttpURLForDataPath( |
| 101 os.path.join('autofill', 'duplicate_profiles_test.html')) | 216 os.path.join('autofill', 'duplicate_profiles_test.html')) |
| 102 self.NavigateToURL(url) | 217 self.NavigateToURL(url) |
| 103 for key, value in profile.iteritems(): | 218 for key, value in profile.iteritems(): |
| 104 script = ('document.getElementById("%s").value = "%s"; ' | 219 script = ('document.getElementById("%s").value = "%s"; ' |
| 105 'window.domAutomationController.send("done");') % (key, value) | 220 'window.domAutomationController.send("done");') % (key, value) |
| 106 self.ExecuteJavascript(script, 0, 0) | 221 self.ExecuteJavascript(script, 0, 0) |
| 107 js_code = """ | 222 js_code = """ |
| 108 document.getElementById("merge_dup").submit(); | 223 document.getElementById("merge_dup").submit(); |
| 109 window.addEventListener("unload", function() { | 224 window.addEventListener("unload", function() { |
| 110 window.domAutomationController.send("done"); | 225 window.domAutomationController.send("done"); |
| 111 }); | 226 }); |
| 112 """ | 227 """ |
| 113 self.ExecuteJavascript(js_code, 0, 0) | 228 self.ExecuteJavascript(js_code, 0, 0) |
| 114 self.assertEqual([], self.GetAutofillProfile()['profiles']) | 229 self.assertFalse(self.GetAutofillProfile()['profiles'], |
| 230 msg='Profile with no address info was aggregated.') | |
| 115 | 231 |
| 116 def testFilterMalformedEmailAddresses(self): | 232 def testProfilesNotAggregatedWithInvalidEmail(self): |
| 117 """Test Autofill filters out malformed email address during form submit.""" | 233 """Test Autofill does not aggregate profiles with an invalid email.""" |
| 118 profile = {'NAME_FIRST': 'Bob', | 234 profile = {'NAME_FIRST': 'Bob', |
| 119 'NAME_LAST': 'Smith', | 235 'NAME_LAST': 'Smith', |
| 120 'EMAIL_ADDRESS': 'garbage', | 236 'EMAIL_ADDRESS': 'garbage', |
| 121 'ADDRESS_HOME_LINE1': '1234 H St.', | 237 'ADDRESS_HOME_LINE1': '1234 H St.', |
| 122 'ADDRESS_HOME_CITY': 'San Jose', | 238 'ADDRESS_HOME_CITY': 'San Jose', |
| 123 'ADDRESS_HOME_STATE': 'CA', | 239 'ADDRESS_HOME_STATE': 'CA', |
| 124 'ADDRESS_HOME_ZIP': '95110', | 240 'ADDRESS_HOME_ZIP': '95110', |
| 125 'COMPANY_NAME': 'Company X', | 241 'COMPANY_NAME': 'Company X', |
| 126 'PHONE_HOME_WHOLE_NUMBER': '408-123-4567',} | 242 'PHONE_HOME_WHOLE_NUMBER': '408-123-4567',} |
| 127 url = self.GetHttpURLForDataPath( | 243 url = self.GetHttpURLForDataPath( |
| 128 os.path.join('autofill', 'duplicate_profiles_test.html')) | 244 os.path.join('autofill', 'duplicate_profiles_test.html')) |
| 129 self.NavigateToURL(url) | 245 self.NavigateToURL(url) |
| 130 for key, value in profile.iteritems(): | 246 for key, value in profile.iteritems(): |
| 131 script = ('document.getElementById("%s").value = "%s"; ' | 247 script = ('document.getElementById("%s").value = "%s"; ' |
| 132 'window.domAutomationController.send("done");') % (key, value) | 248 'window.domAutomationController.send("done");') % (key, value) |
| 133 self.ExecuteJavascript(script, 0, 0) | 249 self.ExecuteJavascript(script, 0, 0) |
| 134 js_code = """ | 250 js_code = """ |
| 135 document.getElementById("merge_dup").submit(); | 251 document.getElementById("merge_dup").submit(); |
| 136 window.addEventListener("unload", function() { | 252 window.addEventListener("unload", function() { |
| 137 window.domAutomationController.send("done"); | 253 window.domAutomationController.send("done"); |
| 138 }); | 254 }); |
| 139 """ | 255 """ |
| 140 self.ExecuteJavascript(js_code, 0, 0) | 256 self.ExecuteJavascript(js_code, 0, 0) |
| 141 if 'EMAIL_ADDRESS' in self.GetAutofillProfile()['profiles'][0]: | 257 self.assertFalse(self.GetAutofillProfile()['profiles'], |
| 142 raise KeyError('TEST FAIL: Malformed email address is saved in profiles.') | 258 msg='Profile with invalid email was aggregated.') |
| 143 | 259 |
| 144 def _SendKeyEventsToPopulateForm(self, tab_index=0, windex=0): | 260 def _SendKeyEventsToPopulateForm(self, tab_index=0, windex=0): |
| 145 """Send key events to populate a web form with Autofill profile data. | 261 """Send key events to populate a web form with Autofill profile data. |
| 146 | 262 |
| 147 Args: | 263 Args: |
| 148 tab_index: The tab index, default is 0. | 264 tab_index: The tab index, default is 0. |
| 149 windex: The window index, default is 0. | 265 windex: The window index, default is 0. |
| 150 """ | 266 """ |
| 151 TAB_KEYPRESS = 0x09 # Tab keyboard key press. | 267 TAB_KEYPRESS = 0x09 # Tab keyboard key press. |
| 152 DOWN_KEYPRESS = 0x28 # Down arrow keyboard key press. | 268 DOWN_KEYPRESS = 0x28 # Down arrow keyboard key press. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 179 form_values = {} | 295 form_values = {} |
| 180 for key, value in profile_expected.iteritems(): | 296 for key, value in profile_expected.iteritems(): |
| 181 js_returning_field_value = ( | 297 js_returning_field_value = ( |
| 182 'var field_value = document.getElementById("%s").value;' | 298 'var field_value = document.getElementById("%s").value;' |
| 183 'window.domAutomationController.send(field_value);' | 299 'window.domAutomationController.send(field_value);' |
| 184 ) % key | 300 ) % key |
| 185 form_values[key] = self.ExecuteJavascript( | 301 form_values[key] = self.ExecuteJavascript( |
| 186 js_returning_field_value, 0, 0) | 302 js_returning_field_value, 0, 0) |
| 187 self.assertEqual( | 303 self.assertEqual( |
| 188 form_values[key], value, | 304 form_values[key], value, |
| 189 ('Original profile not equal to expected profile at key: "%s"\n' | 305 msg=('Original profile not equal to expected profile at key: "%s"\n' |
| 190 'Expected: "%s"\nReturned: "%s"' % (key, value, form_values[key]))) | 306 'Expected: "%s"\nReturned: "%s"' % ( |
| 307 key, value, form_values[key]))) | |
| 191 | 308 |
| 192 def testCCInfoNotStoredWhenAutocompleteOff(self): | 309 def testCCInfoNotStoredWhenAutocompleteOff(self): |
| 193 """Test CC info not offered to be saved when autocomplete=off for CC field. | 310 """Test CC info not offered to be saved when autocomplete=off for CC field. |
| 194 | 311 |
| 195 If the credit card number field has autocomplete turned off, then the credit | 312 If the credit card number field has autocomplete turned off, then the credit |
| 196 card infobar should not offer to save the credit card info. The credit card | 313 card infobar should not offer to save the credit card info. The credit card |
| 197 number must be a valid Luhn number. | 314 number must be a valid Luhn number. |
| 198 """ | 315 """ |
| 199 credit_card_info = {'CREDIT_CARD_NAME': 'Bob Smith', | 316 credit_card_info = {'CREDIT_CARD_NAME': 'Bob Smith', |
| 200 'CREDIT_CARD_NUMBER': '4408041234567893', | 317 'CREDIT_CARD_NUMBER': '4408041234567893', |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 213 window.addEventListener("unload", function() { | 330 window.addEventListener("unload", function() { |
| 214 window.domAutomationController.send("done"); | 331 window.domAutomationController.send("done"); |
| 215 }); | 332 }); |
| 216 """ | 333 """ |
| 217 self.ExecuteJavascript(js_code, 0, 0) | 334 self.ExecuteJavascript(js_code, 0, 0) |
| 218 # Wait until form is submitted and page completes loading. | 335 # Wait until form is submitted and page completes loading. |
| 219 self.WaitUntil( | 336 self.WaitUntil( |
| 220 lambda: self.GetDOMValue('document.readyState'), | 337 lambda: self.GetDOMValue('document.readyState'), |
| 221 expect_retval='complete') | 338 expect_retval='complete') |
| 222 cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] | 339 cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] |
| 223 self.assertEqual(0, len(cc_infobar), | 340 self.assertFalse(cc_infobar, |
| 224 'Save credit card infobar offered to save CC info.') | 341 msg='Save credit card infobar offered to save CC info.') |
| 225 | 342 |
| 226 def testNoAutofillForReadOnlyFields(self): | 343 def testNoAutofillForReadOnlyFields(self): |
| 227 """Test that Autofill does not fill in read-only fields.""" | 344 """Test that Autofill does not fill in read-only fields.""" |
| 228 profile = {'NAME_FIRST': 'Bob', | 345 profile = {'NAME_FIRST': 'Bob', |
| 229 'NAME_LAST': 'Smith', | 346 'NAME_LAST': 'Smith', |
| 230 'EMAIL_ADDRESS': 'bsmith@gmail.com', | 347 'EMAIL_ADDRESS': 'bsmith@gmail.com', |
| 231 'ADDRESS_HOME_LINE1': '1234 H St.', | 348 'ADDRESS_HOME_LINE1': '1234 H St.', |
| 232 'ADDRESS_HOME_CITY': 'San Jose', | 349 'ADDRESS_HOME_CITY': 'San Jose', |
| 233 'ADDRESS_HOME_STATE': 'CA', | 350 'ADDRESS_HOME_STATE': 'CA', |
| 234 'ADDRESS_HOME_ZIP': '95110', | 351 'ADDRESS_HOME_ZIP': '95110', |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 merged_profile = os.path.join(self.DataDir(), 'autofill', | 472 merged_profile = os.path.join(self.DataDir(), 'autofill', |
| 356 'merged-profiles.txt') | 473 'merged-profiles.txt') |
| 357 profile_dict = self.GetAutofillProfile()['profiles'] | 474 profile_dict = self.GetAutofillProfile()['profiles'] |
| 358 output = open(merged_profile, 'wb') | 475 output = open(merged_profile, 'wb') |
| 359 pickle.dump(profile_dict, output) | 476 pickle.dump(profile_dict, output) |
| 360 output.close() | 477 output.close() |
| 361 | 478 |
| 362 | 479 |
| 363 if __name__ == '__main__': | 480 if __name__ == '__main__': |
| 364 pyauto_functional.Main() | 481 pyauto_functional.Main() |
| OLD | NEW |