Chromium Code Reviews| Index: chrome/test/functional/autofill.py |
| =================================================================== |
| --- chrome/test/functional/autofill.py (revision 81676) |
| +++ chrome/test/functional/autofill.py (working copy) |
| @@ -90,8 +90,126 @@ |
| self.assertEqual([without_invalid], |
| self.GetAutofillProfile()['profiles']) |
| - def testFilterIncompleteAddresses(self): |
| - """Test Autofill filters out profile with incomplete address info.""" |
| + def testAutofillPrefsStringSavedAsIs(self): |
| + """Test invalid credit card numbers typed in prefs should be saved as-is.""" |
| + credit_card = {'CREDIT_CARD_NUMBER': 'Not_0123-5Checked'} |
| + self.FillAutofillProfile(credit_cards=[credit_card]) |
| + self.assertEqual([credit_card], |
| + self.GetAutofillProfile()['credit_cards'], |
| + msg='Credit card number in prefs not saved as-is.') |
| + |
| + def _LuhnCreditCardNumberValidator(self, number): |
| + """Validates whether a number is valid or invalid using the Luhn test. |
| + |
| + Validation steps: |
| + 1. Reverse the order of the digits in the number. |
| + 2. Take the first, third, and every other odd digit and sum them up to |
| + form sum s1. |
| + 3. Take the second, fourth, and every even digit, multiply each digit by |
| + two and sum the digits if the answer is greater than nine. |
| + 4. Sum the partial sums of the even digits to form s2. |
| + 5. If s1 + s2 ends in zero then the number is valid. |
| + |
| + Args: |
| + number: the credit card number being validated. |
|
Nirnimesh
2011/04/15 05:29:45
Add: "as a string"
dyu1
2011/04/15 21:43:51
Done.
|
| + |
| + Return: |
| + boolean whether the credit card number is true or false. |
| + """ |
| + reverse = [int(ch) for ch in str(number)][::-1] |
| + # The divmod of the function splits a number into two digits, ready for |
| + # summing. |
| + return (( |
|
Nirnimesh
2011/04/15 05:29:45
you don't really need 3 lines here.
dyu1
2011/04/15 21:43:51
Done.
|
| + sum(reverse[0::2]) + sum(sum(divmod(d*2, 10)) for d in reverse[1::2])) % |
| + 10 == 0) |
| + |
| + def testInvalidCreditCardNumberIsNotAggregated(self): |
| + """Test credit card info with an invalid number is not aggregated. |
| + |
| + When filling out a form with an invalid credit card number (one that |
| + does not pass the Luhn test) the credit card info should not be saved into |
| + Autofill preferences. |
| + """ |
| + invalid_cc_info = {'CREDIT_CARD_NAME': 'Bob Smith', |
| + 'CREDIT_CARD_NUMBER': '4408 0412 3456 7890', |
| + 'CREDIT_CARD_EXP_MONTH': '12', |
| + 'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2014'} |
| + |
| + # Strip the white spaces from the number. |
| + cc_number = invalid_cc_info['CREDIT_CARD_NUMBER'].replace(' ', '') |
| + if self._LuhnCreditCardNumberValidator(cc_number) == False: |
|
Nirnimesh
2011/04/15 05:29:45
Don't do an if.
Do an assert.
dyu1
2011/04/15 21:43:51
Done.
|
| + url = self.GetHttpURLForDataPath( |
| + os.path.join('autofill', 'autofill_creditcard_form.html')) |
| + self.NavigateToURL(url) |
| + for key, value in invalid_cc_info.iteritems(): |
| + script = ('document.getElementById("%s").value = "%s"; ' |
| + 'window.domAutomationController.send("done");') % (key, value) |
| + self.ExecuteJavascript(script, 0, 0) |
| + js_code = """ |
| + document.getElementById("cc_submit").submit(); |
| + window.addEventListener("unload", function() { |
| + window.domAutomationController.send("done"); |
| + }); |
| + """ |
| + self.ExecuteJavascript(js_code, 0, 0) |
| + # Wait until the form is submitted and the page completes loading. |
| + self.WaitUntil( |
| + lambda: self.GetDOMValue('document.readyState'), |
| + expect_retval='complete') |
| + cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] |
| + self.assertFalse( |
| + cc_infobar, msg='Save credit card infobar offered to save CC info.') |
| + else: |
|
Nirnimesh
2011/04/15 05:29:45
remove the else
dyu1
2011/04/15 21:43:51
Done.
|
| + raise Error('This test requires an invalid credit card number.') |
| + |
| + def testWhitespacesAndSeparatorCharsStrippedForValidCCNums(self): |
| + """Test whitespaces and separator chars are stripped for valid CC numbers. |
| + |
| + The credit card number used in this test passes the Luhn test. |
| + For reference: http://www.merriampark.com/anatomycc.htm |
| + """ |
| + credit_card_info = [{'CREDIT_CARD_NAME': 'Bob Smith', |
| + 'CREDIT_CARD_NUMBER': '4408 0412 3456 7893', |
| + 'CREDIT_CARD_EXP_MONTH': '12', |
| + 'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2014'}, |
| + {'CREDIT_CARD_NAME': 'Jane Doe', |
| + 'CREDIT_CARD_NUMBER': '4417-1234-5678-9113', |
| + 'CREDIT_CARD_EXP_MONTH': '10', |
| + 'CREDIT_CARD_EXP_4_DIGIT_YEAR': '2013'}] |
| + |
| + url = self.GetHttpURLForDataPath( |
| + os.path.join('autofill', 'autofill_creditcard_form.html')) |
| + for cc_info in credit_card_info: |
| + self.NavigateToURL(url) |
| + for key, value in cc_info.iteritems(): |
| + script = ('document.getElementById("%s").value = "%s"; ' |
| + 'window.domAutomationController.send("done");') % (key, value) |
| + self.ExecuteJavascript(script, 0, 0) |
| + js_code = """ |
| + document.getElementById("cc_submit").submit(); |
| + window.addEventListener("unload", function() { |
| + window.domAutomationController.send("done"); |
| + }); |
| + """ |
| + self.ExecuteJavascript(js_code, 0, 0) |
| + # Wait until form is submitted and page completes loading. |
| + self.WaitUntil( |
| + lambda: self.GetDOMValue('document.readyState'), |
| + expect_retval='complete') |
| + self.PerformActionOnInfobar('accept', infobar_index=0) |
| + |
| + # Verify the filled in credit card number against the aggregated number. |
| + aggregated_cc_1 = ( |
| + self.GetAutofillProfile()['credit_cards'][0]['CREDIT_CARD_NUMBER']) |
| + aggregated_cc_2 = ( |
| + self.GetAutofillProfile()['credit_cards'][1]['CREDIT_CARD_NUMBER']) |
| + print aggregated_cc_1 |
|
Nirnimesh
2011/04/15 05:29:45
why print?
dyu1
2011/04/15 21:43:51
Forgot to remove this while I was debugging.
|
| + if (' ' in aggregated_cc_1 or ' ' in aggregated_cc_2 or '-' in |
| + aggregated_cc_1 or '-' in aggregated_cc_2): |
| + print 'Whitespaces or separator chars not stripped.' |
|
Nirnimesh
2011/04/15 05:29:45
why print? why not assert?
dyu1
2011/04/15 21:43:51
Done.
|
| + |
| + def testProfilesNotAggregatedWithNoAddress(self): |
| + """Test Autofill does not aggregate profiles with no address info.""" |
| profile = {'NAME_FIRST': 'Bob', |
| 'NAME_LAST': 'Smith', |
| 'EMAIL_ADDRESS': 'bsmith@example.com', |
| @@ -111,10 +229,11 @@ |
| }); |
| """ |
| self.ExecuteJavascript(js_code, 0, 0) |
| - self.assertEqual([], self.GetAutofillProfile()['profiles']) |
| + self.assertEqual([], self.GetAutofillProfile()['profiles'], |
| + msg='Profile with no address info was aggregated.') |
| - def testFilterMalformedEmailAddresses(self): |
| - """Test Autofill filters out malformed email address during form submit.""" |
| + def testProfilesNotAggregatedWithInvalidEmail(self): |
| + """Test Autofill does not aggregate profiles with an invalid email.""" |
| profile = {'NAME_FIRST': 'Bob', |
| 'NAME_LAST': 'Smith', |
| 'EMAIL_ADDRESS': 'garbage', |
| @@ -138,8 +257,8 @@ |
| }); |
| """ |
| self.ExecuteJavascript(js_code, 0, 0) |
| - if 'EMAIL_ADDRESS' in self.GetAutofillProfile()['profiles'][0]: |
| - raise KeyError('TEST FAIL: Malformed email address is saved in profiles.') |
| + self.assertEqual([], self.GetAutofillProfile()['profiles'], |
|
Nirnimesh
2011/04/15 05:29:45
use assertFalse
dyu1
2011/04/15 21:47:40
Done.
|
| + msg='Profile with invalid email was aggregated.') |
| def _SendKeyEventsToPopulateForm(self, tab_index=0, windex=0): |
| """Send key events to populate a web form with Autofill profile data. |
| @@ -186,8 +305,9 @@ |
| js_returning_field_value, 0, 0) |
| self.assertEqual( |
| form_values[key], value, |
| - ('Original profile not equal to expected profile at key: "%s"\n' |
| - 'Expected: "%s"\nReturned: "%s"' % (key, value, form_values[key]))) |
| + msg=('Original profile not equal to expected profile at key: "%s"\n' |
| + 'Expected: "%s"\nReturned: "%s"' % ( |
| + key, value, form_values[key]))) |
| def testCCInfoNotStoredWhenAutocompleteOff(self): |
| """Test CC info not offered to be saved when autocomplete=off for CC field. |
| @@ -220,8 +340,8 @@ |
| lambda: self.GetDOMValue('document.readyState'), |
| expect_retval='complete') |
| cc_infobar = self.GetBrowserInfo()['windows'][0]['tabs'][0]['infobars'] |
| - self.assertEqual(0, len(cc_infobar), |
| - 'Save credit card infobar offered to save CC info.') |
| + self.assertFalse(cc_infobar, |
| + msg='Save credit card infobar offered to save CC info.') |
| def testNoAutofillForReadOnlyFields(self): |
| """Test that Autofill does not fill in read-only fields.""" |