Skip to content

Commit

Permalink
Remove duplicate HTTP mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
bkimminich committed Dec 12, 2017
1 parent 7a3832e commit b9443b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 52 deletions.
54 changes: 9 additions & 45 deletions test/client/navbarControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -23,49 +26,37 @@ 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()

expect(scope.version).toBe('vx.y.z')
}))

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()

expect(scope.version).toBe('')
}))

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()

expect(scope.version).toBe('')
}))

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')

Expand All @@ -74,50 +65,31 @@ 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()

expect(scope.applicationName).toBe('name')
}))

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: '[email protected]'}})
$httpBackend.expectGET('/rest/user/whoami').respond(200, {user: {email: '[email protected]'}})

$httpBackend.flush()

expect(scope.userEmail).toBe('[email protected]')
}))

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')

Expand All @@ -127,29 +99,21 @@ 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()

expect(scope.showGitHubRibbon).toBe(false)
}))

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')

Expand Down
31 changes: 24 additions & 7 deletions test/client/productdetailsControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()

Expand All @@ -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: '<script>alert("XSS3")</script>'}})
$httpBackend.expectGET(/\/api\/Products\/42/).respond(200, {data: {description: '<script>alert("XSS3")</script>'}})
spyOn($sce, 'trustAsHtml')

$httpBackend.flush()
Expand All @@ -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()
Expand Down

0 comments on commit b9443b5

Please sign in to comment.