~ubuntu-branches/ubuntu/trusty/enigmail/trusty-security

« back to all changes in this revision

Viewing changes to services/sync/tests/unit/test_service_verifyLogin.js

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2011-08-12 12:25:06 UTC
  • mfrom: (0.12.4 upstream)
  • Revision ID: package-import@ubuntu.com-20110812122506-zko6c7zfexvyg71q
Tags: 2:1.2.1-0ubuntu1
* New upstream release
* Drop fix_install_rdf_xml_errors.diff - fixed upstream
* Refresh port_to_latest_thunderbird.diff
* Add a proper get-orig-source target which pulls the build system from
  lp:~mozillateam/mozilla-build-system/beta, now that we don't have the old
  build-system.tar.gz from xulrunner

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Cu.import("resource://services-sync/constants.js");
2
 
Cu.import("resource://services-sync/log4moz.js");
3
 
Cu.import("resource://services-sync/service.js");
4
 
Cu.import("resource://services-sync/status.js");
5
 
Cu.import("resource://services-sync/util.js");
6
 
 
7
 
function login_handler(request, response) {
8
 
  // btoa('johndoe:ilovejane') == am9obmRvZTppbG92ZWphbmU=
9
 
  let body;
10
 
  if (request.hasHeader("Authorization") &&
11
 
      request.getHeader("Authorization") == "Basic am9obmRvZTppbG92ZWphbmU=") {
12
 
    body = "{}";
13
 
    response.setStatusLine(request.httpVersion, 200, "OK");
14
 
  } else {
15
 
    body = "Unauthorized";
16
 
    response.setStatusLine(request.httpVersion, 401, "Unauthorized");
17
 
  }
18
 
  response.bodyOutputStream.write(body, body.length);
19
 
}
20
 
 
21
 
function service_unavailable(request, response) {
22
 
  let body = "Service Unavailable";
23
 
  response.setStatusLine(request.httpVersion, 503, "Service Unavailable");
24
 
  response.setHeader("Retry-After", "42");
25
 
  response.bodyOutputStream.write(body, body.length);
26
 
}
27
 
 
28
 
function run_test() {
29
 
  let logger = Log4Moz.repository.rootLogger;
30
 
  Log4Moz.repository.rootLogger.addAppender(new Log4Moz.DumpAppender());
31
 
 
32
 
  // This test expects a clean slate -- no saved passphrase.
33
 
  Weave.Svc.Login.removeAllLogins();
34
 
  
35
 
  do_test_pending();
36
 
  let server = httpd_setup({
37
 
    "/api/1.1/johndoe/info/collections": login_handler,
38
 
    "/api/1.1/janedoe/info/collections": service_unavailable,
39
 
    "/api/1.1/johndoe/storage/meta/global": new ServerWBO().handler(),
40
 
    "/api/1.1/johndoe/storage/crypto/keys": new ServerWBO().handler(),
41
 
    "/user/1.0/johndoe/node/weave": httpd_handler(200, "OK", "http://localhost:8080/api/")
42
 
  });
43
 
 
44
 
  try {
45
 
    Service.serverURL = "http://localhost:8080/";
46
 
 
47
 
    _("Force the initial state.");
48
 
    Status.service = STATUS_OK;
49
 
    do_check_eq(Status.service, STATUS_OK);
50
 
 
51
 
    _("Credentials won't check out because we're not configured yet.");
52
 
    Status.resetSync();
53
 
    do_check_false(Service.verifyLogin());
54
 
    do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
55
 
    do_check_eq(Status.login, LOGIN_FAILED_NO_USERNAME);
56
 
 
57
 
    _("Try again with username and password set.");
58
 
    Status.resetSync();
59
 
    Service.username = "johndoe";
60
 
    Service.password = "ilovejane";
61
 
    do_check_false(Service.verifyLogin());
62
 
    do_check_eq(Status.service, CLIENT_NOT_CONFIGURED);
63
 
    do_check_eq(Status.login, LOGIN_FAILED_NO_PASSPHRASE);
64
 
 
65
 
    _("verifyLogin() has found out the user's cluster URL, though.");
66
 
    do_check_eq(Service.clusterURL, "http://localhost:8080/api/");
67
 
 
68
 
    _("Success if passphrase is set.");
69
 
    Status.resetSync();
70
 
    Service.passphrase = "foo";
71
 
    do_check_true(Service.verifyLogin());
72
 
    do_check_eq(Status.service, STATUS_OK);
73
 
    do_check_eq(Status.login, LOGIN_SUCCEEDED);
74
 
 
75
 
    _("If verifyLogin() encounters a server error, it flips on the backoff flag and notifies observers on a 503 with Retry-After.");
76
 
    Status.resetSync();
77
 
    Service.username = "janedoe";
78
 
    do_check_false(Status.enforceBackoff);
79
 
    let backoffInterval;    
80
 
    Svc.Obs.add("weave:service:backoff:interval", function(subject, data) {
81
 
      backoffInterval = subject;
82
 
    });
83
 
    do_check_false(Service.verifyLogin());
84
 
    do_check_true(Status.enforceBackoff);
85
 
    do_check_eq(backoffInterval, 42);
86
 
    do_check_eq(Status.service, LOGIN_FAILED);
87
 
    do_check_eq(Status.login, LOGIN_FAILED_SERVER_ERROR);
88
 
 
89
 
    _("Ensure a network error when finding the cluster sets the right Status bits.");
90
 
    Status.resetSync();
91
 
    Service.serverURL = "http://localhost:12345/";
92
 
    do_check_false(Service.verifyLogin());
93
 
    do_check_eq(Status.service, LOGIN_FAILED);
94
 
    do_check_eq(Status.login, LOGIN_FAILED_NETWORK_ERROR);
95
 
 
96
 
    _("Ensure a network error when getting the collection info sets the right Status bits.");
97
 
    Status.resetSync();
98
 
    Service.clusterURL = "http://localhost:12345/";
99
 
    do_check_false(Service.verifyLogin());
100
 
    do_check_eq(Status.service, LOGIN_FAILED);
101
 
    do_check_eq(Status.login, LOGIN_FAILED_NETWORK_ERROR);
102
 
 
103
 
  } finally {
104
 
    Svc.Prefs.resetBranch("");
105
 
    server.stop(do_test_finished);
106
 
  }
107
 
}