michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: 'use strict'; michael@0: michael@0: const { store, search, remove } = require("sdk/passwords"); michael@0: michael@0: exports["test store requires `password` field"] = function(assert, done) { michael@0: store({ michael@0: username: "foo", michael@0: realm: "bar", michael@0: onComplete: function onComplete() { michael@0: assert.fail("onComplete should not be called"); michael@0: }, michael@0: onError: function onError() { michael@0: assert.pass("'`password` is required"); michael@0: done(); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports["test store requires `username` field"] = function(assert, done) { michael@0: store({ michael@0: password: "foo", michael@0: realm: "bar", michael@0: onComplete: function onComplete() { michael@0: assert.fail("onComplete should not be called"); michael@0: }, michael@0: onError: function onError() { michael@0: assert.pass("'`username` is required"); michael@0: done(); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports["test onComplete is optional"] = function(assert, done) { michael@0: store({ michael@0: realm: "bla", michael@0: username: "bla", michael@0: password: "bla", michael@0: onError: function onError() { michael@0: assert.fail("onError was called"); michael@0: } michael@0: }); michael@0: assert.pass("exception is not thrown if `onComplete is missing") michael@0: done(); michael@0: }; michael@0: michael@0: exports["test exceptions in onComplete are reported"] = function(assert, done) { michael@0: store({ michael@0: realm: "throws", michael@0: username: "error", michael@0: password: "boom!", michael@0: onComplete: function onComplete(error) { michael@0: throw new Error("Boom!") michael@0: }, michael@0: onError: function onError(error) { michael@0: assert.equal(error.message, "Boom!", "Error thrown is reported"); michael@0: done(); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports["test store requires `realm` field"] = function(assert, done) { michael@0: store({ michael@0: username: "foo", michael@0: password: "bar", michael@0: onComplete: function onComplete() { michael@0: assert.fail("onComplete should not be called"); michael@0: }, michael@0: onError: function onError() { michael@0: assert.pass("'`realm` is required"); michael@0: done(); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports["test can't store same login twice"] = function(assert, done) { michael@0: store({ michael@0: username: "user", michael@0: password: "pass", michael@0: realm: "realm", michael@0: onComplete: function onComplete() { michael@0: assert.pass("credential saved"); michael@0: michael@0: store({ michael@0: username: "user", michael@0: password: "pass", michael@0: realm: "realm", michael@0: onComplete: function onComplete() { michael@0: assert.fail("onComplete should not be called"); michael@0: }, michael@0: onError: function onError() { michael@0: assert.pass("re-saving credential failed"); michael@0: michael@0: remove({ michael@0: username: "user", michael@0: password: "pass", michael@0: realm: "realm", michael@0: onComplete: function onComplete() { michael@0: assert.pass("credential was removed"); michael@0: done(); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("remove should not fail"); michael@0: } michael@0: }); michael@0: } michael@0: }); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports["test remove fails if no login found"] = function(assert, done) { michael@0: remove({ michael@0: username: "foo", michael@0: password: "bar", michael@0: realm: "baz", michael@0: onComplete: function onComplete() { michael@0: assert.fail("should not be able to remove unstored credentials"); michael@0: }, michael@0: onError: function onError() { michael@0: assert.pass("can't remove unstored credentials"); michael@0: done(); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports["test addon associated credentials"] = function(assert, done) { michael@0: store({ michael@0: username: "foo", michael@0: password: "bar", michael@0: realm: "baz", michael@0: onComplete: function onComplete() { michael@0: search({ michael@0: username: "foo", michael@0: password: "bar", michael@0: realm: "baz", michael@0: onComplete: function onComplete([credential]) { michael@0: assert.equal(credential.url.indexOf("addon:"), 0, michael@0: "`addon:` uri is used for add-on credentials"); michael@0: assert.equal(credential.username, "foo", michael@0: "username matches"); michael@0: assert.equal(credential.password, "bar", michael@0: "password matches"); michael@0: assert.equal(credential.realm, "baz", "realm matches"); michael@0: assert.equal(credential.formSubmitURL, null, michael@0: "`formSubmitURL` is `null` for add-on credentials"); michael@0: assert.equal(credential.usernameField, "", "usernameField is empty"); michael@0: assert.equal(credential.passwordField, "", "passwordField is empty"); michael@0: michael@0: remove({ michael@0: username: credential.username, michael@0: password: credential.password, michael@0: realm: credential.realm, michael@0: onComplete: function onComplete() { michael@0: assert.pass("credential is removed"); michael@0: done(); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports["test web page associated credentials"] = function(assert, done) { michael@0: store({ michael@0: url: "http://bar.foo.com/authentication/?login", michael@0: formSubmitURL: "http://login.foo.com/authenticate.cgi", michael@0: username: "user", michael@0: password: "pass", michael@0: usernameField: "user-f", michael@0: passwordField: "pass-f", michael@0: onComplete: function onComplete() { michael@0: search({ michael@0: username: "user", michael@0: password: "pass", michael@0: url: "http://bar.foo.com", michael@0: formSubmitURL: "http://login.foo.com", michael@0: onComplete: function onComplete([credential]) { michael@0: assert.equal(credential.url, "http://bar.foo.com", "url matches"); michael@0: assert.equal(credential.username, "user", "username matches"); michael@0: assert.equal(credential.password, "pass", "password matches"); michael@0: assert.equal(credential.realm, null, "realm is null"); michael@0: assert.equal(credential.formSubmitURL, "http://login.foo.com", michael@0: "formSubmitURL matches"); michael@0: assert.equal(credential.usernameField, "user-f", michael@0: "usernameField is matches"); michael@0: assert.equal(credential.passwordField, "pass-f", michael@0: "passwordField matches"); michael@0: michael@0: remove({ michael@0: url: credential.url, michael@0: formSubmitURL: credential.formSubmitURL, michael@0: username: credential.username, michael@0: password: credential.password, michael@0: usernameField: credential.usernameField, michael@0: passwordField: credential.passwordField, michael@0: michael@0: onComplete: function onComplete() { michael@0: assert.pass("credential is removed"); michael@0: done(); michael@0: }, michael@0: onError: function onError(e) { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: exports["test site authentication credentials"] = function(assert, done) { michael@0: store({ michael@0: url: "http://authentication.com", michael@0: username: "U", michael@0: password: "P", michael@0: realm: "R", michael@0: onComplete: function onComplete() { michael@0: search({ michael@0: url: "http://authentication.com", michael@0: username: "U", michael@0: password: "P", michael@0: realm: "R", michael@0: onComplete: function onComplete([credential]) { michael@0: assert.equal(credential.url,"http://authentication.com", michael@0: "url matches"); michael@0: assert.equal(credential.username, "U", "username matches"); michael@0: assert.equal(credential.password, "P", "password matches"); michael@0: assert.equal(credential.realm, "R", "realm matches"); michael@0: assert.equal(credential.formSubmitURL, null, "formSubmitURL is null"); michael@0: assert.equal(credential.usernameField, "", "usernameField is empty"); michael@0: assert.equal(credential.passwordField, "", "passwordField is empty"); michael@0: michael@0: remove({ michael@0: url: credential.url, michael@0: username: credential.username, michael@0: password: credential.password, michael@0: realm: credential.realm, michael@0: onComplete: function onComplete() { michael@0: assert.pass("credential is removed"); michael@0: done(); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }, michael@0: onError: function onError() { michael@0: assert.fail("onError should not be called"); michael@0: } michael@0: }); michael@0: }; michael@0: michael@0: require("test").run(exports);