Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const { store, search, remove } = require("sdk/passwords/utils"); |
michael@0 | 8 | |
michael@0 | 9 | exports["test store requires `password` field"] = function(assert) { |
michael@0 | 10 | assert.throws(function() { |
michael@0 | 11 | store({ username: "foo", realm: "bar" }); |
michael@0 | 12 | }, "`password` is required"); |
michael@0 | 13 | }; |
michael@0 | 14 | |
michael@0 | 15 | exports["test store requires `username` field"] = function(assert) { |
michael@0 | 16 | assert.throws(function() { |
michael@0 | 17 | store({ password: "foo", realm: "bar" }); |
michael@0 | 18 | }, "`username` is required"); |
michael@0 | 19 | }; |
michael@0 | 20 | |
michael@0 | 21 | exports["test store requires `realm` field"] = function(assert) { |
michael@0 | 22 | assert.throws(function() { |
michael@0 | 23 | store({ username: "foo", password: "bar" }); |
michael@0 | 24 | }, "`password` is required"); |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | exports["test can't store same login twice"] = function(assert) { |
michael@0 | 28 | let options = { username: "user", password: "pass", realm: "realm" }; |
michael@0 | 29 | store(options); |
michael@0 | 30 | assert.throws(function() { |
michael@0 | 31 | store(options); |
michael@0 | 32 | }, "can't store same pass twice"); |
michael@0 | 33 | remove(options); |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | exports["test remove throws if no login found"] = function(assert) { |
michael@0 | 37 | assert.throws(function() { |
michael@0 | 38 | remove({ username: "foo", password: "bar", realm: "baz" }); |
michael@0 | 39 | }, "can't remove unstored credentials"); |
michael@0 | 40 | }; |
michael@0 | 41 | |
michael@0 | 42 | exports["test addon associated credentials"] = function(assert) { |
michael@0 | 43 | let options = { username: "foo", password: "bar", realm: "baz" }; |
michael@0 | 44 | store(options); |
michael@0 | 45 | |
michael@0 | 46 | assert.ok(search().length, "credential was stored"); |
michael@0 | 47 | assert.ok(search(options).length, "stored credential found"); |
michael@0 | 48 | assert.ok(search({ username: options.username }).length, "found by username"); |
michael@0 | 49 | assert.ok(search({ password: options.password }).length, "found by password"); |
michael@0 | 50 | assert.ok(search({ realm: options.realm }).length, "found by realm"); |
michael@0 | 51 | |
michael@0 | 52 | let credential = search(options)[0]; |
michael@0 | 53 | assert.equal(credential.url.indexOf("addon:"), 0, |
michael@0 | 54 | "`addon:` uri is used for add-on associated credentials"); |
michael@0 | 55 | assert.equal(credential.username, options.username, "username matches"); |
michael@0 | 56 | assert.equal(credential.password, options.password, "password matches"); |
michael@0 | 57 | assert.equal(credential.realm, options.realm, "realm matches"); |
michael@0 | 58 | assert.equal(credential.formSubmitURL, null, |
michael@0 | 59 | "`formSubmitURL` is `null` for add-on associated credentials"); |
michael@0 | 60 | assert.equal(credential.usernameField, "", "usernameField is empty"); |
michael@0 | 61 | assert.equal(credential.passwordField, "", "passwordField is empty"); |
michael@0 | 62 | |
michael@0 | 63 | remove(search(options)[0]); |
michael@0 | 64 | assert.ok(!search(options).length, "remove worked"); |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | exports["test web page associated credentials"] = function(assert) { |
michael@0 | 68 | let options = { |
michael@0 | 69 | url: "http://www.example.com", |
michael@0 | 70 | formSubmitURL: "http://login.example.com", |
michael@0 | 71 | username: "user", |
michael@0 | 72 | password: "pass", |
michael@0 | 73 | usernameField: "user-f", |
michael@0 | 74 | passwordField: "pass-f" |
michael@0 | 75 | }; |
michael@0 | 76 | store({ |
michael@0 | 77 | url: "http://www.example.com/login", |
michael@0 | 78 | formSubmitURL: "http://login.example.com/foo/authenticate.cgi", |
michael@0 | 79 | username: options.username, |
michael@0 | 80 | password: options.password, |
michael@0 | 81 | usernameField: options.usernameField, |
michael@0 | 82 | passwordField: options.passwordField |
michael@0 | 83 | }); |
michael@0 | 84 | |
michael@0 | 85 | assert.ok(search().length, "credential was stored"); |
michael@0 | 86 | assert.ok(search(options).length, "stored credential found"); |
michael@0 | 87 | assert.ok(search({ username: options.username }).length, "found by username"); |
michael@0 | 88 | assert.ok(search({ password: options.password }).length, "found by password"); |
michael@0 | 89 | assert.ok(search({ formSubmitURL: options.formSubmitURL }).length, |
michael@0 | 90 | "found by formSubmitURL"); |
michael@0 | 91 | assert.ok(search({ usernameField: options.usernameField }).length, |
michael@0 | 92 | "found by usernameField"); |
michael@0 | 93 | assert.ok(search({ passwordField: options.passwordField }).length, |
michael@0 | 94 | "found by passwordField"); |
michael@0 | 95 | |
michael@0 | 96 | let credential = search(options)[0]; |
michael@0 | 97 | assert.equal(credential.url, options.url, "url matches"); |
michael@0 | 98 | assert.equal(credential.username, options.username, "username matches"); |
michael@0 | 99 | assert.equal(credential.password, options.password, "password matches"); |
michael@0 | 100 | assert.equal(credential.realm, null, "realm is "); |
michael@0 | 101 | assert.equal(credential.formSubmitURL, options.formSubmitURL, |
michael@0 | 102 | "`formSubmitURL` matches"); |
michael@0 | 103 | assert.equal(credential.usernameField, options.usernameField, |
michael@0 | 104 | "usernameField matches"); |
michael@0 | 105 | assert.equal(credential.passwordField, options.passwordField, |
michael@0 | 106 | "passwordField matches"); |
michael@0 | 107 | |
michael@0 | 108 | remove(search(options)[0]); |
michael@0 | 109 | assert.ok(!search(options).length, "remove worked"); |
michael@0 | 110 | }; |
michael@0 | 111 | |
michael@0 | 112 | exports["test site authentication credentials"] = function(assert) { |
michael@0 | 113 | let options = { |
michael@0 | 114 | url: "http://test.authentication.com", |
michael@0 | 115 | username: "u", |
michael@0 | 116 | password: "p", |
michael@0 | 117 | realm: "r" |
michael@0 | 118 | }; |
michael@0 | 119 | |
michael@0 | 120 | store(options); |
michael@0 | 121 | assert.ok(search().length, "credential was stored"); |
michael@0 | 122 | assert.ok(search(options).length, "stored credential found"); |
michael@0 | 123 | assert.ok(search({ username: options.username }).length, "found by username"); |
michael@0 | 124 | assert.ok(search({ password: options.password }).length, "found by password"); |
michael@0 | 125 | assert.ok(search({ realm: options.realm }).length, "found by realm"); |
michael@0 | 126 | assert.ok(search({ url: options.url }).length, "found by url"); |
michael@0 | 127 | |
michael@0 | 128 | let credential = search(options)[0]; |
michael@0 | 129 | assert.equal(credential.url, options.url, "url matches"); |
michael@0 | 130 | assert.equal(credential.username, options.username, "username matches"); |
michael@0 | 131 | assert.equal(credential.password, options.password, "password matches"); |
michael@0 | 132 | assert.equal(credential.realm, options.realm, "realm matches"); |
michael@0 | 133 | assert.equal(credential.formSubmitURL, null, |
michael@0 | 134 | "`formSubmitURL` is `null` for site authentication credentials"); |
michael@0 | 135 | assert.equal(credential.usernameField, "", "usernameField is empty"); |
michael@0 | 136 | assert.equal(credential.passwordField, "", "passwordField is empty"); |
michael@0 | 137 | |
michael@0 | 138 | remove(search(options)[0]); |
michael@0 | 139 | assert.ok(!search(options).length, "remove worked"); |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | require("test").run(exports); |