|
| 1 | +import { provideHttpClient } from '@angular/common/http'; |
| 2 | +import { NO_ERRORS_SCHEMA, signal } from '@angular/core'; |
| 3 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 4 | +import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog'; |
| 5 | +import { MatSnackBarModule } from '@angular/material/snack-bar'; |
| 6 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 7 | +import { provideRouter } from '@angular/router'; |
| 8 | +import { CodeEditorModule } from '@ngstack/code-editor'; |
| 9 | +import { Angulartics2Module } from 'angulartics2'; |
| 10 | +import { of } from 'rxjs'; |
| 11 | +import { DashboardsService } from 'src/app/services/dashboards.service'; |
| 12 | +import { TablesService } from 'src/app/services/tables.service'; |
| 13 | +import { MockCodeEditorComponent } from 'src/app/testing/code-editor.mock'; |
| 14 | +import { CedarPolicyEditorDialogComponent } from './cedar-policy-editor-dialog.component'; |
| 15 | + |
| 16 | +describe('CedarPolicyEditorDialogComponent', () => { |
| 17 | + let component: CedarPolicyEditorDialogComponent; |
| 18 | + let fixture: ComponentFixture<CedarPolicyEditorDialogComponent>; |
| 19 | + let tablesService: TablesService; |
| 20 | + let dashboardsService: Partial<DashboardsService>; |
| 21 | + |
| 22 | + const mockDialogRef = { |
| 23 | + close: () => {}, |
| 24 | + }; |
| 25 | + |
| 26 | + const fakeTables = [ |
| 27 | + { |
| 28 | + table: 'customers', |
| 29 | + display_name: 'Customers', |
| 30 | + permissions: { visibility: true, readonly: false, add: true, delete: true, edit: true }, |
| 31 | + }, |
| 32 | + { |
| 33 | + table: 'orders', |
| 34 | + display_name: 'Orders', |
| 35 | + permissions: { visibility: true, readonly: false, add: true, delete: false, edit: true }, |
| 36 | + }, |
| 37 | + ]; |
| 38 | + |
| 39 | + const cedarPolicyWithConnection = [ |
| 40 | + 'permit(', |
| 41 | + ' principal,', |
| 42 | + ' action == RocketAdmin::Action::"connection:read",', |
| 43 | + ' resource == RocketAdmin::Connection::"conn-123"', |
| 44 | + ');', |
| 45 | + ].join('\n'); |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + dashboardsService = { |
| 49 | + dashboards: signal([ |
| 50 | + { id: 'dash-1', name: 'Sales', description: null, connection_id: 'conn-123', created_at: '', updated_at: '' }, |
| 51 | + ]).asReadonly(), |
| 52 | + setActiveConnection: vi.fn(), |
| 53 | + }; |
| 54 | + |
| 55 | + TestBed.configureTestingModule({ |
| 56 | + imports: [ |
| 57 | + MatDialogModule, |
| 58 | + MatSnackBarModule, |
| 59 | + BrowserAnimationsModule, |
| 60 | + Angulartics2Module.forRoot({}), |
| 61 | + CedarPolicyEditorDialogComponent, |
| 62 | + ], |
| 63 | + providers: [ |
| 64 | + provideHttpClient(), |
| 65 | + provideRouter([]), |
| 66 | + { |
| 67 | + provide: MAT_DIALOG_DATA, |
| 68 | + useValue: { groupId: 'group-123', groupTitle: 'Test Group', cedarPolicy: cedarPolicyWithConnection }, |
| 69 | + }, |
| 70 | + { provide: MatDialogRef, useValue: mockDialogRef }, |
| 71 | + { provide: DashboardsService, useValue: dashboardsService }, |
| 72 | + ], |
| 73 | + }) |
| 74 | + .overrideComponent(CedarPolicyEditorDialogComponent, { |
| 75 | + remove: { imports: [CodeEditorModule] }, |
| 76 | + add: { imports: [MockCodeEditorComponent], schemas: [NO_ERRORS_SCHEMA] }, |
| 77 | + }) |
| 78 | + .compileComponents(); |
| 79 | + |
| 80 | + tablesService = TestBed.inject(TablesService); |
| 81 | + vi.spyOn(tablesService, 'fetchTables').mockReturnValue(of(fakeTables)); |
| 82 | + |
| 83 | + fixture = TestBed.createComponent(CedarPolicyEditorDialogComponent); |
| 84 | + component = fixture.componentInstance; |
| 85 | + fixture.detectChanges(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should create', () => { |
| 89 | + expect(component).toBeTruthy(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should load tables on init', () => { |
| 93 | + expect(tablesService.fetchTables).toHaveBeenCalled(); |
| 94 | + expect(component.allTables.length).toBe(2); |
| 95 | + expect(component.availableTables.length).toBe(2); |
| 96 | + expect(component.loading).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should pre-populate policy items from existing cedar policy', () => { |
| 100 | + expect(component.policyItems.length).toBeGreaterThan(0); |
| 101 | + expect(component.policyItems.some((item) => item.action === 'connection:read')).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should start in form mode', () => { |
| 105 | + expect(component.editorMode).toBe('form'); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should switch to code mode', () => { |
| 109 | + component.onEditorModeChange('code'); |
| 110 | + expect(component.editorMode).toBe('code'); |
| 111 | + expect(component.cedarPolicy).toBeTruthy(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments