mirror of https://github.com/Squidex/squidex.git
10 changed files with 223 additions and 8 deletions
@ -0,0 +1,68 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as TypeMoq from 'typemoq'; |
||||
|
|
||||
|
import { AppMustExistGuard } from './app-must-exist.guard'; |
||||
|
import { AppsStoreService } from 'shared'; |
||||
|
import { RouterMockup } from './router-mockup'; |
||||
|
|
||||
|
describe('AppMustExistGuard', () => { |
||||
|
let appsStore: TypeMoq.Mock<AppsStoreService>; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
appsStore = TypeMoq.Mock.ofType(AppsStoreService); |
||||
|
}); |
||||
|
|
||||
|
it('should navigate to 404 page if app is not found', (done) => { |
||||
|
appsStore.setup(x => x.selectApp('my-app')) |
||||
|
.returns(() => Promise.resolve(false)); |
||||
|
|
||||
|
const router = new RouterMockup(); |
||||
|
const guard = new AppMustExistGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.canActivate(<any> { params: { appName: 'my-app' } }, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeFalsy(); |
||||
|
expect(router.lastNavigation).toEqual(['/404']); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('should navigate to 404 page if app loading fails', (done) => { |
||||
|
appsStore.setup(x => x.selectApp('my-app')) |
||||
|
.returns(() => Promise.reject<boolean>('error')); |
||||
|
|
||||
|
const router = new RouterMockup(); |
||||
|
const guard = new AppMustExistGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.canActivate(<any> { params: { appName: 'my-app' } }, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeFalsy(); |
||||
|
expect(router.lastNavigation).toEqual(['/404']); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('should return true if app is found', (done) => { |
||||
|
appsStore.setup(x => x.selectApp('my-app')) |
||||
|
.returns(() => Promise.resolve(true)); |
||||
|
|
||||
|
const router = new RouterMockup(); |
||||
|
const guard = new AppMustExistGuard(appsStore.object, <any>router); |
||||
|
|
||||
|
guard.canActivate(<any> { params: { appName: 'my-app' } }, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeTruthy(); |
||||
|
expect(router.lastNavigation).toBeUndefined(); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,52 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as TypeMoq from 'typemoq'; |
||||
|
|
||||
|
import { MustBeAuthenticatedGuard } from './must-be-authenticated.guard'; |
||||
|
import { RouterMockup } from './router-mockup'; |
||||
|
import { AuthService } from 'shared'; |
||||
|
|
||||
|
describe('MustBeAuthenticatedGuard', () => { |
||||
|
let authService: TypeMoq.Mock<AuthService>; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
authService = TypeMoq.Mock.ofType(AuthService); |
||||
|
}); |
||||
|
|
||||
|
it('should navigate to default page if not authenticated', (done) => { |
||||
|
authService.setup(x => x.checkLogin()) |
||||
|
.returns(() => Promise.resolve(false)); |
||||
|
|
||||
|
const router = new RouterMockup(); |
||||
|
const guard = new MustBeAuthenticatedGuard(authService.object, <any>router); |
||||
|
|
||||
|
guard.canActivate(null, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeFalsy(); |
||||
|
expect(router.lastNavigation).toEqual(['']); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('should return true if authenticated', (done) => { |
||||
|
authService.setup(x => x.checkLogin()) |
||||
|
.returns(() => Promise.resolve(true)); |
||||
|
|
||||
|
const router = new RouterMockup(); |
||||
|
const guard = new MustBeAuthenticatedGuard(authService.object, <any>router); |
||||
|
|
||||
|
guard.canActivate(null, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeTruthy(); |
||||
|
expect(router.lastNavigation).toBeUndefined(); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,52 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
import * as TypeMoq from 'typemoq'; |
||||
|
|
||||
|
import { MustBeNotAuthenticatedGuard } from './must-be-not-authenticated.guard'; |
||||
|
import { RouterMockup } from './router-mockup'; |
||||
|
import { AuthService } from 'shared'; |
||||
|
|
||||
|
describe('MustBeNotAuthenticatedGuard', () => { |
||||
|
let authService: TypeMoq.Mock<AuthService>; |
||||
|
|
||||
|
beforeEach(() => { |
||||
|
authService = TypeMoq.Mock.ofType(AuthService); |
||||
|
}); |
||||
|
|
||||
|
it('should navigate to app page if authenticated', (done) => { |
||||
|
authService.setup(x => x.checkLogin()) |
||||
|
.returns(() => Promise.resolve(true)); |
||||
|
|
||||
|
const router = new RouterMockup(); |
||||
|
const guard = new MustBeNotAuthenticatedGuard(authService.object, <any>router); |
||||
|
|
||||
|
guard.canActivate(null, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeFalsy(); |
||||
|
expect(router.lastNavigation).toEqual(['app']); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
|
||||
|
it('should return true if not authenticated', (done) => { |
||||
|
authService.setup(x => x.checkLogin()) |
||||
|
.returns(() => Promise.resolve(false)); |
||||
|
|
||||
|
const router = new RouterMockup(); |
||||
|
const guard = new MustBeNotAuthenticatedGuard(authService.object, <any>router); |
||||
|
|
||||
|
guard.canActivate(null, null) |
||||
|
.then(result => { |
||||
|
expect(result).toBeTruthy(); |
||||
|
expect(router.lastNavigation).toBeUndefined(); |
||||
|
|
||||
|
done(); |
||||
|
}); |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,14 @@ |
|||||
|
/* |
||||
|
* Squidex Headless CMS |
||||
|
* |
||||
|
* @license |
||||
|
* Copyright (c) Sebastian Stehle. All rights reserved |
||||
|
*/ |
||||
|
|
||||
|
export class RouterMockup { |
||||
|
public lastNavigation: any[]; |
||||
|
|
||||
|
public navigate(target: any[]) { |
||||
|
this.lastNavigation = target; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue