done
This commit is contained in:
213
apps/backend/test/access-control.e2e-spec.ts
Normal file
213
apps/backend/test/access-control.e2e-spec.ts
Normal file
@@ -0,0 +1,213 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { INestApplication, ValidationPipe } from '@nestjs/common';
|
||||
import * as request from 'supertest';
|
||||
import * as cookieParser from 'cookie-parser';
|
||||
import { AppModule } from '../src/app.module';
|
||||
import { PrismaService } from '../src/modules/prisma/prisma.service';
|
||||
import { PermissionResourceType } from '@prisma/client';
|
||||
|
||||
describe('Security & Access Control (E2E Integration Tests)', () => {
|
||||
let app: INestApplication;
|
||||
let prisma: PrismaService;
|
||||
|
||||
let adminToken: string;
|
||||
let userAToken: string;
|
||||
let userBToken: string;
|
||||
let userAId: string;
|
||||
let userBId: string;
|
||||
let uploadedFileId: string;
|
||||
let grantedPermissionId: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleFixture: TestingModule = await Test.createTestingModule({
|
||||
imports: [AppModule],
|
||||
}).compile();
|
||||
|
||||
app = moduleFixture.createNestApplication();
|
||||
app.use(cookieParser());
|
||||
app.setGlobalPrefix('api/v1');
|
||||
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
|
||||
|
||||
await app.init();
|
||||
prisma = app.get<PrismaService>(PrismaService);
|
||||
|
||||
// 1. Authenticate Super Admin
|
||||
const adminRes = await request(app.getHttpServer())
|
||||
.post('/api/v1/auth/login')
|
||||
.send({ identifier: 'superadmin@fileshare.local', password: 'SuperAdmin@Secure2026!' })
|
||||
.expect(200);
|
||||
adminToken = adminRes.body.accessToken;
|
||||
|
||||
// 2. Authenticate User A
|
||||
const userARes = await request(app.getHttpServer())
|
||||
.post('/api/v1/auth/login')
|
||||
.send({ identifier: 'usera@fileshare.local', password: 'UserA@Secure2026!' })
|
||||
.expect(200);
|
||||
userAToken = userARes.body.accessToken;
|
||||
userAId = userARes.body.user.id;
|
||||
|
||||
// 3. Authenticate User B
|
||||
const userBRes = await request(app.getHttpServer())
|
||||
.post('/api/v1/auth/login')
|
||||
.send({ identifier: 'userb@fileshare.local', password: 'UserB@Secure2026!' })
|
||||
.expect(200);
|
||||
userBToken = userBRes.body.accessToken;
|
||||
userBId = userBRes.body.user.id;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe('Requirement 1 & 2: User A upload, isolation & direct access denial for User B', () => {
|
||||
it('1. User A can upload their own file', async () => {
|
||||
const fileBuffer = Buffer.from('Confidential report content belonging strictly to Alice', 'utf8');
|
||||
|
||||
const res = await request(app.getHttpServer())
|
||||
.post('/api/v1/files/direct-upload')
|
||||
.set('Authorization', `Bearer ${userAToken}`)
|
||||
.attach('file', fileBuffer, 'alice_project.txt')
|
||||
.expect(201);
|
||||
|
||||
expect(res.body.id).toBeDefined();
|
||||
expect(res.body.originalFilename).toBe('alice_project.txt');
|
||||
uploadedFileId = res.body.id;
|
||||
});
|
||||
|
||||
it('1b. User A can download and preview their own file', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}/download`)
|
||||
.set('Authorization', `Bearer ${userAToken}`)
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}/preview`)
|
||||
.set('Authorization', `Bearer ${userAToken}`)
|
||||
.expect(200);
|
||||
});
|
||||
|
||||
it('2. User B cannot access User A private file by guessing ID (IDOR prevention)', async () => {
|
||||
// User B cannot download
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}/download`)
|
||||
.set('Authorization', `Bearer ${userBToken}`)
|
||||
.expect(403);
|
||||
|
||||
// User B cannot preview
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}/preview`)
|
||||
.set('Authorization', `Bearer ${userBToken}`)
|
||||
.expect(403);
|
||||
|
||||
// User B cannot playback
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}/playback`)
|
||||
.set('Authorization', `Bearer ${userBToken}`)
|
||||
.expect(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Requirement 3 & 4: Granular permissions separation (PLAY vs DOWNLOAD)', () => {
|
||||
it('3. Admin grants PLAY to User B, but denies DOWNLOAD', async () => {
|
||||
const permRes = await request(app.getHttpServer())
|
||||
.post('/api/v1/admin/permissions')
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.send({
|
||||
resourceType: PermissionResourceType.FILE,
|
||||
resourceId: uploadedFileId,
|
||||
ownerId: userAId,
|
||||
granteeUserId: userBId,
|
||||
canViewMetadata: true,
|
||||
canPreview: true,
|
||||
canPlay: true,
|
||||
canDownload: false, // Explicitly denied
|
||||
canListFolder: false,
|
||||
})
|
||||
.expect(201);
|
||||
|
||||
expect(permRes.body.id).toBeDefined();
|
||||
grantedPermissionId = permRes.body.id;
|
||||
});
|
||||
|
||||
it('3b. User B can now stream playback of the authorized file', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}/playback`)
|
||||
.set('Authorization', `Bearer ${userBToken}`)
|
||||
.expect(200);
|
||||
});
|
||||
|
||||
it('4. User B is strictly forbidden from downloading when DOWNLOAD is false', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}/download`)
|
||||
.set('Authorization', `Bearer ${userBToken}`)
|
||||
.expect(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Requirement 5: Permission revocation takes effect immediately', () => {
|
||||
it('5. Admin revokes permission from User B', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.post(`/api/v1/admin/permissions/${grantedPermissionId}/revoke`)
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
it('5b. User B subsequent playback request is immediately denied', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}/playback`)
|
||||
.set('Authorization', `Bearer ${userBToken}`)
|
||||
.expect(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Requirement 8: Regular users cannot call administrative endpoints', () => {
|
||||
it('8. Regular user calling /api/v1/admin/users is rejected with 403 Forbidden', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get('/api/v1/admin/users')
|
||||
.set('Authorization', `Bearer ${userAToken}`)
|
||||
.expect(403);
|
||||
});
|
||||
|
||||
it('8b. Regular user calling /api/v1/admin/permissions is rejected with 403 Forbidden', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.get('/api/v1/admin/permissions')
|
||||
.set('Authorization', `Bearer ${userBToken}`)
|
||||
.expect(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Requirement 11: Soft-deleted files are no longer accessible', () => {
|
||||
it('11. User A deletes file and subsequent requests return 404', async () => {
|
||||
await request(app.getHttpServer())
|
||||
.delete(`/api/v1/files/${uploadedFileId}`)
|
||||
.set('Authorization', `Bearer ${userAToken}`)
|
||||
.expect(200);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}`)
|
||||
.set('Authorization', `Bearer ${userAToken}`)
|
||||
.expect(404);
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedFileId}/download`)
|
||||
.set('Authorization', `Bearer ${userAToken}`)
|
||||
.expect(404);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Requirement 14: Audit records are properly created for sensitive operations', () => {
|
||||
it('14. Admin can inspect audit logs for login, file upload, permission grant and revoke', async () => {
|
||||
const logsRes = await request(app.getHttpServer())
|
||||
.get('/api/v1/admin/audit-logs?limit=50')
|
||||
.set('Authorization', `Bearer ${adminToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(logsRes.body.total).toBeGreaterThan(0);
|
||||
const actions = logsRes.body.logs.map((l: any) => l.action);
|
||||
expect(actions).toContain('USER_LOGIN');
|
||||
expect(actions).toContain('FILE_UPLOADED');
|
||||
expect(actions).toContain('PERMISSION_GRANTED');
|
||||
expect(actions).toContain('PERMISSION_REVOKED');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user