1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Cu.import("resource://services-sync/util.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://testing-common/services/sync/utils.js");
function run_test() {
initTestLogging("Trace");
let requestBody;
let secretHeader;
function send(statusCode, status, body) {
return function(request, response) {
requestBody = readBytesFromInputStream(request.bodyInputStream);
if (request.hasHeader("X-Weave-Secret")) {
secretHeader = request.getHeader("X-Weave-Secret");
}
response.setStatusLine(request.httpVersion, statusCode, status);
response.bodyOutputStream.write(body, body.length);
};
}
do_test_pending();
let server = httpd_setup({
// john@doe.com
"/user/1.0/7wohs32cngzuqt466q3ge7indszva4of": send(200, "OK", "0"),
// jane@doe.com
"/user/1.0/vuuf3eqgloxpxmzph27f5a6ve7gzlrms": send(400, "Bad Request", "2"),
// jim@doe.com
"/user/1.0/vz6fhecgw5t3sgx3a4cektoiokyczkqd": send(500, "Server Error", "Server Error")
});
try {
Service.serverURL = server.baseURI;
_("Create an account.");
let res = Service.createAccount("john@doe.com", "mysecretpw",
"challenge", "response");
do_check_eq(res, null);
let payload = JSON.parse(requestBody);
do_check_eq(payload.password, "mysecretpw");
do_check_eq(payload.email, "john@doe.com");
do_check_eq(payload["captcha-challenge"], "challenge");
do_check_eq(payload["captcha-response"], "response");
_("A non-ASCII password is UTF-8 encoded.");
const moneyPassword = "moneyislike$£¥";
res = Service.createAccount("john@doe.com", moneyPassword,
"challenge", "response");
do_check_eq(res, null);
payload = JSON.parse(requestBody);
do_check_eq(payload.password, Utils.encodeUTF8(moneyPassword));
_("Invalid captcha or other user-friendly error.");
res = Service.createAccount("jane@doe.com", "anothersecretpw",
"challenge", "response");
do_check_eq(res, "invalid-captcha");
_("Generic server error.");
res = Service.createAccount("jim@doe.com", "preciousss",
"challenge", "response");
do_check_eq(res, "generic-server-error");
_("Admin secret preference is passed as HTTP header token.");
Svc.Prefs.set("admin-secret", "my-server-secret");
res = Service.createAccount("john@doe.com", "mysecretpw",
"challenge", "response");
do_check_eq(secretHeader, "my-server-secret");
} finally {
Svc.Prefs.resetBranch("");
server.stop(do_test_finished);
}
}
|