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';
6 import { TreeModule } from 'ng2-tree';
7 import { TabsModule } from 'ngx-bootstrap/tabs';
8 import { ToastrModule } from 'ngx-toastr';
9 import { BehaviorSubject, of } from 'rxjs';
15 } from '../../../../testing/unit-test-helper';
16 import { IscsiService } from '../../../shared/api/iscsi.service';
17 import { TableActionsComponent } from '../../../shared/datatable/table-actions/table-actions.component';
18 import { ExecutingTask } from '../../../shared/models/executing-task';
19 import { SummaryService } from '../../../shared/services/summary.service';
20 import { TaskListService } from '../../../shared/services/task-list.service';
21 import { SharedModule } from '../../../shared/shared.module';
22 import { IscsiTabsComponent } from '../iscsi-tabs/iscsi-tabs.component';
23 import { IscsiTargetDetailsComponent } from '../iscsi-target-details/iscsi-target-details.component';
24 import { IscsiTargetListComponent } from './iscsi-target-list.component';
26 describe('IscsiTargetListComponent', () => {
27 let component: IscsiTargetListComponent;
28 let fixture: ComponentFixture<IscsiTargetListComponent>;
29 let summaryService: SummaryService;
30 let iscsiService: IscsiService;
32 const refresh = (data) => {
33 summaryService['summaryDataSource'].next(data);
38 HttpClientTestingModule,
43 ToastrModule.forRoot()
45 declarations: [IscsiTargetListComponent, IscsiTabsComponent, IscsiTargetDetailsComponent],
46 providers: [TaskListService, i18nProviders]
50 fixture = TestBed.createComponent(IscsiTargetListComponent);
51 component = fixture.componentInstance;
52 summaryService = TestBed.get(SummaryService);
53 iscsiService = TestBed.get(IscsiService);
55 // this is needed because summaryService isn't being reset after each test.
56 summaryService['summaryDataSource'] = new BehaviorSubject(null);
57 summaryService['summaryData$'] = summaryService['summaryDataSource'].asObservable();
59 spyOn(iscsiService, 'status').and.returnValue(of({ available: true }));
62 it('should create', () => {
63 expect(component).toBeTruthy();
66 describe('after ngOnInit', () => {
68 spyOn(iscsiService, 'listTargets').and.callThrough();
69 fixture.detectChanges();
72 it('should load targets on init', () => {
74 expect(iscsiService.status).toHaveBeenCalled();
75 expect(iscsiService.listTargets).toHaveBeenCalled();
78 it('should not load targets on init because no data', () => {
80 expect(iscsiService.listTargets).not.toHaveBeenCalled();
83 it('should call error function on init when summary service fails', () => {
84 spyOn(component.table, 'reset');
85 summaryService['summaryDataSource'].error(undefined);
86 expect(component.table.reset).toHaveBeenCalled();
90 describe('handling of executing tasks', () => {
93 const addTarget = (name) => {
96 portals: [{ host: 'node1', ip: '192.168.100.201' }],
97 disks: [{ pool: 'rbd', image: 'disk_1', controls: {} }],
100 client_iqn: 'iqn.1994-05.com.redhat:rh7-client',
101 luns: [{ pool: 'rbd', image: 'disk_1' }],
103 user: 'myiscsiusername',
104 password: 'myiscsipassword',
106 mutual_password: null
116 const addTask = (name: string, target_iqn: string) => {
117 const task = new ExecutingTask();
120 case 'iscsi/target/create':
122 target_iqn: target_iqn
125 case 'iscsi/target/delete':
127 target_iqn: target_iqn
132 target_iqn: target_iqn
136 summaryService.addRunningTask(task);
139 const expectTargetTasks = (target: any, executing: string) => {
140 expect(target.cdExecuting).toEqual(executing);
149 component.targets = targets;
150 refresh({ executing_tasks: [], finished_tasks: [] });
151 spyOn(iscsiService, 'listTargets').and.callFake(() => of(targets));
152 fixture.detectChanges();
155 it('should gets all targets without tasks', () => {
156 expect(component.targets.length).toBe(3);
157 expect(component.targets.every((target) => !target.cdExecuting)).toBeTruthy();
160 it('should add a new target from a task', () => {
161 addTask('iscsi/target/create', 'iqn.d');
162 expect(component.targets.length).toBe(4);
163 expectTargetTasks(component.targets[0], undefined);
164 expectTargetTasks(component.targets[1], undefined);
165 expectTargetTasks(component.targets[2], undefined);
166 expectTargetTasks(component.targets[3], 'Creating');
169 it('should show when an existing target is being modified', () => {
170 addTask('iscsi/target/delete', 'iqn.b');
171 expect(component.targets.length).toBe(3);
172 expectTargetTasks(component.targets[1], 'Deleting');
176 describe('show action buttons and drop down actions depending on permissions', () => {
177 let tableActions: TableActionsComponent;
178 let scenario: { fn; empty; single };
179 let permissionHelper: PermissionHelper;
181 const getTableActionComponent = (): TableActionsComponent => {
182 fixture.detectChanges();
183 return fixture.debugElement.query(By.directive(TableActionsComponent)).componentInstance;
187 permissionHelper = new PermissionHelper(component.permissions.iscsi, () =>
188 getTableActionComponent()
191 fn: () => tableActions.getCurrentButton().name,
197 describe('with all', () => {
199 tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 1);
202 it(`shows 'Edit' for single selection else 'Add' as main action`, () => {
203 permissionHelper.testScenarios(scenario);
206 it('shows all actions', () => {
207 expect(tableActions.tableActions.length).toBe(3);
208 expect(tableActions.tableActions).toEqual(component.tableActions);
212 describe('with read, create and update', () => {
214 tableActions = permissionHelper.setPermissionsAndGetActions(1, 1, 0);
215 scenario.single = 'Edit';
218 it(`should always show 'Edit'`, () => {
219 permissionHelper.testScenarios(scenario);
222 it(`shows all actions except for 'Delete'`, () => {
223 expect(tableActions.tableActions.length).toBe(2);
224 component.tableActions.pop();
225 expect(tableActions.tableActions).toEqual(component.tableActions);
229 describe('with read, create and delete', () => {
231 tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 1);
234 it(`shows 'Delete' for single selection else 'Add' as main action`, () => {
235 scenario.single = 'Delete';
236 permissionHelper.testScenarios(scenario);
239 it(`shows 'Add' and 'Delete' actions`, () => {
240 expect(tableActions.tableActions.length).toBe(2);
241 expect(tableActions.tableActions).toEqual([
242 component.tableActions[0],
243 component.tableActions[2]
248 describe('with read, edit and delete', () => {
250 tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 1);
253 it(`shows always 'Edit' as main action`, () => {
254 scenario.empty = 'Edit';
255 permissionHelper.testScenarios(scenario);
258 it(`shows 'Edit' and 'Delete' actions`, () => {
259 expect(tableActions.tableActions.length).toBe(2);
260 expect(tableActions.tableActions).toEqual([
261 component.tableActions[1],
262 component.tableActions[2]
267 describe('with read and create', () => {
269 tableActions = permissionHelper.setPermissionsAndGetActions(1, 0, 0);
272 it(`shows 'Add' for single selection and 'Add' as main action`, () => {
273 scenario.single = 'Add';
274 permissionHelper.testScenarios(scenario);
277 it(`shows 'Add' actions`, () => {
278 expect(tableActions.tableActions.length).toBe(1);
279 expect(tableActions.tableActions).toEqual([component.tableActions[0]]);
283 describe('with read and edit', () => {
285 tableActions = permissionHelper.setPermissionsAndGetActions(0, 1, 0);
288 it(`shows no actions`, () => {
289 expect(tableActions.tableActions.length).toBe(1);
290 expect(tableActions.tableActions).toEqual([component.tableActions[1]]);
294 describe('with read and delete', () => {
296 tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 1);
299 it(`shows always 'Delete' as main action`, () => {
300 scenario.single = 'Delete';
301 scenario.empty = 'Delete';
302 permissionHelper.testScenarios(scenario);
305 it(`shows 'Delete' actions`, () => {
306 expect(tableActions.tableActions.length).toBe(1);
307 expect(tableActions.tableActions).toEqual([component.tableActions[2]]);
311 describe('with only read', () => {
313 tableActions = permissionHelper.setPermissionsAndGetActions(0, 0, 0);
316 it('shows no main action', () => {
317 permissionHelper.testScenarios({
318 fn: () => tableActions.getCurrentButton(),
324 it('shows no actions', () => {
325 expect(tableActions.tableActions.length).toBe(0);
326 expect(tableActions.tableActions).toEqual([]);