From 0b9dd50717d9f4c896f6e7bf93800ddca459637e Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 25 Nov 2016 00:35:28 +0100 Subject: [PATCH] More tests --- .../services/app-clients.service.spec.ts | 46 +++++++++++++++++++ .../shared/services/app-clients.service.ts | 2 +- .../app/shared/services/apps.service.spec.ts | 17 ++++--- .../app/shared/services/apps.service.ts | 6 +-- 4 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/Squidex/app/shared/services/app-clients.service.spec.ts b/src/Squidex/app/shared/services/app-clients.service.spec.ts index b3c96490a..52fbdc8b7 100644 --- a/src/Squidex/app/shared/services/app-clients.service.spec.ts +++ b/src/Squidex/app/shared/services/app-clients.service.spec.ts @@ -91,6 +91,52 @@ describe('AppClientsService', () => { authService.verifyAll(); }); + it('should throw fallback error on 500 when creating client failed', () => { + const createClient = new AppClientCreateDto('client1'); + + authService.setup(x => x.authPost('http://service/p/api/apps/my-app/clients', TypeMoq.It.is(c => c === createClient))) + .returns(() => Observable.throw( + new Ng2Http.Response( + new Ng2Http.ResponseOptions({ + status: 500 + }) + ) + )); + + let error = ''; + + appClientsService.postClient('my-app', createClient).subscribe(x => {}, result => { + error = result; + }).unsubscribe(); + + expect(error).toBe('A new client could not be created.'); + + authService.verifyAll(); + }); + + it('should throw duplicate error on 400 when creating client failed', () => { + const createClient = new AppClientCreateDto('client1'); + + authService.setup(x => x.authPost('http://service/p/api/apps/my-app/clients', TypeMoq.It.is(c => c === createClient))) + .returns(() => Observable.throw( + new Ng2Http.Response( + new Ng2Http.ResponseOptions({ + status: 400 + }) + ) + )); + + let error = ''; + + appClientsService.postClient('my-app', createClient).subscribe(x => {}, result => { + error = result; + }).unsubscribe(); + + expect(error).toBe('A client with the same name already exists.'); + + authService.verifyAll(); + }); + it('should make delete request to remove client', () => { authService.setup(x => x.authDelete('http://service/p/api/apps/my-app/clients/client1')) .returns(() => Observable.of( diff --git a/src/Squidex/app/shared/services/app-clients.service.ts b/src/Squidex/app/shared/services/app-clients.service.ts index 8995fa4f3..631c0db38 100644 --- a/src/Squidex/app/shared/services/app-clients.service.ts +++ b/src/Squidex/app/shared/services/app-clients.service.ts @@ -54,7 +54,7 @@ export class AppClientsService { .map(response => new AppClientDto(response.clientName, response.clientSecret, DateTime.parseISO_UTC(response.expiresUtc))) .catch(response => { if (response.status === 400) { - return Observable.throw('An client with the same name already exists.'); + return Observable.throw('A client with the same name already exists.'); } else { return Observable.throw('A new client could not be created.'); } diff --git a/src/Squidex/app/shared/services/apps.service.spec.ts b/src/Squidex/app/shared/services/apps.service.spec.ts index 113e5df12..0676b7a00 100644 --- a/src/Squidex/app/shared/services/apps.service.spec.ts +++ b/src/Squidex/app/shared/services/apps.service.spec.ts @@ -66,9 +66,10 @@ describe('AppsService', () => { }); it('should make post request to create app', () => { + const createApp = new AppCreateDto('new-app'); const now = DateTime.now(); - authService.setup(x => x.authPost('http://service/p/api/apps', TypeMoq.It.is(y => y['name'] === 'new-app'))) + authService.setup(x => x.authPost('http://service/p/api/apps', TypeMoq.It.is(a => a === createApp))) .returns(() => Observable.of( new Ng2Http.Response( new Ng2Http.ResponseOptions({ @@ -81,7 +82,7 @@ describe('AppsService', () => { let newApp: AppDto = null; - appsService.postApp(new AppCreateDto('new-app'), now).subscribe(result => { + appsService.postApp(createApp, now).subscribe(result => { newApp = result; }).unsubscribe(); @@ -91,7 +92,9 @@ describe('AppsService', () => { }); it('should throw fallback error on 500 when creating app failed', () => { - authService.setup(x => x.authPost('http://service/p/api/apps', TypeMoq.It.isAny())) + const createApp = new AppCreateDto('new-app'); + + authService.setup(x => x.authPost('http://service/p/api/apps', TypeMoq.It.is(a => a === createApp))) .returns(() => Observable.throw( new Ng2Http.Response( new Ng2Http.ResponseOptions({ @@ -102,7 +105,7 @@ describe('AppsService', () => { let error = ''; - appsService.postApp(new AppCreateDto('new-app')).subscribe(x => {}, result => { + appsService.postApp(createApp).subscribe(x => {}, result => { error = result; }).unsubscribe(); @@ -112,7 +115,9 @@ describe('AppsService', () => { }); it('should throw duplicate error on 400 when creating app failed', () => { - authService.setup(x => x.authPost('http://service/p/api/apps', TypeMoq.It.isAny())) + const createApp = new AppCreateDto('new-app'); + + authService.setup(x => x.authPost('http://service/p/api/apps', TypeMoq.It.is(a => a === createApp))) .returns(() => Observable.throw( new Ng2Http.Response( new Ng2Http.ResponseOptions({ @@ -123,7 +128,7 @@ describe('AppsService', () => { let error = ''; - appsService.postApp(new AppCreateDto('new-app')).subscribe(x => {}, result => { + appsService.postApp(createApp).subscribe(x => {}, result => { error = result; }).unsubscribe(); diff --git a/src/Squidex/app/shared/services/apps.service.ts b/src/Squidex/app/shared/services/apps.service.ts index a0c0033fb..d872f1cdf 100644 --- a/src/Squidex/app/shared/services/apps.service.ts +++ b/src/Squidex/app/shared/services/apps.service.ts @@ -55,12 +55,12 @@ export class AppsService { }); } - public postApp(appToCreate: AppCreateDto, now?: DateTime): Observable { + public postApp(app: AppCreateDto, now?: DateTime): Observable { now = now || DateTime.now(); - return this.authService.authPost(this.apiUrl.buildUrl('api/apps'), appToCreate) + return this.authService.authPost(this.apiUrl.buildUrl('api/apps'), app) .map(response => response.json()) - .map(response => new AppDto(response.id, appToCreate.name, now, now, 'Owner')) + .map(response => new AppDto(response.id, app.name, now, now, 'Owner')) .catch(response => { if (response.status === 400) { return Observable.throw('An app with the same name already exists.');