Skip to content
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
1 change: 1 addition & 0 deletions redisinsight/api/src/__mocks__/custom-tutorial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export const mockCustomTutorialManifestProvider = jest.fn(() => ({
getOriginalManifestJson: jest.fn().mockResolvedValue(mockCustomTutorialManifestJson),
getManifestJson: jest.fn().mockResolvedValue(mockCustomTutorialManifestJson),
generateTutorialManifest: jest.fn().mockResolvedValue(mockCustomTutorialManifest),
isOriginalManifestExists: jest.fn().mockResolvedValue(true),
}));

export const mockCustomTutorialRepository = jest.fn(() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('CustomTutorialService', () => {

it('Should create custom tutorial from external url (w/o manifest)', async () => {
customTutorialManifestProvider.getOriginalManifestJson.mockResolvedValue(null);
customTutorialManifestProvider.isOriginalManifestExists.mockResolvedValue(false);

const result = await service.create(mockUploadCustomTutorialExternalLinkDto);

Expand All @@ -124,6 +125,18 @@ describe('CustomTutorialService', () => {
}
});

it('Should throw BadRequestException in case when manifest exists but unable to parse it', async () => {
customTutorialManifestProvider.getOriginalManifestJson.mockResolvedValueOnce(null);
customTutorialManifestProvider.isOriginalManifestExists.mockResolvedValueOnce(true);

try {
await service.create(mockUploadCustomTutorialExternalLinkDto);
} catch (e) {
expect(e).toBeInstanceOf(BadRequestException);
expect(e.message).toEqual('Unable to parse manifest.json file');
}
});

it('Should throw BadRequestException in case when manifest is not an object', async () => {
customTutorialManifestProvider.getOriginalManifestJson.mockResolvedValue([mockCustomTutorialManifest]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class CustomTutorialService {
private async validateManifestJson(path: string): Promise<void> {
const manifest = await this.customTutorialManifestProvider.getOriginalManifestJson(path);

if (!manifest && await this.customTutorialManifestProvider.isOriginalManifestExists(path)) {
throw new BadRequestException('Unable to parse manifest.json file');
}

if (manifest) {
if (!isPlainObject(manifest)) {
throw new BadRequestException('Manifest json should be an object');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ describe('CustomTutorialManifestProvider', () => {

const result = await service.generateTutorialManifest(mockCustomTutorial);

expect(result).toEqual(null);
expect(result).toEqual({
...mockCustomTutorialManifest,
children: [],
});
});

it('should return null in case of any error', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export class CustomTutorialManifestProvider {
return manifest;
}

public async isOriginalManifestExists(path: string): Promise<boolean> {
return fs.existsSync(join(path, MANIFEST_FILE));
}

public async getOriginalManifestJson(path: string): Promise<RootCustomTutorialManifest> {
try {
return JSON.parse(
Expand Down Expand Up @@ -143,16 +147,16 @@ export class CustomTutorialManifestProvider {
*/
public async generateTutorialManifest(tutorial: CustomTutorial): Promise<RootCustomTutorialManifest> {
try {
const manifest = await this.getManifestJson(tutorial.absolutePath);
const manifest = await this.getManifestJson(tutorial.absolutePath) || {} as RootCustomTutorialManifest;

return {
...manifest,
_actions: tutorial.actions,
_path: tutorial.path,
type: CustomTutorialManifestType.Group,
id: tutorial.id,
label: tutorial.name || manifest.label,
children: manifest.children || [],
label: tutorial.name || manifest?.label,
children: manifest?.children || [],
};
} catch (e) {
this.logger.warn('Unable to generate manifest for tutorial', e);
Expand Down