forked from firebase/firebaseui-web
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauthui_test.js
More file actions
777 lines (727 loc) · 26 KB
/
Copy pathauthui_test.js
File metadata and controls
777 lines (727 loc) · 26 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Tests for app.js
*/
goog.provide('firebaseui.auth.AuthUITest');
goog.require('firebaseui.auth.AuthUI');
goog.require('firebaseui.auth.CredentialHelper');
goog.require('firebaseui.auth.PendingEmailCredential');
goog.require('firebaseui.auth.log');
goog.require('firebaseui.auth.storage');
goog.require('firebaseui.auth.testing.FakeAppClient');
goog.require('firebaseui.auth.testing.FakeUtil');
goog.require('firebaseui.auth.widget.Config');
goog.require('firebaseui.auth.widget.dispatcher');
goog.require('firebaseui.auth.widget.handler');
goog.require('firebaseui.auth.widget.handler.common');
/** @suppress {extraRequire} Required for page navigation after form submission
* to work. */
goog.require('firebaseui.auth.widget.handler.handleCallback');
/** @suppress {extraRequire} Required for page navigation after form submission
* to work. */
goog.require('firebaseui.auth.widget.handler.handlePasswordSignIn');
/** @suppress {extraRequire} Required for page navigation after form submission
* to work. */
goog.require('firebaseui.auth.widget.handler.handleProviderSignIn');
goog.require('goog.Promise');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.dom.classlist');
goog.require('goog.testing.AsyncTestCase');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');
goog.setTestOnly('firebaseui.auth.aputhUITest');
var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
// Test application instances.
var app;
var app1;
var app2;
var app3;
// Test configuration objects.
var config1;
var config2;
var config3;
var config4;
// Test stubs for handlers and widget dispatcher.
var testStubs = new goog.testing.PropertyReplacer();
// Container elements for sign-in button and widget rendering.
var container1;
var container2;
var container3;
// Test util for goto utilities.
var testUtil;
// Firebase Auth test tokens.
var passwordIdToken1 = 'HEADER1.eyJhdWQiOiAiY2xpZW50X2lkIiwgImVtYWlsIjogInVz' +
'ZXJAZXhhbXBsZS5jb20iLCAiaXNzIjogMTQwNDYzMzQ0MiwgImV4cCI6IDE1MDQ2MzM0NDJ' +
'9.SIGNATURE1';
var passwordIdToken2 = 'HEADER2.eyJhdWQiOiAiY2xpZW50X2lkIiwgImVtYWlsIjogInVz' +
'ZXJAZXhhbXBsZS5jb20iLCAiaXNzIjogMTQwNDYzMzQ0MiwgImV4cCI6IDE1MDQ2MzM0NDJ' +
'9.SIGNATURE2';
var passwordIdToken3 = 'HEADER3.eyJhdWQiOiAiY2xpZW50X2lkIiwgImVtYWlsIjogInVz' +
'ZXJAZXhhbXBsZS5jb20iLCAiaXNzIjogMTQwNDYzMzQ0MiwgImV4cCI6IDE1MDQ2MzM0NDJ' +
'9.SIGNATURE3';
var testApp;
var testApp1;
var testApp2;
var testApp3;
var testAuth;
var testAuth1;
var testAuth2;
var testAuth3;
var firebase = {};
var options = {
'apiKey': 'API_KEY',
'authDomain': 'subdomain.firebaseapp.com'
};
/**
* @param {!Element} container The container element to check.
* @param {string} cssName The css class name to check for.
* Asserts the element provided has a child with the provided css name.
*/
function assertHasCssClass(container, cssName) {
var page = container.children[0];
assertTrue(goog.dom.classlist.contains(page, goog.getCssName(cssName)));
}
function setUp() {
// Used to initialize internal Auth instance.
firebase = {};
firebase.instances_ = {};
firebase.initializeApp = function(options, name) {
// Throw an error if a FirebaseApp already exists for the specified name.
var key = name || '[DEFAULT]';
if (firebase.instances_[key]) {
throw new Error('An app instance already exists for ' + key);
} else {
firebase.instances_[key] =
new firebaseui.auth.testing.FakeAppClient(options, name);
}
return firebase.instances_[key];
};
// On FirebaseApp deletion, confirm instance not already deleted and then
// remove it from firebase.instances_.
testStubs.replace(
firebaseui.auth.testing.FakeAppClient.prototype,
'delete',
function() {
// Already deleted.
if (!firebase.instances_[this['name']]) {
throw new Error('Instance ' + key + ' already deleted!');
}
delete firebase.instances_[this['name']];
return goog.Promise.resolve();
});
// Create all test elements and append to document.
container1 = goog.dom.createDom(goog.dom.TagName.DIV, {'id': 'element1'});
document.body.appendChild(container1);
container2 = goog.dom.createDom(goog.dom.TagName.DIV, {'id': 'element2'});
document.body.appendChild(container2);
container3 = goog.dom.createDom(goog.dom.TagName.DIV, {'id': 'element3'});
document.body.appendChild(container3);
// Record all handler functions and widget dispatch functions.
testStubs.set(
firebaseui.auth.widget.handler,
'startSignIn',
goog.testing.recordFunction());
testStubs.set(
firebaseui.auth.widget.dispatcher,
'dispatchOperation',
goog.testing.recordFunction());
// Simulate accountchooser.com loaded.
testStubs.set(
firebaseui.auth.widget.handler.common,
'loadAccountchooserJs',
function(app, callback, opt_forceUiShownCallback) {
callback();
});
// Install fake test utilities.
testUtil = new firebaseui.auth.testing.FakeUtil().install();
}
function tearDown() {
testApp = null;
testApp1 = null;
testApp2 = null;
testApp3 = null;
// Delete all application instances.
// Uninstall internal and external Auth instances.
if (app1) {
app1.getAuth().uninstall();
app1.getExternalAuth().uninstall();
app1.reset();
}
app1 = null;
if (app2) {
app2.getAuth().uninstall();
app2.getExternalAuth().uninstall();
app2.reset();
}
app2 = null;
if (app3) {
app3.getAuth().uninstall();
app3.getExternalAuth().uninstall();
app3.reset();
}
app3 = null;
// Reset internals.
firebaseui.auth.AuthUI.resetAllInternals();
// Clear all web storage.
window.localStorage.clear();
window.sessionStorage.clear();
// Remove all test containers from document.
goog.dom.removeNode(container1);
goog.dom.removeNode(container2);
goog.dom.removeNode(container3);
// Reset test stubs.
testStubs.reset();
testUtil.uninstall();
testAuth.uninstall();
if (testAuth1) {
testAuth1.uninstall();
}
testAuth2.uninstall();
testAuth3.uninstall();
if (app) {
app.getAuth().uninstall();
app.getExternalAuth().uninstall();
app.reset();
app = null;
}
}
/** Creates and installs all auth, app and AuthUI instances for tests. */
function createAndInstallTestInstances() {
// Create and install the developer provided Auth instances.
testApp = new firebaseui.auth.testing.FakeAppClient(options);
testAuth = testApp.auth();
testAuth.install();
testApp1 = new firebaseui.auth.testing.FakeAppClient(options, 'testapp1');
testAuth1 = testApp1.auth();
testAuth1.install();
testApp2 = new firebaseui.auth.testing.FakeAppClient(options, 'testapp2');
testAuth2 = testApp2.auth();
testAuth2.install();
testApp3 = new firebaseui.auth.testing.FakeAppClient(options, 'testapp3');
testAuth3 = testApp3.auth();
testAuth3.install();
// Initialize all test apps. Do not supply an app id for third instance.
app1 = new firebaseui.auth.AuthUI(testAuth1, 'id1');
// Install all internal instances.
app1.getAuth().install();
app2 = new firebaseui.auth.AuthUI(testAuth2, 'id2');
app2.getAuth().install();
app3 = new firebaseui.auth.AuthUI(testAuth3);
app3.getAuth().install();
// Initialize config objects.
config1 = {
'signInSuccessUrl': 'http://localhost/home1',
'widgetUrl': 'http://localhost/firebase1',
};
config2 = {
'signInSuccessUrl': 'http://localhost/home2',
'widgetUrl': 'http://localhost/firebase2',
};
config3 = {
'signInSuccessUrl': 'http://localhost/home3',
'widgetUrl': 'http://localhost/firebase3',
};
config4 = {
'signInSuccessUrl': 'http://localhost/home4',
'widgetUrl': 'http://localhost/firebase4',
};
// Set application configurations.
app1.setConfig(config1);
app2.setConfig(config2);
app3.setConfig(config3);
}
function testGetExternalAuth() {
createAndInstallTestInstances();
// Confirm correct Auth instance stored for each app.
assertEquals(testAuth1, app1.getExternalAuth());
assertEquals(testAuth2, app2.getExternalAuth());
assertEquals(testAuth3, app3.getExternalAuth());
// Confirm internal instances have same options as external.
assertEquals(testAuth1.app.options.apiKey, app1.getAuth().app.options.apiKey);
assertEquals('API_KEY', app1.getAuth().app.options.apiKey);
assertEquals(
'subdomain.firebaseapp.com', app1.getAuth().app.options.authDomain);
assertEquals(
testAuth1.app.options.authDomain, app1.getAuth().app.options.authDomain);
// Confirm correct name used for temp instance.
assertEquals('testapp1-firebaseui-temp', app1.getAuth().app.name);
assertEquals('testapp2-firebaseui-temp', app2.getAuth().app.name);
}
function testAppId() {
createAndInstallTestInstances();
// Confirm correct app id stored for each app.
assertEquals('id1', app1.getAppId());
assertEquals('id2', app2.getAppId());
assertUndefined(app3.getAppId());
}
function testGetInstance() {
// Initially all instances should be null.
assertNull(firebaseui.auth.AuthUI.getInstance());
assertNull(firebaseui.auth.AuthUI.getInstance('id0'));
assertNull(firebaseui.auth.AuthUI.getInstance('id1'));
assertNull(firebaseui.auth.AuthUI.getInstance('id2'));
// Create and install test instances.
createAndInstallTestInstances();
// Confirm expected app instances returned for getInstance().
assertEquals(app1, firebaseui.auth.AuthUI.getInstance('id1'));
assertEquals(app2, firebaseui.auth.AuthUI.getInstance('id2'));
assertEquals(app3, firebaseui.auth.AuthUI.getInstance());
assertNull(firebaseui.auth.AuthUI.getInstance('id0'));
// Trying to create a new instance with an existing appId wil throw the
// expected error.
var error = assertThrows(function() {
new firebaseui.auth.AuthUI(testAuth1, 'id1');
});
assertEquals(
'An AuthUI instance already exists for the key "id1"', error.message);
}
function testIsPending() {
createAndInstallTestInstances();
assertFalse(app1.isPending());
assertFalse(app2.isPending());
assertFalse(app3.isPending());
var pendingEmailCredential =
new firebaseui.auth.PendingEmailCredential('test@gmail.com');
firebaseui.auth.storage.setPendingEmailCredential(pendingEmailCredential);
assertFalse(app1.isPending());
assertFalse(app2.isPending());
assertTrue(app3.isPending());
firebaseui.auth.storage.setPendingEmailCredential(
pendingEmailCredential, 'id1');
assertTrue(app1.isPending());
assertFalse(app2.isPending());
assertTrue(app3.isPending());
firebaseui.auth.storage.setPendingEmailCredential(
pendingEmailCredential, 'id2');
assertTrue(app1.isPending());
assertTrue(app2.isPending());
assertTrue(app3.isPending());
}
function testStart() {
createAndInstallTestInstances();
// Test multiple rendering for the widget in different apps.
asyncTestCase.waitForSignals(1);
var resetWarning = 'UI Widget is already rendered on the page and is pend' +
'ing some user interaction. Only one widget instance can be rendered ' +
'per page. The previous instance has been automatically reset.';
testStubs.reset();
// Record log console warnings.
testStubs.set(
firebaseui.auth.log,
'warning',
goog.testing.recordFunction());
// No rendered AuthUI.
assertNull(app1.getAuthUiGetter()());
testStubs.set(
firebaseui.auth.widget.dispatcher,
'dispatchOperation',
goog.testing.recordFunction(
firebaseui.auth.widget.dispatcher.dispatchOperation));
// Assume pending credential set in app1.
// This will be cleared when app2 interrupts and resets app1.
var pendingEmailCredential =
new firebaseui.auth.PendingEmailCredential('test@gmail.com');
firebaseui.auth.storage.setPendingEmailCredential(
pendingEmailCredential, app1.getAppId());
// Start widget for app1, override configuration for that.
app1.start(container1, config4);
// No automatic reset warning is logged.
assertEquals(0, firebaseui.auth.log.warning.getCallCount());
// Confirm configuration updated to config4.
assertConfigEquals(
config4,
app1.getConfig());
// Current rendered AuthUI should be set correctly.
assertEquals(app1, firebaseui.auth.AuthUI.getAuthUi());
assertEquals(app1, app1.getAuthUiGetter()());
// Dispatch operation should be called.
assertEquals(1,
firebaseui.auth.widget.dispatcher.dispatchOperation.getCallCount());
// app1 instance should be passed.
assertEquals(
app1,
firebaseui.auth.widget.dispatcher.dispatchOperation.getLastCall()
.getArgument(0));
// Container1 should be passed.
assertEquals(
container1,
firebaseui.auth.widget.dispatcher.dispatchOperation.getLastCall()
.getArgument(1));
// Callback page rendered in first app container1.
assertHasCssClass(container1, 'firebaseui-id-page-callback');
// Try to render another widget. This should reset first app widget.
app2.start(container2, config2);
// App1 pending creds cleared.
assertFalse(
firebaseui.auth.storage.hasPendingEmailCredential(app1.getAppId()));
// Automatic reset warning is logged since app1 is still pending.
assertEquals(1, firebaseui.auth.log.warning.getCallCount());
/** @suppress {missingRequire} */
assertEquals(
resetWarning,
firebaseui.auth.log.warning.getLastCall().getArgument(0));
// Current rendered AuthUi should be set correctly.
assertEquals(app2, firebaseui.auth.AuthUI.getAuthUi());
assertEquals(app2, app1.getAuthUiGetter()());
// Dispatch operation should be called.
/** @suppress {missingRequire} */
assertEquals(2,
firebaseui.auth.widget.dispatcher.dispatchOperation.getCallCount());
// app2 instance should be passed.
assertEquals(
app2,
firebaseui.auth.widget.dispatcher.dispatchOperation.getLastCall()
.getArgument(0));
// Container2 should be passed.
/** @suppress {missingRequire} */
assertEquals(
container2,
firebaseui.auth.widget.dispatcher.dispatchOperation.getLastCall()
.getArgument(1));
// First widget container should be reset.
assertEquals(0, container1.children.length);
// Callback rendered on second app.
assertHasCssClass(container2, 'firebaseui-id-page-callback');
// For a specific AuthUI, only the first getRedirectResult will be obtained
// from Auth instance. The next requests will use an empty promise to force
// provider sign-in screen to show for widget re-rendering.
app2.getAuth().assertGetRedirectResult(
[],
{
'user': null,
'credential': null
});
app2.getAuth().process().then(function() {
// Provider sign-in rendered at this stage.
assertHasCssClass(container2, 'firebaseui-id-page-provider-sign-in');
// Try rendering again same app widget. This should not call auth
// getRedirectResult anymore since if there is a pending redirect, it will
// process it and not display the widget.
app2.start(container2, config2);
// No additional Automatic reset warning is logged.
/** @suppress {missingRequire} */
assertEquals(1, firebaseui.auth.log.warning.getCallCount());
assertHasCssClass(container2, 'firebaseui-id-page-callback');
app2.getRedirectResult().then(function(result) {
assertHasCssClass(container2, 'firebaseui-id-page-provider-sign-in');
asyncTestCase.signal();
});
});
}
function testSetLang() {
createAndInstallTestInstances();
testStubs.replace(goog, 'LOCALE', 'de');
app1.start(container1, config1);
assertEquals('de', container1.getAttribute('lang'));
app1.reset();
assertFalse(container1.hasAttribute('lang'));
testStubs.replace(goog, 'LOCALE', 'zh-CN');
app1.start(container1, config1);
assertEquals('zh-CN', container1.getAttribute('lang'));
app1.reset();
assertFalse(container1.hasAttribute('lang'));
testStubs.replace(goog, 'LOCALE', 'zh_CN');
app1.start(container1, config1);
// The lang should have a dash instead of an underscore.
assertEquals('zh-CN', container1.getAttribute('lang'));
app1.reset();
assertFalse(container1.hasAttribute('lang'));
}
function testStart_elementNotFound() {
// Test widget start method with missing element.
// Test correct error message thrown when widget element not found.
createAndInstallTestInstances();
try {
app1.start('#notFound', config4, 'POST_BODY');
firebaseui.auth.widget.dispatcher.dispatchOperation(app, '#notFound');
fails('Should have thrown an error!');
} catch (e) {
// Confirm correct error message thrown.
assertEquals(
e.message,
'Could not find the FirebaseUI widget element on the page.');
}
}
function testUiChangedCallback() {
// Test UI changed callbacks called on UI changed.
createAndInstallTestInstances();
testStubs.reset();
// Simulate accountchooser.com client library loaded.
testStubs.set(
firebaseui.auth.widget.handler.common,
'loadAccountchooserJs',
function(app, callback, opt_forceUiShownCallback) {
callback();
});
asyncTestCase.waitForSignals(1);
// Simulate select mode for current widget mode.
testStubs.set(
firebaseui.auth.widget.dispatcher,
'getMode_',
function() {
return firebaseui.auth.widget.Config.WidgetMode.SELECT;
});
// Track parameters passed to UI changed callback.
var fromPage = null;
var toPage = null;
var uiChangedCallbackCalled = false;
// UI changed callback test function, used to record UI changed calls.
var uiChangedCallback = function(from, to) {
fromPage = from;
toPage = to;
uiChangedCallbackCalled = true;
};
// Set app config to include uiChanged callback.
var config = {
'signInSuccessUrl': 'http://localhost/home1',
'widgetUrl': 'http://localhost/firebase1',
'callbacks': {
'uiChanged': uiChangedCallback
},
'credentialHelper': firebaseui.auth.CredentialHelper.NONE,
};
// Initialize app and pass configuration.
// Make sure internal and external instances installed.
testAuth.install();
app = new firebaseui.auth.AuthUI(testAuth, 'id0');
app.getAuth().install();
app.setConfig(config);
// Confirm UI changed callback for app.
assertEquals(uiChangedCallback, app.getConfig().getUiChangedCallback());
// Start widget mode.
app.start(container1, config);
// UI changed from null to sign in on widget rendering.
assertTrue(uiChangedCallbackCalled);
assertEquals(null, fromPage);
// This is now going to callback first before provider sign-in.
assertEquals('callback', toPage);
// Reset flags.
fromPage = null;
toPage = null;
uiChangedCallbackCalled = false;
app.getAuth().assertGetRedirectResult(
[],
{
'user': null,
'credential': null
});
app.getAuth().process().then(function() {
// When redirect result returns null, it will redirect to provider sign-in.
assertTrue(uiChangedCallbackCalled);
assertEquals('callback', fromPage);
assertEquals('providerSignIn', toPage);
asyncTestCase.signal();
});
}
function testAuthUi_reset() {
createAndInstallTestInstances();
asyncTestCase.waitForSignals(2);
// Reset functions should be run.
var reset1 = goog.testing.recordFunction();
var reset2 = goog.testing.recordFunction();
// Pending promises should be cancelled.
var p1 = new goog.Promise(function(resolve, reject) {
}).thenCatch(function(e) {
assertEquals('cancel', e.name);
asyncTestCase.signal();
});
var p2 = new goog.Promise(function(resolve, reject) {
}).thenCatch(function(e) {
assertEquals('cancel', e.name);
asyncTestCase.signal();
});
// Initialize app.
// Make sure internal and external instances installed.
testAuth.install();
app = new firebaseui.auth.AuthUI(testAuth, 'id0');
app.getAuth().install();
// Render some UI for testing.
firebaseui.auth.widget.handler.handlePasswordSignIn(app, container1);
// Register pending reset functions and pending promises.
app.registerPending(reset1);
app.registerPending(reset2);
app.registerPending(p1);
app.registerPending(p2);
// Trigger reset.
app.reset();
// Reset functions should be called and pending promises cancelled.
assertEquals(1, reset1.getCallCount());
assertEquals(1, reset2.getCallCount());
// Rendered component should be cleared.
assertEquals(0, container1.children.length);
}
/**
* Asserts provided configuration object matches the widget configuration.
*
* @param {?Object} config
* @param {firebaseui.auth.widget.Config} widgetConfig
*/
function assertConfigEquals(config, widgetConfig) {
// Create widget config of provided config plain object.
var temp = new firebaseui.auth.widget.Config();
temp.setConfig(config);
// Compare both widget configs.
assertObjectEquals(temp, widgetConfig);
}
function testConfig() {
createAndInstallTestInstances();
// Check configuration set correctly for each app.
assertConfigEquals(
config1,
app1.getConfig());
assertConfigEquals(
config2,
app2.getConfig());
assertConfigEquals(
config3,
app3.getConfig());
}
function testUpdateConfig() {
createAndInstallTestInstances();
// Original config.
var config = {
'signInSuccessUrl': 'http://localhost/home1',
'widgetUrl': 'http://localhost/firebase1',
'siteName': 'Site_Name_1',
};
// Config with api key field updated.
var expectedConfig = {
'signInSuccessUrl': 'http://localhost/home1',
'widgetUrl': 'http://localhost/firebase1',
'siteName': 'Other_Site_Name',
};
// Set original config.
app1.setConfig(config);
// Confirm original api key.
assertEquals('Site_Name_1', app1.getConfig().getSiteName());
assertConfigEquals(
config,
app1.getConfig());
// Update API key.
app1.updateConfig('siteName', 'Other_Site_Name');
// Confirm API key updated.
assertEquals('Other_Site_Name', app1.getConfig().getSiteName());
// Confirm config updated to new config with API key updated.
assertConfigEquals(
expectedConfig,
app1.getConfig());
}
function testSignIn() {
createAndInstallTestInstances();
// Start sign-in for each app.
app1.signIn();
// Start sign-in handler should be called with app1 as argument.
assertEquals(1, firebaseui.auth.widget.handler.startSignIn.getCallCount());
assertEquals(
app1,
firebaseui.auth.widget.handler.startSignIn.getLastCall().getArgument(0));
app2.signIn();
// Start sign-in handler should be called with app2 as argument.
assertEquals(2, firebaseui.auth.widget.handler.startSignIn.getCallCount());
assertEquals(
app2,
firebaseui.auth.widget.handler.startSignIn.getLastCall().getArgument(0));
app3.signIn();
// Start sign-in handler should be called with app3 as argument.
assertEquals(3, firebaseui.auth.widget.handler.startSignIn.getCallCount());
assertEquals(
app3,
firebaseui.auth.widget.handler.startSignIn.getLastCall().getArgument(0));
}
function testAuthUi_delete() {
createAndInstallTestInstances();
asyncTestCase.waitForSignals(3);
// Reset functions should be run.
var reset1 = goog.testing.recordFunction();
var reset2 = goog.testing.recordFunction();
// Pending promises should be cancelled.
var p1 = new goog.Promise(function(resolve, reject) {
}).thenCatch(function(e) {
assertEquals('cancel', e.name);
asyncTestCase.signal();
});
var p2 = new goog.Promise(function(resolve, reject) {
}).thenCatch(function(e) {
assertEquals('cancel', e.name);
asyncTestCase.signal();
});
// Initialize app.
// Make sure internal and external instances installed.
testAuth.install();
app = new firebaseui.auth.AuthUI(testAuth, 'id0');
assertEquals(app, firebaseui.auth.AuthUI.getInstance('id0'));
app.getAuth().install();
// Confirm error thrown if another instance is initialized with the same
// auth instance and appId.
assertThrows(function() {
new firebaseui.auth.AuthUI(testAuth, 'id0');
});
// Delete instance and then try to reinitialize again.
app.delete().then(function() {
// No reference remains for id0.
assertNull(firebaseui.auth.AuthUI.getInstance('id0'));
// Initializing a new instance with same auth and appId should not throw an
// error after deletion.
assertNotThrows(function() {
app = new firebaseui.auth.AuthUI(testAuth, 'id0');
});
// getInstance should return the new instance.
assertEquals(app, firebaseui.auth.AuthUI.getInstance('id0'));
// Render some UI for testing.
firebaseui.auth.widget.handler.handlePasswordSignIn(app, container1);
// Register pending reset functions and pending promises.
app.registerPending(reset1);
app.registerPending(reset2);
app.registerPending(p1);
app.registerPending(p2);
// Trigger delete.
app.delete().then(function() {
// Reset functions should be called and pending promises cancelled.
assertEquals(1, reset1.getCallCount());
assertEquals(1, reset2.getCallCount());
// Rendered component should be cleared.
assertEquals(0, container1.children.length);
// Confirm calling any public method will throw an error;
var methods = [
'getRedirectResult',
'setCurrentComponent',
'getAuth',
'getExternalAuth',
'getAppId',
'isPending',
'start',
'registerPending',
'getAuthUiGetter',
'reset',
'updateConfig',
'setConfig',
'getConfig',
'signIn',
'delete'
];
// Call all public methods and confirm expected error.
for (var i = 0; i < methods.length; i++) {
var error = assertThrows(function() {
app[methods[i]]();
});
assertEquals('AuthUI instance is deleted!', error.message);
}
app = null;
asyncTestCase.signal();
});
});
}