juice-shop
123 строки · 4.9 Кб
1/*
2* Copyright (c) 2014-2024 Bjoern Kimminich & the OWASP Juice Shop contributors.
3* SPDX-License-Identifier: MIT
4*/
5
6import { TranslateModule } from '@ngx-translate/core'
7import { UserService } from '../Services/user.service'
8import { HttpClientTestingModule } from '@angular/common/http/testing'
9import { type ComponentFixture, fakeAsync, TestBed, waitForAsync } from '@angular/core/testing'
10import { ChangePasswordComponent } from './change-password.component'
11import { ReactiveFormsModule } from '@angular/forms'
12
13import { BrowserAnimationsModule } from '@angular/platform-browser/animations'
14import { MatButtonModule } from '@angular/material/button'
15import { MatInputModule } from '@angular/material/input'
16import { MatFormFieldModule } from '@angular/material/form-field'
17import { MatCardModule } from '@angular/material/card'
18import { of, throwError } from 'rxjs'
19
20describe('ChangePasswordComponent', () => {
21let component: ChangePasswordComponent
22let fixture: ComponentFixture<ChangePasswordComponent>
23let userService: any
24
25beforeEach(waitForAsync(() => {
26userService = jasmine.createSpyObj('UserService', ['changePassword'])
27userService.changePassword.and.returnValue(of({}))
28
29TestBed.configureTestingModule({
30imports: [
31TranslateModule.forRoot(),
32ReactiveFormsModule,
33HttpClientTestingModule,
34BrowserAnimationsModule,
35MatCardModule,
36MatFormFieldModule,
37MatInputModule,
38MatButtonModule
39],
40declarations: [ChangePasswordComponent],
41providers: [{ provide: UserService, useValue: userService }]
42})
43.compileComponents()
44}))
45
46beforeEach(() => {
47fixture = TestBed.createComponent(ChangePasswordComponent)
48component = fixture.componentInstance
49fixture.detectChanges()
50})
51
52it('should create', () => {
53expect(component).toBeTruthy()
54})
55
56it('should be compulsory to give password', () => {
57component.passwordControl.setValue('')
58expect(component.passwordControl.valid).toBeFalsy()
59component.passwordControl.setValue('pass')
60expect(component.passwordControl.valid).toBe(true)
61})
62
63it('length of new password must be 5-40 characters', () => {
64component.newPasswordControl.setValue('old')
65expect(component.newPasswordControl.valid).toBeFalsy()
66component.newPasswordControl.setValue('new password')
67expect(component.newPasswordControl.valid).toBe(true)
68component.newPasswordControl.setValue('new password new password')
69expect(component.newPasswordControl.valid).toBe(true)
70component.newPasswordControl.setValue('new password new password new password new password')
71expect(component.newPasswordControl.valid).toBeFalsy()
72})
73
74it('should be compulsory to repeat new password', () => {
75component.repeatNewPasswordControl.setValue('')
76expect(component.passwordControl.valid).toBeFalsy()
77component.newPasswordControl.setValue('passed')
78component.repeatNewPasswordControl.setValue('passed')
79expect(component.repeatNewPasswordControl.valid).toBe(true)
80})
81
82it('should reinitizalise forms by calling resetForm', () => {
83component.passwordControl.setValue('password')
84component.newPasswordControl.setValue('newPassword')
85component.repeatNewPasswordControl.setValue('newPassword')
86component.resetForm()
87expect(component.passwordControl.value).toBe('')
88expect(component.passwordControl.pristine).toBe(true)
89expect(component.passwordControl.untouched).toBe(true)
90expect(component.newPasswordControl.value).toBe('')
91expect(component.newPasswordControl.pristine).toBe(true)
92expect(component.newPasswordControl.untouched).toBe(true)
93expect(component.repeatNewPasswordControl.value).toBe('')
94expect(component.repeatNewPasswordControl.pristine).toBe(true)
95expect(component.repeatNewPasswordControl.untouched).toBe(true)
96})
97
98it('should clear form and show confirmation after changing password', () => {
99userService.changePassword.and.returnValue(of({}))
100spyOn(component, 'resetForm')
101component.passwordControl.setValue('old')
102component.newPasswordControl.setValue('foobar')
103component.repeatNewPasswordControl.setValue('foobar')
104component.changePassword()
105expect(component.error).toBeUndefined()
106expect(component.confirmation).toBeDefined()
107expect(component.resetForm).toHaveBeenCalled()
108})
109
110it('should clear form and gracefully handle error on password change', fakeAsync(() => {
111userService.changePassword.and.returnValue(throwError('Error'))
112spyOn(component, 'resetPasswords')
113console.log = jasmine.createSpy('log')
114component.passwordControl.setValue('old')
115component.newPasswordControl.setValue('foobar')
116component.repeatNewPasswordControl.setValue('fooabar')
117component.changePassword()
118expect(component.confirmation).toBeUndefined()
119expect(component.error).toBe('Error')
120expect(console.log).toHaveBeenCalledWith('Error')
121expect(component.resetPasswords).toHaveBeenCalled()
122}))
123})
124