Bug 633062 p2 - Make getTokenFromBrowserIDAssertion async. r=markh

Also move promiseStopServer to the common/ head_helpers.js

MozReview-Commit-ID: B3Idnj6rPAZ

--HG--
extra : rebase_source : 0c3ee2b37448467fdd7fec04dbdf39255608901b
This commit is contained in:
Edouard Oger 2017-12-07 14:45:15 -05:00
Родитель a99869ed33
Коммит ffdd8c898f
13 изменённых файлов: 178 добавлений и 310 удалений

Просмотреть файл

@ -105,6 +105,10 @@ function httpd_handler(statusCode, status, body) {
};
}
function promiseStopServer(server) {
return new Promise(resolve => server.stop(resolve));
}
/*
* Read bytes string from an nsIInputStream. If 'count' is omitted,
* all available input is read.

Просмотреть файл

@ -62,7 +62,7 @@ add_task(async function test_authenticated_get_request() {
Assert.equal("Great Success!", result.msg);
await deferredStop(server);
await promiseStopServer(server);
});
async function check_authenticated_request(method) {
@ -82,7 +82,7 @@ async function check_authenticated_request(method) {
Assert.equal("bar", result.foo);
await deferredStop(server);
await promiseStopServer(server);
}
add_task(function test_authenticated_post_request() {
@ -117,7 +117,7 @@ add_task(async function test_extra_headers() {
Assert.equal("bar", result.foo);
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_credentials_optional() {
@ -137,7 +137,7 @@ add_task(async function test_credentials_optional() {
let result = await client.request("/foo", method); // credentials undefined
Assert.equal(JSON.parse(result.body).msg, "you're in the friend zone");
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_server_error() {
@ -160,7 +160,7 @@ add_task(async function test_server_error() {
Assert.equal("I am a Teapot", err.message);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_server_error_json() {
@ -182,7 +182,7 @@ add_task(async function test_server_error_json() {
Assert.equal("Cannot get ye flask.", err.error);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_offset_after_request() {
@ -205,7 +205,7 @@ add_task(async function test_offset_after_request() {
// Should be about an hour off
Assert.ok(Math.abs(client.localtimeOffsetMsec + HOUR_MS) < SECOND_MS);
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_offset_in_hawk_header() {
@ -248,7 +248,7 @@ add_task(async function test_offset_in_hawk_header() {
Assert.ok(Math.abs(client.localtimeOffsetMsec + 12 * HOUR_MS) < MINUTE_MS);
await client.request("/second", method, TEST_CREDS);
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_2xx_success() {
@ -272,7 +272,7 @@ add_task(async function test_2xx_success() {
// Shouldn't be any content in a 202
Assert.equal(response.body, "");
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_retry_request_on_fail() {
@ -323,7 +323,7 @@ add_task(async function test_retry_request_on_fail() {
let response = await client.request("/maybe", method, credentials);
Assert.equal(response.body, "i love you!!!");
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_multiple_401_retry_once() {
@ -369,7 +369,7 @@ add_task(async function test_multiple_401_retry_once() {
}
Assert.equal(attempts, 2);
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_500_no_retry() {
@ -406,7 +406,7 @@ add_task(async function test_500_no_retry() {
Assert.equal(err.code, 500);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_401_then_500() {
@ -466,7 +466,7 @@ add_task(async function test_401_then_500() {
}
Assert.equal(attempts, 2);
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function throw_if_not_json_body() {
@ -488,12 +488,6 @@ function getTimestampDelta(authHeader, now = Date.now()) {
return Math.abs(tsMS - now);
}
function deferredStop(server) {
return new Promise(resolve => {
server.stop(resolve);
});
}
function run_test() {
initTestLogging("Trace");
run_next_test();

Просмотреть файл

@ -4,13 +4,9 @@
ChromeUtils.import("resource://services-common/async.js");
ChromeUtils.import("resource://services-common/tokenserverclient.js");
function run_test() {
initTestLogging("Trace");
initTestLogging("Trace");
run_next_test();
}
add_test(function test_working_bid_exchange() {
add_task(async function test_working_bid_exchange() {
_("Ensure that working BrowserID token exchange works as expected.");
let service = "http://example.com/foo";
@ -37,10 +33,8 @@ add_test(function test_working_bid_exchange() {
});
let client = new TokenServerClient();
let cb = Async.makeSpinningCallback();
let url = server.baseURI + "/1.0/foo/1.0";
client.getTokenFromBrowserIDAssertion(url, "assertion", cb);
let result = cb.wait();
let result = await client.getTokenFromBrowserIDAssertion(url, "assertion");
Assert.equal("object", typeof(result));
do_check_attribute_count(result, 6);
Assert.equal(service, result.endpoint);
@ -48,32 +42,27 @@ add_test(function test_working_bid_exchange() {
Assert.equal("key", result.key);
Assert.equal("uid", result.uid);
Assert.equal(duration, result.duration);
server.stop(run_next_test);
await promiseStopServer(server);
});
add_test(function test_invalid_arguments() {
add_task(async function test_invalid_arguments() {
_("Ensure invalid arguments to APIs are rejected.");
let args = [
[null, "assertion", function() {}],
["http://example.com/", null, function() {}],
["http://example.com/", "assertion", null]
[null, "assertion"],
["http://example.com/", null]
];
for (let arg of args) {
try {
let client = new TokenServerClient();
client.getTokenFromBrowserIDAssertion(arg[0], arg[1], arg[2]);
do_throw("Should never get here.");
} catch (ex) {
let client = new TokenServerClient();
await Assert.rejects(client.getTokenFromBrowserIDAssertion(arg[0], arg[1]), ex => {
Assert.ok(ex instanceof TokenServerClientError);
}
return true;
});
}
run_next_test();
});
add_test(function test_conditions_required_response_handling() {
add_task(async function test_conditions_required_response_handling() {
_("Ensure that a conditions required response is handled properly.");
let description = "Need to accept conditions";
@ -97,22 +86,20 @@ add_test(function test_conditions_required_response_handling() {
let client = new TokenServerClient();
let url = server.baseURI + "/1.0/foo/1.0";
function onResponse(error, token) {
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.ok(error instanceof TokenServerClientServerError);
Assert.equal(error.cause, "conditions-required");
// Check a JSON.stringify works on our errors as our logging will try and use it.
Assert.ok(JSON.stringify(error), "JSON.stringify worked");
Assert.equal(null, token);
Assert.equal(error.urls.tos, tosURL);
return true;
});
server.stop(run_next_test);
}
client.getTokenFromBrowserIDAssertion(url, "assertion", onResponse);
await promiseStopServer(server);
});
add_test(function test_invalid_403_no_content_type() {
add_task(async function test_invalid_403_no_content_type() {
_("Ensure that a 403 without content-type is handled properly.");
let server = httpd_setup({
@ -131,20 +118,18 @@ add_test(function test_invalid_403_no_content_type() {
let client = new TokenServerClient();
let url = server.baseURI + "/1.0/foo/1.0";
function onResponse(error, token) {
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.ok(error instanceof TokenServerClientServerError);
Assert.equal(error.cause, "malformed-response");
Assert.equal(null, token);
Assert.equal(null, error.urls);
return true;
});
server.stop(run_next_test);
}
client.getTokenFromBrowserIDAssertion(url, "assertion", onResponse);
await promiseStopServer(server);
});
add_test(function test_invalid_403_bad_json() {
add_task(async function test_invalid_403_bad_json() {
_("Ensure that a 403 with JSON that isn't proper is handled properly.");
let server = httpd_setup({
@ -162,19 +147,17 @@ add_test(function test_invalid_403_bad_json() {
let client = new TokenServerClient();
let url = server.baseURI + "/1.0/foo/1.0";
function onResponse(error, token) {
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.ok(error instanceof TokenServerClientServerError);
Assert.equal(error.cause, "malformed-response");
Assert.equal(null, token);
Assert.equal(null, error.urls);
return true;
});
server.stop(run_next_test);
}
client.getTokenFromBrowserIDAssertion(url, "assertion", onResponse);
await promiseStopServer(server);
});
add_test(function test_403_no_urls() {
add_task(async function test_403_no_urls() {
_("Ensure that a 403 without a urls field is handled properly.");
let server = httpd_setup({
@ -190,18 +173,16 @@ add_test(function test_403_no_urls() {
let client = new TokenServerClient();
let url = server.baseURI + "/1.0/foo/1.0";
client.getTokenFromBrowserIDAssertion(url, "assertion",
function onResponse(error, result) {
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.ok(error instanceof TokenServerClientServerError);
Assert.equal(error.cause, "malformed-response");
Assert.equal(null, result);
server.stop(run_next_test);
return true;
});
await promiseStopServer(server);
});
add_test(function test_send_extra_headers() {
add_task(async function test_send_extra_headers() {
_("Ensures that the condition acceptance header is sent when asked.");
let duration = 300;
@ -230,40 +211,37 @@ add_test(function test_send_extra_headers() {
let client = new TokenServerClient();
let url = server.baseURI + "/1.0/foo/1.0";
function onResponse(error, token) {
Assert.equal(null, error);
// Other tests validate other things.
server.stop(run_next_test);
}
let extra = {
"X-Foo": 42,
"X-Bar": 17
};
client.getTokenFromBrowserIDAssertion(url, "assertion", onResponse, extra);
await client.getTokenFromBrowserIDAssertion(url, "assertion", extra);
// Other tests validate other things.
await promiseStopServer(server);
});
add_test(function test_error_404_empty() {
add_task(async function test_error_404_empty() {
_("Ensure that 404 responses without proper response are handled properly.");
let server = httpd_setup();
let client = new TokenServerClient();
let url = server.baseURI + "/foo";
client.getTokenFromBrowserIDAssertion(url, "assertion", function(error, r) {
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.ok(error instanceof TokenServerClientServerError);
Assert.equal(error.cause, "malformed-response");
Assert.notEqual(null, error.response);
Assert.equal(null, r);
server.stop(run_next_test);
return true;
});
await promiseStopServer(server);
});
add_test(function test_error_404_proper_response() {
add_task(async function test_error_404_proper_response() {
_("Ensure that a Cornice error report for 404 is handled properly.");
let server = httpd_setup({
@ -280,20 +258,19 @@ add_test(function test_error_404_proper_response() {
}
});
function onResponse(error, token) {
Assert.ok(error instanceof TokenServerClientServerError);
Assert.equal(error.cause, "unknown-service");
Assert.equal(null, token);
server.stop(run_next_test);
}
let client = new TokenServerClient();
let url = server.baseURI + "/1.0/foo/1.0";
client.getTokenFromBrowserIDAssertion(url, "assertion", onResponse);
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.ok(error instanceof TokenServerClientServerError);
Assert.equal(error.cause, "unknown-service");
return true;
});
await promiseStopServer(server);
});
add_test(function test_bad_json() {
add_task(async function test_bad_json() {
_("Ensure that malformed JSON is handled properly.");
let server = httpd_setup({
@ -308,18 +285,19 @@ add_test(function test_bad_json() {
let client = new TokenServerClient();
let url = server.baseURI + "/1.0/foo/1.0";
client.getTokenFromBrowserIDAssertion(url, "assertion", function(error, r) {
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.notEqual(null, error);
Assert.equal("TokenServerClientServerError", error.name);
Assert.equal(error.cause, "malformed-response");
Assert.notEqual(null, error.response);
Assert.equal(null, r);
server.stop(run_next_test);
return true;
});
await promiseStopServer(server);
});
add_test(function test_400_response() {
add_task(async function test_400_response() {
_("Ensure HTTP 400 is converted to malformed-request.");
let server = httpd_setup({
@ -334,17 +312,19 @@ add_test(function test_400_response() {
let client = new TokenServerClient();
let url = server.baseURI + "/1.0/foo/1.0";
client.getTokenFromBrowserIDAssertion(url, "assertion", function(error, r) {
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.notEqual(null, error);
Assert.equal("TokenServerClientServerError", error.name);
Assert.notEqual(null, error.response);
Assert.equal(error.cause, "malformed-request");
server.stop(run_next_test);
return true;
});
await promiseStopServer(server);
});
add_test(function test_401_with_error_cause() {
add_task(async function test_401_with_error_cause() {
_("Ensure 401 cause is specified in body.status");
let server = httpd_setup({
@ -359,17 +339,19 @@ add_test(function test_401_with_error_cause() {
let client = new TokenServerClient();
let url = server.baseURI + "/1.0/foo/1.0";
client.getTokenFromBrowserIDAssertion(url, "assertion", function(error, r) {
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.notEqual(null, error);
Assert.equal("TokenServerClientServerError", error.name);
Assert.notEqual(null, error.response);
Assert.equal(error.cause, "no-soup-for-you");
server.stop(run_next_test);
return true;
});
await promiseStopServer(server);
});
add_test(function test_unhandled_media_type() {
add_task(async function test_unhandled_media_type() {
_("Ensure that unhandled media types throw an error.");
let server = httpd_setup({
@ -384,17 +366,18 @@ add_test(function test_unhandled_media_type() {
let url = server.baseURI + "/1.0/foo/1.0";
let client = new TokenServerClient();
client.getTokenFromBrowserIDAssertion(url, "assertion", function(error, r) {
await Assert.rejects(client.getTokenFromBrowserIDAssertion(url, "assertion"), error => {
Assert.notEqual(null, error);
Assert.equal("TokenServerClientServerError", error.name);
Assert.notEqual(null, error.response);
Assert.equal(null, r);
server.stop(run_next_test);
return true;
});
await promiseStopServer(server);
});
add_test(function test_rich_media_types() {
add_task(async function test_rich_media_types() {
_("Ensure that extra tokens in the media type aren't rejected.");
let duration = 300;
@ -416,51 +399,7 @@ add_test(function test_rich_media_types() {
let url = server.baseURI + "/foo";
let client = new TokenServerClient();
client.getTokenFromBrowserIDAssertion(url, "assertion", function(error, r) {
Assert.equal(null, error);
server.stop(run_next_test);
});
});
add_test(function test_exception_during_callback() {
_("Ensure that exceptions thrown during callback handling are handled.");
let duration = 300;
let server = httpd_setup({
"/foo": function(request, response) {
response.setStatusLine(request.httpVersion, 200, "OK");
response.setHeader("Content-Type", "application/json");
let body = JSON.stringify({
id: "id",
key: "key",
api_endpoint: "foo",
uid: "uid",
duration,
});
response.bodyOutputStream.write(body, body.length);
}
});
let url = server.baseURI + "/foo";
let client = new TokenServerClient();
let cb = Async.makeSpinningCallback();
let callbackCount = 0;
client.getTokenFromBrowserIDAssertion(url, "assertion", function(error, r) {
Assert.equal(null, error);
cb();
callbackCount += 1;
throw new Error("I am a bad function!");
});
cb.wait();
// This relies on some heavy event loop magic. The error in the main
// callback should already have been raised at this point.
Assert.equal(callbackCount, 1);
server.stop(run_next_test);
await client.getTokenFromBrowserIDAssertion(url, "assertion");
await promiseStopServer(server);
});

Просмотреть файл

@ -209,38 +209,22 @@ TokenServerClient.prototype = {
* let assertion = getBrowserIDAssertionFromSomewhere();
* let url = "https://token.services.mozilla.com/1.0/sync/2.0";
*
* client.getTokenFromBrowserIDAssertion(url, assertion,
* function onResponse(error, result) {
* if (error) {
* if (error.cause == "conditions-required") {
* promptConditionsAcceptance(error.urls, function onAccept() {
* client.getTokenFromBrowserIDAssertion(url, assertion,
* onResponse, true);
* }
* return;
* }
*
* // Do other error handling.
* return;
* }
*
* let {
* id: id, key: key, uid: uid, endpoint: endpoint, duration: duration
* } = result;
* try {
* const result = await client.getTokenFromBrowserIDAssertion(url, assertion);
* let {id, key, uid, endpoint, duration} = result;
* // Do stuff with data and carry on.
* });
* } catch (error) {
* // Handle errors.
* }
*
* @param url
* (string) URL to fetch token from.
* @param assertion
* (string) BrowserID assertion to exchange token for.
* @param cb
* (function) Callback to be invoked with result of operation.
* @param conditionsAccepted
* (bool) Whether to send acceptance to service conditions.
*/
getTokenFromBrowserIDAssertion:
function getTokenFromBrowserIDAssertion(url, assertion, cb, addHeaders = {}) {
async getTokenFromBrowserIDAssertion(url, assertion, addHeaders = {}) {
if (!url) {
throw new TokenServerClientError("url argument is not valid.");
}
@ -249,10 +233,6 @@ TokenServerClient.prototype = {
throw new TokenServerClientError("assertion argument is not valid.");
}
if (!cb) {
throw new TokenServerClientError("cb argument is not valid.");
}
this._log.debug("Beginning BID assertion exchange: " + url);
let req = this.newRESTRequest(url);
@ -263,39 +243,25 @@ TokenServerClient.prototype = {
req.setHeader(header, addHeaders[header]);
}
let client = this;
req.get(function onResponse(error) {
if (error) {
cb(new TokenServerClientNetworkError(error), null);
return;
}
let self = this;
function callCallback(error, result) {
if (!cb) {
self._log.warn("Callback already called! Did it throw?");
return;
}
try {
cb(error, result);
} catch (ex) {
self._log.warn("Exception when calling user-supplied callback", ex);
}
cb = null;
}
try {
client._processTokenResponse(this.response, callCallback);
} catch (ex) {
this._log.warn("Error processing token server response", ex);
let error = new TokenServerClientError(ex);
error.response = this.response;
callCallback(error, null);
}
let response = await new Promise((resolve, reject) => {
req.get(function(err) {
// Yes this is weird, the callback's |this| gets bound to the RESTRequest object.
err ? reject(new TokenServerClientNetworkError(err)) :
resolve(this.response);
});
});
try {
return this._processTokenResponse(response);
} catch (ex) {
if (ex instanceof TokenServerClientServerError) {
throw ex;
}
this._log.warn("Error processing token server response", ex);
let error = new TokenServerClientError(ex);
error.response = response;
throw error;
}
},
/**
@ -303,10 +269,8 @@ TokenServerClient.prototype = {
*
* @param response
* RESTResponse from token HTTP request.
* @param cb
* The original callback passed to the public API.
*/
_processTokenResponse: function processTokenResponse(response, cb) {
_processTokenResponse(response) {
this._log.debug("Got token response: " + response.status);
// Responses should *always* be JSON, even in the case of 4xx and 5xx
@ -320,8 +284,7 @@ TokenServerClient.prototype = {
let error = new TokenServerClientServerError("Non-JSON response.",
"malformed-response");
error.response = response;
cb(error, null);
return;
throw error;
}
let result;
@ -332,8 +295,7 @@ TokenServerClient.prototype = {
let error = new TokenServerClientServerError("Malformed JSON.",
"malformed-response");
error.response = response;
cb(error, null);
return;
throw error;
}
// Any response status can have X-Backoff or X-Weave-Backoff headers.
@ -393,8 +355,7 @@ TokenServerClient.prototype = {
// we'll look for it on any error response.
this._maybeNotifyBackoff(response, "retry-after");
cb(error, null);
return;
throw error;
}
for (let k of ["id", "key", "api_endpoint", "uid", "duration"]) {
@ -404,20 +365,19 @@ TokenServerClient.prototype = {
k);
error.cause = "malformed-response";
error.response = response;
cb(error, null);
return;
throw error;
}
}
this._log.debug("Successful token response");
cb(null, {
return {
id: result.id,
key: result.key,
endpoint: result.api_endpoint,
uid: result.uid,
duration: result.duration,
hashed_fxa_uid: result.hashed_fxa_uid,
});
};
},
/*

Просмотреть файл

@ -29,12 +29,6 @@ var ACCOUNT_KEYS = {
"5051525354555657 58595a5b5c5d5e5f"),
};
function deferredStop(server) {
return new Promise(resolve => {
server.stop(resolve);
});
}
add_task(async function test_authenticated_get_request() {
let message = "{\"msg\": \"Great Success!\"}";
let credentials = {
@ -57,7 +51,7 @@ add_task(async function test_authenticated_get_request() {
let result = await client._request("/foo", method, credentials);
Assert.equal("Great Success!", result.msg);
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_authenticated_post_request() {
@ -82,7 +76,7 @@ add_task(async function test_authenticated_post_request() {
let result = await client._request("/foo", method, credentials, {foo: "bar"});
Assert.equal("bar", result.foo);
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_500_error() {
@ -105,7 +99,7 @@ add_task(async function test_500_error() {
Assert.equal("Internal Server Error", e.message);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_backoffError() {
@ -152,7 +146,7 @@ add_task(async function test_backoffError() {
Assert.equal(client.backoffError, null);
Assert.equal(result.working, "yes");
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_signUp() {
@ -237,7 +231,7 @@ add_task(async function test_signUp() {
Assert.equal(101, expectedError.errno);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_signIn() {
@ -324,7 +318,7 @@ add_task(async function test_signIn() {
Assert.equal(102, expectedError.errno);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_signOut() {
@ -360,7 +354,7 @@ add_task(async function test_signOut() {
Assert.equal(102, expectedError.errno);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_recoveryEmailStatus() {
@ -398,7 +392,7 @@ add_task(async function test_recoveryEmailStatus() {
Assert.equal(102, expectedError.errno);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_recoveryEmailStatusWithReason() {
@ -419,7 +413,7 @@ add_task(async function test_recoveryEmailStatusWithReason() {
reason: "push",
});
Assert.equal(result.verified, true);
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_resendVerificationEmail() {
@ -455,7 +449,7 @@ add_task(async function test_resendVerificationEmail() {
Assert.equal(102, expectedError.errno);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_accountKeys() {
@ -536,7 +530,7 @@ add_task(async function test_accountKeys() {
Assert.equal(102, expectedError.errno);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_signCertificate() {
@ -577,7 +571,7 @@ add_task(async function test_signCertificate() {
Assert.equal(102, expectedError.errno);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_accountExists() {
@ -636,7 +630,7 @@ add_task(async function test_accountExists() {
Assert.equal(unexpectedError.code, 500);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_registerDevice() {
@ -689,7 +683,7 @@ add_task(async function test_registerDevice() {
Assert.equal(unexpectedError.code, 500);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_updateDevice() {
@ -735,7 +729,7 @@ add_task(async function test_updateDevice() {
Assert.equal(unexpectedError.code, 500);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_signOutAndDestroyDevice() {
@ -777,7 +771,7 @@ add_task(async function test_signOutAndDestroyDevice() {
Assert.equal(unexpectedError.code, 500);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_getDeviceList() {
@ -810,7 +804,7 @@ add_task(async function test_getDeviceList() {
Assert.equal(unexpectedError.code, 500);
}
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_client_metrics() {
@ -843,7 +837,7 @@ add_task(async function test_client_metrics() {
return err.errno == 111;
});
await deferredStop(server);
await promiseStopServer(server);
});
add_task(async function test_email_case() {
@ -896,7 +890,7 @@ add_task(async function test_email_case() {
Assert.equal(result.areWeHappy, "yes");
Assert.equal(attempts, 2);
await deferredStop(server);
await promiseStopServer(server);
});
// turn formatted test vectors into normal hex strings

Просмотреть файл

@ -45,12 +45,6 @@ function startServer() {
return srv;
}
function promiseStopServer(server) {
return new Promise(resolve => {
server.stop(resolve);
});
}
add_task(async function getAndRevokeToken() {
Services.prefs.setBoolPref("identity.fxaccounts.allowHttp", true);
let server = startServer();

Просмотреть файл

@ -193,9 +193,9 @@ this.configureFxAccountIdentity = function(authService,
fxa.internal._fxAccountsClient = mockFxAClient;
let mockTSC = { // TokenServerClient
getTokenFromBrowserIDAssertion(uri, assertion, cb) {
async getTokenFromBrowserIDAssertion(uri, assertion) {
config.fxaccount.token.uid = config.username;
cb(null, config.fxaccount.token);
return config.fxaccount.token;
},
};
authService._fxaService = fxa;

Просмотреть файл

@ -608,20 +608,13 @@ this.BrowserIDManager.prototype = {
);
};
let getToken = assertion => {
let getToken = async (assertion) => {
log.debug("Getting a token");
let deferred = PromiseUtils.defer();
let cb = function(err, token) {
if (err) {
return deferred.reject(err);
}
log.debug("Successfully got a sync token");
return deferred.resolve(token);
};
let headers = {"X-Client-State": userData.kXCS};
client.getTokenFromBrowserIDAssertion(tokenServerURI, assertion, cb, headers);
return deferred.promise;
// Exceptions will be handled by the caller.
const token = await client.getTokenFromBrowserIDAssertion(tokenServerURI, assertion, headers);
log.debug("Successfully got a sync token");
return token;
};
let getAssertion = () => {

Просмотреть файл

@ -487,9 +487,6 @@ function promiseOneObserver(topic, callback) {
});
}
function promiseStopServer(server) {
return new Promise(resolve => server.stop(resolve));
}
// Avoid an issue where `client.name2` containing unicode characters causes
// a number of tests to fail, due to them assuming that we do not need to utf-8
// encode or decode data sent through the mocked server (see bug 1268912).

Просмотреть файл

@ -16,8 +16,7 @@ var fakeServer = new SyncServer();
fakeServer.start();
registerCleanupFunction(function() {
return new Promise(resolve => {
fakeServer.stop(resolve);
return promiseStopServer(fakeServer).finally(() => {
Svc.Prefs.resetBranch("");
});
});

Просмотреть файл

@ -16,9 +16,7 @@ var fakeServer = new SyncServer();
fakeServer.start();
registerCleanupFunction(function() {
return new Promise(resolve => {
fakeServer.stop(resolve);
});
return promiseStopServer(fakeServer);
});
var fakeServerUrl = "http://localhost:" + fakeServer.port;

Просмотреть файл

@ -71,20 +71,22 @@ function prepareServer(cbAfterTokenFetch) {
let numReassigns = 0;
return configureIdentity(config).then(() => {
Service.identity._tokenServerClient = {
getTokenFromBrowserIDAssertion(uri, assertion, cb) {
// Build a new URL with trailing zeros for the SYNC_VERSION part - this
// will still be seen as equivalent by the test server, but different
// by sync itself.
numReassigns += 1;
let trailingZeros = new Array(numReassigns + 1).join("0");
let token = config.fxaccount.token;
token.endpoint = server.baseURI + "1.1" + trailingZeros + "/johndoe";
token.uid = config.username;
numTokenRequests += 1;
cb(null, token);
if (cbAfterTokenFetch) {
cbAfterTokenFetch();
}
getTokenFromBrowserIDAssertion(uri, assertion) {
return new Promise(res => {
// Build a new URL with trailing zeros for the SYNC_VERSION part - this
// will still be seen as equivalent by the test server, but different
// by sync itself.
numReassigns += 1;
let trailingZeros = new Array(numReassigns + 1).join("0");
let token = config.fxaccount.token;
token.endpoint = server.baseURI + "1.1" + trailingZeros + "/johndoe";
token.uid = config.username;
numTokenRequests += 1;
res(token);
if (cbAfterTokenFetch) {
cbAfterTokenFetch();
}
});
},
};
return server;

Просмотреть файл

@ -78,11 +78,9 @@ async function syncAndExpectNodeReassignment(server, firstNotification, between,
let getTokenCount = 0;
let mockTSC = { // TokenServerClient
getTokenFromBrowserIDAssertion(uri, assertion, cb) {
async getTokenFromBrowserIDAssertion(uri, assertion) {
getTokenCount++;
cb(null, {
endpoint: server.baseURI + "1.1/johndoe/"
});
return {endpoint: server.baseURI + "1.1/johndoe/"};
},
};
Service.identity._tokenServerClient = mockTSC;
@ -280,11 +278,9 @@ add_task(async function test_loop_avoidance_storage() {
let getTokenCount = 0;
let mockTSC = { // TokenServerClient
getTokenFromBrowserIDAssertion(uri, assertion, cb) {
async getTokenFromBrowserIDAssertion(uri, assertion) {
getTokenCount++;
cb(null, {
endpoint: server.baseURI + "1.1/johndoe/"
});
return {endpoint: server.baseURI + "1.1/johndoe/"};
},
};
Service.identity._tokenServerClient = mockTSC;
@ -376,11 +372,9 @@ add_task(async function test_loop_avoidance_engine() {
let getTokenCount = 0;
let mockTSC = { // TokenServerClient
getTokenFromBrowserIDAssertion(uri, assertion, cb) {
getTokenFromBrowserIDAssertion(uri, assertion) {
getTokenCount++;
cb(null, {
endpoint: server.baseURI + "1.1/johndoe/"
});
return {endpoint: server.baseURI + "1.1/johndoe/"};
},
};
Service.identity._tokenServerClient = mockTSC;