juice-shop

Форк
0
/
two-factor-auth.component.spec.ts 
158 строк · 6.4 Кб
1
/*
2
 * Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3
 * SPDX-License-Identifier: MIT
4
 */
5

6
import { type ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'
7
import { TwoFactorAuthComponent } from './two-factor-auth.component'
8

9
import { ReactiveFormsModule } from '@angular/forms'
10
import { HttpClientTestingModule } from '@angular/common/http/testing'
11
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
12

13
import { TranslateModule } from '@ngx-translate/core'
14

15
import { MatCardModule } from '@angular/material/card'
16
import { MatFormFieldModule } from '@angular/material/form-field'
17
import { MatButtonModule } from '@angular/material/button'
18
import { MatInputModule } from '@angular/material/input'
19
import { MatCheckboxModule } from '@angular/material/checkbox'
20
import { MatIconModule } from '@angular/material/icon'
21
import { MatTableModule } from '@angular/material/table'
22
import { MatPaginatorModule } from '@angular/material/paginator'
23
import { MatDialogModule } from '@angular/material/dialog'
24
import { MatDividerModule } from '@angular/material/divider'
25
import { MatSnackBarModule } from '@angular/material/snack-bar'
26
import { MatTooltipModule } from '@angular/material/tooltip'
27

28
import { QRCodeModule } from 'anuglar2-qrcode'
29
import { of } from 'rxjs'
30
import { ConfigurationService } from '../Services/configuration.service'
31
import { TwoFactorAuthService } from '../Services/two-factor-auth-service'
32
import { throwError } from 'rxjs/internal/observable/throwError'
33

34
describe('TwoFactorAuthComponent', () => {
35
  let component: TwoFactorAuthComponent
36
  let fixture: ComponentFixture<TwoFactorAuthComponent>
37
  let twoFactorAuthService: any
38
  let configurationService: any
39

40
  beforeEach(waitForAsync(() => {
41
    twoFactorAuthService = jasmine.createSpyObj('TwoFactorAuthService', ['status', 'setup', 'disable'])
42
    configurationService = jasmine.createSpyObj('ConfigurationService', ['getApplicationConfiguration'])
43
    configurationService.getApplicationConfiguration.and.returnValue(of({ application: { } }))
44
    TestBed.configureTestingModule({
45
      declarations: [TwoFactorAuthComponent],
46
      imports: [
47
        HttpClientTestingModule,
48
        ReactiveFormsModule,
49
        TranslateModule.forRoot(),
50
        BrowserAnimationsModule,
51
        MatCheckboxModule,
52
        MatFormFieldModule,
53
        MatCardModule,
54
        MatIconModule,
55
        MatInputModule,
56
        MatTableModule,
57
        MatPaginatorModule,
58
        MatDialogModule,
59
        MatDividerModule,
60
        MatButtonModule,
61
        QRCodeModule,
62
        MatSnackBarModule,
63
        MatTooltipModule
64
      ],
65
      providers: [
66
        { provide: ConfigurationService, useValue: configurationService },
67
        { provide: TwoFactorAuthService, useValue: twoFactorAuthService }
68
      ]
69
    }).compileComponents()
70
  }))
71

72
  beforeEach(() => {
73
    fixture = TestBed.createComponent(TwoFactorAuthComponent)
74
    component = fixture.componentInstance
75
    fixture.detectChanges()
76
  })
77

78
  it('should compile', () => {
79
    expect(component).toBeTruthy()
80
  })
81

82
  it('should set TOTP secret and URL if 2FA is not already set up', () => {
83
    configurationService.getApplicationConfiguration.and.returnValue(of({ application: { name: 'Test App' } }))
84
    twoFactorAuthService.status.and.returnValue(of({ setup: false, email: 'email', secret: 'secret', setupToken: '12345' }))
85

86
    component.updateStatus()
87

88
    expect(component.setupStatus).toBe(false)
89
    expect(component.totpUrl).toBe('otpauth://totp/Test%20App:email?secret=secret&issuer=Test%20App')
90
    expect(component.totpSecret).toBe('secret')
91
  })
92

93
  it('should not set TOTP secret and URL if 2FA is already set up', () => {
94
    configurationService.getApplicationConfiguration.and.returnValue(of({ application: { name: 'Test App' } }))
95
    twoFactorAuthService.status.and.returnValue(of({ setup: true, email: 'email', secret: 'secret', setupToken: '12345' }))
96

97
    component.updateStatus()
98

99
    expect(component.setupStatus).toBe(true)
100
    expect(component.totpUrl).toBe(undefined)
101
    expect(component.totpSecret).toBe(undefined)
102
  })
103

104
  it('should confirm successful setup of 2FA', () => {
105
    twoFactorAuthService.setup.and.returnValue(of({}))
106
    component.setupStatus = false
107
    component.twoFactorSetupForm.get('passwordControl').setValue('password')
108
    component.twoFactorSetupForm.get('initalTokenControl').setValue('12345')
109

110
    component.setup()
111

112
    expect(component.setupStatus).toBe(true)
113
    expect(twoFactorAuthService.setup).toHaveBeenCalledWith('password', '12345', undefined)
114
  })
115

116
  it('should reset and mark form as errored when 2FA setup fails', () => {
117
    twoFactorAuthService.setup.and.returnValue(throwError(new Error('Error')))
118
    component.setupStatus = false
119
    component.errored = false
120
    component.twoFactorSetupForm.get('passwordControl').markAsDirty()
121
    component.twoFactorSetupForm.get('initalTokenControl').markAsDirty()
122

123
    expect(component.twoFactorSetupForm.get('passwordControl').pristine).toBe(false)
124
    expect(component.twoFactorSetupForm.get('initalTokenControl').pristine).toBe(false)
125
    component.setup()
126

127
    expect(component.setupStatus).toBe(false)
128
    expect(component.errored).toBe(true)
129
    expect(component.twoFactorSetupForm.get('passwordControl').pristine).toBe(true)
130
    expect(component.twoFactorSetupForm.get('initalTokenControl').pristine).toBe(true)
131
  })
132

133
  it('should confirm successfully disabling 2FA', () => {
134
    twoFactorAuthService.status.and.returnValue(of({ setup: true, email: 'email', secret: 'secret', setupToken: '12345' }))
135
    twoFactorAuthService.disable.and.returnValue(of({}))
136
    component.setupStatus = true
137
    component.twoFactorDisableForm.get('passwordControl').setValue('password')
138

139
    component.disable()
140

141
    expect(component.setupStatus).toBe(false)
142
    expect(twoFactorAuthService.disable).toHaveBeenCalledWith('password')
143
  })
144

145
  it('should reset and mark form as errored when disabling 2FA fails', () => {
146
    twoFactorAuthService.disable.and.returnValue(throwError(new Error('Error')))
147
    component.setupStatus = true
148
    component.errored = false
149
    component.twoFactorDisableForm.get('passwordControl').markAsDirty()
150

151
    expect(component.twoFactorDisableForm.get('passwordControl').pristine).toBe(false)
152
    component.disable()
153

154
    expect(component.setupStatus).toBe(true)
155
    expect(component.errored).toBe(true)
156
    expect(component.twoFactorDisableForm.get('passwordControl').pristine).toBe(true)
157
  })
158
})
159

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.