mirror of https://github.com/Squidex/squidex.git
33 changed files with 274 additions and 95 deletions
@ -0,0 +1,105 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Router, RouterStateSnapshot, UrlSegment } from '@angular/router'; |
|||
import { of } from 'rxjs'; |
|||
import { IMock, It, Mock, Times } from 'typemoq'; |
|||
|
|||
import { SchemaDetailsDto } from './../services/schemas.service'; |
|||
import { SchemasState } from './../state/schemas.state'; |
|||
import { SchemaMustNotBeSingletonGuard } from './schema-must-not-be-singleton.guard'; |
|||
|
|||
describe('SchemaMustNotBeSingletonGuard', () => { |
|||
const route: any = { |
|||
params: { |
|||
schemaName: '123' |
|||
}, |
|||
url: [ |
|||
new UrlSegment('schemas', {}), |
|||
new UrlSegment('name', {}), |
|||
new UrlSegment('new', {}) |
|||
] |
|||
}; |
|||
|
|||
let schemasState: IMock<SchemasState>; |
|||
let router: IMock<Router>; |
|||
let schemaGuard: SchemaMustNotBeSingletonGuard; |
|||
|
|||
beforeEach(() => { |
|||
router = Mock.ofType<Router>(); |
|||
schemasState = Mock.ofType<SchemasState>(); |
|||
schemaGuard = new SchemaMustNotBeSingletonGuard(schemasState.object, router.object); |
|||
}); |
|||
|
|||
it('should subscribe to schema and return true when not singleton', () => { |
|||
const state: RouterStateSnapshot = <any>{ url: 'schemas/name/' }; |
|||
|
|||
schemasState.setup(x => x.selectedSchema) |
|||
.returns(() => of(<SchemaDetailsDto>{ id: '123', isSingleton: false })); |
|||
|
|||
let result: boolean; |
|||
|
|||
schemaGuard.canActivate(route, state).subscribe(x => { |
|||
result = x; |
|||
}).unsubscribe(); |
|||
|
|||
expect(result!).toBeTruthy(); |
|||
|
|||
router.verify(x => x.navigate(It.isAny()), Times.never()); |
|||
}); |
|||
|
|||
it('should subscribe to schema and return false when not found', () => { |
|||
const state: RouterStateSnapshot = <any>{ url: 'schemas/name/' }; |
|||
|
|||
schemasState.setup(x => x.selectedSchema) |
|||
.returns(() => of(null)); |
|||
|
|||
let result: boolean; |
|||
|
|||
schemaGuard.canActivate(route, state).subscribe(x => { |
|||
result = x; |
|||
}).unsubscribe(); |
|||
|
|||
expect(result!).toBeFalsy(); |
|||
|
|||
router.verify(x => x.navigate(It.isAny()), Times.never()); |
|||
}); |
|||
|
|||
it('should redirect to content when singleton', () => { |
|||
const state: RouterStateSnapshot = <any>{ url: 'schemas/name/' }; |
|||
|
|||
schemasState.setup(x => x.selectedSchema) |
|||
.returns(() => of(<SchemaDetailsDto>{ id: '123', isSingleton: true })); |
|||
|
|||
let result: boolean; |
|||
|
|||
schemaGuard.canActivate(route, state).subscribe(x => { |
|||
result = x; |
|||
}).unsubscribe(); |
|||
|
|||
expect(result!).toBeFalsy(); |
|||
|
|||
router.verify(x => x.navigate([state.url, '123']), Times.once()); |
|||
}); |
|||
|
|||
it('should redirect to content when singleton on new page', () => { |
|||
const state: RouterStateSnapshot = <any>{ url: 'schemas/name/new/' }; |
|||
|
|||
schemasState.setup(x => x.selectedSchema) |
|||
.returns(() => of(<SchemaDetailsDto>{ id: '123', isSingleton: true })); |
|||
|
|||
let result: boolean; |
|||
|
|||
schemaGuard.canActivate(route, state).subscribe(x => { |
|||
result = x; |
|||
}).unsubscribe(); |
|||
|
|||
expect(result!).toBeFalsy(); |
|||
|
|||
router.verify(x => x.navigate(['schemas/name/', '123']), Times.once()); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,42 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|||
*/ |
|||
|
|||
import { Injectable } from '@angular/core'; |
|||
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; |
|||
import { Observable } from 'rxjs'; |
|||
import { map, take, tap } from 'rxjs/operators'; |
|||
|
|||
import { SchemasState } from './../state/schemas.state'; |
|||
|
|||
@Injectable() |
|||
export class SchemaMustNotBeSingletonGuard implements CanActivate { |
|||
constructor( |
|||
private readonly schemasState: SchemasState, |
|||
private readonly router: Router |
|||
) { |
|||
} |
|||
|
|||
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { |
|||
const result = |
|||
this.schemasState.selectedSchema.pipe( |
|||
take(1), |
|||
tap(dto => { |
|||
if (dto && dto.isSingleton) { |
|||
if (state.url.indexOf('/new') >= 0) { |
|||
const parentUrl = state.url.slice(0, state.url.indexOf(route.url[route.url.length - 1].path)); |
|||
|
|||
this.router.navigate([parentUrl, dto.id]); |
|||
} else { |
|||
this.router.navigate([state.url, dto.id]); |
|||
} |
|||
} |
|||
}), |
|||
map(s => !!s && !s.isSingleton)); |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
|
|||
Microsoft Visual Studio Solution File, Format Version 12.00 |
|||
# Visual Studio 15 |
|||
VisualStudioVersion = 15.0.27703.2042 |
|||
MinimumVisualStudioVersion = 10.0.40219.1 |
|||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GenerateLanguages", "GenerateLanguages.csproj", "{8421C72C-A305-4CDA-9413-715B4A095F56}" |
|||
EndProject |
|||
Global |
|||
GlobalSection(SolutionConfigurationPlatforms) = preSolution |
|||
Debug|Any CPU = Debug|Any CPU |
|||
Release|Any CPU = Release|Any CPU |
|||
EndGlobalSection |
|||
GlobalSection(ProjectConfigurationPlatforms) = postSolution |
|||
{8421C72C-A305-4CDA-9413-715B4A095F56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
|||
{8421C72C-A305-4CDA-9413-715B4A095F56}.Debug|Any CPU.Build.0 = Debug|Any CPU |
|||
{8421C72C-A305-4CDA-9413-715B4A095F56}.Release|Any CPU.ActiveCfg = Release|Any CPU |
|||
{8421C72C-A305-4CDA-9413-715B4A095F56}.Release|Any CPU.Build.0 = Release|Any CPU |
|||
EndGlobalSection |
|||
GlobalSection(SolutionProperties) = preSolution |
|||
HideSolutionNode = FALSE |
|||
EndGlobalSection |
|||
GlobalSection(ExtensibilityGlobals) = postSolution |
|||
SolutionGuid = {25A5F1D2-5109-48B1-B161-8F846145BEFB} |
|||
EndGlobalSection |
|||
EndGlobal |
|||
Loading…
Reference in new issue