Browse Source

feat(core): add checkAccessToken fn to initial-utils

pull/3431/head
mehmet-erim 6 years ago
parent
commit
0f00344749
  1. 1
      npm/ng-packs/package.json
  2. 33
      npm/ng-packs/packages/core/src/lib/tests/initial-utils.spec.ts
  3. 17
      npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts
  4. 12
      npm/ng-packs/yarn.lock

1
npm/ng-packs/package.json

@ -34,6 +34,7 @@
"@abp/ng.tenant-management.config": "~2.3.0",
"@abp/ng.theme.basic": "~2.3.0",
"@abp/ng.theme.shared": "^2.3.0",
"@abp/utils": "~2.3.0",
"@angular-builders/jest": "^8.2.0",
"@angular-devkit/build-angular": "~0.803.21",
"@angular-devkit/build-ng-packagr": "~0.803.21",

33
npm/ng-packs/packages/core/src/lib/tests/initial-utils.spec.ts

@ -1,10 +1,10 @@
import { getInitialData, localeInitializer } from '../utils';
import { Injector } from '@angular/core';
import { Spectator, createComponentFactory } from '@ngneat/spectator/jest';
import { Component } from '@angular/core';
import { Component, Injector } from '@angular/core';
import { createComponentFactory, Spectator } from '@ngneat/spectator/jest';
import { Store } from '@ngxs/store';
import { OAuthService } from 'angular-oauth2-oidc';
import { of } from 'rxjs';
import { GetAppConfiguration } from '../actions';
import { getInitialData, localeInitializer } from '../utils';
@Component({
selector: 'abp-dummy',
@ -14,7 +14,10 @@ export class DummyComponent {}
describe('InitialUtils', () => {
let spectator: Spectator<DummyComponent>;
const createComponent = createComponentFactory({ component: DummyComponent, mocks: [Store] });
const createComponent = createComponentFactory({
component: DummyComponent,
mocks: [Store],
});
beforeEach(() => (spectator = createComponent()));
@ -25,7 +28,8 @@ describe('InitialUtils', () => {
const store = spectator.get(Store);
const dispatchSpy = jest.spyOn(store, 'dispatch');
injectorSpy.mockReturnValue(store);
injectorSpy.mockReturnValueOnce(store);
injectorSpy.mockReturnValueOnce({ hasValidAccessToken: () => false });
dispatchSpy.mockReturnValue(of('test'));
expect(typeof getInitialData(injector)).toBe('function');
@ -34,6 +38,23 @@ describe('InitialUtils', () => {
});
});
describe('#checkAccessToken', () => {
test('should call logOut fn of OAuthService when token is valid and current user not found', async () => {
const injector = spectator.get(Injector);
const injectorSpy = jest.spyOn(injector, 'get');
const store = spectator.get(Store);
const dispatchSpy = jest.spyOn(store, 'dispatch');
const logOutFn = jest.fn();
injectorSpy.mockReturnValueOnce(store);
injectorSpy.mockReturnValueOnce({ hasValidAccessToken: () => true, logOut: logOutFn });
dispatchSpy.mockReturnValue(of({ currentUser: { id: null } }));
getInitialData(injector)();
expect(logOutFn).toHaveBeenCalled();
});
});
describe('#localeInitializer', () => {
test('should resolve registerLocale', async () => {
const injector = spectator.get(Injector);

17
npm/ng-packs/packages/core/src/lib/utils/initial-utils.ts

@ -3,17 +3,32 @@ import { Injector } from '@angular/core';
import { Store } from '@ngxs/store';
import { GetAppConfiguration } from '../actions/config.actions';
import differentLocales from '../constants/different-locales';
import { ApplicationConfiguration } from '../models/application-configuration';
import { tap } from 'rxjs/operators';
import { ConfigState } from '../states/config.state';
import { OAuthService } from 'angular-oauth2-oidc';
export function getInitialData(injector: Injector) {
const fn = () => {
const store: Store = injector.get(Store);
return store.dispatch(new GetAppConfiguration()).toPromise();
return store
.dispatch(new GetAppConfiguration())
.pipe(tap(res => checkAccessToken(store, injector)))
.toPromise();
};
return fn;
}
function checkAccessToken(store: Store, injector: Injector) {
const oAuth = injector.get(OAuthService);
console.warn(oAuth.hasValidAccessToken());
if (oAuth.hasValidAccessToken() && !store.selectSnapshot(ConfigState.getDeep('currentUser.id'))) {
oAuth.logOut();
}
}
export function localeInitializer(injector: Injector) {
const fn = () => {
const store: Store = injector.get(Store);

12
npm/ng-packs/yarn.lock

@ -120,6 +120,13 @@
chart.js "^2.9.3"
tslib "^1.9.0"
"@abp/utils@~2.3.0":
version "2.3.1"
resolved "https://registry.yarnpkg.com/@abp/utils/-/utils-2.3.1.tgz#728b978d598a98b643562250be54c92988b986e5"
integrity sha512-sZ1nrl8pV1Zx3WAqHRUQ2V3aJlsjaq1ggg2VIPYY4FjCYHFZ3L+Fkq5cPPZb/MczG1YZpAQiMtCJ/+8slvPHHQ==
dependencies:
just-compare "^1.3.0"
"@angular-builders/jest@^8.2.0":
version "8.3.2"
resolved "https://registry.yarnpkg.com/@angular-builders/jest/-/jest-8.3.2.tgz#669c350c9df2e3afc9ae048c5067759c36113728"
@ -9578,11 +9585,6 @@ pretty-format@^24.0.0, pretty-format@^24.3.0, pretty-format@^24.8.0, pretty-form
ansi-styles "^3.2.0"
react-is "^16.8.4"
primeicons@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/primeicons/-/primeicons-2.0.0.tgz#ec3e5a8d4d04a09309225a5a57deb911e08dc0d3"
integrity sha512-GJTCeMSQU8UU1GqbsaDrg/IH+b/vSinJQl52NVpdJ7sShYLZA8Eq6jLF48Ye3N/dQloGrE07i7XsZvxQ9pNbqg==
private@^0.1.6:
version "0.1.8"
resolved "https://registry.yarnpkg.com/private/-/private-0.1.8.tgz#2381edb3689f7a53d653190060fcf822d2f368ff"

Loading…
Cancel
Save