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');
|
||||
});
|
||||
});
|
||||
});
|
||||
10
apps/backend/test/jest-e2e.json
Normal file
10
apps/backend/test/jest-e2e.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"moduleFileExtensions": ["js", "json", "ts"],
|
||||
"rootDir": ".",
|
||||
"testEnvironment": "node",
|
||||
"testRegex": ".e2e-spec.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"testTimeout": 30000
|
||||
}
|
||||
97
apps/backend/test/video-processing.e2e-spec.ts
Normal file
97
apps/backend/test/video-processing.e2e-spec.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { INestApplication, ValidationPipe } from '@nestjs/common';
|
||||
import * as request from 'supertest';
|
||||
import * as cookieParser from 'cookie-parser';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { AppModule } from '../src/app.module';
|
||||
import { VideoProcessorService } from '../src/modules/video/video-processor.service';
|
||||
|
||||
describe('Video Processing & HLS Transcoding (E2E Integration Test)', () => {
|
||||
let app: INestApplication;
|
||||
let videoProcessor: VideoProcessorService;
|
||||
let userToken: string;
|
||||
let uploadedVideoId: 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();
|
||||
videoProcessor = app.get<VideoProcessorService>(VideoProcessorService);
|
||||
|
||||
// Login as User A
|
||||
const loginRes = await request(app.getHttpServer())
|
||||
.post('/api/v1/auth/login')
|
||||
.send({ identifier: 'usera@fileshare.local', password: 'UserA@Secure2026!' })
|
||||
.expect(200);
|
||||
|
||||
userToken = loginRes.body.accessToken;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('uploads an MP4 video, probes metadata, extracts thumbnail and generates HLS playlist', async () => {
|
||||
const videoFilePath = path.resolve(__dirname, '../../../test_video.mp4');
|
||||
expect(fs.existsSync(videoFilePath)).toBe(true);
|
||||
|
||||
const videoBuffer = fs.readFileSync(videoFilePath);
|
||||
|
||||
// 1. Upload video
|
||||
const uploadRes = await request(app.getHttpServer())
|
||||
.post('/api/v1/files/direct-upload')
|
||||
.set('Authorization', `Bearer ${userToken}`)
|
||||
.attach('file', videoBuffer, 'sample_demo_clip.mp4')
|
||||
.expect(201);
|
||||
|
||||
expect(uploadRes.body.id).toBeDefined();
|
||||
expect(uploadRes.body.fileCategory).toBe('VIDEO');
|
||||
uploadedVideoId = uploadRes.body.id;
|
||||
|
||||
// 2. Run video processing directly to verify FFmpeg and HLS generation
|
||||
await videoProcessor.processVideo(uploadedVideoId);
|
||||
|
||||
// 3. Verify file record is updated with metadata, thumbnail and HLS key
|
||||
const fileRes = await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedVideoId}`)
|
||||
.set('Authorization', `Bearer ${userToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(fileRes.body.status).toBe('READY');
|
||||
expect(fileRes.body.durationSeconds).toBeGreaterThan(0);
|
||||
expect(fileRes.body.width).toBe(640);
|
||||
expect(fileRes.body.height).toBe(360);
|
||||
expect(fileRes.body.videoCodec).toBe('h264');
|
||||
expect(fileRes.body.hasThumbnail).toBe(true);
|
||||
expect(fileRes.body.hasHlsStream).toBe(true);
|
||||
|
||||
// 4. Verify thumbnail can be fetched
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedVideoId}/thumbnail`)
|
||||
.set('Authorization', `Bearer ${userToken}`)
|
||||
.expect(200);
|
||||
|
||||
// 5. Verify HLS master playlist can be fetched
|
||||
const hlsRes = await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedVideoId}/stream/master.m3u8`)
|
||||
.set('Authorization', `Bearer ${userToken}`)
|
||||
.expect(200);
|
||||
|
||||
expect(hlsRes.text).toContain('#EXTM3U');
|
||||
expect(hlsRes.text).toContain('segment_');
|
||||
|
||||
// 6. Verify HLS media chunk segment can be fetched
|
||||
await request(app.getHttpServer())
|
||||
.get(`/api/v1/files/${uploadedVideoId}/stream/segment_000.ts`)
|
||||
.set('Authorization', `Bearer ${userToken}`)
|
||||
.expect(200);
|
||||
}, 45000);
|
||||
});
|
||||
Reference in New Issue
Block a user