1 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
2 import { Component } from '@angular/core';
3 import { ComponentFixture, TestBed } from '@angular/core/testing';
4 import { ReactiveFormsModule } from '@angular/forms';
5 import { Router, Routes } from '@angular/router';
6 import { RouterTestingModule } from '@angular/router/testing';
8 import { ButtonsModule } from 'ngx-bootstrap/buttons';
9 import { BsDatepickerModule } from 'ngx-bootstrap/datepicker';
10 import { BsModalService } from 'ngx-bootstrap/modal';
11 import { ToastrModule } from 'ngx-toastr';
12 import { of } from 'rxjs';
14 import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
15 import { RoleService } from '../../../shared/api/role.service';
16 import { SettingsService } from '../../../shared/api/settings.service';
17 import { UserService } from '../../../shared/api/user.service';
18 import { ComponentsModule } from '../../../shared/components/components.module';
19 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
20 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
21 import { NotificationService } from '../../../shared/services/notification.service';
22 import { SharedModule } from '../../../shared/shared.module';
23 import { PasswordPolicyService } from './../../../shared/services/password-policy.service';
24 import { UserFormComponent } from './user-form.component';
25 import { UserFormModel } from './user-form.model';
27 describe('UserFormComponent', () => {
28 let component: UserFormComponent;
29 let form: CdFormGroup;
30 let fixture: ComponentFixture<UserFormComponent>;
31 let httpTesting: HttpTestingController;
32 let userService: UserService;
33 let modalService: BsModalService;
35 let formHelper: FormHelper;
37 const setUrl = (url: string) => Object.defineProperty(router, 'url', { value: url });
39 @Component({ selector: 'cd-fake', template: '' })
40 class FakeComponent {}
42 const routes: Routes = [
43 { path: 'login', component: FakeComponent },
44 { path: 'users', component: FakeComponent }
49 RouterTestingModule.withRoutes(routes),
50 HttpClientTestingModule,
53 ToastrModule.forRoot(),
55 ButtonsModule.forRoot(),
56 BsDatepickerModule.forRoot()
58 declarations: [UserFormComponent, FakeComponent],
59 providers: i18nProviders
63 spyOn(TestBed.get(PasswordPolicyService), 'getHelpText').and.callFake(() => of(''));
64 fixture = TestBed.createComponent(UserFormComponent);
65 component = fixture.componentInstance;
66 form = component.userForm;
67 httpTesting = TestBed.get(HttpTestingController);
68 userService = TestBed.get(UserService);
69 modalService = TestBed.get(BsModalService);
70 router = TestBed.get(Router);
71 spyOn(router, 'navigate');
72 fixture.detectChanges();
73 const notify = TestBed.get(NotificationService);
74 spyOn(notify, 'show');
75 formHelper = new FormHelper(form);
78 it('should create', () => {
79 expect(component).toBeTruthy();
80 expect(form).toBeTruthy();
83 describe('create mode', () => {
85 setUrl('/user-management/users/add');
89 it('should not disable fields', () => {
98 ].forEach((key) => expect(form.get(key).disabled).toBeFalsy());
101 it('should validate username required', () => {
102 formHelper.expectErrorChange('username', '', 'required');
103 formHelper.expectValidChange('username', 'user1');
106 it('should validate password match', () => {
107 formHelper.setValue('password', 'aaa');
108 formHelper.expectErrorChange('confirmpassword', 'bbb', 'match');
109 formHelper.expectValidChange('confirmpassword', 'aaa');
112 it('should validate email', () => {
113 formHelper.expectErrorChange('email', 'aaa', 'email');
116 it('should set mode', () => {
117 expect(component.mode).toBeUndefined();
120 it('should submit', () => {
121 const user: UserFormModel = {
125 email: 'user0@email.com',
126 roles: ['administrator'],
128 pwdExpirationDate: undefined,
129 pwdUpdateRequired: true
131 formHelper.setMultipleValues(user);
132 formHelper.setValue('confirmpassword', user.password);
134 const userReq = httpTesting.expectOne('api/user');
135 expect(userReq.request.method).toBe('POST');
136 expect(userReq.request.body).toEqual(user);
138 expect(router.navigate).toHaveBeenCalledWith(['/user-management/users']);
142 describe('edit mode', () => {
143 const user: UserFormModel = {
147 email: 'user1@email.com',
148 roles: ['administrator'],
150 pwdExpirationDate: undefined,
151 pwdUpdateRequired: true
155 name: 'administrator',
156 description: 'Administrator',
157 scopes_permissions: {
158 user: ['create', 'delete', 'read', 'update']
163 description: 'Read-Only',
164 scopes_permissions: {
169 name: 'user-manager',
170 description: 'User Manager',
171 scopes_permissions: {
172 user: ['create', 'delete', 'read', 'update']
178 spyOn(userService, 'get').and.callFake(() => of(user));
179 spyOn(TestBed.get(RoleService), 'list').and.callFake(() => of(roles));
180 setUrl('/user-management/users/edit/user1');
181 spyOn(TestBed.get(SettingsService), 'getStandardSettings').and.callFake(() =>
183 user_pwd_expiration_warning_1: 10,
184 user_pwd_expiration_warning_2: 5,
185 user_pwd_expiration_span: 90
188 component.ngOnInit();
189 const req = httpTesting.expectOne('api/role');
190 expect(req.request.method).toBe('GET');
192 httpTesting.expectOne('ui-api/standard_settings');
196 httpTesting.verify();
199 it('should disable fields if editing', () => {
200 expect(form.get('username').disabled).toBeTruthy();
201 ['name', 'password', 'confirmpassword', 'email', 'roles'].forEach((key) =>
202 expect(form.get(key).disabled).toBeFalsy()
206 it('should set control values', () => {
207 ['username', 'name', 'email', 'roles'].forEach((key) =>
208 expect(form.getValue(key)).toBe(user[key])
210 ['password', 'confirmpassword'].forEach((key) => expect(form.getValue(key)).toBeFalsy());
213 it('should set mode', () => {
214 expect(component.mode).toBe('editing');
217 it('should alert if user is removing needed role permission', () => {
218 spyOn(TestBed.get(AuthStorageService), 'getUsername').and.callFake(() => user.username);
219 let modalBodyTpl = null;
220 spyOn(modalService, 'show').and.callFake((_content, config) => {
221 modalBodyTpl = config.initialState.bodyTpl;
223 formHelper.setValue('roles', ['read-only']);
225 expect(modalBodyTpl).toEqual(component.removeSelfUserReadUpdatePermissionTpl);
228 it('should logout if current user roles have been changed', () => {
229 spyOn(TestBed.get(AuthStorageService), 'getUsername').and.callFake(() => user.username);
230 formHelper.setValue('roles', ['user-manager']);
232 const userReq = httpTesting.expectOne(`api/user/${user.username}`);
233 expect(userReq.request.method).toBe('PUT');
235 const authReq = httpTesting.expectOne('api/auth/logout');
236 expect(authReq.request.method).toBe('POST');
239 it('should submit', () => {
240 spyOn(TestBed.get(AuthStorageService), 'getUsername').and.callFake(() => user.username);
242 const userReq = httpTesting.expectOne(`api/user/${user.username}`);
243 expect(userReq.request.method).toBe('PUT');
244 expect(userReq.request.body).toEqual({
247 pwdUpdateRequired: true,
249 email: 'user1@email.com',
250 roles: ['administrator'],
254 expect(router.navigate).toHaveBeenCalledWith(['/user-management/users']);