mirror of https://github.com/Squidex/squidex.git
11 changed files with 145 additions and 296 deletions
@ -1,102 +0,0 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Router } from '@angular/router'; |
|||
import { IMock, Mock, Times } from 'typemoq'; |
|||
import { Observable } from 'rxjs'; |
|||
|
|||
import { SchemasService, SchemaDetailsDto } from '@app/shared'; |
|||
|
|||
import { ResolvePublishedSchemaGuard } from './resolve-published-schema.guard'; |
|||
|
|||
describe('ResolvePublishedSchemaGuard', () => { |
|||
const route: any = { |
|||
params: { |
|||
appName: 'my-app' |
|||
}, |
|||
parent: { |
|||
params: { |
|||
schemaName: 'my-schema' |
|||
} |
|||
} |
|||
}; |
|||
|
|||
let router: IMock<Router>; |
|||
let schemasService: IMock<SchemasService>; |
|||
let schemaGuard: ResolvePublishedSchemaGuard; |
|||
|
|||
beforeEach(() => { |
|||
router = Mock.ofType<Router>(); |
|||
|
|||
schemasService = Mock.ofType<SchemasService>(); |
|||
schemaGuard = new ResolvePublishedSchemaGuard(schemasService.object, router.object); |
|||
}); |
|||
|
|||
it('should return schema if loading succeeded', () => { |
|||
const schema: any = { isPublished: true }; |
|||
|
|||
schemasService.setup(x => x.getSchema('my-app', 'my-schema')) |
|||
.returns(() => Observable.of(schema)); |
|||
|
|||
let result: SchemaDetailsDto; |
|||
|
|||
schemaGuard.resolve(route).subscribe(x => { |
|||
result = x!; |
|||
}); |
|||
|
|||
expect(result!).toBe(schema); |
|||
|
|||
schemasService.verify(x => x.getSchema('my-app', 'my-schema'), Times.once()); |
|||
}); |
|||
|
|||
it('should navigate to 404 page if schema is not found', () => { |
|||
schemasService.setup(x => x.getSchema('my-app', 'my-schema')) |
|||
.returns(() => Observable.of(null!)); |
|||
|
|||
let result: SchemaDetailsDto; |
|||
|
|||
schemaGuard.resolve(route).subscribe(x => { |
|||
result = x!!; |
|||
}); |
|||
|
|||
expect(result!).toBeNull(); |
|||
|
|||
router.verify(x => x.navigate(['/404']), Times.once()); |
|||
}); |
|||
|
|||
it('should navigate to 404 page if schema is not published', () => { |
|||
const schema: any = { isPublished: false }; |
|||
|
|||
schemasService.setup(x => x.getSchema('my-app', 'my-schema')) |
|||
.returns(() => Observable.of(schema)); |
|||
|
|||
let result: SchemaDetailsDto; |
|||
|
|||
schemaGuard.resolve(route).subscribe(x => { |
|||
result = x!; |
|||
}); |
|||
|
|||
expect(result!).toBeNull(); |
|||
|
|||
router.verify(x => x.navigate(['/404']), Times.once()); |
|||
}); |
|||
|
|||
it('should navigate to 404 page if schema loading fails', () => { |
|||
schemasService.setup(x => x.getSchema('my-app', 'my-schema')) |
|||
.returns(() => Observable.throw({})); |
|||
|
|||
let result: SchemaDetailsDto; |
|||
|
|||
schemaGuard.resolve(route).subscribe(x => { |
|||
result = x!; |
|||
}); |
|||
|
|||
expect(result!).toBeNull(); |
|||
|
|||
router.verify(x => x.navigate(['/404']), Times.once()); |
|||
}); |
|||
}); |
|||
@ -1,52 +0,0 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Injectable } from '@angular/core'; |
|||
import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router'; |
|||
import { Observable } from 'rxjs'; |
|||
|
|||
import { allParams } from '@app/framework'; |
|||
|
|||
import { SchemaDetailsDto, SchemasService } from './../services/schemas.service'; |
|||
|
|||
@Injectable() |
|||
export class ResolvePublishedSchemaGuard implements Resolve<SchemaDetailsDto | null> { |
|||
constructor( |
|||
private readonly schemasService: SchemasService, |
|||
private readonly router: Router |
|||
) { |
|||
} |
|||
|
|||
public resolve(route: ActivatedRouteSnapshot): Observable<SchemaDetailsDto | null> { |
|||
const params = allParams(route); |
|||
|
|||
const appName = params['appName']; |
|||
const schemaName = params['schemaName']; |
|||
|
|||
const result = |
|||
this.schemasService.getSchema(appName, schemaName) |
|||
.map(dto => { |
|||
if (dto && !dto.isPublished) { |
|||
return null; |
|||
} |
|||
|
|||
return dto; |
|||
}) |
|||
.do(dto => { |
|||
if (!dto) { |
|||
this.router.navigate(['/404']); |
|||
} |
|||
}) |
|||
.catch(error => { |
|||
this.router.navigate(['/404']); |
|||
|
|||
return Observable.of(null); |
|||
}); |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
@ -1,85 +0,0 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Router } from '@angular/router'; |
|||
import { IMock, Mock, Times } from 'typemoq'; |
|||
import { Observable } from 'rxjs'; |
|||
|
|||
import { SchemasService, SchemaDetailsDto } from '@app/shared'; |
|||
|
|||
import { ResolveSchemaGuard } from './resolve-schema.guard'; |
|||
|
|||
describe('ResolveSchemaGuard', () => { |
|||
const route: any = { |
|||
params: { |
|||
appName: 'my-app' |
|||
}, |
|||
parent: { |
|||
params: { |
|||
schemaName: 'my-schema' |
|||
} |
|||
} |
|||
}; |
|||
|
|||
let router: IMock<Router>; |
|||
let schemasService: IMock<SchemasService>; |
|||
let schemaGuard: ResolveSchemaGuard; |
|||
|
|||
beforeEach(() => { |
|||
router = Mock.ofType<Router>(); |
|||
|
|||
schemasService = Mock.ofType<SchemasService>(); |
|||
schemaGuard = new ResolveSchemaGuard(schemasService.object, router.object); |
|||
}); |
|||
|
|||
it('should return schema if loading succeeded', () => { |
|||
const schema: any = {}; |
|||
|
|||
schemasService.setup(x => x.getSchema('my-app', 'my-schema')) |
|||
.returns(() => Observable.of(schema)); |
|||
|
|||
let result: SchemaDetailsDto; |
|||
|
|||
schemaGuard.resolve(route).subscribe(x => { |
|||
result = x!; |
|||
}); |
|||
|
|||
expect(result!).toBe(schema); |
|||
|
|||
schemasService.verify(x => x.getSchema('my-app', 'my-schema'), Times.once()); |
|||
}); |
|||
|
|||
it('should navigate to 404 page if schema is not found', () => { |
|||
schemasService.setup(x => x.getSchema('my-app', 'my-schema')) |
|||
.returns(() => Observable.of(null!)); |
|||
|
|||
let result: SchemaDetailsDto; |
|||
|
|||
schemaGuard.resolve(route).subscribe(x => { |
|||
result = x!; |
|||
}); |
|||
|
|||
expect(result!).toBeNull(); |
|||
|
|||
router.verify(x => x.navigate(['/404']), Times.once()); |
|||
}); |
|||
|
|||
it('should navigate to 404 page if schema loading fails', () => { |
|||
schemasService.setup(x => x.getSchema('my-app', 'my-schema')) |
|||
.returns(() => Observable.throw({})); |
|||
|
|||
let result: SchemaDetailsDto; |
|||
|
|||
schemaGuard.resolve(route).subscribe(x => { |
|||
result = x!; |
|||
}); |
|||
|
|||
expect(result!).toBeNull(); |
|||
|
|||
router.verify(x => x.navigate(['/404']), Times.once()); |
|||
}); |
|||
}); |
|||
@ -1,45 +0,0 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Injectable } from '@angular/core'; |
|||
import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router'; |
|||
import { Observable } from 'rxjs'; |
|||
|
|||
import { allParams } from '@app/framework'; |
|||
|
|||
import { SchemaDetailsDto, SchemasService } from './../services/schemas.service'; |
|||
|
|||
@Injectable() |
|||
export class ResolveSchemaGuard implements Resolve<SchemaDetailsDto | null> { |
|||
constructor( |
|||
private readonly schemasService: SchemasService, |
|||
private readonly router: Router |
|||
) { |
|||
} |
|||
|
|||
public resolve(route: ActivatedRouteSnapshot): Observable<SchemaDetailsDto | null> { |
|||
const params = allParams(route); |
|||
|
|||
const appName = params['appName']; |
|||
const schemaName = params['schemaName']; |
|||
|
|||
const result = |
|||
this.schemasService.getSchema(appName, schemaName) |
|||
.do(dto => { |
|||
if (!dto) { |
|||
this.router.navigate(['/404']); |
|||
} |
|||
}) |
|||
.catch(error => { |
|||
this.router.navigate(['/404']); |
|||
|
|||
return Observable.of(null); |
|||
}); |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Router } from '@angular/router'; |
|||
import { Observable } from 'rxjs'; |
|||
import { IMock, Mock, Times } from 'typemoq'; |
|||
|
|||
import { SchemaDetailsDto } from './../services/schemas.service'; |
|||
import { SchemasState } from './../state/schemas.state'; |
|||
import { SchemaMustExistPublishedGuard } from './schema-must-exist-published.guard'; |
|||
|
|||
describe('SchemaMustExistPublishedGuard', () => { |
|||
const route: any = { |
|||
params: { |
|||
schemaName: '123' |
|||
} |
|||
}; |
|||
|
|||
let schemasState: IMock<SchemasState>; |
|||
let router: IMock<Router>; |
|||
let schemaGuard: SchemaMustExistPublishedGuard; |
|||
|
|||
beforeEach(() => { |
|||
router = Mock.ofType<Router>(); |
|||
schemasState = Mock.ofType<SchemasState>(); |
|||
schemaGuard = new SchemaMustExistPublishedGuard(schemasState.object, router.object); |
|||
}); |
|||
|
|||
it('should load schema and return true when found', () => { |
|||
schemasState.setup(x => x.selectSchema('123')) |
|||
.returns(() => Observable.of(<SchemaDetailsDto>{ isPublished: true })); |
|||
|
|||
let result: boolean; |
|||
|
|||
schemaGuard.canActivate(route).subscribe(x => { |
|||
result = x; |
|||
}).unsubscribe(); |
|||
|
|||
expect(result!).toBeTruthy(); |
|||
|
|||
schemasState.verify(x => x.selectSchema('123'), Times.once()); |
|||
}); |
|||
|
|||
it('should load schema and return false when not found', () => { |
|||
schemasState.setup(x => x.selectSchema('123')) |
|||
.returns(() => Observable.of(<SchemaDetailsDto>{ isPublished: false })); |
|||
|
|||
let result: boolean; |
|||
|
|||
schemaGuard.canActivate(route).subscribe(x => { |
|||
result = x; |
|||
}).unsubscribe(); |
|||
|
|||
expect(result!).toBeFalsy(); |
|||
|
|||
router.verify(x => x.navigate(['/404']), Times.once()); |
|||
}); |
|||
|
|||
it('should load schema and return false when not found', () => { |
|||
schemasState.setup(x => x.selectSchema('123')) |
|||
.returns(() => Observable.of(null)); |
|||
|
|||
let result: boolean; |
|||
|
|||
schemaGuard.canActivate(route).subscribe(x => { |
|||
result = x; |
|||
}).unsubscribe(); |
|||
|
|||
expect(result!).toBeFalsy(); |
|||
|
|||
router.verify(x => x.navigate(['/404']), Times.once()); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,62 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Router } from '@angular/router'; |
|||
import { Observable } from 'rxjs'; |
|||
import { IMock, Mock, Times } from 'typemoq'; |
|||
|
|||
import { SchemaDetailsDto } from './../services/schemas.service'; |
|||
import { SchemasState } from './../state/schemas.state'; |
|||
import { SchemaMustExistGuard } from './schema-must-exist.guard'; |
|||
|
|||
describe('SchemaMustExistGuard', () => { |
|||
const route: any = { |
|||
params: { |
|||
schemaName: '123' |
|||
} |
|||
}; |
|||
|
|||
let schemasState: IMock<SchemasState>; |
|||
let router: IMock<Router>; |
|||
let schemaGuard: SchemaMustExistGuard; |
|||
|
|||
beforeEach(() => { |
|||
router = Mock.ofType<Router>(); |
|||
schemasState = Mock.ofType<SchemasState>(); |
|||
schemaGuard = new SchemaMustExistGuard(schemasState.object, router.object); |
|||
}); |
|||
|
|||
it('should load schema and return true when found', () => { |
|||
schemasState.setup(x => x.selectSchema('123')) |
|||
.returns(() => Observable.of(<SchemaDetailsDto>{})); |
|||
|
|||
let result: boolean; |
|||
|
|||
schemaGuard.canActivate(route).subscribe(x => { |
|||
result = x; |
|||
}).unsubscribe(); |
|||
|
|||
expect(result!).toBeTruthy(); |
|||
|
|||
schemasState.verify(x => x.selectSchema('123'), Times.once()); |
|||
}); |
|||
|
|||
it('should load schema and return false when not found', () => { |
|||
schemasState.setup(x => x.selectSchema('123')) |
|||
.returns(() => Observable.of(null)); |
|||
|
|||
let result: boolean; |
|||
|
|||
schemaGuard.canActivate(route).subscribe(x => { |
|||
result = x; |
|||
}).unsubscribe(); |
|||
|
|||
expect(result!).toBeFalsy(); |
|||
|
|||
router.verify(x => x.navigate(['/404']), Times.once()); |
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue