<NME> app.guard.spec.ts
<BEF> /*
 * Copyright (c) 2014-2022 Bjoern Kimminich & the OWASP Juice Shop contributors.
 * SPDX-License-Identifier: MIT
 */

import { inject, TestBed } from '@angular/core/testing'
import { AccountingGuard, AdminGuard, DeluxeGuard, LoginGuard } from './app.guard'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { RouterTestingModule } from '@angular/router/testing'
import { ErrorPageComponent } from './error-page/error-page.component'

describe('LoginGuard', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
        RouterTestingModule.withRoutes([
          { path: '403', component: ErrorPageComponent }
        ]
        )],
      providers: [LoginGuard]
    })
  })

  it('should be created', inject([LoginGuard], (guard: LoginGuard) => {
    expect(guard).toBeTruthy()
  }))
})

describe('AdminGuard', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,
        RouterTestingModule.withRoutes([
            { path: '403', component: ErrorPageComponent }
        ]
        )],
      providers: [AdminGuard, LoginGuard]
    })
  })

  it('should be created', inject([AdminGuard], (guard: AdminGuard) => {
    expect(guard).toBeTruthy()
  }))
})

describe('AccountingGuard', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,

            { path: '403', component: ErrorPageComponent }
        ]
        )],
      providers: [AccountingGuard, LoginGuard]
    })
  })

  it('should be created', inject([AccountingGuard], (guard: AccountingGuard) => {
    expect(guard).toBeTruthy()
  }))
})

describe('DeluxeGuard', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        HttpClientTestingModule,

            { path: '403', component: ErrorPageComponent }
        ]
        )],
      providers: [DeluxeGuard, LoginGuard]
    })
  })

  it('should be created', inject([DeluxeGuard], (guard: DeluxeGuard) => {
    expect(guard).toBeTruthy()
  }))
})
        { provide: LoginGuard, useValue: loginGuard }
      ]
    })
  })

  it('should be created', inject([DeluxeGuard], (guard: DeluxeGuard) => {
    expect(guard).toBeTruthy()
  }))

  it('should open for deluxe customers', inject([DeluxeGuard], (guard: DeluxeGuard) => {
    loginGuard.tokenDecode.and.returnValue({ data: { role: 'deluxe' } })
    expect(guard.isDeluxe()).toBeTrue()
  }))

  it('should close for regular customers', inject([DeluxeGuard], (guard: DeluxeGuard) => {
    loginGuard.tokenDecode.and.returnValue({ data: { role: 'customer' } })
    expect(guard.isDeluxe()).toBeFalse()
  }))

  it('should close for admins', inject([DeluxeGuard], (guard: DeluxeGuard) => {
    loginGuard.tokenDecode.and.returnValue({ data: { role: 'admin' } })
    expect(guard.isDeluxe()).toBeFalse()
  }))

  it('should close for accountants', inject([DeluxeGuard], (guard: DeluxeGuard) => {
    loginGuard.tokenDecode.and.returnValue({ data: { role: 'accounting' } })
    expect(guard.isDeluxe()).toBeFalse()
  }))
})

<MSG> Add functional unit tests for admin, accounting and deluxe guards

<DFF> @@ -28,25 +28,59 @@ describe('LoginGuard', () => {
 })
 
 describe('AdminGuard', () => {
+  let loginGuard: any
+
   beforeEach(() => {
+    loginGuard = jasmine.createSpyObj('LoginGuard',['tokenDecode', 'forbidRoute'])
+
     TestBed.configureTestingModule({
       imports: [
         HttpClientTestingModule,
         RouterTestingModule.withRoutes([
             { path: '403', component: ErrorPageComponent }
-        ]
+          ]
         )],
-      providers: [AdminGuard, LoginGuard]
+      providers: [
+        AdminGuard,
+        { provide: LoginGuard, useValue: loginGuard }
+      ]
     })
   })
 
   it('should be created', inject([AdminGuard], (guard: AdminGuard) => {
     expect(guard).toBeTruthy()
   }))
+
+  it('should open for admins', inject([AdminGuard], (guard: AdminGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'admin' } })
+    expect(guard.canActivate()).toBeTrue()
+  }))
+
+  it('should close for regular customers', inject([AdminGuard], (guard: AdminGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'customer' } })
+    expect(guard.canActivate()).toBeFalse()
+    expect(loginGuard.forbidRoute).toHaveBeenCalled()
+  }))
+
+  it('should close for deluxe customers', inject([AdminGuard], (guard: AdminGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'deluxe' } })
+    expect(guard.canActivate()).toBeFalse()
+    expect(loginGuard.forbidRoute).toHaveBeenCalled()
+  }))
+
+  it('should close for accountants', inject([AdminGuard], (guard: AdminGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'accounting' } })
+    expect(guard.canActivate()).toBeFalse()
+    expect(loginGuard.forbidRoute).toHaveBeenCalled()
+  }))
 })
 
 describe('AccountingGuard', () => {
+  let loginGuard: any
+
   beforeEach(() => {
+    loginGuard = jasmine.createSpyObj('LoginGuard',['tokenDecode', 'forbidRoute'])
+
     TestBed.configureTestingModule({
       imports: [
         HttpClientTestingModule,
@@ -54,17 +88,47 @@ describe('AccountingGuard', () => {
             { path: '403', component: ErrorPageComponent }
         ]
         )],
-      providers: [AccountingGuard, LoginGuard]
+      providers: [
+        AccountingGuard,
+        { provide: LoginGuard, useValue: loginGuard }
+      ]
     })
   })
 
   it('should be created', inject([AccountingGuard], (guard: AccountingGuard) => {
     expect(guard).toBeTruthy()
   }))
+
+  it('should open for accountants', inject([AccountingGuard], (guard: AccountingGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'accounting' } })
+    expect(guard.canActivate()).toBeTrue()
+  }))
+
+  it('should close for regular customers', inject([AccountingGuard], (guard: AccountingGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'customer' } })
+    expect(guard.canActivate()).toBeFalse()
+    expect(loginGuard.forbidRoute).toHaveBeenCalled()
+  }))
+
+  it('should close for deluxe customers', inject([AccountingGuard], (guard: AccountingGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'deluxe' } })
+    expect(guard.canActivate()).toBeFalse()
+    expect(loginGuard.forbidRoute).toHaveBeenCalled()
+  }))
+
+  it('should close for admins', inject([AccountingGuard], (guard: AccountingGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'admin' } })
+    expect(guard.canActivate()).toBeFalse()
+    expect(loginGuard.forbidRoute).toHaveBeenCalled()
+  }))
 })
 
 describe('DeluxeGuard', () => {
+  let loginGuard: any
+
   beforeEach(() => {
+    loginGuard = jasmine.createSpyObj('LoginGuard',['tokenDecode'])
+
     TestBed.configureTestingModule({
       imports: [
         HttpClientTestingModule,
@@ -72,11 +136,34 @@ describe('DeluxeGuard', () => {
             { path: '403', component: ErrorPageComponent }
         ]
         )],
-      providers: [DeluxeGuard, LoginGuard]
+      providers: [
+        DeluxeGuard,
+        { provide: LoginGuard, useValue: loginGuard },
+      ]
     })
   })
 
   it('should be created', inject([DeluxeGuard], (guard: DeluxeGuard) => {
     expect(guard).toBeTruthy()
   }))
+
+  it('should open for deluxe customers', inject([DeluxeGuard], (guard: DeluxeGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'deluxe' } })
+    expect(guard.isDeluxe()).toBeTrue()
+  }))
+
+  it('should close for regular customers', inject([DeluxeGuard], (guard: DeluxeGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'customer' } })
+    expect(guard.isDeluxe()).toBeFalse()
+  }))
+
+  it('should close for admins', inject([DeluxeGuard], (guard: DeluxeGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'admin' } })
+    expect(guard.isDeluxe()).toBeFalse()
+  }))
+
+  it('should close for accountants', inject([DeluxeGuard], (guard: DeluxeGuard) => {
+    loginGuard.tokenDecode.and.returnValue({ data: { role: 'accounting' } })
+    expect(guard.isDeluxe()).toBeFalse()
+  }))
 })

{'addition_count': 91, 'commit_subject': 'Add functional unit tests for admin, accounting and deluxe guards', 'deletion_count': 4, 'file_extension': '.ts', 'lang': 'guard', 'license': 'mit', 'repo_name': 'bkimminich/juice-shop'}