juice-shop

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

6
import { TranslateModule } from '@ngx-translate/core'
7
import { UserService } from '../Services/user.service'
8
import { HttpClientTestingModule } from '@angular/common/http/testing'
9
import { type ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing'
10
import { ChangePasswordComponent } from './change-password.component'
11
import { ReactiveFormsModule } from '@angular/forms'
12

13
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
14
import { MatButtonModule } from '@angular/material/button'
15
import { MatInputModule } from '@angular/material/input'
16
import { MatFormFieldModule } from '@angular/material/form-field'
17
import { MatCardModule } from '@angular/material/card'
18
import { of, throwError } from 'rxjs'
19

20
describe('ChangePasswordComponent', () => {
21
  let component: ChangePasswordComponent
22
  let fixture: ComponentFixture<ChangePasswordComponent>
23
  let userService: any
24

25
  beforeEach(waitForAsync(() => {
26
    userService = jasmine.createSpyObj('UserService', ['changePassword'])
27
    userService.changePassword.and.returnValue(of({}))
28

29
    TestBed.configureTestingModule({
30
      imports: [
31
        TranslateModule.forRoot(),
32
        ReactiveFormsModule,
33
        HttpClientTestingModule,
34
        BrowserAnimationsModule,
35
        MatCardModule,
36
        MatFormFieldModule,
37
        MatInputModule,
38
        MatButtonModule
39
      ],
40
      declarations: [ChangePasswordComponent],
41
      providers: [{ provide: UserService, useValue: userService }]
42
    })
43
      .compileComponents()
44
  }))
45

46
  beforeEach(() => {
47
    fixture = TestBed.createComponent(ChangePasswordComponent)
48
    component = fixture.componentInstance
49
    fixture.detectChanges()
50
  })
51

52
  it('should create', () => {
53
    expect(component).toBeTruthy()
54
  })
55

56
  it('should be compulsory to give password', () => {
57
    component.passwordControl.setValue('')
58
    expect(component.passwordControl.valid).toBeFalsy()
59
    component.passwordControl.setValue('pass')
60
    expect(component.passwordControl.valid).toBe(true)
61
  })
62

63
  it('length of new password must be 5-40 characters', () => {
64
    component.newPasswordControl.setValue('old')
65
    expect(component.newPasswordControl.valid).toBeFalsy()
66
    component.newPasswordControl.setValue('new password')
67
    expect(component.newPasswordControl.valid).toBe(true)
68
    component.newPasswordControl.setValue('new password new password')
69
    expect(component.newPasswordControl.valid).toBe(true)
70
    component.newPasswordControl.setValue('new password new password new password new password')
71
    expect(component.newPasswordControl.valid).toBeFalsy()
72
  })
73

74
  it('should be compulsory to repeat new password', () => {
75
    component.repeatNewPasswordControl.setValue('')
76
    expect(component.passwordControl.valid).toBeFalsy()
77
    component.newPasswordControl.setValue('passed')
78
    component.repeatNewPasswordControl.setValue('passed')
79
    expect(component.repeatNewPasswordControl.valid).toBe(true)
80
  })
81

82
  it('should reinitizalise forms by calling resetForm', () => {
83
    component.passwordControl.setValue('password')
84
    component.newPasswordControl.setValue('newPassword')
85
    component.repeatNewPasswordControl.setValue('newPassword')
86
    component.resetForm()
87
    expect(component.passwordControl.value).toBe('')
88
    expect(component.passwordControl.pristine).toBe(true)
89
    expect(component.passwordControl.untouched).toBe(true)
90
    expect(component.newPasswordControl.value).toBe('')
91
    expect(component.newPasswordControl.pristine).toBe(true)
92
    expect(component.newPasswordControl.untouched).toBe(true)
93
    expect(component.repeatNewPasswordControl.value).toBe('')
94
    expect(component.repeatNewPasswordControl.pristine).toBe(true)
95
    expect(component.repeatNewPasswordControl.untouched).toBe(true)
96
  })
97

98
  it('should clear form and show confirmation after changing password', () => {
99
    userService.changePassword.and.returnValue(of({}))
100
    spyOn(component, 'resetForm')
101
    component.passwordControl.setValue('old')
102
    component.newPasswordControl.setValue('foobar')
103
    component.repeatNewPasswordControl.setValue('foobar')
104
    component.changePassword()
105
    expect(component.error).toBeUndefined()
106
    expect(component.confirmation).toBeDefined()
107
    expect(component.resetForm).toHaveBeenCalled()
108
  })
109

110
  it('should clear form and gracefully handle error on password change', fakeAsync(() => {
111
    userService.changePassword.and.returnValue(throwError('Error'))
112
    spyOn(component, 'resetPasswords')
113
    console.log = jasmine.createSpy('log')
114
    component.passwordControl.setValue('old')
115
    component.newPasswordControl.setValue('foobar')
116
    component.repeatNewPasswordControl.setValue('fooabar')
117
    component.changePassword()
118
    expect(component.confirmation).toBeUndefined()
119
    expect(component.error).toBe('Error')
120
    expect(console.log).toHaveBeenCalledWith('Error')
121
    expect(component.resetPasswords).toHaveBeenCalled()
122
  }))
123
})
124

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

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

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

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