summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-form/role-form.component.spec.ts
blob: 7552f594bf3524f476b347165d64e30552cb498a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { Router, Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';

import { ToastrModule } from 'ngx-toastr';
import { of } from 'rxjs';

import { RoleService } from '~/app/shared/api/role.service';
import { ScopeService } from '~/app/shared/api/scope.service';
import { LoadingPanelComponent } from '~/app/shared/components/loading-panel/loading-panel.component';
import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
import { NotificationService } from '~/app/shared/services/notification.service';
import { SharedModule } from '~/app/shared/shared.module';
import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
import { RoleFormComponent } from './role-form.component';
import { RoleFormModel } from './role-form.model';

describe('RoleFormComponent', () => {
  let component: RoleFormComponent;
  let form: CdFormGroup;
  let fixture: ComponentFixture<RoleFormComponent>;
  let httpTesting: HttpTestingController;
  let roleService: RoleService;
  let router: Router;
  const setUrl = (url: string) => Object.defineProperty(router, 'url', { value: url });

  @Component({ selector: 'cd-fake', template: '' })
  class FakeComponent {}

  const routes: Routes = [{ path: 'roles', component: FakeComponent }];

  configureTestBed(
    {
      imports: [
        RouterTestingModule.withRoutes(routes),
        HttpClientTestingModule,
        ReactiveFormsModule,
        ToastrModule.forRoot(),
        SharedModule
      ],
      declarations: [RoleFormComponent, FakeComponent]
    },
    [LoadingPanelComponent]
  );

  beforeEach(() => {
    fixture = TestBed.createComponent(RoleFormComponent);
    component = fixture.componentInstance;
    form = component.roleForm;
    httpTesting = TestBed.inject(HttpTestingController);
    roleService = TestBed.inject(RoleService);
    router = TestBed.inject(Router);
    spyOn(router, 'navigate');
    fixture.detectChanges();
    const notify = TestBed.inject(NotificationService);
    spyOn(notify, 'show');
  });

  it('should create', () => {
    expect(component).toBeTruthy();
    expect(form).toBeTruthy();
  });

  describe('create mode', () => {
    let formHelper: FormHelper;

    beforeEach(() => {
      setUrl('/user-management/roles/add');
      component.ngOnInit();
      formHelper = new FormHelper(form);
    });

    it('should not disable fields', () => {
      ['name', 'description', 'scopes_permissions'].forEach((key) =>
        expect(form.get(key).disabled).toBeFalsy()
      );
    });

    it('should validate name required', () => {
      formHelper.expectErrorChange('name', '', 'required');
    });

    it('should set mode', () => {
      expect(component.mode).toBeUndefined();
    });

    it('should submit', () => {
      const role: RoleFormModel = {
        name: 'role1',
        description: 'Role 1',
        scopes_permissions: { osd: ['read'] }
      };
      formHelper.setMultipleValues(role);
      component.submit();
      const roleReq = httpTesting.expectOne('api/role');
      expect(roleReq.request.method).toBe('POST');
      expect(roleReq.request.body).toEqual(role);
      roleReq.flush({});
      expect(router.navigate).toHaveBeenCalledWith(['/user-management/roles']);
    });

    it('should check all perms for a scope', () => {
      formHelper.setValue('scopes_permissions', { cephfs: ['read'] });
      component.onClickCellCheckbox('grafana', 'scope');
      const scopes_permissions = form.getValue('scopes_permissions');
      expect(Object.keys(scopes_permissions)).toContain('grafana');
      expect(scopes_permissions['grafana']).toEqual(['create', 'delete', 'read', 'update']);
    });

    it('should uncheck all perms for a scope', () => {
      formHelper.setValue('scopes_permissions', { cephfs: ['read', 'create', 'update', 'delete'] });
      component.onClickCellCheckbox('cephfs', 'scope');
      const scopes_permissions = form.getValue('scopes_permissions');
      expect(Object.keys(scopes_permissions)).not.toContain('cephfs');
    });

    it('should uncheck all scopes and perms', () => {
      component.scopes = ['cephfs', 'grafana'];
      formHelper.setValue('scopes_permissions', {
        cephfs: ['read', 'delete'],
        grafana: ['update']
      });
      component.onClickHeaderCheckbox('scope', ({
        target: { checked: false }
      } as unknown) as Event);
      const scopes_permissions = form.getValue('scopes_permissions');
      expect(scopes_permissions).toEqual({});
    });

    it('should check all scopes and perms', () => {
      component.scopes = ['cephfs', 'grafana'];
      formHelper.setValue('scopes_permissions', {
        cephfs: ['create', 'update'],
        grafana: ['delete']
      });
      component.onClickHeaderCheckbox('scope', ({ target: { checked: true } } as unknown) as Event);
      const scopes_permissions = form.getValue('scopes_permissions');
      const keys = Object.keys(scopes_permissions);
      expect(keys).toEqual(['cephfs', 'grafana']);
      keys.forEach((key) => {
        expect(scopes_permissions[key].sort()).toEqual(['create', 'delete', 'read', 'update']);
      });
    });

    it('should check if column is checked', () => {
      component.scopes_permissions = [
        { scope: 'a', read: true, create: true, update: true, delete: true },
        { scope: 'b', read: false, create: true, update: false, delete: true }
      ];
      expect(component.isRowChecked('a')).toBeTruthy();
      expect(component.isRowChecked('b')).toBeFalsy();
      expect(component.isRowChecked('c')).toBeFalsy();
    });

    it('should check if header is checked', () => {
      component.scopes_permissions = [
        { scope: 'a', read: true, create: true, update: false, delete: true },
        { scope: 'b', read: false, create: true, update: false, delete: true }
      ];
      expect(component.isHeaderChecked('read')).toBeFalsy();
      expect(component.isHeaderChecked('create')).toBeTruthy();
      expect(component.isHeaderChecked('update')).toBeFalsy();
    });
  });

  describe('edit mode', () => {
    const role: RoleFormModel = {
      name: 'role1',
      description: 'Role 1',
      scopes_permissions: { osd: ['read', 'create'] }
    };
    const scopes = ['osd', 'user'];
    beforeEach(() => {
      spyOn(roleService, 'get').and.callFake(() => of(role));
      spyOn(TestBed.inject(ScopeService), 'list').and.callFake(() => of(scopes));
      setUrl('/user-management/roles/edit/role1');
      component.ngOnInit();
      const reqScopes = httpTesting.expectOne('ui-api/scope');
      expect(reqScopes.request.method).toBe('GET');
    });

    afterEach(() => {
      httpTesting.verify();
    });

    it('should disable fields if editing', () => {
      expect(form.get('name').disabled).toBeTruthy();
      ['description', 'scopes_permissions'].forEach((key) =>
        expect(form.get(key).disabled).toBeFalsy()
      );
    });

    it('should set control values', () => {
      ['name', 'description', 'scopes_permissions'].forEach((key) =>
        expect(form.getValue(key)).toBe(role[key])
      );
    });

    it('should set mode', () => {
      expect(component.mode).toBe('editing');
    });

    it('should submit', () => {
      component.onClickCellCheckbox('osd', 'update');
      component.onClickCellCheckbox('osd', 'create');
      component.onClickCellCheckbox('user', 'read');
      component.submit();
      const roleReq = httpTesting.expectOne(`api/role/${role.name}`);
      expect(roleReq.request.method).toBe('PUT');
      expect(roleReq.request.body).toEqual({
        name: 'role1',
        description: 'Role 1',
        scopes_permissions: { osd: ['read', 'update'], user: ['read'] }
      });
      roleReq.flush({});
      expect(router.navigate).toHaveBeenCalledWith(['/user-management/roles']);
    });
  });
});