]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
3ac5f040fb0944f66d14cfac580409a0ae25b550
[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, 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     {
27       imports: [
28         HttpClientTestingModule,
29         RouterTestingModule,
30         ReactiveFormsModule,
31         ComponentsModule,
32         ToastrModule.forRoot(),
33         SharedModule
34       ],
35       declarations: [UserPasswordFormComponent],
36       providers: i18nProviders
37     },
38     true
39   );
40
41   beforeEach(() => {
42     fixture = TestBed.createComponent(UserPasswordFormComponent);
43     component = fixture.componentInstance;
44     form = component.userForm;
45     httpTesting = TestBed.get(HttpTestingController);
46     router = TestBed.get(Router);
47     authStorageService = TestBed.get(AuthStorageService);
48     spyOn(router, 'navigate');
49     fixture.detectChanges();
50     formHelper = new FormHelper(form);
51   });
52
53   it('should create', () => {
54     expect(component).toBeTruthy();
55   });
56
57   it('should validate old password required', () => {
58     formHelper.expectErrorChange('oldpassword', '', 'required');
59     formHelper.expectValidChange('oldpassword', 'foo');
60   });
61
62   it('should validate password match', () => {
63     formHelper.setValue('newpassword', 'aaa');
64     formHelper.expectErrorChange('confirmnewpassword', 'bbb', 'match');
65     formHelper.expectValidChange('confirmnewpassword', 'aaa');
66   });
67
68   it('should submit', () => {
69     spyOn(component, 'onPasswordChange').and.callThrough();
70     spyOn(authStorageService, 'getUsername').and.returnValue('xyz');
71     formHelper.setMultipleValues({
72       oldpassword: 'foo',
73       newpassword: 'bar'
74     });
75     formHelper.setValue('confirmnewpassword', 'bar', true);
76     component.onSubmit();
77     const request = httpTesting.expectOne('api/user/xyz/change_password');
78     expect(request.request.method).toBe('POST');
79     expect(request.request.body).toEqual({
80       old_password: 'foo',
81       new_password: 'bar'
82     });
83     request.flush({});
84     expect(component.onPasswordChange).toHaveBeenCalled();
85     expect(router.navigate).toHaveBeenCalledWith(['/logout']);
86   });
87 });