-
Notifications
You must be signed in to change notification settings - Fork 948
Add persistence tests and fix some issues. #4620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
147 changes: 147 additions & 0 deletions
147
packages-exp/auth-exp/test/integration/webdriver/persistence.test.ts
This file contains hidden or 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,147 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* | ||
* 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. | ||
*/ | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { UserCredential } from '@firebase/auth-exp'; | ||
import { expect } from 'chai'; | ||
import { API_KEY } from '../../helpers/integration/settings'; | ||
import { AnonFunction, PersistenceFunction } from './util/functions'; | ||
import { browserDescribe } from './util/test_runner'; | ||
|
||
browserDescribe('WebDriver persistence test', driver => { | ||
context('default persistence hierarchy (indexedDB > localStorage)', () => { | ||
it('stores user in indexedDB by default', async () => { | ||
const cred: UserCredential = await driver.call( | ||
AnonFunction.SIGN_IN_ANONYMOUSLY | ||
); | ||
const uid = cred.user.uid; | ||
|
||
expect(await driver.getUserSnapshot()).to.eql(cred.user); | ||
expect(await driver.call(PersistenceFunction.LOCAL_STORAGE_SNAP)).to.eql( | ||
{} | ||
); | ||
expect( | ||
await driver.call(PersistenceFunction.SESSION_STORAGE_SNAP) | ||
).to.eql({}); | ||
|
||
const snap = await driver.call(PersistenceFunction.INDEXED_DB_SNAP); | ||
expect(snap) | ||
.to.have.property(`firebase:authUser:${API_KEY}:[DEFAULT]`) | ||
.that.contains({ uid }); | ||
|
||
// Persistence should survive a refresh: | ||
await driver.webDriver.navigate().refresh(); | ||
await driver.injectConfigAndInitAuth(); | ||
await driver.waitForAuthInit(); | ||
expect(await driver.getUserSnapshot()).to.contain({ uid }); | ||
}); | ||
|
||
it('should work fine if indexedDB is available while localStorage is not', async () => { | ||
await driver.webDriver.navigate().refresh(); | ||
// Simulate browsers that do not support localStorage. | ||
await driver.webDriver.executeScript('delete window.localStorage;'); | ||
await driver.injectConfigAndInitAuth(); | ||
await driver.waitForAuthInit(); | ||
|
||
const cred: UserCredential = await driver.call( | ||
AnonFunction.SIGN_IN_ANONYMOUSLY | ||
); | ||
const uid = cred.user.uid; | ||
|
||
expect(await driver.getUserSnapshot()).to.eql(cred.user); | ||
expect(await driver.call(PersistenceFunction.LOCAL_STORAGE_SNAP)).to.eql( | ||
{} | ||
); | ||
expect( | ||
await driver.call(PersistenceFunction.SESSION_STORAGE_SNAP) | ||
).to.eql({}); | ||
|
||
const snap = await driver.call(PersistenceFunction.INDEXED_DB_SNAP); | ||
expect(snap) | ||
.to.have.property(`firebase:authUser:${API_KEY}:[DEFAULT]`) | ||
.that.contains({ uid }); | ||
|
||
// Persistence should survive a refresh: | ||
await driver.webDriver.navigate().refresh(); | ||
await driver.injectConfigAndInitAuth(); | ||
await driver.waitForAuthInit(); | ||
expect(await driver.getUserSnapshot()).to.contain({ uid }); | ||
}); | ||
|
||
it('stores user in localStorage if indexedDB is not available', async () => { | ||
await driver.webDriver.navigate().refresh(); | ||
// Simulate browsers that do not support indexedDB. | ||
await driver.webDriver.executeScript('delete window.indexedDB;'); | ||
await driver.injectConfigAndInitAuth(); | ||
await driver.waitForAuthInit(); | ||
|
||
const cred: UserCredential = await driver.call( | ||
AnonFunction.SIGN_IN_ANONYMOUSLY | ||
); | ||
const uid = cred.user.uid; | ||
|
||
expect(await driver.getUserSnapshot()).to.eql(cred.user); | ||
expect( | ||
await driver.call(PersistenceFunction.SESSION_STORAGE_SNAP) | ||
).to.eql({}); | ||
|
||
const snap = await driver.call(PersistenceFunction.LOCAL_STORAGE_SNAP); | ||
expect(snap) | ||
.to.have.property(`firebase:authUser:${API_KEY}:[DEFAULT]`) | ||
.that.contains({ uid }); | ||
|
||
// Persistence should survive a refresh: | ||
await driver.webDriver.navigate().refresh(); | ||
await driver.injectConfigAndInitAuth(); | ||
await driver.waitForAuthInit(); | ||
expect(await driver.getUserSnapshot()).to.contain({ uid }); | ||
}); | ||
|
||
it('fall back to in-memory if neither indexedDB or localStorage is present', async () => { | ||
await driver.webDriver.navigate().refresh(); | ||
// Simulate browsers that do not support indexedDB or localStorage. | ||
await driver.webDriver.executeScript( | ||
'delete window.indexedDB; delete window.localStorage;' | ||
); | ||
await driver.injectConfigAndInitAuth(); | ||
await driver.waitForAuthInit(); | ||
|
||
const cred: UserCredential = await driver.call( | ||
AnonFunction.SIGN_IN_ANONYMOUSLY | ||
); | ||
|
||
expect(await driver.getUserSnapshot()).to.eql(cred.user); | ||
expect( | ||
await driver.call(PersistenceFunction.SESSION_STORAGE_SNAP) | ||
).to.eql({}); | ||
expect(await driver.call(PersistenceFunction.LOCAL_STORAGE_SNAP)).to.eql( | ||
{} | ||
); | ||
expect(await driver.call(PersistenceFunction.INDEXED_DB_SNAP)).to.eql({}); | ||
|
||
// User will be gone (a.k.a. logged out) after refresh. | ||
await driver.webDriver.navigate().refresh(); | ||
await driver.injectConfigAndInitAuth(); | ||
await driver.waitForAuthInit(); | ||
expect(await driver.getUserSnapshot()).to.equal(null); | ||
}); | ||
}); | ||
|
||
// TODO: Upgrade tests (e.g. migrate user from localStorage to indexedDB). | ||
|
||
// TODO: Compatibility tests (e.g. sign in with JS SDK and should stay logged in with TS SDK). | ||
}); |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: we should write some tests for the persistence events and cross-tab/cross-browser synchronization (in a different PR) b/182567813