services/sync/tests/unit/test_service_changePassword.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_service_changePassword.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://gre/modules/Log.jsm");
     1.8 +Cu.import("resource://services-sync/constants.js");
     1.9 +Cu.import("resource://services-sync/service.js");
    1.10 +Cu.import("resource://services-sync/util.js");
    1.11 +Cu.import("resource://testing-common/services/sync/utils.js");
    1.12 +
    1.13 +function run_test() {
    1.14 +  initTestLogging("Trace");
    1.15 +  Log.repository.getLogger("Sync.AsyncResource").level = Log.Level.Trace;
    1.16 +  Log.repository.getLogger("Sync.Resource").level = Log.Level.Trace;
    1.17 +  Log.repository.getLogger("Sync.Service").level = Log.Level.Trace;
    1.18 +
    1.19 +  ensureLegacyIdentityManager();
    1.20 +
    1.21 +  run_next_test();
    1.22 +}
    1.23 +
    1.24 +add_test(function test_change_password() {
    1.25 +  let requestBody;
    1.26 +  let server;
    1.27 +
    1.28 +  function send(statusCode, status, body) {
    1.29 +    return function(request, response) {
    1.30 +      requestBody = readBytesFromInputStream(request.bodyInputStream);
    1.31 +      response.setStatusLine(request.httpVersion, statusCode, status);
    1.32 +      response.bodyOutputStream.write(body, body.length);
    1.33 +    };
    1.34 +  }
    1.35 +
    1.36 +  try {
    1.37 +    Service.baseURI = "http://localhost:9999/";
    1.38 +    Service.serverURL = "http://localhost:9999/";
    1.39 +    setBasicCredentials("johndoe", "ilovejane");
    1.40 +
    1.41 +    _("changePassword() returns false for a network error, the password won't change.");
    1.42 +    let res = Service.changePassword("ILoveJane83");
    1.43 +    do_check_false(res);
    1.44 +    do_check_eq(Service.identity.basicPassword, "ilovejane");
    1.45 +
    1.46 +    _("Let's fire up the server and actually change the password.");
    1.47 +    server = httpd_setup({
    1.48 +      "/user/1.0/johndoe/password": send(200, "OK", ""),
    1.49 +      "/user/1.0/janedoe/password": send(401, "Unauthorized", "Forbidden!")
    1.50 +    });
    1.51 +
    1.52 +    Service.serverURL = server.baseURI;
    1.53 +    res = Service.changePassword("ILoveJane83");
    1.54 +    do_check_true(res);
    1.55 +    do_check_eq(Service.identity.basicPassword, "ILoveJane83");
    1.56 +    do_check_eq(requestBody, "ILoveJane83");
    1.57 +
    1.58 +    _("Make sure the password has been persisted in the login manager.");
    1.59 +    let logins = Services.logins.findLogins({}, PWDMGR_HOST, null,
    1.60 +                                            PWDMGR_PASSWORD_REALM);
    1.61 +    do_check_eq(logins.length, 1);
    1.62 +    do_check_eq(logins[0].password, "ILoveJane83");
    1.63 +
    1.64 +    _("A non-ASCII password is UTF-8 encoded.");
    1.65 +    const moneyPassword = "moneyislike$£¥";
    1.66 +    res = Service.changePassword(moneyPassword);
    1.67 +    do_check_true(res);
    1.68 +    do_check_eq(Service.identity.basicPassword, Utils.encodeUTF8(moneyPassword));
    1.69 +    do_check_eq(requestBody, Utils.encodeUTF8(moneyPassword));
    1.70 +
    1.71 +    _("changePassword() returns false for a server error, the password won't change.");
    1.72 +    Services.logins.removeAllLogins();
    1.73 +    setBasicCredentials("janedoe", "ilovejohn");
    1.74 +    res = Service.changePassword("ILoveJohn86");
    1.75 +    do_check_false(res);
    1.76 +    do_check_eq(Service.identity.basicPassword, "ilovejohn");
    1.77 +
    1.78 +  } finally {
    1.79 +    Svc.Prefs.resetBranch("");
    1.80 +    Services.logins.removeAllLogins();
    1.81 +    server.stop(run_next_test);
    1.82 +  }
    1.83 +});

mercurial