]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/core/auth/role-list/role-list.component.spec.ts
7274125f04dc162a67e1a291bf6ec67425b35641
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / core / auth / role-list / role-list.component.spec.ts
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4 import { RouterTestingModule } from '@angular/router/testing';
5
6 import { TabsModule } from 'ngx-bootstrap/tabs';
7 import { ToastrModule } from 'ngx-toastr';
8
9 import {
10 configureTestBed,
11 i18nProviders,
12 PermissionHelper
13 } from '../../../../testing/unit-test-helper';
14 import { ActionLabels } from '../../../shared/constants/app.constants';
15 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
16 import { SharedModule } from '../../../shared/shared.module';
17 import { RoleDetailsComponent } from '../role-details/role-details.component';
18 import { UserTabsComponent } from '../user-tabs/user-tabs.component';
19 import { RoleListComponent } from './role-list.component';
20
21 describe('RoleListComponent', () => {
22 let component: RoleListComponent;
23 let fixture: ComponentFixture<RoleListComponent>;
24
25 configureTestBed({
26 declarations: [RoleListComponent, RoleDetailsComponent, UserTabsComponent],
27 imports: [
28 SharedModule,
29 ToastrModule.forRoot(),
30 TabsModule.forRoot(),
31 RouterTestingModule,
32 HttpClientTestingModule
33 ],
34 providers: i18nProviders
35 });
36
37 beforeEach(() => {
38 fixture = TestBed.createComponent(RoleListComponent);
39 component = fixture.componentInstance;
40 });
41
42 it('should create', () => {
43 fixture.detectChanges();
44 expect(component).toBeTruthy();
45 });
46
47 describe('show action buttons and drop down actions depending on permissions', () => {
48 let tableActions: TableActionsComponent;
49 let scenario: { fn; empty; single };
50 let permissionHelper: PermissionHelper;
51
52 const getTableActionComponent = (): TableActionsComponent => {
53 fixture.detectChanges();
54 return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance;
55 };
56
57 beforeEach(() => {
58 permissionHelper = new PermissionHelper(component.permission, () =>
59 getTableActionComponent()
60 );
61 scenario = {
62 fn: () => tableActions.getCurrentButton().name,
63 single: ActionLabels.EDIT,
64 empty: ActionLabels.CREATE
65 };
66 });
67
68 describe('with all', () => {
69 beforeEach(() => {
70 tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1);
71 });
72
73 it(`shows 'Edit' for single selection else 'Add' as main action`, () =>
74 permissionHelper.testScenarios(scenario));
75
76 it('shows all actions', () => {
77 expect(tableActions.tableActions.length).toBe(3);
78 expect(tableActions.tableActions).toEqual(component.tableActions);
79 });
80 });
81
82 describe('with read, create and update', () => {
83 beforeEach(() => {
84 tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0);
85 });
86
87 it(`shows 'Edit' for single selection else 'Add' as main action`, () =>
88 permissionHelper.testScenarios(scenario));
89
90 it(`shows 'Add' and 'Edit' action`, () => {
91 expect(tableActions.tableActions.length).toBe(2);
92 component.tableActions.pop();
93 expect(tableActions.tableActions).toEqual(component.tableActions);
94 });
95 });
96
97 describe('with read, create and delete', () => {
98 beforeEach(() => {
99 tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1);
100 });
101
102 it(`shows 'Delete' for single selection else 'Add' as main action`, () => {
103 scenario.single = 'Delete';
104 permissionHelper.testScenarios(scenario);
105 });
106
107 it(`shows 'Add' and 'Delete' action`, () => {
108 expect(tableActions.tableActions.length).toBe(2);
109 expect(tableActions.tableActions).toEqual([
110 component.tableActions[0],
111 component.tableActions[2]
112 ]);
113 });
114 });
115
116 describe('with read, edit and delete', () => {
117 beforeEach(() => {
118 tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1);
119 });
120
121 it(`shows always 'Edit' as main action`, () => {
122 scenario.empty = ActionLabels.EDIT;
123 permissionHelper.testScenarios(scenario);
124 });
125
126 it(`shows 'Edit' and 'Delete' action`, () => {
127 expect(tableActions.tableActions.length).toBe(2);
128 expect(tableActions.tableActions).toEqual([
129 component.tableActions[1],
130 component.tableActions[2]
131 ]);
132 });
133 });
134
135 describe('with read and create', () => {
136 beforeEach(() => {
137 tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0);
138 });
139
140 it(`shows always 'Add' as main action`, () => {
141 scenario.single = ActionLabels.CREATE;
142 permissionHelper.testScenarios(scenario);
143 });
144
145 it(`shows only 'Add' action`, () => {
146 expect(tableActions.tableActions.length).toBe(1);
147 expect(tableActions.tableActions).toEqual([component.tableActions[0]]);
148 });
149 });
150
151 describe('with read and update', () => {
152 beforeEach(() => {
153 tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0);
154 });
155
156 it(`shows always 'Edit' as main action`, () => {
157 scenario.empty = ActionLabels.EDIT;
158 permissionHelper.testScenarios(scenario);
159 });
160
161 it(`shows only 'Edit' action`, () => {
162 expect(tableActions.tableActions.length).toBe(1);
163 expect(tableActions.tableActions).toEqual([component.tableActions[1]]);
164 });
165 });
166
167 describe('with read and delete', () => {
168 beforeEach(() => {
169 tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1);
170 });
171
172 it(`shows always 'Delete' as main action`, () => {
173 scenario.single = 'Delete';
174 scenario.empty = 'Delete';
175 permissionHelper.testScenarios(scenario);
176 });
177
178 it(`shows only 'Delete' action`, () => {
179 expect(tableActions.tableActions.length).toBe(1);
180 expect(tableActions.tableActions).toEqual([component.tableActions[2]]);
181 });
182 });
183
184 describe('with only read', () => {
185 beforeEach(() => {
186 tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0);
187 });
188
189 it('shows no main action', () => {
190 permissionHelper.testScenarios({
191 fn: () => tableActions.getCurrentButton(),
192 single: undefined,
193 empty: undefined
194 });
195 });
196
197 it('shows no actions', () => {
198 expect(tableActions.tableActions.length).toBe(0);
199 expect(tableActions.tableActions).toEqual([]);
200 });
201 });
202 });
203 });