diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index dd8a69ac7..adf1f70d6 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -17,37 +17,6 @@ export class Layout extends Component { this.getAccessTokenConfig(); } - getAccessTokenConfig() { - this.$http.get('AccessTokenConfigs').then(json => { - const firtsResult = json.data[0]; - if (!firtsResult) return; - window.localStorage.renewPeriod = firtsResult.renewPeriod; - window.localStorage.renewInterval = firtsResult.renewInterval; - - const intervalMilliseconds = firtsResult.renewInterval * 1000; - this.inservalId = setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); - }); - } - - checkTokenValidity() { - const now = new Date(); - const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); - const differenceSeconds = Math.floor(differenceMilliseconds / 1000); - - if (differenceSeconds > window.localStorage.renewPeriod) { - this.$http.post('VnUsers/renewToken') - .then(json => { - if (json.data.token) { - let remember = true; - if (window.sessionStorage.vnToken) remember = false; - - this.vnToken.set(json.data.token, remember); - this.vnTokenCreated.set(json.data.created, remember); - } - }); - } - } - getUserData() { this.$http.get('VnUsers/getCurrentUserData').then(json => { this.$.$root.user = json.data; @@ -67,6 +36,38 @@ export class Layout extends Component { window.location.reload(); } + getAccessTokenConfig() { + this.$http.get('AccessTokenConfigs').then(json => { + const firtsResult = json.data[0]; + if (!firtsResult) return; + window.localStorage.renewPeriod = firtsResult.renewPeriod; + window.localStorage.renewInterval = firtsResult.renewInterval; + + const intervalMilliseconds = firtsResult.renewInterval * 1000; + this.inservalId = setInterval(this.checkTokenValidity.bind(this), intervalMilliseconds); + }); + } + + checkTokenValidity() { + const now = new Date(); + const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); + const differenceSeconds = Math.floor(differenceMilliseconds / 1000); + + console.log(differenceSeconds, window.localStorage.renewPeriod); + if (differenceSeconds > window.localStorage.renewPeriod) { + this.$http.post('VnUsers/renewToken') + .then(json => { + if (json.data.token) { + let remember = true; + if (window.sessionStorage.vnToken) remember = false; + + this.vnToken.set(json.data.token, remember); + this.vnTokenCreated.set(json.data.created, remember); + } + }); + } + } + $onDestroy() { clearInterval(this.inservalId); } diff --git a/front/salix/components/layout/index.spec.js b/front/salix/components/layout/index.spec.js index 0d70c4806..d43f9a3d3 100644 --- a/front/salix/components/layout/index.spec.js +++ b/front/salix/components/layout/index.spec.js @@ -37,4 +37,51 @@ describe('Component vnLayout', () => { expect(url).not.toBeDefined(); }); }); + + describe('getAccessTokenConfig()', () => { + it(`should set the renewPeriod and renewInterval properties in localStorage`, () => { + const response = [{ + renewPeriod: 100, + renewInterval: 5 + }]; + + $httpBackend.expect('GET', `AccessTokenConfigs`).respond(response); + controller.getAccessTokenConfig(); + $httpBackend.flush(); + + const renewPeriod = localStorage.getItem('renewPeriod'); + const renewInterval = localStorage.getItem('renewInterval'); + + expect(renewPeriod).toBe('100'); + expect(renewInterval).toBe('5'); + expect(controller.inservalId).toBeDefined(); + }); + }); + + describe('checkTokenValidity()', () => { + it(`should not call renewToken and not set vnToken in the controller`, () => { + localStorage.setItem('renewPeriod', 100); + controller.vnTokenCreated.created = new Date(); + + controller.checkTokenValidity(); + + expect(controller.vnToken.token).toBeNull(); + }); + + it(`should call renewToken and set vnToken and vnTokenCreated properties in the controller`, () => { + const response = { + token: 999, + created: new Date() + }; + localStorage.setItem('renewPeriod', 100); + controller.vnTokenCreated.created = new Date(Date.now() - (60 * 60 * 1000)); + + $httpBackend.expect('POST', `VnUsers/renewToken`).respond(response); + controller.checkTokenValidity(); + $httpBackend.flush(); + + expect(controller.vnToken.token).toBe(999); + expect(controller.vnTokenCreated.created).toEqual(response.created); + }); + }); });