fix test on the new option for accounts storage

This commit is contained in:
Nacho Codoñer
2024-04-24 17:51:53 +02:00
parent 4381b01eb5
commit 1dfd3ca80f
2 changed files with 51 additions and 20 deletions

View File

@@ -27,8 +27,7 @@ export class AccountsClient extends AccountsCommon {
this.savedHash = window.location.hash;
this._initUrlMatching();
// Determine whether to use local or session storage to storage credentials and anything else.
this.storageLocation = (options?.clientStorage === 'session' || Meteor.settings?.public?.packages?.accounts?.clientStorage === 'session') ? window.sessionStorage : Meteor._localStorage;
this.initStorageLocation();
// Defined in localstorage_token.js.
this._initLocalStorage();
@@ -41,6 +40,17 @@ export class AccountsClient extends AccountsCommon {
this._loginCallbacksCalled = false;
}
initStorageLocation(options) {
// Determine whether to use local or session storage to storage credentials and anything else.
this.storageLocation = (options?.clientStorage === 'session' || Meteor.settings?.public?.packages?.accounts?.clientStorage === 'session') ? window.sessionStorage : Meteor._localStorage;
}
config(options) {
super.config(options);
this.initStorageLocation(options);
}
///
/// CURRENT USER
///

View File

@@ -304,21 +304,42 @@ Tinytest.addAsync(
}
);
testAsyncMulti('accounts - storage', [
function (test, expect) {
Accounts.config({ clientStorage: 'session' }) // No need to set the default value
test.isTrue(Accounts._options.clientStorage)
test.isNotUndefined(sessionStorage.getItem('Meteor.loginToken'))
test.isUndefined(localStorage.getItem('Meteor.loginToken'))
Accounts.logout()
removeTestUser()
},
function (test, expect) {
Accounts.config({ clientStorage: 'local' })
test.isFalse(Accounts._options.clientStorage)
test.isUndefined(sessionStorage.getItem('Meteor.loginToken'))
test.isNotUndefined(localStorage.getItem('Meteor.loginToken'))
Accounts.logout()
removeTestUser()
}
])
Tinytest.addAsync('accounts - storage',
async function(test) {
const expectWhenSessionStorage = () => {
test.isNotUndefined(sessionStorage.getItem('Meteor.loginToken'));
test.isNull(localStorage.getItem('Meteor.loginToken'));
};
const expectWhenLocalStorage = () => {
test.isNotUndefined(localStorage.getItem('Meteor.loginToken'));
test.isNull(sessionStorage.getItem('Meteor.loginToken'));
};
const testCases = [{
clientStorage: undefined,
expectStorage: expectWhenLocalStorage,
}, {
clientStorage: 'local',
expectStorage: expectWhenLocalStorage,
}, {
clientStorage: 'session',
expectStorage: expectWhenSessionStorage,
}];
for await (const testCase of testCases) {
await new Promise(resolve => {
sessionStorage.clear();
localStorage.clear();
const { clientStorage, expectStorage } = testCase;
Accounts.config({ clientStorage });
test.equal(Accounts._options.clientStorage, clientStorage);
// Login a user and test that tokens are in expected storage
logoutAndCreateUser(test, resolve, () => {
Accounts.logout();
expectStorage();
removeTestUser(resolve);
});
});
}
});