1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-passwords-utils.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,142 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict"; 1.9 + 1.10 +const { store, search, remove } = require("sdk/passwords/utils"); 1.11 + 1.12 +exports["test store requires `password` field"] = function(assert) { 1.13 + assert.throws(function() { 1.14 + store({ username: "foo", realm: "bar" }); 1.15 + }, "`password` is required"); 1.16 +}; 1.17 + 1.18 +exports["test store requires `username` field"] = function(assert) { 1.19 + assert.throws(function() { 1.20 + store({ password: "foo", realm: "bar" }); 1.21 + }, "`username` is required"); 1.22 +}; 1.23 + 1.24 +exports["test store requires `realm` field"] = function(assert) { 1.25 + assert.throws(function() { 1.26 + store({ username: "foo", password: "bar" }); 1.27 + }, "`password` is required"); 1.28 +}; 1.29 + 1.30 +exports["test can't store same login twice"] = function(assert) { 1.31 + let options = { username: "user", password: "pass", realm: "realm" }; 1.32 + store(options); 1.33 + assert.throws(function() { 1.34 + store(options); 1.35 + }, "can't store same pass twice"); 1.36 + remove(options); 1.37 +}; 1.38 + 1.39 +exports["test remove throws if no login found"] = function(assert) { 1.40 + assert.throws(function() { 1.41 + remove({ username: "foo", password: "bar", realm: "baz" }); 1.42 + }, "can't remove unstored credentials"); 1.43 +}; 1.44 + 1.45 +exports["test addon associated credentials"] = function(assert) { 1.46 + let options = { username: "foo", password: "bar", realm: "baz" }; 1.47 + store(options); 1.48 + 1.49 + assert.ok(search().length, "credential was stored"); 1.50 + assert.ok(search(options).length, "stored credential found"); 1.51 + assert.ok(search({ username: options.username }).length, "found by username"); 1.52 + assert.ok(search({ password: options.password }).length, "found by password"); 1.53 + assert.ok(search({ realm: options.realm }).length, "found by realm"); 1.54 + 1.55 + let credential = search(options)[0]; 1.56 + assert.equal(credential.url.indexOf("addon:"), 0, 1.57 + "`addon:` uri is used for add-on associated credentials"); 1.58 + assert.equal(credential.username, options.username, "username matches"); 1.59 + assert.equal(credential.password, options.password, "password matches"); 1.60 + assert.equal(credential.realm, options.realm, "realm matches"); 1.61 + assert.equal(credential.formSubmitURL, null, 1.62 + "`formSubmitURL` is `null` for add-on associated credentials"); 1.63 + assert.equal(credential.usernameField, "", "usernameField is empty"); 1.64 + assert.equal(credential.passwordField, "", "passwordField is empty"); 1.65 + 1.66 + remove(search(options)[0]); 1.67 + assert.ok(!search(options).length, "remove worked"); 1.68 +}; 1.69 + 1.70 +exports["test web page associated credentials"] = function(assert) { 1.71 + let options = { 1.72 + url: "http://www.example.com", 1.73 + formSubmitURL: "http://login.example.com", 1.74 + username: "user", 1.75 + password: "pass", 1.76 + usernameField: "user-f", 1.77 + passwordField: "pass-f" 1.78 + }; 1.79 + store({ 1.80 + url: "http://www.example.com/login", 1.81 + formSubmitURL: "http://login.example.com/foo/authenticate.cgi", 1.82 + username: options.username, 1.83 + password: options.password, 1.84 + usernameField: options.usernameField, 1.85 + passwordField: options.passwordField 1.86 + }); 1.87 + 1.88 + assert.ok(search().length, "credential was stored"); 1.89 + assert.ok(search(options).length, "stored credential found"); 1.90 + assert.ok(search({ username: options.username }).length, "found by username"); 1.91 + assert.ok(search({ password: options.password }).length, "found by password"); 1.92 + assert.ok(search({ formSubmitURL: options.formSubmitURL }).length, 1.93 + "found by formSubmitURL"); 1.94 + assert.ok(search({ usernameField: options.usernameField }).length, 1.95 + "found by usernameField"); 1.96 + assert.ok(search({ passwordField: options.passwordField }).length, 1.97 + "found by passwordField"); 1.98 + 1.99 + let credential = search(options)[0]; 1.100 + assert.equal(credential.url, options.url, "url matches"); 1.101 + assert.equal(credential.username, options.username, "username matches"); 1.102 + assert.equal(credential.password, options.password, "password matches"); 1.103 + assert.equal(credential.realm, null, "realm is "); 1.104 + assert.equal(credential.formSubmitURL, options.formSubmitURL, 1.105 + "`formSubmitURL` matches"); 1.106 + assert.equal(credential.usernameField, options.usernameField, 1.107 + "usernameField matches"); 1.108 + assert.equal(credential.passwordField, options.passwordField, 1.109 + "passwordField matches"); 1.110 + 1.111 + remove(search(options)[0]); 1.112 + assert.ok(!search(options).length, "remove worked"); 1.113 +}; 1.114 + 1.115 +exports["test site authentication credentials"] = function(assert) { 1.116 + let options = { 1.117 + url: "http://test.authentication.com", 1.118 + username: "u", 1.119 + password: "p", 1.120 + realm: "r" 1.121 + }; 1.122 + 1.123 + store(options); 1.124 + assert.ok(search().length, "credential was stored"); 1.125 + assert.ok(search(options).length, "stored credential found"); 1.126 + assert.ok(search({ username: options.username }).length, "found by username"); 1.127 + assert.ok(search({ password: options.password }).length, "found by password"); 1.128 + assert.ok(search({ realm: options.realm }).length, "found by realm"); 1.129 + assert.ok(search({ url: options.url }).length, "found by url"); 1.130 + 1.131 + let credential = search(options)[0]; 1.132 + assert.equal(credential.url, options.url, "url matches"); 1.133 + assert.equal(credential.username, options.username, "username matches"); 1.134 + assert.equal(credential.password, options.password, "password matches"); 1.135 + assert.equal(credential.realm, options.realm, "realm matches"); 1.136 + assert.equal(credential.formSubmitURL, null, 1.137 + "`formSubmitURL` is `null` for site authentication credentials"); 1.138 + assert.equal(credential.usernameField, "", "usernameField is empty"); 1.139 + assert.equal(credential.passwordField, "", "passwordField is empty"); 1.140 + 1.141 + remove(search(options)[0]); 1.142 + assert.ok(!search(options).length, "remove worked"); 1.143 +}; 1.144 + 1.145 +require("test").run(exports);