| <!DOCTYPE html> |
| <title>Credential Manager: PasswordCredential basics.</title> |
| <script src="../resources/testharness.js"></script> |
| <script src="../resources/testharnessreport.js"></script> |
| <script src="resources/interfaces.js"></script> |
| <script> |
| test(function() { |
| var credential = new PasswordCredential({ |
| id: 'id', |
| password: 'pencil', |
| name: 'name', |
| iconURL: 'https://example.com/icon.png' |
| }); |
| |
| verify_interface('PasswordCredential', credential, { |
| id: 'string', |
| name: 'string', |
| iconURL: 'string', |
| type: 'string' |
| }); |
| |
| assert_equals(credential.id, 'id'); |
| assert_equals(credential.name, 'name'); |
| assert_equals(credential.iconURL, 'https://example.com/icon.png'); |
| assert_equals(credential.type, 'password'); |
| assert_equals(credential.idName, 'username'); |
| assert_equals(credential.passwordName, 'password'); |
| assert_equals(credential.additionalData, null); |
| assert_equals(credential.password, 'pencil'); |
| }, 'Interfaces and attributes of PasswordCredential'); |
| |
| test(function() { |
| assert_throws(new SyntaxError(), function () { |
| var credential = new PasswordCredential({ |
| id: 'id', |
| password: 'pencil', |
| name: 'name', |
| iconURL: '-' |
| }); |
| }); |
| }, 'Construct a PasswordCredential with an invalid icon URL.'); |
| |
| test(function() { |
| var credential = new PasswordCredential({ |
| id: 'id', |
| password: 'pencil', |
| name: 'name', |
| }); |
| |
| assert_equals(credential.id, 'id'); |
| assert_equals(credential.name, 'name'); |
| assert_equals(credential.iconURL, ''); |
| assert_equals(credential.type, 'password'); |
| assert_equals(credential.idName, 'username'); |
| assert_equals(credential.passwordName, 'password'); |
| assert_equals(credential.additionalData, null); |
| assert_equals(credential.password, 'pencil'); |
| |
| }, 'Construct a PasswordCredential with an empty icon URL.'); |
| |
| test(function() { |
| var credential = new PasswordCredential({ |
| id: 'id', |
| password: 'pencil', |
| }); |
| |
| assert_equals(credential.id, 'id'); |
| assert_equals(credential.name, ''); |
| assert_equals(credential.iconURL, ''); |
| assert_equals(credential.type, 'password'); |
| assert_equals(credential.idName, 'username'); |
| assert_equals(credential.passwordName, 'password'); |
| assert_equals(credential.additionalData, null); |
| assert_equals(credential.password, 'pencil'); |
| }, 'Construct a PasswordCredential with an empty name and icon URL.'); |
| |
| test(function() { |
| var credential = new PasswordCredential({ |
| id: 'id', |
| password: 'pencil', |
| }); |
| |
| credential.idName = 'yay'; |
| assert_equals(credential.idName, 'yay'); |
| |
| credential.passwordName = 'boo'; |
| assert_equals(credential.passwordName, 'boo'); |
| |
| var additionalData = new FormData(); |
| credential.additionalData = additionalData; |
| assert_equals(credential.additionalData, additionalData); |
| |
| assert_equals(credential.password, 'pencil'); |
| }, 'Verify the basics of "idName", "passwordName", and "additionalData"'); |
| |
| test(function() { |
| assert_throws(new TypeError(), |
| () => { new PasswordCredential(); }); |
| assert_throws(new TypeError(), |
| () => { new PasswordCredential({}); }); |
| assert_throws(new TypeError(), |
| () => { new PasswordCredential({ 'id': undefined }); }); |
| assert_throws(new TypeError(), |
| () => { new PasswordCredential({ 'id': '' }); }); |
| assert_throws(new TypeError(), |
| () => { new PasswordCredential({ 'password': undefined }); }); |
| assert_throws(new TypeError(), |
| () => { new PasswordCredential({ 'password': '' }); }); |
| assert_throws(new TypeError(), |
| () => { new PasswordCredential({ 'id': undefined, 'password': undefined }); }); |
| assert_throws(new TypeError(), |
| () => { new PasswordCredential({ 'id': undefined, 'password': '' }); }); |
| assert_throws(new TypeError(), |
| () => { new PasswordCredential({ 'id': undefined, 'password': undefined }); }); |
| }, 'PasswordCredential objects require IDs and Passwords'); |
| |
| function verify_form_credential(f, credential) { |
| assert_equals(credential.id, 'musterman'); |
| assert_equals(credential.name, 'friendly name'); |
| assert_equals(credential.iconURL, 'https://example.com/photo'); |
| assert_equals(credential.idName, 'theId'); |
| assert_equals(credential.passwordName, 'thePassword'); |
| assert_equals(credential.type, 'password'); |
| |
| assert_equals(credential.additionalData.get('theId'), 'musterman'); |
| assert_equals(credential.additionalData.get('thePassword'), 'sekrit'); |
| assert_equals(credential.additionalData.get('theIcon'), 'https://example.com/photo'); |
| assert_equals(credential.additionalData.get('theName'), 'friendly name'); |
| assert_equals(credential.additionalData.get('theExtraField'), 'extra'); |
| |
| assert_equals(credential.password, 'sekrit'); |
| |
| var password = f.querySelector('[name=thePassword]'); |
| password.value = ""; |
| assert_throws(new TypeError(), _ => new PasswordCredential(f)); |
| f.removeChild(password); |
| assert_throws(new TypeError(), _ => new PasswordCredential(f)); |
| |
| // Reset the password, do the same test for the ID. |
| f.appendChild(password); |
| password.value = "sekrit"; |
| credential = new PasswordCredential(f); |
| assert_true(credential instanceof PasswordCredential); |
| |
| var id = f.querySelector('[name=theId]'); |
| id.value = ""; |
| assert_throws(new TypeError(), _ => new PasswordCredential(f)); |
| f.removeChild(id); |
| assert_throws(new TypeError(), _ => new PasswordCredential(f)); |
| } |
| |
| test(_ => { |
| var f = document.createElement('form'); |
| f.enctype = 'mUlTiPaRt/fOrm-dAtA'; |
| f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='username'>" |
| + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>" |
| + "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>" |
| + "<input type='text' name='theExtraField' value='extra'>" |
| + "<input type='text' name='theName' value='friendly name' autocomplete='name'>"; |
| |
| var credential = new PasswordCredential(f); |
| assert_true(credential.additionalData instanceof FormData); |
| verify_form_credential(f, credential); |
| }, 'PasswordCredential creation from `HTMLFormElement` with a multipart enctype'); |
| |
| test(_ => { |
| var f = document.createElement('form'); |
| f.enctype = 'aPplIcaTion/X-wWw-form-urLencoded'; |
| f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='username'>" |
| + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>" |
| + "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>" |
| + "<input type='text' name='theExtraField' value='extra'>" |
| + "<input type='text' name='theName' value='friendly name' autocomplete='name'>"; |
| |
| var credential = new PasswordCredential(f); |
| assert_true(credential.additionalData instanceof URLSearchParams); |
| verify_form_credential(f, credential); |
| }, 'PasswordCredential creation from `HTMLFormElement` with a urlencoded enctype'); |
| |
| test(_ => { |
| var f = document.createElement('form'); |
| f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='username'>" |
| + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password'>" |
| + "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo'>" |
| + "<input type='text' name='theExtraField' value='extra'>" |
| + "<input type='text' name='theName' value='friendly name' autocomplete='name'>"; |
| |
| var credential = new PasswordCredential(f); |
| assert_true(credential.additionalData instanceof URLSearchParams); |
| verify_form_credential(f, credential); |
| }, 'PasswordCredential creation from `HTMLFormElement` with no enctype'); |
| |
| test(_ => { |
| var f = document.createElement('form'); |
| f.innerHTML = "<input type='text' name='theId' value='musterman' autocomplete='email username'>" |
| + "<input type='text' name='thePassword' value='sekrit' autocomplete='current-password value1'>" |
| + "<input type='text' name='theIcon' value='https://example.com/photo' autocomplete='photo value2'>" |
| + "<input type='text' name='theExtraField' value='extra'>" |
| + "<input type='text' name='theName' value='friendly name' autocomplete='name value3'>"; |
| |
| var credential = new PasswordCredential(f); |
| assert_true(credential.additionalData instanceof URLSearchParams); |
| verify_form_credential(f, credential); |
| }, 'PasswordCredential creation from `HTMLFormElement` with multiple autocomplete attributes'); |
| </script> |