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