michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: Cu.import("resource://services-sync/util.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://testing-common/services/sync/utils.js"); michael@0: michael@0: function run_test() { michael@0: initTestLogging("Trace"); michael@0: michael@0: let requestBody; michael@0: let secretHeader; michael@0: function send(statusCode, status, body) { michael@0: return function(request, response) { michael@0: requestBody = readBytesFromInputStream(request.bodyInputStream); michael@0: if (request.hasHeader("X-Weave-Secret")) { michael@0: secretHeader = request.getHeader("X-Weave-Secret"); michael@0: } michael@0: michael@0: response.setStatusLine(request.httpVersion, statusCode, status); michael@0: response.bodyOutputStream.write(body, body.length); michael@0: }; michael@0: } michael@0: michael@0: do_test_pending(); michael@0: let server = httpd_setup({ michael@0: // john@doe.com michael@0: "/user/1.0/7wohs32cngzuqt466q3ge7indszva4of": send(200, "OK", "0"), michael@0: // jane@doe.com michael@0: "/user/1.0/vuuf3eqgloxpxmzph27f5a6ve7gzlrms": send(400, "Bad Request", "2"), michael@0: // jim@doe.com michael@0: "/user/1.0/vz6fhecgw5t3sgx3a4cektoiokyczkqd": send(500, "Server Error", "Server Error") michael@0: }); michael@0: try { michael@0: Service.serverURL = server.baseURI; michael@0: michael@0: _("Create an account."); michael@0: let res = Service.createAccount("john@doe.com", "mysecretpw", michael@0: "challenge", "response"); michael@0: do_check_eq(res, null); michael@0: let payload = JSON.parse(requestBody); michael@0: do_check_eq(payload.password, "mysecretpw"); michael@0: do_check_eq(payload.email, "john@doe.com"); michael@0: do_check_eq(payload["captcha-challenge"], "challenge"); michael@0: do_check_eq(payload["captcha-response"], "response"); michael@0: michael@0: _("A non-ASCII password is UTF-8 encoded."); michael@0: const moneyPassword = "moneyislike$£¥"; michael@0: res = Service.createAccount("john@doe.com", moneyPassword, michael@0: "challenge", "response"); michael@0: do_check_eq(res, null); michael@0: payload = JSON.parse(requestBody); michael@0: do_check_eq(payload.password, Utils.encodeUTF8(moneyPassword)); michael@0: michael@0: _("Invalid captcha or other user-friendly error."); michael@0: res = Service.createAccount("jane@doe.com", "anothersecretpw", michael@0: "challenge", "response"); michael@0: do_check_eq(res, "invalid-captcha"); michael@0: michael@0: _("Generic server error."); michael@0: res = Service.createAccount("jim@doe.com", "preciousss", michael@0: "challenge", "response"); michael@0: do_check_eq(res, "generic-server-error"); michael@0: michael@0: _("Admin secret preference is passed as HTTP header token."); michael@0: Svc.Prefs.set("admin-secret", "my-server-secret"); michael@0: res = Service.createAccount("john@doe.com", "mysecretpw", michael@0: "challenge", "response"); michael@0: do_check_eq(secretHeader, "my-server-secret"); michael@0: michael@0: } finally { michael@0: Svc.Prefs.resetBranch(""); michael@0: server.stop(do_test_finished); michael@0: } michael@0: }