mirror of https://github.com/Squidex/squidex.git
18 changed files with 121 additions and 787 deletions
@ -1,86 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|
||||
*/ |
|
||||
|
|
||||
import { IMock, Mock } from 'typemoq'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
|
|
||||
import { UserManagementService } from 'shared'; |
|
||||
|
|
||||
import { ResolveUserGuard } from './resolve-user.guard'; |
|
||||
import { RouterMockup } from './router-mockup'; |
|
||||
|
|
||||
describe('ResolveUserGuard', () => { |
|
||||
const route = { |
|
||||
params: {}, |
|
||||
parent: { |
|
||||
params: { |
|
||||
userId: 'my-user' |
|
||||
} |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
let usersService: IMock<UserManagementService>; |
|
||||
|
|
||||
beforeEach(() => { |
|
||||
usersService = Mock.ofType(UserManagementService); |
|
||||
}); |
|
||||
|
|
||||
it('should throw if route does not contain parameter', () => { |
|
||||
const guard = new ResolveUserGuard(usersService.object, <any>new RouterMockup()); |
|
||||
|
|
||||
expect(() => guard.resolve(<any>{ params: {} }, <any>{})).toThrow('Route must contain user id.'); |
|
||||
}); |
|
||||
|
|
||||
it('should navigate to 404 page if user is not found', (done) => { |
|
||||
usersService.setup(x => x.getUser('my-user')) |
|
||||
.returns(() => Observable.of(null!)); |
|
||||
const router = new RouterMockup(); |
|
||||
|
|
||||
const guard = new ResolveUserGuard(usersService.object, <any>router); |
|
||||
|
|
||||
guard.resolve(<any>route, <any>{}) |
|
||||
.subscribe(result => { |
|
||||
expect(result).toBeFalsy(); |
|
||||
expect(router.lastNavigation).toEqual(['/404']); |
|
||||
|
|
||||
done(); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
it('should navigate to 404 page if user loading fails', (done) => { |
|
||||
usersService.setup(x => x.getUser('my-user')) |
|
||||
.returns(() => Observable.throw(null!)); |
|
||||
const router = new RouterMockup(); |
|
||||
|
|
||||
const guard = new ResolveUserGuard(usersService.object, <any>router); |
|
||||
|
|
||||
guard.resolve(<any>route, <any>{}) |
|
||||
.subscribe(result => { |
|
||||
expect(result).toBeFalsy(); |
|
||||
expect(router.lastNavigation).toEqual(['/404']); |
|
||||
|
|
||||
done(); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
it('should return user if loading succeeded', (done) => { |
|
||||
const user: any = {}; |
|
||||
|
|
||||
usersService.setup(x => x.getUser('my-user')) |
|
||||
.returns(() => Observable.of(user)); |
|
||||
const router = new RouterMockup(); |
|
||||
|
|
||||
const guard = new ResolveUserGuard(usersService.object, <any>router); |
|
||||
|
|
||||
guard.resolve(<any>route, <any>{}) |
|
||||
.subscribe(result => { |
|
||||
expect(result).toBe(user); |
|
||||
|
|
||||
done(); |
|
||||
}); |
|
||||
}); |
|
||||
}); |
|
||||
@ -1,48 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|
||||
*/ |
|
||||
|
|
||||
import { Injectable } from '@angular/core'; |
|
||||
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
|
|
||||
import { allParams } from 'framework'; |
|
||||
|
|
||||
import { UserDto, UserManagementService } from './../services/users.service'; |
|
||||
|
|
||||
@Injectable() |
|
||||
export class ResolveUserGuard implements Resolve<UserDto | null> { |
|
||||
constructor( |
|
||||
private readonly userManagementService: UserManagementService, |
|
||||
private readonly router: Router |
|
||||
) { |
|
||||
} |
|
||||
|
|
||||
public resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<UserDto | null> { |
|
||||
const params = allParams(route); |
|
||||
|
|
||||
const userId = params['userId']; |
|
||||
|
|
||||
if (!userId) { |
|
||||
throw 'Route must contain user id.'; |
|
||||
} |
|
||||
|
|
||||
const result = |
|
||||
this.userManagementService.getUser(userId) |
|
||||
.do(dto => { |
|
||||
if (!dto) { |
|
||||
this.router.navigate(['/404']); |
|
||||
} |
|
||||
}) |
|
||||
.catch(error => { |
|
||||
this.router.navigate(['/404']); |
|
||||
|
|
||||
return Observable.of(null); |
|
||||
}); |
|
||||
|
|
||||
return result; |
|
||||
} |
|
||||
} |
|
||||
@ -1,132 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|
||||
*/ |
|
||||
|
|
||||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
|
||||
import { inject, TestBed } from '@angular/core/testing'; |
|
||||
|
|
||||
import { |
|
||||
ApiUrlConfig, |
|
||||
EventConsumerDto, |
|
||||
EventConsumersService |
|
||||
} from './../'; |
|
||||
|
|
||||
describe('EventConsumerDto', () => { |
|
||||
it('should update isStopped property when starting', () => { |
|
||||
const consumer_1 = new EventConsumerDto('consumer', true, false, 'error', 'position'); |
|
||||
const consumer_2 = consumer_1.start(); |
|
||||
|
|
||||
expect(consumer_2.isStopped).toBeFalsy(); |
|
||||
}); |
|
||||
|
|
||||
it('should update isStopped property when starting', () => { |
|
||||
const consumer_1 = new EventConsumerDto('consumer', false, false, 'error', 'position'); |
|
||||
const consumer_2 = consumer_1.stop(); |
|
||||
|
|
||||
expect(consumer_2.isStopped).toBeTruthy(); |
|
||||
}); |
|
||||
|
|
||||
it('should update isResetting property when resetting', () => { |
|
||||
const consumer_1 = new EventConsumerDto('consumer', false, false, 'error', 'position'); |
|
||||
const consumer_2 = consumer_1.reset(); |
|
||||
|
|
||||
expect(consumer_2.isResetting).toBeTruthy(); |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
describe('EventConsumersService', () => { |
|
||||
beforeEach(() => { |
|
||||
TestBed.configureTestingModule({ |
|
||||
imports: [ |
|
||||
HttpClientTestingModule |
|
||||
], |
|
||||
providers: [ |
|
||||
EventConsumersService, |
|
||||
{ provide: ApiUrlConfig, useValue: new ApiUrlConfig('http://service/p/') } |
|
||||
] |
|
||||
}); |
|
||||
}); |
|
||||
|
|
||||
afterEach(inject([HttpTestingController], (httpMock: HttpTestingController) => { |
|
||||
httpMock.verify(); |
|
||||
})); |
|
||||
|
|
||||
it('should make get request to get event consumers', |
|
||||
inject([EventConsumersService, HttpTestingController], (eventConsumersService: EventConsumersService, httpMock: HttpTestingController) => { |
|
||||
|
|
||||
let eventConsumers: EventConsumerDto[] | null = null; |
|
||||
|
|
||||
eventConsumersService.getEventConsumers().subscribe(result => { |
|
||||
eventConsumers = result; |
|
||||
}); |
|
||||
|
|
||||
const req = httpMock.expectOne('http://service/p/api/event-consumers'); |
|
||||
|
|
||||
expect(req.request.method).toEqual('GET'); |
|
||||
expect(req.request.headers.get('If-Match')).toBeNull(); |
|
||||
|
|
||||
req.flush([ |
|
||||
{ |
|
||||
name: 'event-consumer1', |
|
||||
position: '13', |
|
||||
isStopped: true, |
|
||||
isResetting: true, |
|
||||
error: 'an error 1' |
|
||||
}, |
|
||||
{ |
|
||||
name: 'event-consumer2', |
|
||||
position: '29', |
|
||||
isStopped: true, |
|
||||
isResetting: true, |
|
||||
error: 'an error 2' |
|
||||
} |
|
||||
]); |
|
||||
|
|
||||
expect(eventConsumers).toEqual([ |
|
||||
new EventConsumerDto('event-consumer1', true, true, 'an error 1', '13'), |
|
||||
new EventConsumerDto('event-consumer2', true, true, 'an error 2', '29') |
|
||||
]); |
|
||||
})); |
|
||||
|
|
||||
it('should make put request to start event consumer', |
|
||||
inject([EventConsumersService, HttpTestingController], (eventConsumersService: EventConsumersService, httpMock: HttpTestingController) => { |
|
||||
|
|
||||
eventConsumersService.startEventConsumer('event-consumer1').subscribe(); |
|
||||
|
|
||||
const req = httpMock.expectOne('http://service/p/api/event-consumers/event-consumer1/start'); |
|
||||
|
|
||||
expect(req.request.method).toEqual('PUT'); |
|
||||
expect(req.request.headers.get('If-Match')).toBeNull(); |
|
||||
|
|
||||
req.flush({}); |
|
||||
})); |
|
||||
|
|
||||
it('should make put request to stop event consumer', |
|
||||
inject([EventConsumersService, HttpTestingController], (eventConsumersService: EventConsumersService, httpMock: HttpTestingController) => { |
|
||||
|
|
||||
eventConsumersService.stopEventConsumer('event-consumer1').subscribe(); |
|
||||
|
|
||||
const req = httpMock.expectOne('http://service/p/api/event-consumers/event-consumer1/stop'); |
|
||||
|
|
||||
expect(req.request.method).toEqual('PUT'); |
|
||||
expect(req.request.headers.get('If-Match')).toBeNull(); |
|
||||
|
|
||||
req.flush({}); |
|
||||
})); |
|
||||
|
|
||||
it('should make put request to reset event consumer', |
|
||||
inject([EventConsumersService, HttpTestingController], (eventConsumersService: EventConsumersService, httpMock: HttpTestingController) => { |
|
||||
|
|
||||
eventConsumersService.resetEventConsumer('event-consumer1').subscribe(); |
|
||||
|
|
||||
const req = httpMock.expectOne('http://service/p/api/event-consumers/event-consumer1/reset'); |
|
||||
|
|
||||
expect(req.request.method).toEqual('PUT'); |
|
||||
expect(req.request.headers.get('If-Match')).toBeNull(); |
|
||||
|
|
||||
req.flush({}); |
|
||||
})); |
|
||||
}); |
|
||||
@ -1,88 +0,0 @@ |
|||||
/* |
|
||||
* Squidex Headless CMS |
|
||||
* |
|
||||
* @license |
|
||||
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. |
|
||||
*/ |
|
||||
|
|
||||
import { HttpClient } from '@angular/common/http'; |
|
||||
import { Injectable } from '@angular/core'; |
|
||||
import { Observable } from 'rxjs'; |
|
||||
|
|
||||
import 'framework/angular/http-extensions'; |
|
||||
|
|
||||
import { ApiUrlConfig, HTTP } from 'framework'; |
|
||||
|
|
||||
export class EventConsumerDto { |
|
||||
constructor( |
|
||||
public readonly name: string, |
|
||||
public readonly isStopped: boolean, |
|
||||
public readonly isResetting: boolean, |
|
||||
public readonly error: string, |
|
||||
public readonly position: string |
|
||||
) { |
|
||||
} |
|
||||
|
|
||||
public start(): EventConsumerDto { |
|
||||
return new EventConsumerDto(this.name, false, false, this.error, this.position); |
|
||||
} |
|
||||
|
|
||||
public stop(): EventConsumerDto { |
|
||||
return new EventConsumerDto(this.name, true, false, this.error, this.position); |
|
||||
} |
|
||||
|
|
||||
public reset(): EventConsumerDto { |
|
||||
return new EventConsumerDto(this.name, this.isStopped, true, this.error, this.position); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
@Injectable() |
|
||||
export class EventConsumersService { |
|
||||
constructor( |
|
||||
private readonly http: HttpClient, |
|
||||
private readonly apiUrl: ApiUrlConfig |
|
||||
) { |
|
||||
} |
|
||||
|
|
||||
public getEventConsumers(): Observable<EventConsumerDto[]> { |
|
||||
const url = this.apiUrl.buildUrl('/api/event-consumers'); |
|
||||
|
|
||||
return HTTP.getVersioned<any>(this.http, url) |
|
||||
.map(response => { |
|
||||
const body = response.payload.body; |
|
||||
|
|
||||
const items: any[] = body; |
|
||||
|
|
||||
return items.map(item => { |
|
||||
return new EventConsumerDto( |
|
||||
item.name, |
|
||||
item.isStopped, |
|
||||
item.isResetting, |
|
||||
item.error, |
|
||||
item.position); |
|
||||
}); |
|
||||
}) |
|
||||
.pretifyError('Failed to load event consumers. Please reload.'); |
|
||||
} |
|
||||
|
|
||||
public startEventConsumer(name: string): Observable<any> { |
|
||||
const url = this.apiUrl.buildUrl(`api/event-consumers/${name}/start`); |
|
||||
|
|
||||
return HTTP.putVersioned(this.http, url, {}) |
|
||||
.pretifyError('Failed to start event consumer. Please reload.'); |
|
||||
} |
|
||||
|
|
||||
public stopEventConsumer(name: string): Observable<any> { |
|
||||
const url = this.apiUrl.buildUrl(`api/event-consumers/${name}/stop`); |
|
||||
|
|
||||
return HTTP.putVersioned(this.http, url, {}) |
|
||||
.pretifyError('Failed to stop event consumer. Please reload.'); |
|
||||
} |
|
||||
|
|
||||
public resetEventConsumer(name: string): Observable<any> { |
|
||||
const url = this.apiUrl.buildUrl(`api/event-consumers/${name}/reset`); |
|
||||
|
|
||||
return HTTP.putVersioned(this.http, url, {}) |
|
||||
.pretifyError('Failed to reset event consumer. Please reload.'); |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue