]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/auth-storage.directive.spec.ts
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / shared / directives / auth-storage.directive.spec.ts
1 import { Component } from '@angular/core';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3
4 import { Permissions } from '~/app/shared/models/permissions';
5 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
6 import { configureTestBed } from '~/testing/unit-test-helper';
7 import { AuthStorageDirective } from './auth-storage.directive';
8 @Component({
9 template: `<div id="permitted" *cdScope="condition; matchAll: matchAll"></div>`
10 })
11 export class AuthStorageDirectiveTestComponent {
12 condition: string | string[] | object;
13 matchAll = true;
14 }
15
16 describe('AuthStorageDirective', () => {
17 let fixture: ComponentFixture<AuthStorageDirectiveTestComponent>;
18 let component: AuthStorageDirectiveTestComponent;
19 let getPermissionsSpy: jasmine.Spy;
20 let el: HTMLElement;
21
22 configureTestBed({
23 declarations: [AuthStorageDirective, AuthStorageDirectiveTestComponent],
24 providers: [AuthStorageService]
25 });
26
27 beforeEach(() => {
28 getPermissionsSpy = spyOn(TestBed.inject(AuthStorageService), 'getPermissions');
29 getPermissionsSpy.and.returnValue(new Permissions({ osd: ['read'], rgw: ['read'] }));
30 fixture = TestBed.createComponent(AuthStorageDirectiveTestComponent);
31 el = fixture.debugElement.nativeElement;
32 component = fixture.componentInstance;
33 });
34
35 it('should show div on valid condition', () => {
36 // String condition
37 component.condition = 'rgw';
38 fixture.detectChanges();
39 expect(el.querySelector('#permitted')).toBeTruthy();
40
41 // Array condition
42 component.condition = ['osd', 'rgw'];
43 fixture.detectChanges();
44 expect(el.querySelector('#permitted')).toBeTruthy();
45
46 // Object condition
47 component.condition = { rgw: ['read'], osd: ['read'] };
48 fixture.detectChanges();
49 expect(el.querySelector('#permitted')).toBeTruthy();
50 });
51
52 it('should show div with loose matching', () => {
53 component.matchAll = false;
54 fixture.detectChanges();
55
56 // Array condition
57 component.condition = ['configOpt', 'osd', 'rgw'];
58 fixture.detectChanges();
59 expect(el.querySelector('#permitted')).toBeTruthy();
60
61 // Object condition
62 component.condition = { rgw: ['read', 'update', 'fake'], osd: ['read'] };
63 fixture.detectChanges();
64 expect(el.querySelector('#permitted')).toBeTruthy();
65 });
66
67 it('should not show div on invalid condition', () => {
68 // String condition
69 component.condition = 'fake';
70 fixture.detectChanges();
71 expect(el.querySelector('#permitted')).toBeFalsy();
72
73 // Array condition
74 component.condition = ['configOpt', 'osd', 'rgw'];
75 fixture.detectChanges();
76 expect(el.querySelector('#permitted')).toBeFalsy();
77
78 // Object condition
79 component.condition = { rgw: ['read', 'update'], osd: ['read'] };
80 fixture.detectChanges();
81 expect(el.querySelector('#permitted')).toBeFalsy();
82 });
83
84 it('should hide div on condition change', () => {
85 component.condition = 'osd';
86 fixture.detectChanges();
87 expect(el.querySelector('#permitted')).toBeTruthy();
88
89 component.condition = 'grafana';
90 fixture.detectChanges();
91 expect(el.querySelector('#permitted')).toBeFalsy();
92 });
93
94 it('should hide div on permission change', () => {
95 component.condition = ['osd'];
96 fixture.detectChanges();
97 expect(el.querySelector('#permitted')).toBeTruthy();
98
99 getPermissionsSpy.and.returnValue(new Permissions({}));
100 component.condition = 'osd';
101 fixture.detectChanges();
102 expect(el.querySelector('#permitted')).toBeFalsy();
103 });
104 });