services/sync/tests/unit/test_service_getStorageInfo.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 Cu.import("resource://services-common/rest.js");
     5 Cu.import("resource://services-sync/constants.js");
     6 Cu.import("resource://services-sync/service.js");
     7 Cu.import("resource://services-sync/util.js");
     8 Cu.import("resource://testing-common/services/sync/utils.js");
    10 let collections = {steam:  65.11328,
    11                    petrol: 82.488281,
    12                    diesel: 2.25488281};
    14 function run_test() {
    15   Log.repository.getLogger("Sync.Service").level = Log.Level.Trace;
    16   Log.repository.getLogger("Sync.StorageRequest").level = Log.Level.Trace;
    17   initTestLogging();
    19   ensureLegacyIdentityManager();
    20   setBasicCredentials("johndoe", "ilovejane");
    22   run_next_test();
    23 }
    25 add_test(function test_success() {
    26   let handler = httpd_handler(200, "OK", JSON.stringify(collections));
    27   let server = httpd_setup({"/1.1/johndoe/info/collections": handler});
    28   Service.serverURL = server.baseURI + "/";
    29   Service.clusterURL = server.baseURI + "/";
    31   let request = Service.getStorageInfo("collections", function (error, info) {
    32     do_check_eq(error, null);
    33     do_check_true(Utils.deepEquals(info, collections));
    35     // Ensure that the request is sent off with the right bits.
    36     do_check_true(basic_auth_matches(handler.request,
    37                                      Service.identity.username,
    38                                      Service.identity.basicPassword));
    39     let expectedUA = Services.appinfo.name + "/" + Services.appinfo.version +
    40                      " FxSync/" + WEAVE_VERSION + "." +
    41                      Services.appinfo.appBuildID + ".desktop";
    42     do_check_eq(handler.request.getHeader("User-Agent"), expectedUA);
    44     server.stop(run_next_test);
    45   });
    46   do_check_true(request instanceof RESTRequest);
    47 });
    49 add_test(function test_invalid_type() {
    50   do_check_throws(function () {
    51     Service.getStorageInfo("invalid", function (error, info) {
    52       do_throw("Shouldn't get here!");
    53     });
    54   });
    55   run_next_test();
    56 });
    58 add_test(function test_network_error() {
    59   Service.getStorageInfo(INFO_COLLECTIONS, function (error, info) {
    60     do_check_eq(error.result, Cr.NS_ERROR_CONNECTION_REFUSED);
    61     do_check_eq(info, null);
    62     run_next_test();
    63   });
    64 });
    66 add_test(function test_http_error() {
    67   let handler = httpd_handler(500, "Oh noez", "Something went wrong!");
    68   let server = httpd_setup({"/1.1/johndoe/info/collections": handler});
    69   Service.serverURL = server.baseURI + "/";
    70   Service.clusterURL = server.baseURI + "/";
    72   let request = Service.getStorageInfo(INFO_COLLECTIONS, function (error, info) {
    73     do_check_eq(error.status, 500);
    74     do_check_eq(info, null);
    75     server.stop(run_next_test);
    76   });
    77 });
    79 add_test(function test_invalid_json() {
    80   let handler = httpd_handler(200, "OK", "Invalid JSON");
    81   let server = httpd_setup({"/1.1/johndoe/info/collections": handler});
    82   Service.serverURL = server.baseURI + "/";
    83   Service.clusterURL = server.baseURI + "/";
    85   let request = Service.getStorageInfo(INFO_COLLECTIONS, function (error, info) {
    86     do_check_eq(error.name, "SyntaxError");
    87     do_check_eq(info, null);
    88     server.stop(run_next_test);
    89   });
    90 });

mercurial