[ADF-3887] Different local storages for different ADF apps (#4539)

* [ADF-3887] Different local storages for different ADF apps

* [ADF-3887] Add documentation

* [ADF-3887] Add unit tests and improve code

* [ADF-3887] Add unit tests

* [ADF-3887] Fix e2e tests

* fix test

* fix test

* Update storage.service.md
This commit is contained in:
davidcanonieto
2019-04-08 15:23:46 +01:00
committed by Eugenio Romano
parent f89bf507f5
commit dee63e3f3b
22 changed files with 181 additions and 43 deletions

View File

@@ -25,7 +25,6 @@ import {
} from '@alfresco/js-api';
import { AlfrescoApiCompatibility, AlfrescoApiConfig } from '@alfresco/js-api';
import { AppConfigService, AppConfigValues } from '../app-config/app-config.service';
import { StorageService } from './storage.service';
import { Subject } from 'rxjs';
import { OauthConfigModel } from '../models/oauth-config.model';
@@ -96,8 +95,7 @@ export class AlfrescoApiService {
return this.getInstance().core.groupsApi;
}
constructor(protected appConfig: AppConfigService,
protected storage: StorageService) {
constructor(protected appConfig: AppConfigService) {
}
async load() {

View File

@@ -0,0 +1,100 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* 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.
*/
import { TestBed } from '@angular/core/testing';
import { AppConfigService } from '../app-config/app-config.service';
import { StorageService } from './storage.service';
import { setupTestBed } from '../testing/setupTestBed';
import { CoreTestingModule } from '../testing/core.testing.module';
import { AppConfigServiceMock } from '../mock/app-config.service.mock';
describe('StorageService', () => {
let storage: StorageService;
let appConfig: AppConfigServiceMock;
const key = 'test_key';
const value = 'test_value';
setupTestBed({
imports: [CoreTestingModule],
providers: [
{ provide: AppConfigService, useClass: AppConfigServiceMock }
]
});
beforeEach(() => {
appConfig = TestBed.get(AppConfigService);
appConfig.config = {
application: {
storagePrefix: 'ADF_APP'
}
};
storage = TestBed.get(StorageService);
});
it('should get the prefix for the storage from app config', (done) => {
appConfig.load().then(() => {
expect(storage.storagePrefix).toBe('ADF_APP_');
done();
});
});
it('should set an empty prefix when the it is not defined in the app config', (done) => {
appConfig.config.application.storagePrefix = '';
appConfig.load().then(() => {
expect(storage.storagePrefix).toBe('');
done();
});
});
it('should set a property with the prefix in the local storage', (done) => {
storage.clear();
appConfig.load().then(() => {
storage.setItem(key, value);
const storageKey = localStorage.key(0);
expect(storageKey).toBe('ADF_APP_' + key);
expect(localStorage.getItem(storageKey)).toBe(value);
done();
});
});
it('should set a property without a prefix in the local storage', (done) => {
storage.clear();
appConfig.config.application.storagePrefix = '';
appConfig.load().then(() => {
storage.setItem(key, value);
const storageKey = localStorage.key(0);
expect(storageKey).toBe(key);
expect(localStorage.getItem(storageKey)).toBe(value);
done();
});
});
it('should be able to get a property from the local storage', (done) => {
storage.clear();
appConfig.load().then(() => {
storage.setItem(key, value);
expect(storage.getItem(key)).toBe(value);
done();
});
});
});

View File

@@ -16,6 +16,7 @@
*/
import { Injectable } from '@angular/core';
import { AppConfigService } from '../app-config/app-config.service';
@Injectable({
providedIn: 'root'
@@ -24,9 +25,11 @@ export class StorageService {
private memoryStore: { [key: string]: any } = {};
private useLocalStorage: boolean = false;
storagePrefix: string;
constructor() {
constructor(private appConfigService: AppConfigService) {
this.useLocalStorage = this.storageAvailable('localStorage');
this.appConfigService.onLoad.subscribe(this.getAppPrefix.bind(this));
}
/**
@@ -36,9 +39,9 @@ export class StorageService {
*/
getItem(key: string): string | null {
if (this.useLocalStorage) {
return localStorage.getItem(key);
return localStorage.getItem(this.storagePrefix + key);
} else {
return this.memoryStore.hasOwnProperty(key) ? this.memoryStore[key] : null;
return this.memoryStore.hasOwnProperty(this.storagePrefix + key) ? this.memoryStore[this.storagePrefix + key] : null;
}
}
@@ -49,9 +52,9 @@ export class StorageService {
*/
setItem(key: string, data: string) {
if (this.useLocalStorage) {
localStorage.setItem(key, data);
localStorage.setItem(this.storagePrefix + key, data);
} else {
this.memoryStore[key] = data.toString();
this.memoryStore[this.storagePrefix + key] = data.toString();
}
}
@@ -70,9 +73,9 @@ export class StorageService {
*/
removeItem(key: string) {
if (this.useLocalStorage) {
localStorage.removeItem(key);
localStorage.removeItem(this.storagePrefix + key);
} else {
delete this.memoryStore[key];
delete this.memoryStore[this.storagePrefix + key];
}
}
@@ -83,7 +86,7 @@ export class StorageService {
*/
hasItem(key: string): boolean {
if (this.useLocalStorage) {
return localStorage.getItem(key) ? true : false;
return localStorage.getItem(this.storagePrefix + key) ? true : false;
} else {
return this.memoryStore.hasOwnProperty(key);
}
@@ -101,4 +104,18 @@ export class StorageService {
}
}
/**
* Sets the prefix that is used for the local storage of the app
* It assigns the string that is defined i the app config,
* empty prefix otherwise.
*/
getAppPrefix() {
const appConfiguration = this.appConfigService.get<any>('application');
if (appConfiguration && appConfiguration.storagePrefix) {
this.storagePrefix = appConfiguration.storagePrefix + '_';
} else {
this.storagePrefix = '';
}
}
}

View File

@@ -153,6 +153,7 @@ export class UserPreferencesService {
*/
setStoragePrefix(value: string) {
this.storage.setItem('USER_PROFILE', value || 'GUEST');
this.initUserPreferenceStatus();
}
/**