summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_errorhandler_eol.js
blob: c8d2ff4be0c152f160db8e9a53124b19488768e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/status.js");
Cu.import("resource://services-sync/util.js");

Cu.import("resource://testing-common/services/sync/fakeservices.js");
Cu.import("resource://testing-common/services/sync/utils.js");

function baseHandler(eolCode, request, response, statusCode, status, body) {
  let alertBody = {
    code: eolCode,
    message: "Service is EOLed.",
    url: "http://getfirefox.com",
  };
  response.setHeader("X-Weave-Timestamp", "" + new_timestamp(), false);
  response.setHeader("X-Weave-Alert", "" + JSON.stringify(alertBody), false);
  response.setStatusLine(request.httpVersion, statusCode, status);
  response.bodyOutputStream.write(body, body.length);
}

function handler513(request, response) {
  let statusCode = 513;
  let status = "Upgrade Required";
  let body = "{}";
  baseHandler("hard-eol", request, response, statusCode, status, body);
}

function handler200(eolCode) {
  return function (request, response) {
    let statusCode = 200;
    let status = "OK";
    let body = "{\"meta\": 123456789010}";
    baseHandler(eolCode, request, response, statusCode, status, body);
  };
}

function sync_httpd_setup(infoHandler) {
  let handlers = {
    "/1.1/johndoe/info/collections": infoHandler,
  };
  return httpd_setup(handlers);
}

function* setUp(server) {
  yield configureIdentity({username: "johndoe"});
  Service.serverURL = server.baseURI + "/";
  Service.clusterURL = server.baseURI + "/";
  new FakeCryptoService();
}

function run_test() {
  run_next_test();
}

function do_check_soft_eol(eh, start) {
  // We subtract 1000 because the stored value is in second precision.
  do_check_true(eh.earliestNextAlert >= (start + eh.MINIMUM_ALERT_INTERVAL_MSEC - 1000));
  do_check_eq("soft-eol", eh.currentAlertMode);
}
function do_check_hard_eol(eh, start) {
  // We subtract 1000 because the stored value is in second precision.
  do_check_true(eh.earliestNextAlert >= (start + eh.MINIMUM_ALERT_INTERVAL_MSEC - 1000));
  do_check_eq("hard-eol", eh.currentAlertMode);
  do_check_true(Status.eol);
}

add_identity_test(this, function* test_200_hard() {
  let eh = Service.errorHandler;
  let start = Date.now();
  let server = sync_httpd_setup(handler200("hard-eol"));
  yield setUp(server);

  let deferred = Promise.defer();
  let obs = function (subject, topic, data) {
    Svc.Obs.remove("weave:eol", obs);
    do_check_eq("hard-eol", subject.code);
    do_check_hard_eol(eh, start);
    do_check_eq(Service.scheduler.eolInterval, Service.scheduler.syncInterval);
    eh.clearServerAlerts();
    server.stop(deferred.resolve);
  };

  Svc.Obs.add("weave:eol", obs);
  Service._fetchInfo();
  Service.scheduler.adjustSyncInterval();   // As if we failed or succeeded in syncing.
  yield deferred.promise;
});

add_identity_test(this, function* test_513_hard() {
  let eh = Service.errorHandler;
  let start = Date.now();
  let server = sync_httpd_setup(handler513);
  yield setUp(server);

  let deferred = Promise.defer();
  let obs = function (subject, topic, data) {
    Svc.Obs.remove("weave:eol", obs);
    do_check_eq("hard-eol", subject.code);
    do_check_hard_eol(eh, start);
    do_check_eq(Service.scheduler.eolInterval, Service.scheduler.syncInterval);
    eh.clearServerAlerts();
    server.stop(deferred.resolve);
  };

  Svc.Obs.add("weave:eol", obs);
  try {
    Service._fetchInfo();
    Service.scheduler.adjustSyncInterval();   // As if we failed or succeeded in syncing.
  } catch (ex) {
    // Because fetchInfo will fail on a 513.
  }
  yield deferred.promise;
});

add_identity_test(this, function* test_200_soft() {
  let eh = Service.errorHandler;
  let start = Date.now();
  let server = sync_httpd_setup(handler200("soft-eol"));
  yield setUp(server);

  let deferred = Promise.defer();
  let obs = function (subject, topic, data) {
    Svc.Obs.remove("weave:eol", obs);
    do_check_eq("soft-eol", subject.code);
    do_check_soft_eol(eh, start);
    do_check_eq(Service.scheduler.singleDeviceInterval, Service.scheduler.syncInterval);
    eh.clearServerAlerts();
    server.stop(deferred.resolve);
  };

  Svc.Obs.add("weave:eol", obs);
  Service._fetchInfo();
  Service.scheduler.adjustSyncInterval();   // As if we failed or succeeded in syncing.
  yield deferred.promise;
});