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

Add checkDirExists endpoint + tests #38

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {addGetFileEndpoint} from './endpoints/file/getFile';
import {addUploadFileEndpoint} from './endpoints/file/uploadFile';
import {addCheckFileExistsEndpoint} from './endpoints/file/checkFileExists';
import {addGetFileSizeEndpoint} from './endpoints/file/getFileSize';
import {addCheckDirExistsEndpoint} from './endpoints/dir/checkDirExists';

export function buildApp() {
let app = new Elysia();
Expand All @@ -20,5 +21,6 @@ export function buildApp() {
addGetFileSizeEndpoint(app);
addUploadFileEndpoint(app);
addCheckFileExistsEndpoint(app);
addCheckDirExistsEndpoint(app);
return app;
}
1 change: 1 addition & 0 deletions src/constants/commonResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const fileAlreadyExistsMsg = 'The file already exists';
export const fileEmptyMsg = 'The file is empty';
export const fileMustNotEmptyMsg = 'File contents cannot be empty';
export const provideValidPathMsg = 'You must provide a valid path to a file';
export const provideValidPathDirMsg = 'You must provide a valid path to a directory';
export const unsupportedContentTypeMsg = 'Unsupported content type';
export const unsupportedBinaryDataTypeMsg = 'Unsupported binary data type';
export const invalidFileFormatInFormDataMsg = 'Invalid file format in form data';
Expand Down
58 changes: 58 additions & 0 deletions src/endpoints/dir/checkDirExists.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {afterEach, beforeEach, expect, test} from 'bun:test';
import {buildApp} from '../../app';
import {RequestBuilder} from '../../utils/requestBuilder';
import {UrlBuilder} from '../../utils/urlBuilder';
import {fs1TestDirectoryContents} from '../../testing/constants';
import {createTestFileSystem, destroyTestFileSystem} from '../../testing/testFileSystem';

function buildDirRequest(path: string | null) {
let url = new UrlBuilder('http://localhost/dir/exists');
if (path != null) {
url.setParam('path', path);
}
const requestBuilder = new RequestBuilder(url).setMethod('GET');
return requestBuilder.build();
}

// App instance
let app: ReturnType<typeof buildApp>;

const directories = fs1TestDirectoryContents;
const directoriesList = Object.keys(directories).filter(directory => !directory.includes('.'));
const testDirectoryContents = fs1TestDirectoryContents;

beforeEach(async () => {
await createTestFileSystem(testDirectoryContents);
app = buildApp();
});

afterEach(async () => {
await destroyTestFileSystem();
});

test('should return HTTP Status 204 if the directory is found', async () => {
for (const directory of directoriesList) {
const request = buildDirRequest(directory);
const response = await app.handle(request);
expect(response.status).toBe(204);
}
});

test('should return a 404 if the directory is not found', async () => {
const dirPath = `/path/to/nowhere`;
const request = buildDirRequest(dirPath);
const response = await app.handle(request);
expect(response.status).toBe(404);
});

test('should return 400 when no dir is provided', async () => {
// Empty string
let request = buildDirRequest('');
let response = await app.handle(request);
expect(response.status).toBe(400);

// The `path` param is not provided at all
request = buildDirRequest(null);
response = await app.handle(request);
expect(response.status).toBe(400);
});
31 changes: 31 additions & 0 deletions src/endpoints/dir/checkDirExists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Elysia from 'elysia';
import {validateRelativePath} from '../../utils/pathUtils';
import {checkIfDirectoryExists} from '../../utils/directoryUtils';
import {dirNotExistsMsg, provideValidPathDirMsg} from '../../constants/commonResponses';

export function addCheckDirExistsEndpoint(app: Elysia) {
return app.get('dir/exists', async ({query, set}) => {
// Verify that the path is valid
const dirPathValidationResult = await validateRelativePath(query.path);
if (dirPathValidationResult.isErr()) {
set.status = 400;
return provideValidPathDirMsg;
}
// Get the full path to the directory
const {absolutePath} = dirPathValidationResult.value;

try {
// Get the directory
const dir = await checkIfDirectoryExists(absolutePath);
if (dir === false) {
set.status = 404;
return dirNotExistsMsg;
}
set.status = 204;
return '';
} catch (error) {
set.status = 400;
return provideValidPathDirMsg;
}
});
}
Loading