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: michael@0: "use strict"; michael@0: michael@0: const { store, search, remove } = require("sdk/passwords/utils"); michael@0: michael@0: exports["test store requires `password` field"] = function(assert) { michael@0: assert.throws(function() { michael@0: store({ username: "foo", realm: "bar" }); michael@0: }, "`password` is required"); michael@0: }; michael@0: michael@0: exports["test store requires `username` field"] = function(assert) { michael@0: assert.throws(function() { michael@0: store({ password: "foo", realm: "bar" }); michael@0: }, "`username` is required"); michael@0: }; michael@0: michael@0: exports["test store requires `realm` field"] = function(assert) { michael@0: assert.throws(function() { michael@0: store({ username: "foo", password: "bar" }); michael@0: }, "`password` is required"); michael@0: }; michael@0: michael@0: exports["test can't store same login twice"] = function(assert) { michael@0: let options = { username: "user", password: "pass", realm: "realm" }; michael@0: store(options); michael@0: assert.throws(function() { michael@0: store(options); michael@0: }, "can't store same pass twice"); michael@0: remove(options); michael@0: }; michael@0: michael@0: exports["test remove throws if no login found"] = function(assert) { michael@0: assert.throws(function() { michael@0: remove({ username: "foo", password: "bar", realm: "baz" }); michael@0: }, "can't remove unstored credentials"); michael@0: }; michael@0: michael@0: exports["test addon associated credentials"] = function(assert) { michael@0: let options = { username: "foo", password: "bar", realm: "baz" }; michael@0: store(options); michael@0: michael@0: assert.ok(search().length, "credential was stored"); michael@0: assert.ok(search(options).length, "stored credential found"); michael@0: assert.ok(search({ username: options.username }).length, "found by username"); michael@0: assert.ok(search({ password: options.password }).length, "found by password"); michael@0: assert.ok(search({ realm: options.realm }).length, "found by realm"); michael@0: michael@0: let credential = search(options)[0]; michael@0: assert.equal(credential.url.indexOf("addon:"), 0, michael@0: "`addon:` uri is used for add-on associated credentials"); michael@0: assert.equal(credential.username, options.username, "username matches"); michael@0: assert.equal(credential.password, options.password, "password matches"); michael@0: assert.equal(credential.realm, options.realm, "realm matches"); michael@0: assert.equal(credential.formSubmitURL, null, michael@0: "`formSubmitURL` is `null` for add-on associated credentials"); michael@0: assert.equal(credential.usernameField, "", "usernameField is empty"); michael@0: assert.equal(credential.passwordField, "", "passwordField is empty"); michael@0: michael@0: remove(search(options)[0]); michael@0: assert.ok(!search(options).length, "remove worked"); michael@0: }; michael@0: michael@0: exports["test web page associated credentials"] = function(assert) { michael@0: let options = { michael@0: url: "http://www.example.com", michael@0: formSubmitURL: "http://login.example.com", michael@0: username: "user", michael@0: password: "pass", michael@0: usernameField: "user-f", michael@0: passwordField: "pass-f" michael@0: }; michael@0: store({ michael@0: url: "http://www.example.com/login", michael@0: formSubmitURL: "http://login.example.com/foo/authenticate.cgi", michael@0: username: options.username, michael@0: password: options.password, michael@0: usernameField: options.usernameField, michael@0: passwordField: options.passwordField michael@0: }); michael@0: michael@0: assert.ok(search().length, "credential was stored"); michael@0: assert.ok(search(options).length, "stored credential found"); michael@0: assert.ok(search({ username: options.username }).length, "found by username"); michael@0: assert.ok(search({ password: options.password }).length, "found by password"); michael@0: assert.ok(search({ formSubmitURL: options.formSubmitURL }).length, michael@0: "found by formSubmitURL"); michael@0: assert.ok(search({ usernameField: options.usernameField }).length, michael@0: "found by usernameField"); michael@0: assert.ok(search({ passwordField: options.passwordField }).length, michael@0: "found by passwordField"); michael@0: michael@0: let credential = search(options)[0]; michael@0: assert.equal(credential.url, options.url, "url matches"); michael@0: assert.equal(credential.username, options.username, "username matches"); michael@0: assert.equal(credential.password, options.password, "password matches"); michael@0: assert.equal(credential.realm, null, "realm is "); michael@0: assert.equal(credential.formSubmitURL, options.formSubmitURL, michael@0: "`formSubmitURL` matches"); michael@0: assert.equal(credential.usernameField, options.usernameField, michael@0: "usernameField matches"); michael@0: assert.equal(credential.passwordField, options.passwordField, michael@0: "passwordField matches"); michael@0: michael@0: remove(search(options)[0]); michael@0: assert.ok(!search(options).length, "remove worked"); michael@0: }; michael@0: michael@0: exports["test site authentication credentials"] = function(assert) { michael@0: let options = { michael@0: url: "http://test.authentication.com", michael@0: username: "u", michael@0: password: "p", michael@0: realm: "r" michael@0: }; michael@0: michael@0: store(options); michael@0: assert.ok(search().length, "credential was stored"); michael@0: assert.ok(search(options).length, "stored credential found"); michael@0: assert.ok(search({ username: options.username }).length, "found by username"); michael@0: assert.ok(search({ password: options.password }).length, "found by password"); michael@0: assert.ok(search({ realm: options.realm }).length, "found by realm"); michael@0: assert.ok(search({ url: options.url }).length, "found by url"); michael@0: michael@0: let credential = search(options)[0]; michael@0: assert.equal(credential.url, options.url, "url matches"); michael@0: assert.equal(credential.username, options.username, "username matches"); michael@0: assert.equal(credential.password, options.password, "password matches"); michael@0: assert.equal(credential.realm, options.realm, "realm matches"); michael@0: assert.equal(credential.formSubmitURL, null, michael@0: "`formSubmitURL` is `null` for site authentication credentials"); michael@0: assert.equal(credential.usernameField, "", "usernameField is empty"); michael@0: assert.equal(credential.passwordField, "", "passwordField is empty"); michael@0: michael@0: remove(search(options)[0]); michael@0: assert.ok(!search(options).length, "remove worked"); michael@0: }; michael@0: michael@0: require("test").run(exports);