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 { ToastModule } from 'ng2-toastr';
9 import { BsModalService } from 'ngx-bootstrap/modal';
10 import { of } from 'rxjs';
12 import { configureTestBed, FormHelper, i18nProviders } from '../../../../testing/unit-test-helper';
13 import { RoleService } from '../../../shared/api/role.service';
14 import { UserService } from '../../../shared/api/user.service';
15 import { ComponentsModule } from '../../../shared/components/components.module';
16 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
17 import { AuthStorageService } from '../../../shared/services/auth-storage.service';
18 import { NotificationService } from '../../../shared/services/notification.service';
19 import { SharedModule } from '../../../shared/shared.module';
20 import { UserFormComponent } from './user-form.component';
21 import { UserFormModel } from './user-form.model';
23 describe('UserFormComponent', () => {
24 let component: UserFormComponent;
25 let form: CdFormGroup;
26 let fixture: ComponentFixture<UserFormComponent>;
27 let httpTesting: HttpTestingController;
28 let userService: UserService;
29 let modalService: BsModalService;
31 let formHelper: FormHelper;
33 const setUrl = (url) => Object.defineProperty(router, 'url', { value: url });
35 @Component({ selector: 'cd-fake', template: '' })
36 class FakeComponent {}
38 const routes: Routes = [
39 { path: 'login', component: FakeComponent },
40 { path: 'users', component: FakeComponent }
46 RouterTestingModule.withRoutes(routes),
47 HttpClientTestingModule,
50 ToastModule.forRoot(),
53 declarations: [UserFormComponent, FakeComponent],
54 providers: i18nProviders
60 fixture = TestBed.createComponent(UserFormComponent);
61 component = fixture.componentInstance;
62 form = component.userForm;
63 httpTesting = TestBed.get(HttpTestingController);
64 userService = TestBed.get(UserService);
65 modalService = TestBed.get(BsModalService);
66 router = TestBed.get(Router);
67 spyOn(router, 'navigate');
68 fixture.detectChanges();
69 const notify = TestBed.get(NotificationService);
70 spyOn(notify, 'show');
71 formHelper = new FormHelper(form);
74 it('should create', () => {
75 expect(component).toBeTruthy();
76 expect(form).toBeTruthy();
79 describe('create mode', () => {
81 setUrl('/user-management/users/add');
85 it('should not disable fields', () => {
86 ['username', 'name', 'password', 'confirmpassword', 'email', 'roles'].forEach((key) =>
87 expect(form.get(key).disabled).toBeFalsy()
91 it('should validate username required', () => {
92 formHelper.expectErrorChange('username', '', 'required');
93 formHelper.expectValidChange('username', 'user1');
96 it('should validate password match', () => {
97 formHelper.setValue('password', 'aaa');
98 formHelper.expectErrorChange('confirmpassword', 'bbb', 'match');
99 formHelper.expectValidChange('confirmpassword', 'aaa');
102 it('should validate email', () => {
103 formHelper.expectErrorChange('email', 'aaa', 'email');
106 it('should set mode', () => {
107 expect(component.mode).toBeUndefined();
110 it('should submit', () => {
111 const user: UserFormModel = {
115 email: 'user0@email.com',
116 roles: ['administrator']
118 formHelper.setMultipleValues(user);
119 formHelper.setValue('confirmpassword', user.password);
121 const userReq = httpTesting.expectOne('api/user');
122 expect(userReq.request.method).toBe('POST');
123 expect(userReq.request.body).toEqual(user);
125 expect(router.navigate).toHaveBeenCalledWith(['/user-management/users']);
129 describe('edit mode', () => {
130 const user: UserFormModel = {
134 email: 'user1@email.com',
135 roles: ['administrator']
139 name: 'administrator',
140 description: 'Administrator',
141 scopes_permissions: {
142 user: ['create', 'delete', 'read', 'update']
147 description: 'Read-Only',
148 scopes_permissions: {
153 name: 'user-manager',
154 description: 'User Manager',
155 scopes_permissions: {
156 user: ['create', 'delete', 'read', 'update']
162 spyOn(userService, 'get').and.callFake(() => of(user));
163 spyOn(TestBed.get(RoleService), 'list').and.callFake(() => of(roles));
164 setUrl('/user-management/users/edit/user1');
165 component.ngOnInit();
166 const req = httpTesting.expectOne('api/role');
167 expect(req.request.method).toBe('GET');
172 httpTesting.verify();
175 it('should disable fields if editing', () => {
176 expect(form.get('username').disabled).toBeTruthy();
177 ['name', 'password', 'confirmpassword', 'email', 'roles'].forEach((key) =>
178 expect(form.get(key).disabled).toBeFalsy()
182 it('should set control values', () => {
183 ['username', 'name', 'email', 'roles'].forEach((key) =>
184 expect(form.getValue(key)).toBe(user[key])
186 ['password', 'confirmpassword'].forEach((key) => expect(form.getValue(key)).toBeFalsy());
189 it('should set mode', () => {
190 expect(component.mode).toBe('editing');
193 it('should alert if user is removing needed role permission', () => {
194 spyOn(TestBed.get(AuthStorageService), 'getUsername').and.callFake(() => user.username);
195 let modalBodyTpl = null;
196 spyOn(modalService, 'show').and.callFake((_content, config) => {
197 modalBodyTpl = config.initialState.bodyTpl;
199 formHelper.setValue('roles', ['read-only']);
201 expect(modalBodyTpl).toEqual(component.removeSelfUserReadUpdatePermissionTpl);
204 it('should logout if current user roles have been changed', () => {
205 spyOn(TestBed.get(AuthStorageService), 'getUsername').and.callFake(() => user.username);
206 formHelper.setValue('roles', ['user-manager']);
208 const userReq = httpTesting.expectOne(`api/user/${user.username}`);
209 expect(userReq.request.method).toBe('PUT');
211 const authReq = httpTesting.expectOne('api/auth/logout');
212 expect(authReq.request.method).toBe('POST');
215 it('should submit', () => {
216 spyOn(TestBed.get(AuthStorageService), 'getUsername').and.callFake(() => user.username);
218 const userReq = httpTesting.expectOne(`api/user/${user.username}`);
219 expect(userReq.request.method).toBe('PUT');
220 expect(userReq.request.body).toEqual({
224 email: 'user1@email.com',
225 roles: ['administrator']
228 expect(router.navigate).toHaveBeenCalledWith(['/user-management/users']);