]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
8cf922aa41f20af546bbeff8447a476d5761ab1e
[ceph.git] /
1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { Router } from '@angular/router';
5 import { RouterTestingModule } from '@angular/router/testing';
6
7 import { ToastrModule } from 'ngx-toastr';
8
9 import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
10 import { ComponentsModule } from '~/app/shared/components/components.module';
11 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
12 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
13 import { SharedModule } from '~/app/shared/shared.module';
14 import { UserPasswordFormComponent } from './user-password-form.component';
15
16 describe('UserPasswordFormComponent', () => {
17   let component: UserPasswordFormComponent;
18   let fixture: ComponentFixture<UserPasswordFormComponent>;
19   let form: CdFormGroup;
20   let formHelper: FormHelper;
21   let httpTesting: HttpTestingController;
22   let router: Router;
23   let authStorageService: AuthStorageService;
24
25   configureTestBed({
26     imports: [
27       HttpClientTestingModule,
28       RouterTestingModule,
29       ReactiveFormsModule,
30       ComponentsModule,
31       ToastrModule.forRoot(),
32       SharedModule
33     ],
34     declarations: [UserPasswordFormComponent]
35   });
36
37   beforeEach(() => {
38     fixture = TestBed.createComponent(UserPasswordFormComponent);
39     component = fixture.componentInstance;
40     form = component.userForm;
41     httpTesting = TestBed.inject(HttpTestingController);
42     router = TestBed.inject(Router);
43     authStorageService = TestBed.inject(AuthStorageService);
44     spyOn(router, 'navigate');
45     fixture.detectChanges();
46     formHelper = new FormHelper(form);
47   });
48
49   it('should create', () => {
50     expect(component).toBeTruthy();
51   });
52
53   it('should validate old password required', () => {
54     formHelper.expectErrorChange('oldpassword', '', 'required');
55     formHelper.expectValidChange('oldpassword', 'foo');
56   });
57
58   it('should validate password match', () => {
59     formHelper.setValue('newpassword', 'aaa');
60     formHelper.expectErrorChange('confirmnewpassword', 'bbb', 'match');
61     formHelper.expectValidChange('confirmnewpassword', 'aaa');
62   });
63
64   it('should submit', () => {
65     spyOn(component, 'onPasswordChange').and.callThrough();
66     spyOn(authStorageService, 'getUsername').and.returnValue('xyz');
67     formHelper.setMultipleValues({
68       oldpassword: 'foo',
69       newpassword: 'bar'
70     });
71     formHelper.setValue('confirmnewpassword', 'bar', true);
72     component.onSubmit();
73     const request = httpTesting.expectOne('api/user/xyz/change_password');
74     expect(request.request.method).toBe('POST');
75     expect(request.request.body).toEqual({
76       old_password: 'foo',
77       new_password: 'bar'
78     });
79     request.flush({});
80     expect(component.onPasswordChange).toHaveBeenCalled();
81     expect(router.navigate).toHaveBeenCalledWith(['/login']);
82   });
83 });