Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds check empty file upload and tests added tests for file upload. #160

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/common/src/controllers/file-upload.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { FastifyReply } from 'fastify';

@Controller('files')
export class FileUploadController {
constructor(private readonly filesService: FileUploadService) {}
constructor(private readonly filesService: FileUploadService) { }

@Post('upload-file')
@UseInterceptors(FastifyFileInterceptor('file', {}))
Expand All @@ -29,6 +29,13 @@ export class FileUploadController {
file?: { url: string } | undefined;
}> {
try {
// file.size comes from MultiPartFile Interface defined in file-upload.interface
if (file.size === 0) {
return {
statusCode: 400,
message: 'empty file uploads are not allowed'
}
}
const directory = await this.filesService.upload(
file,
destination,
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/interceptors/file-upload.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export function FastifyFileInterceptor(
next: CallHandler,
): Promise<Observable<any>> {
const ctx = context.switchToHttp();
const request = ctx.getRequest();
request.raw = request.raw || request;

await new Promise<void>((resolve, reject) =>
this.multer.single(fieldName)(
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/interfaces/file-upload.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ export interface MultipartFile {
encoding: string;
mimetype: string;
fields: fastifyMutipart.MultipartFields;
size: number;
buffer: Buffer;
}
34 changes: 34 additions & 0 deletions packages/common/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import * as fs from 'fs';
import * as path from 'path';

describe('AppController (e2e)', () => {
let app: INestApplication;
Expand All @@ -21,4 +23,36 @@ describe('AppController (e2e)', () => {
.expect(200)
.expect('Hello World!');
});

it('/files/upload-file (POST); for file with content', async () => {
const mockDestination = 'uploads';
const mockFilename = 'empty.txt';
const response = await request(app.getHttpServer())
.post(
`/files/upload-file?destination=${mockDestination}&filename=${mockFilename}`,
)
.attach('file', Buffer.from('abcd'), mockFilename);
expect(response.body).toEqual({
message: 'File uploaded successfully',
file: { url: mockDestination },
});

// clean up created file from uploads dir
const emptyFilePath = path.join(__dirname, '../uploads/empty.txt');
fs.unlinkSync(emptyFilePath);
});

it('/files/upload-file (POST); for empty file check', async () => {
const mockDestination = 'uploads';
const mockFilename = 'empty.txt';
const response = await request(app.getHttpServer())
.post(
`/files/upload-file?destination=${mockDestination}&filename=${mockFilename}`,
)
.attach('file', Buffer.from(''), mockFilename);
expect(response.body).toEqual({
statusCode: 400,
message: 'empty file uploads are not allowed',
});
});
});
34 changes: 34 additions & 0 deletions sample/06-file-upload/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
import * as fs from 'fs';
import * as path from 'path';

describe('AppController (e2e)', () => {
let app: INestApplication;
Expand All @@ -21,4 +23,36 @@ describe('AppController (e2e)', () => {
.expect(200)
.expect('Hello World from file upload.');
});

it('/files/upload-file (POST); for file with content', async () => {
const mockDestination = 'uploads';
const mockFilename = 'empty.txt';
const response = await request(app.getHttpServer())
.post(
`/files/upload-file?destination=${mockDestination}&filename=${mockFilename}`,
)
.attach('file', Buffer.from('abcd'), mockFilename);
expect(response.body).toEqual({
message: 'File uploaded successfully',
file: { url: mockDestination },
});

// clean up created file from uploads dir
const emptyFilePath = path.join(__dirname, '../uploads/empty.txt');
fs.unlinkSync(emptyFilePath);
});

it('/files/upload-file (POST); for empty file check', async () => {
const mockDestination = 'uploads';
const mockFilename = 'empty.txt';
const response = await request(app.getHttpServer())
.post(
`/files/upload-file?destination=${mockDestination}&filename=${mockFilename}`,
)
.attach('file', Buffer.from(''), mockFilename);
expect(response.body).toEqual({
statusCode: 400,
message: 'empty file uploads are not allowed',
});
});
});