From b9443b5e7eed29c4a8fbd4da4c4fb05d65625eb3 Mon Sep 17 00:00:00 2001 From: Bjoern Kimminich Date: Tue, 12 Dec 2017 14:55:35 +0100 Subject: [PATCH] Remove duplicate HTTP mocking --- test/client/navbarControllerSpec.js | 54 ++++----------------- test/client/productdetailsControllerSpec.js | 31 +++++++++--- 2 files changed, 33 insertions(+), 52 deletions(-) diff --git a/test/client/navbarControllerSpec.js b/test/client/navbarControllerSpec.js index e0a6741a92b..76f9855f4b2 100644 --- a/test/client/navbarControllerSpec.js +++ b/test/client/navbarControllerSpec.js @@ -5,6 +5,9 @@ describe('controllers', function () { beforeEach(inject(function ($injector) { $httpBackend = $injector.get('$httpBackend') $httpBackend.whenGET(/\/i18n\/.*\.json/).respond(200, {}) + $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) + $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) + $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) })) afterEach(function () { @@ -23,19 +26,13 @@ describe('controllers', function () { })) it('should be defined', inject(function () { - $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) - $httpBackend.flush() expect(controller).toBeDefined() })) it('should hold application version', inject(function () { - $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {version: 'x.y.z'}) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) + $httpBackend.expectGET('/rest/admin/application-version').respond(200, {version: 'x.y.z'}) $httpBackend.flush() @@ -43,9 +40,7 @@ describe('controllers', function () { })) it('should show nothing on missing application version', inject(function () { - $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) + $httpBackend.expectGET('/rest/admin/application-version').respond(200, {}) $httpBackend.flush() @@ -53,9 +48,7 @@ describe('controllers', function () { })) it('should show nothing on error retrieving application version', inject(function () { - $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) - $httpBackend.whenGET('/rest/admin/application-version').respond(500) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) + $httpBackend.expectGET('/rest/admin/application-version').respond(500) $httpBackend.flush() @@ -63,9 +56,7 @@ describe('controllers', function () { })) it('should log errors directly to browser console', inject(function () { - $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) - $httpBackend.whenGET('/rest/admin/application-version').respond(500, 'error') - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) + $httpBackend.expectGET('/rest/admin/application-version').respond(500, 'error') console.log = jasmine.createSpy('log') @@ -74,30 +65,14 @@ describe('controllers', function () { expect(console.log).toHaveBeenCalledWith('error') })) - it('should be defined', inject(function () { - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) - $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) - - $httpBackend.flush() - - expect(controller).toBeDefined() - })) - it('should use default application name if not customized', inject(function () { - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) - $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) - $httpBackend.flush() expect(scope.applicationName).toBe('OWASP Juice Shop') })) it('should use custom application name URL if configured', inject(function () { - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) $httpBackend.expectGET('/rest/admin/application-configuration').respond(200, {config: {application: {name: 'name'}}}) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) $httpBackend.flush() @@ -105,9 +80,8 @@ describe('controllers', function () { })) it('should set user email if user authenticated', inject(function () { - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) $httpBackend.expectGET('/rest/admin/application-configuration').respond(200, {config: {application: {name: 'name'}}}) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {email: 'dummy@dummy.com'}}) + $httpBackend.expectGET('/rest/user/whoami').respond(200, {user: {email: 'dummy@dummy.com'}}) $httpBackend.flush() @@ -115,9 +89,7 @@ describe('controllers', function () { })) it('should log errors directly to browser console when getting user failed', inject(function () { - $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {config: {application: {name: 'name'}}}) - $httpBackend.whenGET('/rest/user/whoami').respond(500, 'error-user') + $httpBackend.expectGET('/rest/user/whoami').respond(500, 'error-user') console.log = jasmine.createSpy('log') @@ -127,19 +99,13 @@ describe('controllers', function () { })) it('should show GitHub ribbon by default', inject(function () { - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) - $httpBackend.whenGET('/rest/admin/application-configuration').respond(200, {config: {}}) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) - $httpBackend.flush() expect(scope.showGitHubRibbon).toBe(true) })) it('should hide GitHub ribbon if configured as such', inject(function () { - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) $httpBackend.expectGET('/rest/admin/application-configuration').respond(200, {config: {application: {showGitHubRibbon: false}}}) - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) $httpBackend.flush() @@ -147,9 +113,7 @@ describe('controllers', function () { })) it('should log error while getting application configuration from backend API directly to browser console', inject(function () { - $httpBackend.whenGET('/rest/admin/application-version').respond(200, {}) $httpBackend.expectGET('/rest/admin/application-configuration').respond(500, 'error') - $httpBackend.whenGET('/rest/user/whoami').respond(200, {user: {}}) console.log = jasmine.createSpy('log') diff --git a/test/client/productdetailsControllerSpec.js b/test/client/productdetailsControllerSpec.js index c399f1653bd..e3dad57607a 100644 --- a/test/client/productdetailsControllerSpec.js +++ b/test/client/productdetailsControllerSpec.js @@ -5,6 +5,7 @@ describe('controllers', function () { beforeEach(inject(function ($injector) { $httpBackend = $injector.get('$httpBackend') $httpBackend.whenGET(/\/i18n\/.*\.json/).respond(200, {}) + $httpBackend.whenGET(/\/api\/Products\/42/).respond(200, {data: {}}) $httpBackend.whenGET('/rest/product/42/reviews').respond(200, {data: {}}) $httpBackend.whenGET('/rest/user/whoami').respond(200, {data: {}}) $sce = $injector.get('$sce') @@ -25,15 +26,13 @@ describe('controllers', function () { })) it('should be defined', inject(function () { - $httpBackend.whenGET(/\/api\/Products\/42/).respond(200, {data: {}}) - $httpBackend.flush() expect(controller).toBeDefined() })) it('should hold single product with given id', inject(function () { - $httpBackend.whenGET(/\/api\/Products\/42/).respond(200, {data: {name: 'Test Juice'}}) + $httpBackend.expectGET(/\/api\/Products\/42/).respond(200, {data: {name: 'Test Juice'}}) $httpBackend.flush() @@ -42,7 +41,7 @@ describe('controllers', function () { })) it('should render product description as trusted HTML', inject(function () { - $httpBackend.whenGET(/\/api\/Products\/42/).respond(200, {data: {description: ''}}) + $httpBackend.expectGET(/\/api\/Products\/42/).respond(200, {data: {description: ''}}) spyOn($sce, 'trustAsHtml') $httpBackend.flush() @@ -51,15 +50,33 @@ describe('controllers', function () { })) it('should hold no product if API call fails', inject(function () { - $httpBackend.whenGET(/\/api\/Products\/42/).respond(500) + $httpBackend.expectGET(/\/api\/Products\/42/).respond(500) $httpBackend.flush() expect(scope.product).toBeUndefined() })) - it('should log errors directly to browser console', inject(function () { - $httpBackend.whenGET(/\/api\/Products\/42/).respond(500, 'error') + it('should log errors when retrieving product directly to browser console', inject(function () { + $httpBackend.expectGET(/\/api\/Products\/42/).respond(500, 'error') + console.log = jasmine.createSpy('log') + + $httpBackend.flush() + + expect(console.log).toHaveBeenCalledWith('error') + })) + + it('should log errors when retrieving reviews directly to browser console', inject(function () { + $httpBackend.expectGET('/rest/product/42/reviews').respond(500, 'error') + console.log = jasmine.createSpy('log') + + $httpBackend.flush() + + expect(console.log).toHaveBeenCalledWith('error') + })) + + it('should log errors when retrieving user directly to browser console', inject(function () { + $httpBackend.expectGET('/rest/user/whoami').respond(500, 'error') console.log = jasmine.createSpy('log') $httpBackend.flush()