2010-07-19 15:28:54 -07:00
|
|
|
Cu.import("resource://services-sync/util.js");
|
2010-06-17 08:47:13 -07:00
|
|
|
Cu.import("resource://services-sync/service.js");
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
var requestBody;
|
|
|
|
var 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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-08-02 22:37:13 -07:00
|
|
|
do_test_pending();
|
2010-06-17 08:47:13 -07:00
|
|
|
let server = httpd_setup({
|
2010-09-13 08:17:37 -07:00
|
|
|
// john@doe.com
|
|
|
|
"/user/1.0/7wohs32cngzuqt466q3ge7indszva4of": send(200, "OK", "0"),
|
2010-09-15 03:54:33 -07:00
|
|
|
// 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"),
|
|
|
|
"/user/1.0/johndoe": send(200, "OK", "0"),
|
2010-06-17 08:47:13 -07:00
|
|
|
"/user/1.0/janedoe": send(400, "Bad Request", "2"),
|
|
|
|
"/user/1.0/jimdoe": send(500, "Server Error", "Server Error")
|
|
|
|
});
|
|
|
|
try {
|
2010-08-25 15:49:45 -07:00
|
|
|
Service.serverURL = "http://localhost:8080/";
|
2010-06-17 08:47:13 -07:00
|
|
|
|
|
|
|
_("Create an account.");
|
2010-09-15 03:54:33 -07:00
|
|
|
let res = Service.createAccount("john@doe.com", "mysecretpw",
|
|
|
|
"challenge", "response");
|
2010-06-17 08:47:13 -07:00
|
|
|
do_check_eq(res, null);
|
|
|
|
let payload = JSON.parse(requestBody);
|
|
|
|
do_check_eq(payload.password, "mysecretpw");
|
2010-09-15 03:54:33 -07:00
|
|
|
do_check_eq(payload.email, "john@doe.com");
|
2010-06-17 08:47:13 -07:00
|
|
|
do_check_eq(payload["captcha-challenge"], "challenge");
|
|
|
|
do_check_eq(payload["captcha-response"], "response");
|
|
|
|
|
2010-07-19 15:28:54 -07:00
|
|
|
_("A non-ASCII password is UTF-8 encoded.");
|
2010-09-15 03:54:33 -07:00
|
|
|
res = Service.createAccount("john@doe.com", "moneyislike$\u20ac\xa5\u5143",
|
|
|
|
"challenge", "response");
|
2010-07-19 15:28:54 -07:00
|
|
|
do_check_eq(res, null);
|
|
|
|
payload = JSON.parse(requestBody);
|
|
|
|
do_check_eq(payload.password,
|
|
|
|
Utils.encodeUTF8("moneyislike$\u20ac\xa5\u5143"));
|
|
|
|
|
2010-06-17 08:47:13 -07:00
|
|
|
_("Invalid captcha or other user-friendly error.");
|
2010-09-15 03:54:33 -07:00
|
|
|
res = Service.createAccount("jane@doe.com", "anothersecretpw",
|
|
|
|
"challenge", "response");
|
2010-06-17 08:47:13 -07:00
|
|
|
do_check_eq(res, "invalid-captcha");
|
|
|
|
|
|
|
|
_("Generic server error.");
|
2010-09-15 03:54:33 -07:00
|
|
|
res = Service.createAccount("jim@doe.com", "preciousss",
|
|
|
|
"challenge", "response");
|
2010-06-17 08:47:13 -07:00
|
|
|
do_check_eq(res, "generic-server-error");
|
|
|
|
|
|
|
|
_("Admin secret preference is passed as HTTP header token.");
|
2010-08-25 15:49:45 -07:00
|
|
|
Svc.Prefs.set("admin-secret", "my-server-secret");
|
2010-09-15 03:54:33 -07:00
|
|
|
res = Service.createAccount("john@doe.com", "mysecretpw",
|
|
|
|
"challenge", "response");
|
2010-06-17 08:47:13 -07:00
|
|
|
do_check_eq(secretHeader, "my-server-secret");
|
|
|
|
|
2010-09-15 03:54:33 -07:00
|
|
|
|
|
|
|
// Backwards compat with the Firefox UI. Remove once bug 595066 has landed.
|
|
|
|
|
|
|
|
_("Create an old-style account.");
|
|
|
|
res = Service.createAccount("johndoe", "mysecretpw", "john@doe.com",
|
|
|
|
"challenge", "response");
|
|
|
|
do_check_eq(res, null);
|
|
|
|
|
|
|
|
_("Invalid captcha or other user-friendly error.");
|
|
|
|
res = Service.createAccount("janedoe", "anothersecretpw", "jane@doe.com",
|
|
|
|
"challenge", "response");
|
|
|
|
do_check_eq(res, "invalid-captcha");
|
|
|
|
|
|
|
|
_("Generic server error.");
|
|
|
|
res = Service.createAccount("jimdoe", "preciousss", "jim@doe.com",
|
|
|
|
"challenge", "response");
|
|
|
|
do_check_eq(res, "generic-server-error");
|
|
|
|
|
2010-06-17 08:47:13 -07:00
|
|
|
} finally {
|
2010-08-25 15:49:45 -07:00
|
|
|
Svc.Prefs.resetBranch("");
|
2010-08-02 22:37:13 -07:00
|
|
|
server.stop(do_test_finished);
|
2010-06-17 08:47:13 -07:00
|
|
|
}
|
|
|
|
}
|