|
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 login_handling(handler) { |
|
11 return function (request, response) { |
|
12 if (basic_auth_matches(request, "johndoe", "ilovejane")) { |
|
13 handler(request, response); |
|
14 } else { |
|
15 let body = "Unauthorized"; |
|
16 response.setStatusLine(request.httpVersion, 401, "Unauthorized"); |
|
17 response.bodyOutputStream.write(body, body.length); |
|
18 } |
|
19 }; |
|
20 } |
|
21 |
|
22 function service_unavailable(request, response) { |
|
23 let body = "Service Unavailable"; |
|
24 response.setStatusLine(request.httpVersion, 503, "Service Unavailable"); |
|
25 response.setHeader("Retry-After", "42"); |
|
26 response.bodyOutputStream.write(body, body.length); |
|
27 } |
|
28 |
|
29 function run_test() { |
|
30 let logger = Log.repository.rootLogger; |
|
31 Log.repository.rootLogger.addAppender(new Log.DumpAppender()); |
|
32 |
|
33 ensureLegacyIdentityManager(); |
|
34 // This test expects a clean slate -- no saved passphrase. |
|
35 Services.logins.removeAllLogins(); |
|
36 let johnHelper = track_collections_helper(); |
|
37 let johnU = johnHelper.with_updated_collection; |
|
38 let johnColls = johnHelper.collections; |
|
39 |
|
40 do_test_pending(); |
|
41 |
|
42 let server; |
|
43 function weaveHandler (request, response) { |
|
44 response.setStatusLine(request.httpVersion, 200, "OK"); |
|
45 let body = server.baseURI + "/api/"; |
|
46 response.bodyOutputStream.write(body, body.length); |
|
47 } |
|
48 |
|
49 server = httpd_setup({ |
|
50 "/api/1.1/johndoe/info/collections": login_handling(johnHelper.handler), |
|
51 "/api/1.1/janedoe/info/collections": service_unavailable, |
|
52 |
|
53 "/api/1.1/johndoe/storage/crypto/keys": johnU("crypto", new ServerWBO("keys").handler()), |
|
54 "/api/1.1/johndoe/storage/meta/global": johnU("meta", new ServerWBO("global").handler()), |
|
55 "/user/1.0/johndoe/node/weave": weaveHandler, |
|
56 }); |
|
57 |
|
58 try { |
|
59 Service.serverURL = server.baseURI; |
|
60 |
|
61 _("Force the initial state."); |
|
62 Service.status.service = STATUS_OK; |
|
63 do_check_eq(Service.status.service, STATUS_OK); |
|
64 |
|
65 _("Credentials won't check out because we're not configured yet."); |
|
66 Service.status.resetSync(); |
|
67 do_check_false(Service.verifyLogin()); |
|
68 do_check_eq(Service.status.service, CLIENT_NOT_CONFIGURED); |
|
69 do_check_eq(Service.status.login, LOGIN_FAILED_NO_USERNAME); |
|
70 |
|
71 _("Try again with username and password set."); |
|
72 Service.status.resetSync(); |
|
73 setBasicCredentials("johndoe", "ilovejane", null); |
|
74 do_check_false(Service.verifyLogin()); |
|
75 do_check_eq(Service.status.service, CLIENT_NOT_CONFIGURED); |
|
76 do_check_eq(Service.status.login, LOGIN_FAILED_NO_PASSPHRASE); |
|
77 |
|
78 _("verifyLogin() has found out the user's cluster URL, though."); |
|
79 do_check_eq(Service.clusterURL, server.baseURI + "/api/"); |
|
80 |
|
81 _("Success if passphrase is set."); |
|
82 Service.status.resetSync(); |
|
83 Service.identity.syncKey = "foo"; |
|
84 do_check_true(Service.verifyLogin()); |
|
85 do_check_eq(Service.status.service, STATUS_OK); |
|
86 do_check_eq(Service.status.login, LOGIN_SUCCEEDED); |
|
87 |
|
88 _("If verifyLogin() encounters a server error, it flips on the backoff flag and notifies observers on a 503 with Retry-After."); |
|
89 Service.status.resetSync(); |
|
90 Service.identity.account = "janedoe"; |
|
91 Service._updateCachedURLs(); |
|
92 do_check_false(Service.status.enforceBackoff); |
|
93 let backoffInterval; |
|
94 Svc.Obs.add("weave:service:backoff:interval", function observe(subject, data) { |
|
95 Svc.Obs.remove("weave:service:backoff:interval", observe); |
|
96 backoffInterval = subject; |
|
97 }); |
|
98 do_check_false(Service.verifyLogin()); |
|
99 do_check_true(Service.status.enforceBackoff); |
|
100 do_check_eq(backoffInterval, 42); |
|
101 do_check_eq(Service.status.service, LOGIN_FAILED); |
|
102 do_check_eq(Service.status.login, SERVER_MAINTENANCE); |
|
103 |
|
104 _("Ensure a network error when finding the cluster sets the right Status bits."); |
|
105 Service.status.resetSync(); |
|
106 Service.serverURL = "http://localhost:12345/"; |
|
107 do_check_false(Service.verifyLogin()); |
|
108 do_check_eq(Service.status.service, LOGIN_FAILED); |
|
109 do_check_eq(Service.status.login, LOGIN_FAILED_NETWORK_ERROR); |
|
110 |
|
111 _("Ensure a network error when getting the collection info sets the right Status bits."); |
|
112 Service.status.resetSync(); |
|
113 Service.clusterURL = "http://localhost:12345/"; |
|
114 do_check_false(Service.verifyLogin()); |
|
115 do_check_eq(Service.status.service, LOGIN_FAILED); |
|
116 do_check_eq(Service.status.login, LOGIN_FAILED_NETWORK_ERROR); |
|
117 |
|
118 } finally { |
|
119 Svc.Prefs.resetBranch(""); |
|
120 server.stop(do_test_finished); |
|
121 } |
|
122 } |