]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
44e9201381b5d32059e387c562bb8e8b2a79d197
[ceph-ci.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, i18nProviders } from '../../../../testing/unit-test-helper';
10 import { ComponentsModule } from '../../../shared/components/components.module';
11 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
12 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
13 import { SharedModule } from '../../../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     providers: i18nProviders
36   });
37
38   beforeEach(() => {
39     fixture = TestBed.createComponent(UserPasswordFormComponent);
40     component = fixture.componentInstance;
41     form = component.userForm;
42     httpTesting = TestBed.get(HttpTestingController);
43     router = TestBed.get(Router);
44     authStorageService = TestBed.get(AuthStorageService);
45     spyOn(router, 'navigate');
46     fixture.detectChanges();
47     formHelper = new FormHelper(form);
48   });
49
50   it('should create', () => {
51     expect(component).toBeTruthy();
52   });
53
54   it('should validate old password required', () => {
55     formHelper.expectErrorChange('oldpassword', '', 'required');
56     formHelper.expectValidChange('oldpassword', 'foo');
57   });
58
59   it('should validate password match', () => {
60     formHelper.setValue('newpassword', 'aaa');
61     formHelper.expectErrorChange('confirmnewpassword', 'bbb', 'match');
62     formHelper.expectValidChange('confirmnewpassword', 'aaa');
63   });
64
65   it('should submit', () => {
66     spyOn(component, 'onPasswordChange').and.callThrough();
67     spyOn(authStorageService, 'getUsername').and.returnValue('xyz');
68     formHelper.setMultipleValues({
69       oldpassword: 'foo',
70       newpassword: 'bar'
71     });
72     formHelper.setValue('confirmnewpassword', 'bar', true);
73     component.onSubmit();
74     const request = httpTesting.expectOne('api/user/xyz/change_password');
75     expect(request.request.method).toBe('POST');
76     expect(request.request.body).toEqual({
77       old_password: 'foo',
78       new_password: 'bar'
79     });
80     request.flush({});
81     expect(component.onPasswordChange).toHaveBeenCalled();
82     expect(router.navigate).toHaveBeenCalledWith(['/login']);
83   });
84 });