Bug 1090529 - Add lack-of-valid-key error handling as Service Unavailable error. r=felipe

This commit is contained in:
Iaroslav Sheptykin 2014-11-10 10:29:30 +01:00
parent d5a0a90008
commit 9001864cc9
3 changed files with 128 additions and 36 deletions

View File

@ -94,6 +94,15 @@ this.BingTranslator.prototype = {
}.bind(this));
},
/**
* Resets the expiration time of the current token, in order to
* force the token manager to ask for a new token during the next request.
*/
_resetToken : function() {
// Force the token manager to get update token
BingTokenManager._currentExpiryTime = 0;
},
/**
* Function called when a request sent to the server completed successfully.
* This function handles calling the function to parse the result and the
@ -115,17 +124,19 @@ this.BingTranslator.prototype = {
/**
* Function called when a request sent to the server has failed.
* This function handles deciding if the error is transient or means the
* service is unavailable (zero balance on the key) and calling the
* function to resolve the promise returned by the public `translate()`
* method when there's no pending request left.
* service is unavailable (zero balance on the key or request credentials are
* not in an active state) and calling the function to resolve the promise
* returned by the public `translate()` method when there's no pending.
* request left.
*
* @param aError [optional] The RESTRequest that failed.
*/
_chunkFailed: function(aError) {
if (aError instanceof RESTRequest &&
aError.response.status == 400) {
[400, 401].indexOf(aError.response.status) != -1) {
let body = aError.response.body;
if (body.contains("TranslateApiException") && body.contains("balance"))
if (body.contains("TranslateApiException") &&
(body.contains("balance") || body.contains("active state")))
this._serviceUnavailable = true;
}

View File

@ -128,6 +128,13 @@ function checkAuth(req) {
let auth = req.getHeader("Authorization");
if (!auth.startsWith("Bearer "))
throw new HTTPError(401, "Invalid Authorization header content: '" + auth + "'");
// Rejecting inactive subscriptions.
if (auth.contains("inactive")) {
const INACTIVE_STATE_RESPONSE = "<html><body><h1>TranslateApiException</h1><p>Method: TranslateArray()</p><p>Message: The Azure Market Place Translator Subscription associated with the request credentials is not in an active state.</p><code></code><p>message id=5641.V2_Rest.TranslateArray.48CC6470</p></body></html>";
throw new HTTPError(401, INACTIVE_STATE_RESPONSE);
}
}
function reallyHandleRequest(req, res) {
@ -189,8 +196,17 @@ const methodHandlers = {
return;
}
// Defines the tokens for certain client ids.
const TOKEN_MAP = {
'testInactive' : 'inactive',
'testClient' : 'test'
};
let token = 'test'; // Default token.
if((params.client_id in TOKEN_MAP)){
token = TOKEN_MAP[params.client_id];
}
let content = JSON.stringify({
access_token: "test",
access_token: token,
expires_in: 600
});

View File

@ -11,46 +11,111 @@ const kClientSecretPref = "browser.translation.bing.apiKeyOverride";
const {BingTranslator} = Cu.import("resource:///modules/translation/BingTranslator.jsm", {});
const {TranslationDocument} = Cu.import("resource:///modules/translation/TranslationDocument.jsm", {});
const {Promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
function test() {
waitForExplicitFinish();
add_task(function* setup() {
Services.prefs.setCharPref(kClientIdPref, "testClient");
Services.prefs.setCharPref(kClientSecretPref, "testSecret");
registerCleanupFunction(function () {
Services.prefs.clearUserPref(kClientIdPref);
Services.prefs.clearUserPref(kClientSecretPref);
});
});
/**
* Checks if the translation is happening.
*/
add_task(function* test_bing_translation() {
// Ensure the correct client id is used for authentication.
Services.prefs.setCharPref(kClientIdPref, "testClient");
// Loading the fixture page.
let url = constructFixtureURL("bug1022725-fr.html");
let tab = yield promiseTestPageLoad(url);
// Translating the contents of the loaded tab.
gBrowser.selectedTab = tab;
let browser = tab.linkedBrowser;
let client = new BingTranslator(
new TranslationDocument(browser.contentDocument), "fr", "en");
let result = yield client.translate();
// XXXmikedeboer; here you would continue the test/ content inspection.
Assert.ok(result, "There should be a result.");
gBrowser.removeTab(tab);
});
/**
* Ensures that the BingTranslator handles out-of-valid-key response
* correctly. Sometimes Bing Translate replies with
* "request credentials is not in an active state" error. BingTranslator
* should catch this error and classify it as Service Unavailable.
*
*/
add_task(function* test_handling_out_of_valid_key_error() {
// Simulating request from inactive subscription.
Services.prefs.setCharPref(kClientIdPref, "testInactive");
// Loading the fixture page.
let url = constructFixtureURL("bug1022725-fr.html");
let tab = yield promiseTestPageLoad(url);
// Translating the contents of the loaded tab.
gBrowser.selectedTab = tab;
let browser = tab.linkedBrowser;
let client = new BingTranslator(
new TranslationDocument(browser.contentDocument), "fr", "en");
client._resetToken();
try {
yield client.translate();
} catch (ex) {
// It is alright that the translation fails.
}
client._resetToken();
// Checking if the client detected service and unavailable.
Assert.ok(client._serviceUnavailable, "Service should be detected unavailable.");
// Cleaning up.
Services.prefs.setCharPref(kClientIdPref, "testClient");
gBrowser.removeTab(tab);
});
/**
* A helper function for constructing a URL to a page stored in the
* local fixture folder.
*
* @param filename Name of a fixture file.
*/
function constructFixtureURL(filename){
// Deduce the Mochitest server address in use from a pref that was pre-processed.
let server = Services.prefs.getCharPref("browser.translation.bing.authURL")
.replace("http://", "");
server = server.substr(0, server.indexOf("/"));
let tab = gBrowser.addTab("http://" + server +
"/browser/browser/components/translation/test/fixtures/bug1022725-fr.html");
gBrowser.selectedTab = tab;
let url = "http://" + server +
"/browser/browser/components/translation/test/fixtures/" + filename;
return url;
}
registerCleanupFunction(function () {
gBrowser.removeTab(tab);
Services.prefs.clearUserPref(kClientIdPref);
Services.prefs.clearUserPref(kClientSecretPref);
});
let browser = tab.linkedBrowser;
browser.addEventListener("load", function onload() {
/**
* A helper function to open a new tab and wait for its content to load.
*
* @param String url A URL to be loaded in the new tab.
*/
function promiseTestPageLoad(url) {
let deferred = Promise.defer();
let tab = gBrowser.selectedTab = gBrowser.addTab(url);
let browser = gBrowser.selectedBrowser;
browser.addEventListener("load", function listener() {
if (browser.currentURI.spec == "about:blank")
return;
browser.removeEventListener("load", onload, true);
let client = new BingTranslator(
new TranslationDocument(browser.contentDocument), "fr", "en");
client.translate().then(
result => {
// XXXmikedeboer; here you would continue the test/ content inspection.
ok(result, "There should be a result.");
finish();
},
error => {
ok(false, "Unexpected Client Error: " + error);
finish();
}
);
info("Page loaded: " + browser.currentURI.spec);
browser.removeEventListener("load", listener, true);
deferred.resolve(tab);
}, true);
return deferred.promise;
}