|
| 1 | +import { Signal, signal, WritableSignal } from '@angular/core'; |
| 2 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 4 | +import { of } from 'rxjs'; |
| 5 | +import { PublicPermissions } from 'src/app/models/user'; |
| 6 | +import { ConnectionsService } from 'src/app/services/connections.service'; |
| 7 | +import { TablesService } from 'src/app/services/tables.service'; |
| 8 | +import { UsersService } from 'src/app/services/users.service'; |
| 9 | +import { PublicAccessPanelComponent } from './public-access-panel.component'; |
| 10 | + |
| 11 | +type PublicAccessPanelTestable = PublicAccessPanelComponent & { |
| 12 | + statusLabel: Signal<string>; |
| 13 | + selectedCount: Signal<number>; |
| 14 | + canDisable: Signal<boolean>; |
| 15 | + tablesLoading: WritableSignal<boolean>; |
| 16 | + tables: Signal<Array<{ tableName: string; displayName: string }>>; |
| 17 | +}; |
| 18 | + |
| 19 | +const CONNECTION_ID = '5a2d4e0c-6d3a-4b0f-9d1a-4a0f0a4c8b11'; |
| 20 | + |
| 21 | +const fakeTables = [ |
| 22 | + { table: 'customers', display_name: 'Customers' }, |
| 23 | + { table: 'orders', display_name: '' }, |
| 24 | +]; |
| 25 | + |
| 26 | +const fakeStructure = { |
| 27 | + structure: [{ column_name: 'id' }, { column_name: 'name' }, { column_name: 'secret' }], |
| 28 | +}; |
| 29 | + |
| 30 | +describe('PublicAccessPanelComponent', () => { |
| 31 | + let component: PublicAccessPanelComponent; |
| 32 | + let testable: PublicAccessPanelTestable; |
| 33 | + let fixture: ComponentFixture<PublicAccessPanelComponent>; |
| 34 | + let publicPermissions: WritableSignal<PublicPermissions>; |
| 35 | + let mockUsersService: Partial<UsersService>; |
| 36 | + let mockTablesService: Partial<TablesService>; |
| 37 | + |
| 38 | + beforeEach(async () => { |
| 39 | + publicPermissions = signal<PublicPermissions>({ enabled: false, tables: [] }); |
| 40 | + |
| 41 | + mockUsersService = { |
| 42 | + publicPermissions: publicPermissions.asReadonly(), |
| 43 | + publicPermissionsLoading: signal(false).asReadonly(), |
| 44 | + loadPublicPermissions: vi.fn(), |
| 45 | + savePublicPermissions: vi.fn().mockResolvedValue(undefined), |
| 46 | + }; |
| 47 | + |
| 48 | + mockTablesService = { |
| 49 | + fetchTables: vi.fn().mockReturnValue(of(fakeTables)), |
| 50 | + fetchTableStructure: vi.fn().mockReturnValue(of(fakeStructure)), |
| 51 | + }; |
| 52 | + |
| 53 | + const mockConnectionsService: Partial<ConnectionsService> = { |
| 54 | + get currentConnectionID() { |
| 55 | + return CONNECTION_ID; |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + await TestBed.configureTestingModule({ |
| 60 | + imports: [BrowserAnimationsModule, PublicAccessPanelComponent], |
| 61 | + providers: [ |
| 62 | + { provide: UsersService, useValue: mockUsersService }, |
| 63 | + { provide: TablesService, useValue: mockTablesService }, |
| 64 | + { provide: ConnectionsService, useValue: mockConnectionsService }, |
| 65 | + ], |
| 66 | + }).compileComponents(); |
| 67 | + |
| 68 | + fixture = TestBed.createComponent(PublicAccessPanelComponent); |
| 69 | + component = fixture.componentInstance; |
| 70 | + testable = component as PublicAccessPanelTestable; |
| 71 | + fixture.detectChanges(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should create', () => { |
| 75 | + expect(component).toBeTruthy(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should opt in to loading public permissions for this connection on init', () => { |
| 79 | + expect(mockUsersService.loadPublicPermissions).toHaveBeenCalledWith(CONNECTION_ID); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should load the connection tables and fall back to a normalized display name', () => { |
| 83 | + expect(mockTablesService.fetchTables).toHaveBeenCalledWith(CONNECTION_ID); |
| 84 | + expect(testable.tablesLoading()).toBe(false); |
| 85 | + expect(testable.tables()).toEqual([ |
| 86 | + { tableName: 'customers', displayName: 'Customers' }, |
| 87 | + { tableName: 'orders', displayName: 'Orders' }, |
| 88 | + ]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should report disabled status when nothing is selected', () => { |
| 92 | + expect(testable.selectedCount()).toBe(0); |
| 93 | + expect(testable.statusLabel()).toBe('Disabled'); |
| 94 | + expect(testable.canDisable()).toBe(false); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should seed selection and columns from the stored permissions', () => { |
| 98 | + publicPermissions.set({ |
| 99 | + enabled: true, |
| 100 | + tables: [{ tableName: 'customers', readableColumns: ['id', 'name'] }, { tableName: 'orders' }], |
| 101 | + }); |
| 102 | + fixture.detectChanges(); |
| 103 | + |
| 104 | + expect(component.isSelected('customers')).toBe(true); |
| 105 | + expect(component.selectedColumns('customers')).toEqual(['id', 'name']); |
| 106 | + expect(component.isSelected('orders')).toBe(true); |
| 107 | + expect(component.selectedColumns('orders')).toEqual([]); |
| 108 | + expect(testable.statusLabel()).toBe('2 tables'); |
| 109 | + expect(testable.canDisable()).toBe(true); |
| 110 | + // Columns are only fetched for tables that already carry a whitelist. |
| 111 | + expect(mockTablesService.fetchTableStructure).toHaveBeenCalledWith(CONNECTION_ID, 'customers'); |
| 112 | + expect(mockTablesService.fetchTableStructure).not.toHaveBeenCalledWith(CONNECTION_ID, 'orders'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should singularize the status label for one table', () => { |
| 116 | + publicPermissions.set({ enabled: true, tables: [{ tableName: 'customers' }] }); |
| 117 | + fixture.detectChanges(); |
| 118 | + |
| 119 | + expect(testable.statusLabel()).toBe('1 table'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should fetch columns lazily when a table is checked', () => { |
| 123 | + component.toggleTable('customers', true); |
| 124 | + |
| 125 | + expect(mockTablesService.fetchTableStructure).toHaveBeenCalledWith(CONNECTION_ID, 'customers'); |
| 126 | + expect(component.availableColumns('customers')).toEqual(['id', 'name', 'secret']); |
| 127 | + expect(component.isLoadingColumns('customers')).toBe(false); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should not refetch columns for a table already loaded', () => { |
| 131 | + component.toggleTable('customers', true); |
| 132 | + component.toggleTable('customers', false); |
| 133 | + component.toggleTable('customers', true); |
| 134 | + |
| 135 | + expect(mockTablesService.fetchTableStructure).toHaveBeenCalledTimes(1); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should save an empty column selection as undefined readableColumns', async () => { |
| 139 | + component.toggleTable('customers', true); |
| 140 | + await component.save(); |
| 141 | + |
| 142 | + expect(mockUsersService.savePublicPermissions).toHaveBeenCalledWith(CONNECTION_ID, [ |
| 143 | + { tableName: 'customers', readableColumns: undefined }, |
| 144 | + ]); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should save an explicit column whitelist', async () => { |
| 148 | + component.toggleTable('customers', true); |
| 149 | + component.setColumns('customers', ['id', 'name']); |
| 150 | + await component.save(); |
| 151 | + |
| 152 | + expect(mockUsersService.savePublicPermissions).toHaveBeenCalledWith(CONNECTION_ID, [ |
| 153 | + { tableName: 'customers', readableColumns: ['id', 'name'] }, |
| 154 | + ]); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should drop a table from the payload when unchecked', async () => { |
| 158 | + component.toggleTable('customers', true); |
| 159 | + component.toggleTable('orders', true); |
| 160 | + component.toggleTable('customers', false); |
| 161 | + await component.save(); |
| 162 | + |
| 163 | + expect(mockUsersService.savePublicPermissions).toHaveBeenCalledWith(CONNECTION_ID, [ |
| 164 | + { tableName: 'orders', readableColumns: undefined }, |
| 165 | + ]); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should render the table list and column picker once expanded', async () => { |
| 169 | + publicPermissions.set({ enabled: true, tables: [{ tableName: 'customers', readableColumns: ['id'] }] }); |
| 170 | + fixture.detectChanges(); |
| 171 | + |
| 172 | + // The body lives in an ng-template matExpansionPanelContent, so nothing above renders it. |
| 173 | + fixture.nativeElement.querySelector('mat-expansion-panel-header').click(); |
| 174 | + fixture.detectChanges(); |
| 175 | + await fixture.whenStable(); |
| 176 | + |
| 177 | + const host: HTMLElement = fixture.nativeElement; |
| 178 | + expect(host.querySelector('.public-access-warning')).toBeTruthy(); |
| 179 | + expect(host.querySelectorAll('.public-access-table').length).toBe(2); |
| 180 | + expect(host.textContent).toContain('Customers'); |
| 181 | + expect(host.textContent).toContain('Orders'); |
| 182 | + // Only the selected table exposes a column picker. |
| 183 | + expect(host.querySelectorAll('.public-access-columns').length).toBe(1); |
| 184 | + expect(host.textContent).toContain('Disable public access'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should disable public access by saving an empty table list', async () => { |
| 188 | + publicPermissions.set({ enabled: true, tables: [{ tableName: 'customers' }] }); |
| 189 | + fixture.detectChanges(); |
| 190 | + |
| 191 | + await component.disablePublicAccess(); |
| 192 | + |
| 193 | + expect(mockUsersService.savePublicPermissions).toHaveBeenCalledWith(CONNECTION_ID, []); |
| 194 | + expect(testable.selectedCount()).toBe(0); |
| 195 | + }); |
| 196 | +}); |
0 commit comments