]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-form/user-form.component.spec.ts
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / core / auth / user-form / user-form.component.spec.ts
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';
7
8 import { ToastModule } from 'ng2-toastr';
9 import { BsModalService } from 'ngx-bootstrap/modal';
10 import { of } from 'rxjs';
11
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';
22
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;
30 let router: Router;
31 let formHelper: FormHelper;
32
33 const setUrl = (url) => Object.defineProperty(router, 'url', { value: url });
34
35 @Component({ selector: 'cd-fake', template: '' })
36 class FakeComponent {}
37
38 const routes: Routes = [
39 { path: 'login', component: FakeComponent },
40 { path: 'users', component: FakeComponent }
41 ];
42
43 configureTestBed(
44 {
45 imports: [
46 RouterTestingModule.withRoutes(routes),
47 HttpClientTestingModule,
48 ReactiveFormsModule,
49 ComponentsModule,
50 ToastModule.forRoot(),
51 SharedModule
52 ],
53 declarations: [UserFormComponent, FakeComponent],
54 providers: i18nProviders
55 },
56 true
57 );
58
59 beforeEach(() => {
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);
72 });
73
74 it('should create', () => {
75 expect(component).toBeTruthy();
76 expect(form).toBeTruthy();
77 });
78
79 describe('create mode', () => {
80 beforeEach(() => {
81 setUrl('/user-management/users/add');
82 component.ngOnInit();
83 });
84
85 it('should not disable fields', () => {
86 ['username', 'name', 'password', 'confirmpassword', 'email', 'roles'].forEach((key) =>
87 expect(form.get(key).disabled).toBeFalsy()
88 );
89 });
90
91 it('should validate username required', () => {
92 formHelper.expectErrorChange('username', '', 'required');
93 formHelper.expectValidChange('username', 'user1');
94 });
95
96 it('should validate password match', () => {
97 formHelper.setValue('password', 'aaa');
98 formHelper.expectErrorChange('confirmpassword', 'bbb', 'match');
99 formHelper.expectValidChange('confirmpassword', 'aaa');
100 });
101
102 it('should validate email', () => {
103 formHelper.expectErrorChange('email', 'aaa', 'email');
104 });
105
106 it('should set mode', () => {
107 expect(component.mode).toBeUndefined();
108 });
109
110 it('should submit', () => {
111 const user: UserFormModel = {
112 username: 'user0',
113 password: 'pass0',
114 name: 'User 0',
115 email: 'user0@email.com',
116 roles: ['administrator']
117 };
118 formHelper.setMultipleValues(user);
119 formHelper.setValue('confirmpassword', user.password);
120 component.submit();
121 const userReq = httpTesting.expectOne('api/user');
122 expect(userReq.request.method).toBe('POST');
123 expect(userReq.request.body).toEqual(user);
124 userReq.flush({});
125 expect(router.navigate).toHaveBeenCalledWith(['/user-management/users']);
126 });
127 });
128
129 describe('edit mode', () => {
130 const user: UserFormModel = {
131 username: 'user1',
132 password: undefined,
133 name: 'User 1',
134 email: 'user1@email.com',
135 roles: ['administrator']
136 };
137 const roles = [
138 {
139 name: 'administrator',
140 description: 'Administrator',
141 scopes_permissions: {
142 user: ['create', 'delete', 'read', 'update']
143 }
144 },
145 {
146 name: 'read-only',
147 description: 'Read-Only',
148 scopes_permissions: {
149 user: ['read']
150 }
151 },
152 {
153 name: 'user-manager',
154 description: 'User Manager',
155 scopes_permissions: {
156 user: ['create', 'delete', 'read', 'update']
157 }
158 }
159 ];
160
161 beforeEach(() => {
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');
168 req.flush(roles);
169 });
170
171 afterEach(() => {
172 httpTesting.verify();
173 });
174
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()
179 );
180 });
181
182 it('should set control values', () => {
183 ['username', 'name', 'email', 'roles'].forEach((key) =>
184 expect(form.getValue(key)).toBe(user[key])
185 );
186 ['password', 'confirmpassword'].forEach((key) => expect(form.getValue(key)).toBeFalsy());
187 });
188
189 it('should set mode', () => {
190 expect(component.mode).toBe('editing');
191 });
192
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;
198 });
199 formHelper.setValue('roles', ['read-only']);
200 component.submit();
201 expect(modalBodyTpl).toEqual(component.removeSelfUserReadUpdatePermissionTpl);
202 });
203
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']);
207 component.submit();
208 const userReq = httpTesting.expectOne(`api/user/${user.username}`);
209 expect(userReq.request.method).toBe('PUT');
210 userReq.flush({});
211 const authReq = httpTesting.expectOne('api/auth/logout');
212 expect(authReq.request.method).toBe('POST');
213 });
214
215 it('should submit', () => {
216 spyOn(TestBed.get(AuthStorageService), 'getUsername').and.callFake(() => user.username);
217 component.submit();
218 const userReq = httpTesting.expectOne(`api/user/${user.username}`);
219 expect(userReq.request.method).toBe('PUT');
220 expect(userReq.request.body).toEqual({
221 username: 'user1',
222 password: '',
223 name: 'User 1',
224 email: 'user1@email.com',
225 roles: ['administrator']
226 });
227 userReq.flush({});
228 expect(router.navigate).toHaveBeenCalledWith(['/user-management/users']);
229 });
230 });
231 });