Skip to content

Commit d3ec2e0

Browse files
Adds Auth tests to demo app for web worker environment functionality. (#567)
This helps test that Auth API works as expected in that environment.
1 parent df183a5 commit d3ec2e0

File tree

4 files changed

+261
-1
lines changed

4 files changed

+261
-1
lines changed

packages/auth/demo/public/common.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,58 @@ if ('serviceWorker' in navigator) {
4949
console.log('Registration failed with ' + error.message);
5050
});
5151
}
52+
53+
var webWorker = null;
54+
if (window.Worker) {
55+
webWorker = new Worker('/web-worker.js');
56+
/**
57+
* Handles the incoming message from the web worker.
58+
* @param {!Object} e The message event received.
59+
*/
60+
webWorker.onmessage = function(e) {
61+
console.log('User data passed through web worker: ', e.data);
62+
switch (e.data.type) {
63+
case 'GET_USER_INFO':
64+
alertSuccess(
65+
'User data passed through web worker: ' + JSON.stringify(e.data));
66+
break;
67+
case 'RUN_TESTS':
68+
if (e.data.status == 'success') {
69+
alertSuccess('Web worker tests ran successfully!');
70+
} else {
71+
alertError('Error: ' + JSON.stringify(e.data.error));
72+
}
73+
break;
74+
default:
75+
return;
76+
}
77+
};
78+
}
79+
80+
/**
81+
* Asks the web worker, if supported in current browser, to return the user info
82+
* corresponding to the currentUser as seen within the worker.
83+
*/
84+
function onGetCurrentUserDataFromWebWorker() {
85+
if (webWorker) {
86+
webWorker.postMessage({type: 'GET_USER_INFO'});
87+
} else {
88+
alertError('Error: Web workers are not supported in the current browser!');
89+
}
90+
}
91+
92+
/**
93+
* Runs various Firebase Auth tests in a web worker environment and confirms the
94+
* expected behavior. This is useful for manual testing in different browsers.
95+
* @param {string} googleIdToken The Google ID token to sign in with.
96+
*/
97+
function runWebWorkerTests(googleIdToken) {
98+
if (webWorker) {
99+
webWorker.postMessage({
100+
type: 'RUN_TESTS',
101+
googleIdToken: googleIdToken
102+
});
103+
} else {
104+
alertError('Error: Web workers are not supported in the current browser!');
105+
}
106+
}

packages/auth/demo/public/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@
161161

162162
<div class="tab-content">
163163
<div class="tab-pane active" id="auth-section">
164+
<!-- Worker tests -->
165+
<div class="group">Web Worker Testing</div>
166+
<form class="form form-bordered no-submit">
167+
<button class="btn btn-block btn-primary"
168+
id="run-web-worker-tests">
169+
Run Tests
170+
</button>
171+
</form>
172+
164173
<!-- Persistence-->
165174
<div class="group">Auth State Persistence</div>
166175
<form class="form form-bordered no-submit">
@@ -441,6 +450,9 @@
441450
<button class="btn btn-block btn-default" id="refresh-token">
442451
Refresh ID Token
443452
</button>
453+
<button class="btn btn-block btn-default" id="get-token-worker">
454+
Get ID Token from Web Worker
455+
</button>
444456
<button class="btn btn-block btn-danger" id="sign-out">
445457
Sign Out
446458
</button>

packages/auth/demo/public/script.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,22 @@ function checkDatabaseAuthAccess() {
12191219
}
12201220

12211221

1222+
/** Runs all web worker tests if web workers are supported. */
1223+
function onRunWebWorkTests() {
1224+
if (!webWorker) {
1225+
alertError('Error: Web workers are not supported in the current browser!');
1226+
return;
1227+
}
1228+
var onError = function(error) {
1229+
alertError('Error: ' + error.code);
1230+
};
1231+
auth.signInWithPopup(new firebase.auth.GoogleAuthProvider())
1232+
.then(function(result) {
1233+
runWebWorkerTests(result.credential.idToken);
1234+
}, onError);
1235+
}
1236+
1237+
12221238
/**
12231239
* Initiates the application by setting event listeners on the various buttons.
12241240
*/
@@ -1236,7 +1252,7 @@ function initApp(){
12361252
// The action code for email verification or password reset
12371253
// can be passed in the url address as a parameter, and for convenience
12381254
// this preloads the input field.
1239-
populateActionCodes();
1255+
populateActionCodes();
12401256

12411257
// Allows to login the user if previously logged in.
12421258
if (auth.onIdTokenChanged) {
@@ -1356,6 +1372,7 @@ function initApp(){
13561372
$('#confirm-email-verification').click(onApplyActionCode);
13571373
$('#get-token').click(onGetIdToken);
13581374
$('#refresh-token').click(onRefreshToken);
1375+
$('#get-token-worker').click(onGetCurrentUserDataFromWebWorker);
13591376
$('#sign-out').click(onSignOut);
13601377

13611378
$('.popup-redirect-provider').click(onPopupRedirectProviderClick);
@@ -1375,6 +1392,8 @@ function initApp(){
13751392

13761393
$('#fetch-providers-for-email').click(onFetchProvidersForEmail);
13771394
$('#fetch-sign-in-methods-for-email').click(onFetchSignInMethodsForEmail);
1395+
1396+
$('#run-web-worker-tests').click(onRunWebWorkTests);
13781397
}
13791398

13801399
$(initApp);
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/**
2+
* @fileoverview Web worker for Firebase Auth test app application. The
3+
* web worker tries to run operations on the Auth instance for testing purposes.
4+
*/
5+
6+
importScripts('/dist/firebase-app.js');
7+
importScripts('/dist/firebase-auth.js');
8+
importScripts('config.js');
9+
10+
// Initialize the Firebase app in the web worker.
11+
firebase.initializeApp(config);
12+
13+
/**
14+
* Returns a promise that resolves with an ID token if available.
15+
* @return {!Promise<?string>} The promise that resolves with an ID token if
16+
* available. Otherwise, the promise resolves with null.
17+
*/
18+
var getIdToken = function() {
19+
return new Promise(function(resolve, reject) {
20+
firebase.auth().onAuthStateChanged(function(user) {
21+
if (user) {
22+
user.getIdToken().then(function(idToken) {
23+
resolve(idToken);
24+
}, function(error) {
25+
resolve(null);
26+
});
27+
} else {
28+
resolve(null);
29+
}
30+
});
31+
}).catch(function(error) {
32+
console.log(error);
33+
});
34+
};
35+
36+
/**
37+
* Runs various Firebase Auth tests in a web worker environment and confirms the
38+
* expected behavior. This is useful for manual testing in different browsers.
39+
* @param {string} googleIdToken The Google ID token to sign in with.
40+
* @return {!Promise<void>} A promise that resolves when all tests run
41+
* successfully.
42+
*/
43+
var runWorkerTests = function(googleIdToken) {
44+
var inMemoryPersistence = firebase.auth.Auth.Persistence.NONE;
45+
var expectedDisplayName = 'Test User';
46+
var oauthCredential = firebase.auth.GoogleAuthProvider.credential(
47+
googleIdToken);
48+
var provider = new firebase.auth.GoogleAuthProvider();
49+
var OPERATION_NOT_SUPPORTED_CODE =
50+
'auth/operation-not-supported-in-this-environment';
51+
var email = 'user' + Math.floor(Math.random() * 10000000000).toString() +
52+
'@example.com';
53+
var pass = 'password';
54+
return firebase.auth().setPersistence(inMemoryPersistence)
55+
.then(function() {
56+
firebase.auth().useDeviceLanguage();
57+
return firebase.auth().signInAnonymously();
58+
})
59+
.then(function(user) {
60+
if (!user.uid) {
61+
throw new Error('signInAnonymously unexpectedly failed!');
62+
}
63+
return user.updateProfile({displayName: expectedDisplayName});
64+
})
65+
.then(function() {
66+
if (firebase.auth().currentUser.displayName != expectedDisplayName) {
67+
throw new Error('Profile update failed!');
68+
}
69+
return firebase.auth().currentUser.delete();
70+
})
71+
.then(function() {
72+
if (firebase.auth().currentUser) {
73+
throw new Error('currentUser.delete unexpectedly failed!');
74+
}
75+
return firebase.auth().createUserWithEmailAndPassword(email, pass);
76+
})
77+
.then(function(user) {
78+
if (user.email != email) {
79+
throw new Error(
80+
'createUserWithEmailAndPassword unexpectedly failed!');
81+
}
82+
return firebase.auth().fetchProvidersForEmail(email);
83+
}).then(function(providers) {
84+
if (providers.length == 0 || providers[0] != 'password') {
85+
throw new Error('fetchProvidersForEmail failed!');
86+
}
87+
return firebase.auth().signInWithEmailAndPassword(email, pass);
88+
})
89+
.then(function(user) {
90+
if (user.email != email) {
91+
throw new Error('signInWithEmailAndPassword unexpectedly failed!');
92+
}
93+
return user.delete();
94+
})
95+
.then(function() {
96+
return firebase.auth().signInWithPopup(provider)
97+
.catch(function(error) {
98+
if (error.code != OPERATION_NOT_SUPPORTED_CODE) {
99+
throw error;
100+
}
101+
});
102+
})
103+
.then(function() {
104+
return firebase.auth().signInWithRedirect(provider)
105+
.catch(function(error) {
106+
if (error.code != OPERATION_NOT_SUPPORTED_CODE) {
107+
throw error;
108+
}
109+
});
110+
})
111+
.then(function() {
112+
return Promise.resolve().then(function() {
113+
return new firebase.auth.RecaptchaVerifier('id');
114+
}).then(function() {
115+
throw new Error(
116+
'RecaptchaVerifer instantiation succeeded unexpectedly!');
117+
}).catch(function(error) {
118+
if (error.code != OPERATION_NOT_SUPPORTED_CODE) {
119+
throw error;
120+
}
121+
});
122+
})
123+
.then(function() {
124+
return firebase.auth().signInAndRetrieveDataWithCredential(
125+
oauthCredential);
126+
})
127+
.then(function(result) {
128+
if (!result.user ||
129+
!result.user.uid ||
130+
!result.credential ||
131+
!result.additionalUserInfo) {
132+
throw new Error(
133+
'signInAndRetrieveDataWithCredential unexpectedly failed!');
134+
}
135+
return firebase.auth().signOut();
136+
})
137+
.then(function() {
138+
if (firebase.auth().currentUser) {
139+
throw new Error('signOut unexpectedly failed!');
140+
}
141+
});
142+
};
143+
144+
/**
145+
* Handles the incoming message from the main script.
146+
* @param {!Object} e The message event received.
147+
*/
148+
self.onmessage = function(e) {
149+
if (e.data && e.data.type) {
150+
var result = {type: e.data.type};
151+
switch (e.data.type) {
152+
case 'GET_USER_INFO':
153+
getIdToken().then(function(idToken) {
154+
result.idToken = idToken;
155+
result.uid = firebase.auth().currentUser &&
156+
firebase.auth().currentUser.uid;
157+
self.postMessage(result);
158+
});
159+
break;
160+
case 'RUN_TESTS':
161+
runWorkerTests(e.data.googleIdToken).then(function() {
162+
result.status = 'success';
163+
self.postMessage(result);
164+
}).catch(function(error) {
165+
result.status = 'failure';
166+
result.error = error;
167+
self.postMessage(result);
168+
});
169+
break;
170+
default:
171+
self.postMessage({});
172+
}
173+
}
174+
};

0 commit comments

Comments
 (0)