summaryrefslogtreecommitdiffstats
path: root/b2g/components/test/unit/test_signintowebsite.js
blob: 38d4fa79e25d1fcf165bb4bb5aaadc1d7dbefbda (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests for b2g/components/SignInToWebsite.jsm

"use strict";

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

XPCOMUtils.defineLazyModuleGetter(this, "SignInToWebsiteController",
                                  "resource://gre/modules/SignInToWebsite.jsm",
                                  "SignInToWebsiteController");

Cu.import("resource://gre/modules/identity/LogUtils.jsm");

function log(...aMessageArgs) {
  Logger.log.apply(Logger, ["test_signintowebsite"].concat(aMessageArgs));
}

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

function objectContains(object, subset) {
  let objectKeys = Object.keys(object);
  let subsetKeys = Object.keys(subset);

  // can't have fewer keys than the subset
  if (objectKeys.length < subsetKeys.length) {
    return false;
  }

  let key;
  let success = true;
  if (subsetKeys.length > 0) {
    for (let i=0; i<subsetKeys.length; i++) {
      key = subsetKeys[i];

      // key exists in the source object
      if (typeof object[key] === 'undefined') {
        success = false;
        break;
      }

      // recursively check object values
      else if (typeof subset[key] === 'object') {
        if (typeof object[key] !== 'object') {
          success = false;
          break;
        }
        if (! objectContains(object[key], subset[key])) {
          success = false;
          break;
        }
      }

      else if (object[key] !== subset[key]) {
        success = false;
        break;
      }
    }
  }

  return success;
}

function test_object_contains() {
  do_test_pending();

  let someObj = {
    pies: 42,
    green: "spam",
    flan: {yes: "please"}
  };
  let otherObj = {
    pies: 42,
    flan: {yes: "please"}
  };
  do_check_true(objectContains(someObj, otherObj));
  do_test_finished();
  run_next_test();
}

function test_mock_doc() {
  do_test_pending();
  let mockedDoc = mockDoc({loggedInUser: null}, function(action, params) {
    do_check_eq(action, 'coffee');
    do_test_finished();
    run_next_test();
  });

  // A smoke test to ensure that mockedDoc is functioning correctly.
  // There is presently no doCoffee method in Persona.
  mockedDoc.doCoffee();
}

function test_watch() {
  do_test_pending();

  setup_test_identity("pie@food.gov", TEST_CERT, function() {
    let controller = SignInToWebsiteController;

    let mockedDoc = mockDoc({loggedInUser: null}, function(action, params) {
      do_check_eq(action, 'ready');
      controller.uninit();
      MinimalIDService.RP.unwatch(mockedDoc.id);
      do_test_finished();
      run_next_test();
    });

    controller.init({pipe: mockReceivingPipe()});
    MinimalIDService.RP.watch(mockedDoc, {});
  });
}

function test_request_login() {
  do_test_pending();

  setup_test_identity("flan@food.gov", TEST_CERT, function() {
    let controller = SignInToWebsiteController;

    let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially(
      function(action, params) {
        do_check_eq(action, 'ready');
        do_check_eq(params, undefined);
      },
      function(action, params) {
        do_check_eq(action, 'login');
        do_check_eq(params, TEST_CERT);
        controller.uninit();
        MinimalIDService.RP.unwatch(mockedDoc.id);
        do_test_finished();
        run_next_test();
      }
    ));

    controller.init({pipe: mockReceivingPipe()});
    MinimalIDService.RP.watch(mockedDoc, {});
    MinimalIDService.RP.request(mockedDoc.id, {});
  });
}

function test_request_logout() {
  do_test_pending();

  setup_test_identity("flan@food.gov", TEST_CERT, function() {
    let controller = SignInToWebsiteController;

    let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially(
      function(action, params) {
        do_check_eq(action, 'ready');
        do_check_eq(params, undefined);
      },
      function(action, params) {
        do_check_eq(action, 'logout');
        do_check_eq(params, undefined);
        controller.uninit();
        MinimalIDService.RP.unwatch(mockedDoc.id);
        do_test_finished();
        run_next_test();
      }
    ));

    controller.init({pipe: mockReceivingPipe()});
    MinimalIDService.RP.watch(mockedDoc, {});
    MinimalIDService.RP.logout(mockedDoc.id, {});
  });
}

function test_request_login_logout() {
  do_test_pending();

  setup_test_identity("unagi@food.gov", TEST_CERT, function() {
    let controller = SignInToWebsiteController;

    let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially(
      function(action, params) {
        do_check_eq(action, 'ready');
        do_check_eq(params, undefined);
      },
      function(action, params) {
        do_check_eq(action, 'login');
        do_check_eq(params, TEST_CERT);
      },
      function(action, params) {
        do_check_eq(action, 'logout');
        do_check_eq(params, undefined);
        controller.uninit();
        MinimalIDService.RP.unwatch(mockedDoc.id);
        do_test_finished();
        run_next_test();
      }
    ));

    controller.init({pipe: mockReceivingPipe()});
    MinimalIDService.RP.watch(mockedDoc, {});
    MinimalIDService.RP.request(mockedDoc.id, {});
    MinimalIDService.RP.logout(mockedDoc.id, {});
  });
}

function test_logout_everywhere() {
  do_test_pending();
  let logouts = 0;

  setup_test_identity("fugu@food.gov", TEST_CERT, function() {
    let controller = SignInToWebsiteController;

    let mockedDoc1 = mockDoc({loggedInUser: null}, call_sequentially(
      function(action, params) {
        do_check_eq(action, 'ready');
      },
      function(action, params) {
        do_check_eq(action, 'login');
      },
      function(action, params) {
        // Result of logout from doc2.
        // We don't know what order the logouts will occur in.
        do_check_eq(action, 'logout');
        if (++logouts === 2) {
          do_test_finished();
          run_next_test();
        }
      }
    ));

    let mockedDoc2 = mockDoc({loggedInUser: null}, call_sequentially(
      function(action, params) {
        do_check_eq(action, 'ready');
      },
      function(action, params) {
        do_check_eq(action, 'login');
      },
      function(action, params) {
        do_check_eq(action, 'logout');
        if (++logouts === 2) {
          do_test_finished();
          run_next_test();
        }
      }
    ));

    controller.init({pipe: mockReceivingPipe()});
    MinimalIDService.RP.watch(mockedDoc1, {});
    MinimalIDService.RP.request(mockedDoc1.id, {});

    MinimalIDService.RP.watch(mockedDoc2, {});
    MinimalIDService.RP.request(mockedDoc2.id, {});

    // Logs out of both docs because they share the
    // same origin.
    MinimalIDService.RP.logout(mockedDoc2.id, {});
  });
}

function test_options_pass_through() {
  do_test_pending();

  // An meaningless structure for testing that RP messages preserve
  // objects and their parameters as they are passed back and forth.
  let randomMixedParams = {
    loggedInUser: "juanita@mozilla.com",
    forceAuthentication: true,
    forceIssuer: "foo.com",
    someThing: {
      name: "Pertelote",
      legs: 4,
      nested: {bee: "Eric", remaining: "1/2"}
      }
    };

  let mockedDoc = mockDoc(randomMixedParams, function(action, params) {});

  function pipeOtherEnd(rpOptions, gaiaOptions) {
    // Ensure that every time we receive a message, our mixed
    // random params are contained in that message
    do_check_true(objectContains(rpOptions, randomMixedParams));

    switch (gaiaOptions.message) {
      case "identity-delegate-watch":
        MinimalIDService.RP.request(mockedDoc.id, {});
        break;
      case "identity-delegate-request":
        MinimalIDService.RP.logout(mockedDoc.id, {});
        break;
      case "identity-delegate-logout":
        do_test_finished();
        controller.uninit();
        MinimalIDService.RP.unwatch(mockedDoc.id);
        run_next_test();
        break;
    }
  }

  let controller = SignInToWebsiteController;
  controller.init({pipe: mockSendingPipe(pipeOtherEnd)});

  MinimalIDService.RP.watch(mockedDoc, {});
}

var TESTS = [
  test_overall,
  test_mock_doc,
  test_object_contains,

  test_watch,
  test_request_login,
  test_request_logout,
  test_request_login_logout,
  test_logout_everywhere,

  test_options_pass_through
];

TESTS.forEach(add_test);

function run_test() {
  run_next_test();
}