-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Auth tests to demo app for web worker environment functionality. (…
…#567) This helps test that Auth API works as expected in that environment.
- Loading branch information
1 parent
df183a5
commit d3ec2e0
Showing
4 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/** | ||
* @fileoverview Web worker for Firebase Auth test app application. The | ||
* web worker tries to run operations on the Auth instance for testing purposes. | ||
*/ | ||
|
||
importScripts('/dist/firebase-app.js'); | ||
importScripts('/dist/firebase-auth.js'); | ||
importScripts('config.js'); | ||
|
||
// Initialize the Firebase app in the web worker. | ||
firebase.initializeApp(config); | ||
|
||
/** | ||
* Returns a promise that resolves with an ID token if available. | ||
* @return {!Promise<?string>} The promise that resolves with an ID token if | ||
* available. Otherwise, the promise resolves with null. | ||
*/ | ||
var getIdToken = function() { | ||
return new Promise(function(resolve, reject) { | ||
firebase.auth().onAuthStateChanged(function(user) { | ||
if (user) { | ||
user.getIdToken().then(function(idToken) { | ||
resolve(idToken); | ||
}, function(error) { | ||
resolve(null); | ||
}); | ||
} else { | ||
resolve(null); | ||
} | ||
}); | ||
}).catch(function(error) { | ||
console.log(error); | ||
}); | ||
}; | ||
|
||
/** | ||
* Runs various Firebase Auth tests in a web worker environment and confirms the | ||
* expected behavior. This is useful for manual testing in different browsers. | ||
* @param {string} googleIdToken The Google ID token to sign in with. | ||
* @return {!Promise<void>} A promise that resolves when all tests run | ||
* successfully. | ||
*/ | ||
var runWorkerTests = function(googleIdToken) { | ||
var inMemoryPersistence = firebase.auth.Auth.Persistence.NONE; | ||
var expectedDisplayName = 'Test User'; | ||
var oauthCredential = firebase.auth.GoogleAuthProvider.credential( | ||
googleIdToken); | ||
var provider = new firebase.auth.GoogleAuthProvider(); | ||
var OPERATION_NOT_SUPPORTED_CODE = | ||
'auth/operation-not-supported-in-this-environment'; | ||
var email = 'user' + Math.floor(Math.random() * 10000000000).toString() + | ||
'@example.com'; | ||
var pass = 'password'; | ||
return firebase.auth().setPersistence(inMemoryPersistence) | ||
.then(function() { | ||
firebase.auth().useDeviceLanguage(); | ||
return firebase.auth().signInAnonymously(); | ||
}) | ||
.then(function(user) { | ||
if (!user.uid) { | ||
throw new Error('signInAnonymously unexpectedly failed!'); | ||
} | ||
return user.updateProfile({displayName: expectedDisplayName}); | ||
}) | ||
.then(function() { | ||
if (firebase.auth().currentUser.displayName != expectedDisplayName) { | ||
throw new Error('Profile update failed!'); | ||
} | ||
return firebase.auth().currentUser.delete(); | ||
}) | ||
.then(function() { | ||
if (firebase.auth().currentUser) { | ||
throw new Error('currentUser.delete unexpectedly failed!'); | ||
} | ||
return firebase.auth().createUserWithEmailAndPassword(email, pass); | ||
}) | ||
.then(function(user) { | ||
if (user.email != email) { | ||
throw new Error( | ||
'createUserWithEmailAndPassword unexpectedly failed!'); | ||
} | ||
return firebase.auth().fetchProvidersForEmail(email); | ||
}).then(function(providers) { | ||
if (providers.length == 0 || providers[0] != 'password') { | ||
throw new Error('fetchProvidersForEmail failed!'); | ||
} | ||
return firebase.auth().signInWithEmailAndPassword(email, pass); | ||
}) | ||
.then(function(user) { | ||
if (user.email != email) { | ||
throw new Error('signInWithEmailAndPassword unexpectedly failed!'); | ||
} | ||
return user.delete(); | ||
}) | ||
.then(function() { | ||
return firebase.auth().signInWithPopup(provider) | ||
.catch(function(error) { | ||
if (error.code != OPERATION_NOT_SUPPORTED_CODE) { | ||
throw error; | ||
} | ||
}); | ||
}) | ||
.then(function() { | ||
return firebase.auth().signInWithRedirect(provider) | ||
.catch(function(error) { | ||
if (error.code != OPERATION_NOT_SUPPORTED_CODE) { | ||
throw error; | ||
} | ||
}); | ||
}) | ||
.then(function() { | ||
return Promise.resolve().then(function() { | ||
return new firebase.auth.RecaptchaVerifier('id'); | ||
}).then(function() { | ||
throw new Error( | ||
'RecaptchaVerifer instantiation succeeded unexpectedly!'); | ||
}).catch(function(error) { | ||
if (error.code != OPERATION_NOT_SUPPORTED_CODE) { | ||
throw error; | ||
} | ||
}); | ||
}) | ||
.then(function() { | ||
return firebase.auth().signInAndRetrieveDataWithCredential( | ||
oauthCredential); | ||
}) | ||
.then(function(result) { | ||
if (!result.user || | ||
!result.user.uid || | ||
!result.credential || | ||
!result.additionalUserInfo) { | ||
throw new Error( | ||
'signInAndRetrieveDataWithCredential unexpectedly failed!'); | ||
} | ||
return firebase.auth().signOut(); | ||
}) | ||
.then(function() { | ||
if (firebase.auth().currentUser) { | ||
throw new Error('signOut unexpectedly failed!'); | ||
} | ||
}); | ||
}; | ||
|
||
/** | ||
* Handles the incoming message from the main script. | ||
* @param {!Object} e The message event received. | ||
*/ | ||
self.onmessage = function(e) { | ||
if (e.data && e.data.type) { | ||
var result = {type: e.data.type}; | ||
switch (e.data.type) { | ||
case 'GET_USER_INFO': | ||
getIdToken().then(function(idToken) { | ||
result.idToken = idToken; | ||
result.uid = firebase.auth().currentUser && | ||
firebase.auth().currentUser.uid; | ||
self.postMessage(result); | ||
}); | ||
break; | ||
case 'RUN_TESTS': | ||
runWorkerTests(e.data.googleIdToken).then(function() { | ||
result.status = 'success'; | ||
self.postMessage(result); | ||
}).catch(function(error) { | ||
result.status = 'failure'; | ||
result.error = error; | ||
self.postMessage(result); | ||
}); | ||
break; | ||
default: | ||
self.postMessage({}); | ||
} | ||
} | ||
}; |