services/sync/tests/unit/test_service_sync_401.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 Cu.import("resource://services-sync/constants.js");
michael@0 5 Cu.import("resource://services-sync/policies.js");
michael@0 6 Cu.import("resource://services-sync/service.js");
michael@0 7 Cu.import("resource://services-sync/util.js");
michael@0 8 Cu.import("resource://testing-common/services/sync/utils.js");
michael@0 9
michael@0 10 function login_handling(handler) {
michael@0 11 return function (request, response) {
michael@0 12 if (basic_auth_matches(request, "johndoe", "ilovejane")) {
michael@0 13 handler(request, response);
michael@0 14 } else {
michael@0 15 let body = "Unauthorized";
michael@0 16 response.setStatusLine(request.httpVersion, 401, "Unauthorized");
michael@0 17 response.bodyOutputStream.write(body, body.length);
michael@0 18 }
michael@0 19 };
michael@0 20 }
michael@0 21
michael@0 22 function run_test() {
michael@0 23 let logger = Log.repository.rootLogger;
michael@0 24 Log.repository.rootLogger.addAppender(new Log.DumpAppender());
michael@0 25
michael@0 26 let collectionsHelper = track_collections_helper();
michael@0 27 let upd = collectionsHelper.with_updated_collection;
michael@0 28 let collections = collectionsHelper.collections;
michael@0 29
michael@0 30 do_test_pending();
michael@0 31 let server = httpd_setup({
michael@0 32 "/1.1/johndoe/storage/crypto/keys": upd("crypto", new ServerWBO("keys").handler()),
michael@0 33 "/1.1/johndoe/storage/meta/global": upd("meta", new ServerWBO("global").handler()),
michael@0 34 "/1.1/johndoe/info/collections": login_handling(collectionsHelper.handler)
michael@0 35 });
michael@0 36
michael@0 37 const GLOBAL_SCORE = 42;
michael@0 38
michael@0 39 try {
michael@0 40 _("Set up test fixtures.");
michael@0 41 new SyncTestingInfrastructure(server, "johndoe", "ilovejane", "foo");
michael@0 42 Service.scheduler.globalScore = GLOBAL_SCORE;
michael@0 43 // Avoid daily ping
michael@0 44 Svc.Prefs.set("lastPing", Math.floor(Date.now() / 1000));
michael@0 45
michael@0 46 let threw = false;
michael@0 47 Svc.Obs.add("weave:service:sync:error", function (subject, data) {
michael@0 48 threw = true;
michael@0 49 });
michael@0 50
michael@0 51 _("Initial state: We're successfully logged in.");
michael@0 52 Service.login();
michael@0 53 do_check_true(Service.isLoggedIn);
michael@0 54 do_check_eq(Service.status.login, LOGIN_SUCCEEDED);
michael@0 55
michael@0 56 _("Simulate having changed the password somewhere else.");
michael@0 57 Service.identity.basicPassword = "ilovejosephine";
michael@0 58
michael@0 59 _("Let's try to sync.");
michael@0 60 Service.sync();
michael@0 61
michael@0 62 _("Verify that sync() threw an exception.");
michael@0 63 do_check_true(threw);
michael@0 64
michael@0 65 _("We're no longer logged in.");
michael@0 66 do_check_false(Service.isLoggedIn);
michael@0 67
michael@0 68 _("Sync status won't have changed yet, because we haven't tried again.");
michael@0 69
michael@0 70 _("globalScore is reset upon starting a sync.");
michael@0 71 do_check_eq(Service.scheduler.globalScore, 0);
michael@0 72
michael@0 73 _("Our next sync will fail appropriately.");
michael@0 74 try {
michael@0 75 Service.sync();
michael@0 76 } catch (ex) {
michael@0 77 }
michael@0 78 do_check_eq(Service.status.login, LOGIN_FAILED_LOGIN_REJECTED);
michael@0 79
michael@0 80 } finally {
michael@0 81 Svc.Prefs.resetBranch("");
michael@0 82 server.stop(do_test_finished);
michael@0 83 }
michael@0 84 }

mercurial