|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 2 | + |
| 3 | +import { INestApplication, ValidationPipe } from '@nestjs/common'; |
| 4 | +import { Test } from '@nestjs/testing'; |
| 5 | +import test from 'ava'; |
| 6 | +import { ValidationError } from 'class-validator'; |
| 7 | +import cookieParser from 'cookie-parser'; |
| 8 | +import request from 'supertest'; |
| 9 | +import { ApplicationModule } from '../../../src/app.module.js'; |
| 10 | +import { CedarAction } from '../../../src/entities/cedar-authorization/cedar-action-map.js'; |
| 11 | +import { WinstonLogger } from '../../../src/entities/logging/winston-logger.js'; |
| 12 | +import { AllExceptionsFilter } from '../../../src/exceptions/all-exceptions.filter.js'; |
| 13 | +import { ValidationException } from '../../../src/exceptions/custom-exceptions/validation-exception.js'; |
| 14 | +import { Cacher } from '../../../src/helpers/cache/cacher.js'; |
| 15 | +import { DatabaseModule } from '../../../src/shared/database/database.module.js'; |
| 16 | +import { DatabaseService } from '../../../src/shared/database/database.service.js'; |
| 17 | +import { registerUserAndReturnUserInfo } from '../../utils/register-user-and-return-user-info.js'; |
| 18 | +import { setSaasEnvVariable } from '../../utils/set-saas-env-variable.js'; |
| 19 | +import { TestUtils } from '../../utils/test.utils.js'; |
| 20 | + |
| 21 | +let app: INestApplication; |
| 22 | + |
| 23 | +test.before(async () => { |
| 24 | + setSaasEnvVariable(); |
| 25 | + const moduleFixture = await Test.createTestingModule({ |
| 26 | + imports: [ApplicationModule, DatabaseModule], |
| 27 | + providers: [DatabaseService, TestUtils], |
| 28 | + }).compile(); |
| 29 | + app = moduleFixture.createNestApplication(); |
| 30 | + |
| 31 | + app.use(cookieParser()); |
| 32 | + app.useGlobalFilters(new AllExceptionsFilter(app.get(WinstonLogger))); |
| 33 | + app.useGlobalPipes( |
| 34 | + new ValidationPipe({ |
| 35 | + exceptionFactory(validationErrors: ValidationError[] = []) { |
| 36 | + return new ValidationException(validationErrors); |
| 37 | + }, |
| 38 | + }), |
| 39 | + ); |
| 40 | + await app.init(); |
| 41 | + app.getHttpServer().listen(0); |
| 42 | +}); |
| 43 | + |
| 44 | +test.after(async () => { |
| 45 | + await Cacher.clearAllCache(); |
| 46 | + await app.close(); |
| 47 | +}); |
| 48 | + |
| 49 | +test.serial('GET /permissions/available returns catalog covering every CedarAction', async (t) => { |
| 50 | + const token = (await registerUserAndReturnUserInfo(app)).token; |
| 51 | + |
| 52 | + const response = await request(app.getHttpServer()) |
| 53 | + .get('/permissions/available') |
| 54 | + .set('Cookie', token) |
| 55 | + .set('Accept', 'application/json'); |
| 56 | + |
| 57 | + t.is(response.status, 200); |
| 58 | + |
| 59 | + const body = response.body as { |
| 60 | + actions: Array<{ value: string; resource?: string }>; |
| 61 | + }; |
| 62 | + |
| 63 | + t.true(Array.isArray(body.actions)); |
| 64 | + t.true(body.actions.length > 0); |
| 65 | + |
| 66 | + const values = new Set(body.actions.map((a) => a.value)); |
| 67 | + |
| 68 | + for (const cedarValue of Object.values(CedarAction)) { |
| 69 | + t.true(values.has(cedarValue), `catalog missing CedarAction ${cedarValue}`); |
| 70 | + } |
| 71 | + |
| 72 | + t.false(values.has('*'), 'catalog must NOT include synthesized wildcards'); |
| 73 | + t.false(values.has('table:*'), 'catalog must NOT include synthesized wildcards'); |
| 74 | + t.false(values.has('dashboard:*'), 'catalog must NOT include synthesized wildcards'); |
| 75 | + |
| 76 | + const byValue = new Map(body.actions.map((a) => [a.value, a])); |
| 77 | + |
| 78 | + t.is(byValue.get('connection:read')!.resource, 'connection'); |
| 79 | + t.is(byValue.get('group:edit')!.resource, 'group'); |
| 80 | + t.is(byValue.get('table:read')!.resource, 'table'); |
| 81 | + t.is(byValue.get('actionEvent:trigger')!.resource, 'actionEvent'); |
| 82 | + t.is(byValue.get('dashboard:read')!.resource, 'dashboard'); |
| 83 | + t.is(byValue.get('dashboard:create')!.resource, 'dashboard'); |
| 84 | + t.is(byValue.get('panel:read')!.resource, 'panel'); |
| 85 | + |
| 86 | + for (const action of body.actions) { |
| 87 | + t.is(Object.hasOwn(action, 'label'), false, `action ${action.value} should not have label`); |
| 88 | + t.is(Object.hasOwn(action, 'shortLabel'), false, `action ${action.value} should not have shortLabel`); |
| 89 | + t.is(Object.hasOwn(action, 'icon'), false, `action ${action.value} should not have icon`); |
| 90 | + } |
| 91 | +}); |
| 92 | + |
| 93 | +test.serial('GET /permissions/available requires authentication', async (t) => { |
| 94 | + const response = await request(app.getHttpServer()).get('/permissions/available').set('Accept', 'application/json'); |
| 95 | + |
| 96 | + t.is(response.status, 401); |
| 97 | +}); |
0 commit comments