Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
feat: add tests for validateLocalFile func
Browse files Browse the repository at this point in the history
Signed-off-by: michalrozekariane <michal.rozek@arianelabs.com>
  • Loading branch information
michalrozekariane committed Feb 12, 2024
1 parent 3104fd9 commit 50de01e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/utils/constants/dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const dictionary = {
requiredFieldMissing: 'Required field is missing',
requiredTypeFieldIsMissing: 'The required "type" field is missing. ',
requiredAttributeFieldMissing: 'The required "attributes" field is missing. ',
filePermissionDenied: 'Permission denied',
fileEmptyOrFormattingError: 'Unexpected end of JSON input',
},
hederaActions: {
clientRequired: 'client is required. You need to log in first.',
Expand Down
16 changes: 13 additions & 3 deletions src/utils/services/Hip412Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,19 @@ export class Hip412Validator {
};

static validateLocalFile(filePath: string): FileValidationResult {
const fileContent = fs.readFileSync(filePath, 'utf8');
const object: MetadataObject = JSON.parse(fileContent);
return this.validateSingleMetadataObject(object);
try {
const fileContent = fs.readFileSync(filePath, 'utf8');
const object: MetadataObject = JSON.parse(fileContent);
return this.validateSingleMetadataObject(object);
} catch (error) {
return {
isValid: false,
errors: {
general: [errorToMessage(error)],
missingAttributes: [],
},
};
}
}

static validateLocalDirectory(directoryPath: string): DirectoryValidationResult {
Expand Down
54 changes: 54 additions & 0 deletions test/unit/validateLocalFile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from 'fs';
import { Hip412Validator } from '../../src/utils/services/Hip412Validator';
import { dictionary } from '../../src/utils/constants/dictionary';

jest.mock('fs');

const mockReadFileSync = fs.readFileSync as jest.Mock;

describe('Hip412Validator.validateLocalFile', () => {
beforeEach(() => {
jest.resetAllMocks();
});

it('should throw errors during file reading without permissions', () => {
mockReadFileSync.mockImplementation(() => {
throw new Error(dictionary.validation.filePermissionDenied);
});
const validationResult = Hip412Validator.validateLocalFile('mockPath.json');
expect(validationResult.isValid).toBe(false);
expect(validationResult.errors.general).toContain(dictionary.validation.filePermissionDenied);
});

it('should handle JSON files with formatting errors', () => {
mockReadFileSync.mockImplementation(() => {
throw new Error(dictionary.validation.fileEmptyOrFormattingError);
});
const validationResult = Hip412Validator.validateLocalFile('mockPath.json');
expect(validationResult.isValid).toBe(false);
expect(validationResult.errors.general).toEqual([
dictionary.validation.fileEmptyOrFormattingError,
]);
});

it('should handle empty or non-existent JSON files', () => {
mockReadFileSync.mockReturnValue('');
const validationResult = Hip412Validator.validateLocalFile('path/to/empty.json');
expect(validationResult.isValid).toBe(false);
expect(validationResult.errors.general).toEqual([
dictionary.validation.fileEmptyOrFormattingError,
]);
});

it('should validate correctly structured JSON file', () => {
const validJson = JSON.stringify({
name: 'Example NFT',
image: 'https://example.com/nft.jpg',
type: 'image/jpeg',
});
mockReadFileSync.mockReturnValue(validJson);
const validationResult = Hip412Validator.validateLocalFile('path/to/valid.json');
expect(validationResult.isValid).toBe(true);
expect(validationResult.errors.general).toHaveLength(0);
});
});

0 comments on commit 50de01e

Please sign in to comment.