summaryrefslogtreecommitdiffstats
path: root/toolkit/identity/tests/unit/test_firefox_accounts.js
blob: c0c63deb6ff247a700a9d0feb7f8e07ea8201486 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

Cu.import("resource://gre/modules/Promise.jsm");
Cu.import("resource://gre/modules/DOMIdentity.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "FirefoxAccounts",
                                  "resource://gre/modules/identity/FirefoxAccounts.jsm");

// Make the profile dir available; this is necessary so that
// services/fxaccounts/FxAccounts.jsm can read and write its signed-in user
// data.
do_get_profile();

function MockFXAManager() {
  this.signedInUser = true;
}
MockFXAManager.prototype = {
  getAssertion: function(audience) {
    let result = this.signedInUser ? TEST_ASSERTION : null;
    return Promise.resolve(result);
  },

  signOut: function() {
    this.signedInUser = false;
    return Promise.resolve(null);
  },

  signIn: function(user) {
    this.signedInUser = user;
    return Promise.resolve(user);
  },
}

var originalManager = FirefoxAccounts.fxAccountsManager;
FirefoxAccounts.fxAccountsManager = new MockFXAManager();
do_register_cleanup(() => {
  log("restoring fxaccountsmanager");
  FirefoxAccounts.fxAccountsManager = originalManager;
});

function withNobodySignedIn() {
  return FirefoxAccounts.fxAccountsManager.signOut();
}

function withSomebodySignedIn() {
  return FirefoxAccounts.fxAccountsManager.signIn('Pertelote');
}

function test_overall() {
  do_check_neq(FirefoxAccounts, null);
  run_next_test();
}

function test_mock() {
  do_test_pending();

  withSomebodySignedIn().then(() => {
    FirefoxAccounts.fxAccountsManager.getAssertion().then(assertion => {
      do_check_eq(assertion, TEST_ASSERTION);
      do_test_finished();
      run_next_test();
    });
  });
}

function test_watch_signed_in() {
  do_test_pending();

  let received = [];

  let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, data) {
    received.push([method, data]);

    if (method == "ready") {
      // confirm that we were signed in and then ready was called
      do_check_eq(received.length, 2);
      do_check_eq(received[0][0], "login");
      do_check_eq(received[0][1], TEST_ASSERTION);
      do_check_eq(received[1][0], "ready");
      do_test_finished();
      run_next_test();
    }
  });

  withSomebodySignedIn().then(() => {
    FirefoxAccounts.RP.watch(mockedRP);
  });
}

function test_watch_signed_out() {
  do_test_pending();

  let received = [];

  let mockedRP = mock_fxa_rp(null, TEST_URL, function(method) {
    received.push(method);

    if (method == "ready") {
      // confirm that we were signed out and then ready was called
      do_check_eq(received.length, 2);
      do_check_eq(received[0], "logout");
      do_check_eq(received[1], "ready");

      do_test_finished();
      run_next_test();
    }
  });

  withNobodySignedIn().then(() => {
    FirefoxAccounts.RP.watch(mockedRP);
  });
}

function test_request() {
  do_test_pending();

  let received = [];

  let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, data) {
    received.push([method, data]);

    // On watch(), we are signed out.  Then we call request().
    if (received.length === 2) {
      do_check_eq(received[0][0], "logout");
      do_check_eq(received[1][0], "ready");

      // Pretend request() showed ux and the user signed in
      withSomebodySignedIn().then(() => {
        FirefoxAccounts.RP.request(mockedRP.id);
      });
    }

    if (received.length === 3) {
      do_check_eq(received[2][0], "login");
      do_check_eq(received[2][1], TEST_ASSERTION);

      do_test_finished();
      run_next_test();
    }
  });

  // First, call watch() with nobody signed in
  withNobodySignedIn().then(() => {
    FirefoxAccounts.RP.watch(mockedRP);
  });
}

function test_logout() {
  do_test_pending();

  let received = [];

  let mockedRP = mock_fxa_rp(null, TEST_URL, function(method) {
    received.push(method);

    // At first, watch() signs us in automatically.  Then we sign out.
    if (received.length === 2) {
      do_check_eq(received[0], "login");
      do_check_eq(received[1], "ready");

      FirefoxAccounts.RP.logout(mockedRP.id);
    }

    if (received.length === 3) {
      do_check_eq(received[2], "logout");
      do_test_finished();
      run_next_test();
    }
  });

  // First, call watch()
  withSomebodySignedIn().then(() => {
    FirefoxAccounts.RP.watch(mockedRP);
  });
}

function test_error() {
  do_test_pending();

  let received = [];

  // Mock the fxAccountsManager so that getAssertion rejects its promise and
  // triggers our onerror handler.  (This is the method that's used internally
  // by FirefoxAccounts.RP.request().)
  let originalGetAssertion = FirefoxAccounts.fxAccountsManager.getAssertion;
  FirefoxAccounts.fxAccountsManager.getAssertion = function(audience) {
    return Promise.reject(new Error("barf!"));
  };

  let mockedRP = mock_fxa_rp(null, TEST_URL, function(method, message) {
    // We will immediately receive an error, due to watch()'s attempt
    // to getAssertion().
    do_check_eq(method, "error");
    do_check_true(/barf/.test(message));

    // Put things back the way they were
    FirefoxAccounts.fxAccountsManager.getAssertion = originalGetAssertion;

    do_test_finished();
    run_next_test();
  });

  // First, call watch()
  withSomebodySignedIn().then(() => {
    FirefoxAccounts.RP.watch(mockedRP);
  });
}

function test_child_process_shutdown() {
  do_test_pending();
  let rpCount = FirefoxAccounts.RP._rpFlows.size;

  makeObserver("identity-child-process-shutdown", (aTopic, aSubject, aData) => {
    // Last of all, the shutdown observer message will be fired.
    // This takes place after the RP has a chance to delete flows
    // and clean up.
    do_check_eq(FirefoxAccounts.RP._rpFlows.size, rpCount);
    do_test_finished();
    run_next_test();
  });

  let mockedRP = mock_fxa_rp(null, TEST_URL, (method) => {
    // We should enter this function for 'ready' and 'child-process-shutdown'.
    // After we have a chance to do our thing, the shutdown observer message
    // will fire and be caught by the function above.
    do_check_eq(FirefoxAccounts.RP._rpFlows.size, rpCount + 1);
    switch (method) {
      case "ready":
        DOMIdentity._childProcessShutdown("my message manager");
        break;

      case "child-process-shutdown":
        // We have to call this explicitly because there's no real
        // dom window here.
        FirefoxAccounts.RP.childProcessShutdown(mockedRP._mm);
        break;

      default:
        break;
    }
  });

  mockedRP._mm = "my message manager";
  withSomebodySignedIn().then(() => {
    FirefoxAccounts.RP.watch(mockedRP);
  });

  // fake a dom window context
  DOMIdentity.newContext(mockedRP, mockedRP._mm);
}

var TESTS = [
  test_overall,
  test_mock,
  test_watch_signed_in,
  test_watch_signed_out,
  test_request,
  test_logout,
  test_error,
  test_child_process_shutdown,
];

TESTS.forEach(add_test);

function run_test() {
  run_next_test();
}