michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://gre/modules/Log.jsm"); michael@0: Cu.import("resource://services-sync/constants.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: Cu.import("resource://testing-common/services/sync/utils.js"); michael@0: michael@0: function run_test() { michael@0: initTestLogging("Trace"); michael@0: Log.repository.getLogger("Sync.AsyncResource").level = Log.Level.Trace; michael@0: Log.repository.getLogger("Sync.Resource").level = Log.Level.Trace; michael@0: Log.repository.getLogger("Sync.Service").level = Log.Level.Trace; michael@0: michael@0: ensureLegacyIdentityManager(); michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_test(function test_change_password() { michael@0: let requestBody; michael@0: let server; michael@0: michael@0: function send(statusCode, status, body) { michael@0: return function(request, response) { michael@0: requestBody = readBytesFromInputStream(request.bodyInputStream); michael@0: response.setStatusLine(request.httpVersion, statusCode, status); michael@0: response.bodyOutputStream.write(body, body.length); michael@0: }; michael@0: } michael@0: michael@0: try { michael@0: Service.baseURI = "http://localhost:9999/"; michael@0: Service.serverURL = "http://localhost:9999/"; michael@0: setBasicCredentials("johndoe", "ilovejane"); michael@0: michael@0: _("changePassword() returns false for a network error, the password won't change."); michael@0: let res = Service.changePassword("ILoveJane83"); michael@0: do_check_false(res); michael@0: do_check_eq(Service.identity.basicPassword, "ilovejane"); michael@0: michael@0: _("Let's fire up the server and actually change the password."); michael@0: server = httpd_setup({ michael@0: "/user/1.0/johndoe/password": send(200, "OK", ""), michael@0: "/user/1.0/janedoe/password": send(401, "Unauthorized", "Forbidden!") michael@0: }); michael@0: michael@0: Service.serverURL = server.baseURI; michael@0: res = Service.changePassword("ILoveJane83"); michael@0: do_check_true(res); michael@0: do_check_eq(Service.identity.basicPassword, "ILoveJane83"); michael@0: do_check_eq(requestBody, "ILoveJane83"); michael@0: michael@0: _("Make sure the password has been persisted in the login manager."); michael@0: let logins = Services.logins.findLogins({}, PWDMGR_HOST, null, michael@0: PWDMGR_PASSWORD_REALM); michael@0: do_check_eq(logins.length, 1); michael@0: do_check_eq(logins[0].password, "ILoveJane83"); michael@0: michael@0: _("A non-ASCII password is UTF-8 encoded."); michael@0: const moneyPassword = "moneyislike$£¥"; michael@0: res = Service.changePassword(moneyPassword); michael@0: do_check_true(res); michael@0: do_check_eq(Service.identity.basicPassword, Utils.encodeUTF8(moneyPassword)); michael@0: do_check_eq(requestBody, Utils.encodeUTF8(moneyPassword)); michael@0: michael@0: _("changePassword() returns false for a server error, the password won't change."); michael@0: Services.logins.removeAllLogins(); michael@0: setBasicCredentials("janedoe", "ilovejohn"); michael@0: res = Service.changePassword("ILoveJohn86"); michael@0: do_check_false(res); michael@0: do_check_eq(Service.identity.basicPassword, "ilovejohn"); michael@0: michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: Services.logins.removeAllLogins(); michael@0: server.stop(run_next_test); michael@0: } michael@0: });