Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Cu.import("resource://gre/modules/Log.jsm"); |
michael@0 | 5 | Cu.import("resource://services-sync/constants.js"); |
michael@0 | 6 | Cu.import("resource://services-sync/service.js"); |
michael@0 | 7 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 8 | Cu.import("resource://testing-common/services/sync/utils.js"); |
michael@0 | 9 | |
michael@0 | 10 | function run_test() { |
michael@0 | 11 | initTestLogging("Trace"); |
michael@0 | 12 | Log.repository.getLogger("Sync.AsyncResource").level = Log.Level.Trace; |
michael@0 | 13 | Log.repository.getLogger("Sync.Resource").level = Log.Level.Trace; |
michael@0 | 14 | Log.repository.getLogger("Sync.Service").level = Log.Level.Trace; |
michael@0 | 15 | |
michael@0 | 16 | ensureLegacyIdentityManager(); |
michael@0 | 17 | |
michael@0 | 18 | run_next_test(); |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | add_test(function test_change_password() { |
michael@0 | 22 | let requestBody; |
michael@0 | 23 | let server; |
michael@0 | 24 | |
michael@0 | 25 | function send(statusCode, status, body) { |
michael@0 | 26 | return function(request, response) { |
michael@0 | 27 | requestBody = readBytesFromInputStream(request.bodyInputStream); |
michael@0 | 28 | response.setStatusLine(request.httpVersion, statusCode, status); |
michael@0 | 29 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 30 | }; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | try { |
michael@0 | 34 | Service.baseURI = "http://localhost:9999/"; |
michael@0 | 35 | Service.serverURL = "http://localhost:9999/"; |
michael@0 | 36 | setBasicCredentials("johndoe", "ilovejane"); |
michael@0 | 37 | |
michael@0 | 38 | _("changePassword() returns false for a network error, the password won't change."); |
michael@0 | 39 | let res = Service.changePassword("ILoveJane83"); |
michael@0 | 40 | do_check_false(res); |
michael@0 | 41 | do_check_eq(Service.identity.basicPassword, "ilovejane"); |
michael@0 | 42 | |
michael@0 | 43 | _("Let's fire up the server and actually change the password."); |
michael@0 | 44 | server = httpd_setup({ |
michael@0 | 45 | "/user/1.0/johndoe/password": send(200, "OK", ""), |
michael@0 | 46 | "/user/1.0/janedoe/password": send(401, "Unauthorized", "Forbidden!") |
michael@0 | 47 | }); |
michael@0 | 48 | |
michael@0 | 49 | Service.serverURL = server.baseURI; |
michael@0 | 50 | res = Service.changePassword("ILoveJane83"); |
michael@0 | 51 | do_check_true(res); |
michael@0 | 52 | do_check_eq(Service.identity.basicPassword, "ILoveJane83"); |
michael@0 | 53 | do_check_eq(requestBody, "ILoveJane83"); |
michael@0 | 54 | |
michael@0 | 55 | _("Make sure the password has been persisted in the login manager."); |
michael@0 | 56 | let logins = Services.logins.findLogins({}, PWDMGR_HOST, null, |
michael@0 | 57 | PWDMGR_PASSWORD_REALM); |
michael@0 | 58 | do_check_eq(logins.length, 1); |
michael@0 | 59 | do_check_eq(logins[0].password, "ILoveJane83"); |
michael@0 | 60 | |
michael@0 | 61 | _("A non-ASCII password is UTF-8 encoded."); |
michael@0 | 62 | const moneyPassword = "moneyislike$£¥"; |
michael@0 | 63 | res = Service.changePassword(moneyPassword); |
michael@0 | 64 | do_check_true(res); |
michael@0 | 65 | do_check_eq(Service.identity.basicPassword, Utils.encodeUTF8(moneyPassword)); |
michael@0 | 66 | do_check_eq(requestBody, Utils.encodeUTF8(moneyPassword)); |
michael@0 | 67 | |
michael@0 | 68 | _("changePassword() returns false for a server error, the password won't change."); |
michael@0 | 69 | Services.logins.removeAllLogins(); |
michael@0 | 70 | setBasicCredentials("janedoe", "ilovejohn"); |
michael@0 | 71 | res = Service.changePassword("ILoveJohn86"); |
michael@0 | 72 | do_check_false(res); |
michael@0 | 73 | do_check_eq(Service.identity.basicPassword, "ilovejohn"); |
michael@0 | 74 | |
michael@0 | 75 | } finally { |
michael@0 | 76 | Svc.Prefs.resetBranch(""); |
michael@0 | 77 | Services.logins.removeAllLogins(); |
michael@0 | 78 | server.stop(run_next_test); |
michael@0 | 79 | } |
michael@0 | 80 | }); |