Skip to content

Refactor Auth storage test #530

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 7 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/auth/src/authstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ fireauth.authStorage.Manager.getInstance = function() {
};


/** Clears storage manager instances. This is used for testing. */
fireauth.authStorage.Manager.clear = function() {
fireauth.authStorage.Manager.instance_ = null;
};


/**
* Returns the storage corresponding to the specified persistence.
* @param {!fireauth.authStorage.Persistence} persistent The type of storage
Expand Down Expand Up @@ -404,9 +410,9 @@ fireauth.authStorage.Manager.prototype.startListeners_ = function() {
// TODO: refactor this implementation to be handled by the underlying
// storage mechanism.
if (!this.runsInBackground_ &&
// Add an exception for IE11 and Edge browsers, we should stick to
// indexedDB in that case.
!fireauth.util.isLocalStorageNotSynchronized() &&
// Add an exception for browsers that persist storage with indexedDB, we
// should stick with indexedDB listener implementation in that case.
!fireauth.util.persistsStorageWithIndexedDB() &&
// Confirm browser web storage is supported as polling relies on it.
this.webStorageSupported_) {
this.startManualListeners_();
Expand Down
15 changes: 10 additions & 5 deletions packages/auth/src/iframeclient/iframewrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@ fireauth.iframeclient.IframeWrapper.prototype.registerEvent =
this.onIframeOpen_.then(function() {
self.iframe_.register(
eventName,
handler,
/** @type {function(this:gapi.iframes.Iframe,
* *, gapi.iframes.Iframe): *}
*/ (handler),
/** @type {!gapi.iframes.IframesFilter} */ (
fireauth.util.getObjectRef(
'gapi.iframes.CROSS_ORIGIN_IFRAMES_FILTER')));
fireauth.util.getObjectRef(
'gapi.iframes.CROSS_ORIGIN_IFRAMES_FILTER')));
});
};

Expand All @@ -191,12 +193,15 @@ fireauth.iframeclient.IframeWrapper.prototype.unregisterEvent =
function(eventName, handler) {
var self = this;
this.onIframeOpen_.then(function() {
self.iframe_.unregister(eventName, handler);
self.iframe_.unregister(
eventName,
/** @type {(function(this:gapi.iframes.Iframe,
* *, gapi.iframes.Iframe): *|undefined)}
*/ (handler));
});
};



/** @private @const {!goog.string.Const} The GApi loader URL. */
fireauth.iframeclient.IframeWrapper.GAPI_LOADER_SRC_ = goog.string.Const.from(
'https://apis.google.com/js/api.js?onload=%{onload}');
Expand Down
5 changes: 2 additions & 3 deletions packages/auth/src/storage/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ fireauth.storage.Factory.getEnvConfig = function() {
* @return {!fireauth.storage.Storage} The persistent storage instance.
*/
fireauth.storage.Factory.prototype.makePersistentStorage = function() {
if (fireauth.util.isLocalStorageNotSynchronized()) {
// In a browser environment, when an iframe and a popup web storage are not
// synchronized, use the indexedDB fireauth.storage.Storage implementation.
if (fireauth.util.persistsStorageWithIndexedDB()) {
// If persistent storage is implemented using indexedDB, use indexedDB.
return fireauth.storage.IndexedDB.getFireauthManager();
}
return new this.env_.persistent();
Expand Down
18 changes: 9 additions & 9 deletions packages/auth/src/storage/inmemorystorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ goog.require('goog.Promise');
* @implements {fireauth.storage.Storage}
*/
fireauth.storage.InMemoryStorage = function() {
/** @private {!Object} The object where we store values. */
this.storage_ = {};
/** @protected {!Object} The object where we store values. */
this.storage = {};
};


Expand All @@ -42,7 +42,7 @@ fireauth.storage.InMemoryStorage = function() {
* @override
*/
fireauth.storage.InMemoryStorage.prototype.get = function(key) {
return goog.Promise.resolve(/** @type {*} */ (this.storage_[key]));
return goog.Promise.resolve(/** @type {*} */ (this.storage[key]));
};


Expand All @@ -53,7 +53,7 @@ fireauth.storage.InMemoryStorage.prototype.get = function(key) {
* @override
*/
fireauth.storage.InMemoryStorage.prototype.set = function(key, value) {
this.storage_[key] = value;
this.storage[key] = value;
return goog.Promise.resolve();
};

Expand All @@ -64,14 +64,14 @@ fireauth.storage.InMemoryStorage.prototype.set = function(key, value) {
* @override
*/
fireauth.storage.InMemoryStorage.prototype.remove = function(key) {
delete this.storage_[key];
delete this.storage[key];
return goog.Promise.resolve();
};


/**
* @param {function(!goog.events.BrowserEvent)} listener The storage event
* listener.
* @param {function((!goog.events.BrowserEvent|!Array<string>))} listener The
* storage event listener.
* @override
*/
fireauth.storage.InMemoryStorage.prototype.addStorageListener =
Expand All @@ -80,8 +80,8 @@ fireauth.storage.InMemoryStorage.prototype.addStorageListener =


/**
* @param {function(!goog.events.BrowserEvent)} listener The storage event
* listener.
* @param {function((!goog.events.BrowserEvent|!Array<string>))} listener The
* storage event listener.
* @override
*/
fireauth.storage.InMemoryStorage.prototype.removeStorageListener = function(
Expand Down
99 changes: 99 additions & 0 deletions packages/auth/src/storage/mockstorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright 2018 Google Inc.
*
* 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.
*/

goog.provide('fireauth.storage.MockStorage');

goog.require('fireauth.storage.InMemoryStorage');
goog.require('fireauth.storage.Storage');
goog.require('fireauth.util');
goog.require('goog.array');


/**
* Mock storage structure useful for testing and mocking local storage and other
* types of storage without depending on any native type of storage.
* @constructor
* @implements {fireauth.storage.Storage}
* @extends {fireauth.storage.InMemoryStorage}
*/
fireauth.storage.MockStorage = function() {
/**
* @private {!Array<function((!goog.events.BrowserEvent|!Array<string>))>} The
* storage listeners.
*/
this.storageListeners_ = [];
fireauth.storage.MockStorage.base(this, 'constructor');
};
goog.inherits(fireauth.storage.MockStorage, fireauth.storage.InMemoryStorage);


/**
* @param {function((!goog.events.BrowserEvent|!Array<string>))} listener The
* storage event listener.
* @override
*/
fireauth.storage.MockStorage.prototype.addStorageListener = function(listener) {
this.storageListeners_.push(listener);
};


/**
* @param {function((!goog.events.BrowserEvent|!Array<string>))} listener The
* storage event listener.
* @override
*/
fireauth.storage.MockStorage.prototype.removeStorageListener = function(
listener) {
goog.array.removeAllIf(this.storageListeners_, function(ele) {
return ele == listener;
});
};


/**
* Simulates a storage event getting triggered which would trigger any attached
* listener. Any fired event would also update the underlying storage map.
* @param {!Event} storageEvent The storage event triggered.
*/
fireauth.storage.MockStorage.prototype.fireBrowserEvent =
function(storageEvent) {
// Get key of storage event.
var key = storageEvent.key;
if (key != null) {
// If key available, get newValue.
var newValue = storageEvent.newValue;
if (newValue != null) {
// newValue available, update corresponding value.
this.storage[key] = fireauth.util.parseJSON(newValue);
} else {
// newValue not available, delete the corresponding key's entry.
delete this.storage[key];
}
} else {
// If key not available, clear storage.
this.clear();
}
// Trigger all attached storage listeners.
goog.array.forEach(this.storageListeners_, function(listener) {
listener([key]);
});
};


/** Clears all stored data. */
fireauth.storage.MockStorage.prototype.clear = function() {
this.storage = {};
};
3 changes: 2 additions & 1 deletion packages/auth/src/storageusermanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ goog.provide('fireauth.storage.UserManager');

goog.require('fireauth.AuthUser');
goog.require('fireauth.authStorage');
goog.require('goog.Promise');


/**
Expand Down Expand Up @@ -127,7 +128,7 @@ fireauth.storage.UserManager.prototype.switchToLocalOnExternalEvent_ =
// local.
this.waitForReady_(function() {
return goog.Promise.resolve().then(function() {
// In current persistence is not already local.
// If current persistence is not already local.
if (self.currentAuthUserKey_ &&
self.currentAuthUserKey_.persistent !=
fireauth.authStorage.Persistence.LOCAL) {
Expand Down
18 changes: 18 additions & 0 deletions packages/auth/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1335,3 +1335,21 @@ fireauth.util.utcTimestampToDateString = function(utcTimestamp) {
}
return null;
};


/** @return {boolean} Whether indexedDB is available. */
fireauth.util.isIndexedDBAvailable = function() {
return !!goog.global['indexedDB'];
};


/** @return {boolean} Whether indexedDB is used to persist storage. */
fireauth.util.persistsStorageWithIndexedDB = function() {
// This will cover:
// IE11, Edge when indexedDB is available (this is unavailable in InPrivate
// mode).
// In a browser environment, when an iframe and a popup web storage are not
// synchronized, use the indexedDB fireauth.storage.Storage implementation.
return fireauth.util.isLocalStorageNotSynchronized() &&
fireauth.util.isIndexedDBAvailable();
};
Loading