addon-sdk/source/test/test-passwords.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-passwords.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,280 @@
     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 +'use strict';
     1.8 +
     1.9 +const { store, search, remove } = require("sdk/passwords");
    1.10 +
    1.11 +exports["test store requires `password` field"] = function(assert, done) {
    1.12 +  store({
    1.13 +    username: "foo",
    1.14 +    realm: "bar",
    1.15 +    onComplete: function onComplete() {
    1.16 +      assert.fail("onComplete should not be called");
    1.17 +    },
    1.18 +    onError: function onError() {
    1.19 +      assert.pass("'`password` is required");
    1.20 +      done();
    1.21 +    }
    1.22 +  });
    1.23 +};
    1.24 +
    1.25 +exports["test store requires `username` field"] = function(assert, done) {
    1.26 +  store({
    1.27 +    password: "foo",
    1.28 +    realm: "bar",
    1.29 +    onComplete: function onComplete() {
    1.30 +      assert.fail("onComplete should not be called");
    1.31 +    },
    1.32 +    onError: function onError() {
    1.33 +      assert.pass("'`username` is required");
    1.34 +      done();
    1.35 +    }
    1.36 +  });
    1.37 +};
    1.38 +
    1.39 +exports["test onComplete is optional"] = function(assert, done) {
    1.40 +  store({
    1.41 +    realm: "bla",
    1.42 +    username: "bla",
    1.43 +    password: "bla",
    1.44 +    onError: function onError() {
    1.45 +      assert.fail("onError was called");
    1.46 +    }
    1.47 +  });
    1.48 +  assert.pass("exception is not thrown if `onComplete is missing")
    1.49 +  done();
    1.50 +};
    1.51 +
    1.52 +exports["test exceptions in onComplete are reported"] = function(assert, done) {
    1.53 +  store({
    1.54 +    realm: "throws",
    1.55 +    username: "error",
    1.56 +    password: "boom!",
    1.57 +    onComplete: function onComplete(error) {
    1.58 +      throw new Error("Boom!")
    1.59 +    },
    1.60 +    onError: function onError(error) {
    1.61 +      assert.equal(error.message, "Boom!", "Error thrown is reported");
    1.62 +      done();
    1.63 +    }
    1.64 +  });
    1.65 +};
    1.66 +
    1.67 +exports["test store requires `realm` field"] = function(assert, done) {
    1.68 +  store({
    1.69 +    username: "foo",
    1.70 +    password: "bar",
    1.71 +    onComplete: function onComplete() {
    1.72 +      assert.fail("onComplete should not be called");
    1.73 +    },
    1.74 +    onError: function onError() {
    1.75 +      assert.pass("'`realm` is required");
    1.76 +      done();
    1.77 +    }
    1.78 +  });
    1.79 +};
    1.80 +
    1.81 +exports["test can't store same login twice"] = function(assert, done) {
    1.82 +  store({
    1.83 +    username: "user",
    1.84 +    password: "pass",
    1.85 +    realm: "realm",
    1.86 +    onComplete: function onComplete() {
    1.87 +      assert.pass("credential saved");
    1.88 +
    1.89 +      store({
    1.90 +        username: "user",
    1.91 +        password: "pass",
    1.92 +        realm: "realm",
    1.93 +        onComplete: function onComplete() {
    1.94 +          assert.fail("onComplete should not be called");
    1.95 +        },
    1.96 +        onError: function onError() {
    1.97 +          assert.pass("re-saving credential failed");
    1.98 +
    1.99 +          remove({
   1.100 +            username: "user",
   1.101 +            password: "pass",
   1.102 +            realm: "realm",
   1.103 +            onComplete: function onComplete() {
   1.104 +              assert.pass("credential was removed");
   1.105 +              done();
   1.106 +            },
   1.107 +            onError: function onError() {
   1.108 +              assert.fail("remove should not fail");
   1.109 +            }
   1.110 +          });
   1.111 +        }
   1.112 +      });
   1.113 +    },
   1.114 +    onError: function onError() {
   1.115 +      assert.fail("onError should not be called");
   1.116 +    }
   1.117 +  });
   1.118 +};
   1.119 +
   1.120 +exports["test remove fails if no login found"] = function(assert, done) {
   1.121 +  remove({
   1.122 +    username: "foo",
   1.123 +    password: "bar",
   1.124 +    realm: "baz",
   1.125 +    onComplete: function onComplete() {
   1.126 +      assert.fail("should not be able to remove unstored credentials");
   1.127 +    },
   1.128 +    onError: function onError() {
   1.129 +      assert.pass("can't remove unstored credentials");
   1.130 +      done();
   1.131 +    }
   1.132 +  });
   1.133 +};
   1.134 +
   1.135 +exports["test addon associated credentials"] = function(assert, done) {
   1.136 +  store({
   1.137 +    username: "foo",
   1.138 +    password: "bar",
   1.139 +    realm: "baz",
   1.140 +    onComplete: function onComplete() {
   1.141 +      search({
   1.142 +        username: "foo",
   1.143 +        password: "bar",
   1.144 +        realm: "baz",
   1.145 +        onComplete: function onComplete([credential]) {
   1.146 +          assert.equal(credential.url.indexOf("addon:"), 0,
   1.147 +                       "`addon:` uri is used for add-on credentials");
   1.148 +          assert.equal(credential.username, "foo",
   1.149 +                       "username matches");
   1.150 +          assert.equal(credential.password, "bar",
   1.151 +                       "password matches");
   1.152 +          assert.equal(credential.realm, "baz", "realm matches");
   1.153 +          assert.equal(credential.formSubmitURL, null,
   1.154 +                       "`formSubmitURL` is `null` for add-on credentials");
   1.155 +          assert.equal(credential.usernameField, "", "usernameField is empty");
   1.156 +          assert.equal(credential.passwordField, "", "passwordField is empty");
   1.157 +
   1.158 +          remove({
   1.159 +            username: credential.username,
   1.160 +            password: credential.password,
   1.161 +            realm: credential.realm,
   1.162 +            onComplete: function onComplete() {
   1.163 +              assert.pass("credential is removed");
   1.164 +              done();
   1.165 +            },
   1.166 +            onError: function onError() {
   1.167 +              assert.fail("onError should not be called");
   1.168 +            }
   1.169 +          });
   1.170 +        },
   1.171 +        onError: function onError() {
   1.172 +          assert.fail("onError should not be called");
   1.173 +        }
   1.174 +      });
   1.175 +    },
   1.176 +    onError: function onError() {
   1.177 +      assert.fail("onError should not be called");
   1.178 +    }
   1.179 +  });
   1.180 +};
   1.181 +
   1.182 +exports["test web page associated credentials"] = function(assert, done) {
   1.183 +  store({
   1.184 +    url: "http://bar.foo.com/authentication/?login",
   1.185 +    formSubmitURL: "http://login.foo.com/authenticate.cgi",
   1.186 +    username: "user",
   1.187 +    password: "pass",
   1.188 +    usernameField: "user-f",
   1.189 +    passwordField: "pass-f",
   1.190 +    onComplete: function onComplete() {
   1.191 +      search({
   1.192 +        username: "user",
   1.193 +        password: "pass",
   1.194 +        url: "http://bar.foo.com",
   1.195 +        formSubmitURL: "http://login.foo.com",
   1.196 +        onComplete: function onComplete([credential]) {
   1.197 +          assert.equal(credential.url, "http://bar.foo.com", "url matches");
   1.198 +          assert.equal(credential.username, "user", "username matches");
   1.199 +          assert.equal(credential.password, "pass", "password matches");
   1.200 +          assert.equal(credential.realm, null, "realm is null");
   1.201 +          assert.equal(credential.formSubmitURL, "http://login.foo.com",
   1.202 +                       "formSubmitURL matches");
   1.203 +          assert.equal(credential.usernameField, "user-f",
   1.204 +                       "usernameField is matches");
   1.205 +          assert.equal(credential.passwordField, "pass-f",
   1.206 +                       "passwordField matches");
   1.207 +
   1.208 +          remove({
   1.209 +            url: credential.url,
   1.210 +            formSubmitURL: credential.formSubmitURL,
   1.211 +            username: credential.username,
   1.212 +            password: credential.password,
   1.213 +            usernameField: credential.usernameField,
   1.214 +            passwordField: credential.passwordField,
   1.215 +
   1.216 +            onComplete: function onComplete() {
   1.217 +              assert.pass("credential is removed");
   1.218 +              done();
   1.219 +            },
   1.220 +            onError: function onError(e) {
   1.221 +              assert.fail("onError should not be called");
   1.222 +            }
   1.223 +          });
   1.224 +        },
   1.225 +        onError: function onError() {
   1.226 +          assert.fail("onError should not be called");
   1.227 +        }
   1.228 +      });
   1.229 +    },
   1.230 +    onError: function onError() {
   1.231 +      assert.fail("onError should not be called");
   1.232 +    }
   1.233 +  });
   1.234 +};
   1.235 +
   1.236 +exports["test site authentication credentials"] = function(assert, done) {
   1.237 +  store({
   1.238 +    url: "http://authentication.com",
   1.239 +    username: "U",
   1.240 +    password: "P",
   1.241 +    realm: "R",
   1.242 +    onComplete: function onComplete() {
   1.243 +      search({
   1.244 +        url: "http://authentication.com",
   1.245 +        username: "U",
   1.246 +        password: "P",
   1.247 +        realm: "R",
   1.248 +        onComplete: function onComplete([credential]) {
   1.249 +          assert.equal(credential.url,"http://authentication.com",
   1.250 +                       "url matches");
   1.251 +          assert.equal(credential.username, "U", "username matches");
   1.252 +          assert.equal(credential.password, "P", "password matches");
   1.253 +          assert.equal(credential.realm, "R", "realm matches");
   1.254 +          assert.equal(credential.formSubmitURL, null, "formSubmitURL is null");
   1.255 +          assert.equal(credential.usernameField, "", "usernameField is empty");
   1.256 +          assert.equal(credential.passwordField, "", "passwordField is empty");
   1.257 +
   1.258 +          remove({
   1.259 +            url: credential.url,
   1.260 +            username: credential.username,
   1.261 +            password: credential.password,
   1.262 +            realm: credential.realm,
   1.263 +            onComplete: function onComplete() {
   1.264 +              assert.pass("credential is removed");
   1.265 +              done();
   1.266 +            },
   1.267 +            onError: function onError() {
   1.268 +              assert.fail("onError should not be called");
   1.269 +            }
   1.270 +          });
   1.271 +        },
   1.272 +        onError: function onError() {
   1.273 +          assert.fail("onError should not be called");
   1.274 +        }
   1.275 +      });
   1.276 +    },
   1.277 +    onError: function onError() {
   1.278 +      assert.fail("onError should not be called");
   1.279 +    }
   1.280 +  });
   1.281 +};
   1.282 +
   1.283 +require("test").run(exports);

mercurial