Browse Source

Tests for guards.

pull/282/head
Sebastian Stehle 9 years ago
parent
commit
1040d8e346
  1. 4
      src/Squidex/app/shared/guards/resolve-content.guard.ts
  2. 102
      src/Squidex/app/shared/guards/resolve-published-schema.guard.spec.ts
  3. 52
      src/Squidex/app/shared/guards/resolve-published-schema.guard.ts
  4. 85
      src/Squidex/app/shared/guards/resolve-schema.guard.spec.ts
  5. 45
      src/Squidex/app/shared/guards/resolve-schema.guard.ts
  6. 77
      src/Squidex/app/shared/guards/schema-must-exist-published.guard.spec.ts
  7. 4
      src/Squidex/app/shared/guards/schema-must-exist-published.guard.ts
  8. 62
      src/Squidex/app/shared/guards/schema-must-exist.guard.spec.ts
  9. 4
      src/Squidex/app/shared/guards/schema-must-exist.guard.ts
  10. 2
      src/Squidex/app/shared/internal.ts
  11. 4
      src/Squidex/app/shared/module.ts

4
src/Squidex/app/shared/guards/resolve-content.guard.ts

@ -6,7 +6,7 @@
*/ */
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router'; import { ActivatedRouteSnapshot, Resolve, Router } from '@angular/router';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { allParams } from '@app/framework'; import { allParams } from '@app/framework';
@ -21,7 +21,7 @@ export class ResolveContentGuard implements Resolve<ContentDto | null> {
) { ) {
} }
public resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<ContentDto | null> { public resolve(route: ActivatedRouteSnapshot): Observable<ContentDto | null> {
const params = allParams(route); const params = allParams(route);
const appName = params['appName']; const appName = params['appName'];

102
src/Squidex/app/shared/guards/resolve-published-schema.guard.spec.ts

@ -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());
});
});

52
src/Squidex/app/shared/guards/resolve-published-schema.guard.ts

@ -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;
}
}

85
src/Squidex/app/shared/guards/resolve-schema.guard.spec.ts

@ -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());
});
});

45
src/Squidex/app/shared/guards/resolve-schema.guard.ts

@ -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;
}
}

77
src/Squidex/app/shared/guards/schema-must-exist-published.guard.spec.ts

@ -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());
});
});

4
src/Squidex/app/shared/guards/schema-must-exist-published.guard.ts

@ -6,7 +6,7 @@
*/ */
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { allParams } from '@app/framework'; import { allParams } from '@app/framework';
@ -21,7 +21,7 @@ export class SchemaMustExistPublishedGuard implements CanActivate {
) { ) {
} }
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { public canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
const schemaName = allParams(route)['schemaName']; const schemaName = allParams(route)['schemaName'];
const result = const result =

62
src/Squidex/app/shared/guards/schema-must-exist.guard.spec.ts

@ -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());
});
});

4
src/Squidex/app/shared/guards/schema-must-exist.guard.ts

@ -6,7 +6,7 @@
*/ */
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { allParams } from '@app/framework'; import { allParams } from '@app/framework';
@ -21,7 +21,7 @@ export class SchemaMustExistGuard implements CanActivate {
) { ) {
} }
public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> { public canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
const schemaName = allParams(route)['schemaName']; const schemaName = allParams(route)['schemaName'];
const result = const result =

2
src/Squidex/app/shared/internal.ts

@ -11,8 +11,6 @@ export * from './guards/must-be-authenticated.guard';
export * from './guards/must-be-not-authenticated.guard'; export * from './guards/must-be-not-authenticated.guard';
export * from './guards/resolve-app-languages.guard'; export * from './guards/resolve-app-languages.guard';
export * from './guards/resolve-content.guard'; export * from './guards/resolve-content.guard';
export * from './guards/resolve-published-schema.guard';
export * from './guards/resolve-schema.guard';
export * from './guards/schema-must-exist-published.guard'; export * from './guards/schema-must-exist-published.guard';
export * from './guards/schema-must-exist.guard'; export * from './guards/schema-must-exist.guard';
export * from './guards/unset-app.guard'; export * from './guards/unset-app.guard';

4
src/Squidex/app/shared/module.ts

@ -49,8 +49,6 @@ import {
PlansService, PlansService,
ResolveAppLanguagesGuard, ResolveAppLanguagesGuard,
ResolveContentGuard, ResolveContentGuard,
ResolvePublishedSchemaGuard,
ResolveSchemaGuard,
SchemaMustExistGuard, SchemaMustExistGuard,
SchemaMustExistPublishedGuard, SchemaMustExistPublishedGuard,
SchemasService, SchemasService,
@ -150,8 +148,6 @@ export class SqxSharedModule {
PlansService, PlansService,
ResolveAppLanguagesGuard, ResolveAppLanguagesGuard,
ResolveContentGuard, ResolveContentGuard,
ResolvePublishedSchemaGuard,
ResolveSchemaGuard,
RulesService, RulesService,
SchemaMustExistGuard, SchemaMustExistGuard,
SchemaMustExistPublishedGuard, SchemaMustExistPublishedGuard,

Loading…
Cancel
Save