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
3 changes: 2 additions & 1 deletion redisinsight/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"format": "prettier --write \"src/**/*.ts\"",
"lint": "eslint --ext .ts .",
"start": "nest start",
"start:dev": "cross-env NODE_ENV=development SERVER_STATIC_CONTENT=1 nest start --watch --preserveWatchOutput",
"start:dev": "cross-env NODE_ENV=development SERVER_STATIC_CONTENT=1 nest start --watch",
"start:debug": "nest start --debug --watch",
"start:stage": "cross-env NODE_ENV=staging SERVER_STATIC_CONTENT=true node dist/src/main",
"start:prod": "cross-env NODE_ENV=production node dist/src/main",
Expand Down Expand Up @@ -84,6 +84,7 @@
"@nestjs/cli": "^9.1.2",
"@nestjs/schematics": "^9.0.3",
"@nestjs/testing": "^9.0.11",
"@types/adm-zip": "^0.5.0",
"@types/axios": "^0.14.0",
"@types/express": "^4.17.3",
"@types/jest": "^26.0.15",
Expand Down
69 changes: 46 additions & 23 deletions redisinsight/api/src/__mocks__/custom-tutorial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { CustomTutorialEntity } from 'src/modules/custom-tutorial/entities/custo
import { CustomTutorialManifestType } from 'src/modules/custom-tutorial/models/custom-tutorial.manifest';
import { MemoryStoredFile } from 'nestjs-form-data';
import { UploadCustomTutorialDto } from 'src/modules/custom-tutorial/dto/upload.custom-tutorial.dto';
import AdmZip from 'adm-zip';

export const mockCustomTutorialId = 'a77b23c1-7816-4ea4-b61f-d37795a0f805-ct-id';

export const mockCustomTutorialId2 = 'a77b23c1-7816-4ea4-b61f-d37795a0f805-ct-id-2';

export const mockCustomTutorialTmpPath = '/tmp/path';

export const mockCustomTutorialsHttpLink = 'https://somesime.com/archive.zip';

export const mockCustomTutorial = Object.assign(new CustomTutorial(), {
id: mockCustomTutorialId,
name: 'custom tutorial',
Expand All @@ -23,6 +26,7 @@ export const mockCustomTutorialEntity = Object.assign(new CustomTutorialEntity()
export const mockCustomTutorial2 = Object.assign(new CustomTutorial(), {
id: mockCustomTutorialId2,
name: 'custom tutorial 2',
link: mockCustomTutorialsHttpLink,
createdAt: new Date(),
});

Expand All @@ -31,77 +35,94 @@ export const mockCustomTutorialZipFile = Object.assign(new MemoryStoredFile(), {
buffer: Buffer.from('zip-content', 'utf8'),
});

export const mockCustomTutorialZipFileAxiosResponse = {
data: mockCustomTutorialZipFile.buffer,
};

export const mockCustomTutorialAdmZipEntry = {
entryName: 'somefolder/info.md',
} as AdmZip.IZipEntry;

export const mockCustomTutorialMacosxAdmZipEntry = {
entryName: '__MACOSX/info.md',
} as AdmZip.IZipEntry;

export const mockUploadCustomTutorialDto = Object.assign(new UploadCustomTutorialDto(), {
name: mockCustomTutorial.name,
file: mockCustomTutorialZipFile,
});

export const mockCustomTutorialManifestManifestJson = {
'ct-folder-1': {
export const mockUploadCustomTutorialExternalLinkDto = Object.assign(new UploadCustomTutorialDto(), {
name: mockCustomTutorial.name,
link: mockCustomTutorialsHttpLink,
});

export const mockCustomTutorialManifestManifestJson = [
{
type: 'group',
id: 'ct-folder-1',
label: 'ct-folder-1',
// args: {
// withBorder: true,
// initialIsOpen: true,
// },
children: {
'ct-sub-folder-1': {
children: [
{
type: CustomTutorialManifestType.Group,
id: 'ct-sub-folder-1',
label: 'ct-sub-folder-1',
// args: {
// initialIsOpen: false,
// },
children: {
introduction: {
children: [
{
type: CustomTutorialManifestType.InternalLink,
id: 'introduction',
label: 'introduction',
args: {
path: '/ct-folder-1/ct-sub-folder-1/introduction.md',
},
},
'working-with-hashes': {
{
type: CustomTutorialManifestType.InternalLink,
id: 'working-with-hashes',
label: 'working-with-hashes',
args: {
path: '/ct-folder-1/ct-sub-folder-1/working-with-hashes.md',
},
},
},
],
},
'ct-sub-folder-2': {
{
type: CustomTutorialManifestType.Group,
id: 'ct-sub-folder-2',
label: 'ct-sub-folder-2',
// args: {
// withBorder: true,
// initialIsOpen: false,
// },
children: {
introduction: {
children: [
{
type: CustomTutorialManifestType.InternalLink,
id: 'introduction',
label: 'introduction',
args: {
path: '/ct-folder-1/ct-sub-folder-2/introduction.md',
},
},
'working-with-graphs': {
{
type: CustomTutorialManifestType.InternalLink,
id: 'working-with-graphs',
label: 'working-with-graphs',
args: {
path: '/ct-folder-1/ct-sub-folder-2/working-with-graphs.md',
},
},
},
],
},
},
],
},
};
];

export const mockCustomTutorialManifestManifest = {
type: CustomTutorialManifestType.Group,
Expand All @@ -121,24 +142,26 @@ export const mockCustomTutorialManifestManifest2 = {
children: mockCustomTutorialManifestManifestJson,
};

export const globalCustomTutorialManifest = {
'custom-tutorials': {
export const globalCustomTutorialManifest = [
{
type: CustomTutorialManifestType.Group,
id: 'custom-tutorials',
label: 'My Tutorials',
label: 'MY TUTORIALS',
_actions: [CustomTutorialActions.CREATE],
args: {
withBorder: true,
initialIsOpen: true,
},
children: {
[mockCustomTutorialManifestManifest.id]: mockCustomTutorialManifestManifest,
[mockCustomTutorialManifestManifest2.id]: mockCustomTutorialManifestManifest2,
},
children: [
mockCustomTutorialManifestManifest,
mockCustomTutorialManifestManifest2,
],
},
};
];

export const mockCustomTutorialFsProvider = jest.fn(() => ({
unzipFromMemoryStoredFile: jest.fn().mockResolvedValue(mockCustomTutorialTmpPath),
unzipFromExternalLink: jest.fn().mockResolvedValue(mockCustomTutorialTmpPath),
unzipToTmpFolder: jest.fn().mockResolvedValue(mockCustomTutorialTmpPath),
moveFolder: jest.fn(),
removeFolder: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions redisinsight/api/src/common/utils/errors.util.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { HttpException, InternalServerErrorException } from '@nestjs/common';

export const wrapHttpError = (error: Error) => {
export const wrapHttpError = (error: Error, message?: string) => {
if (error instanceof HttpException) {
return error;
}

return new InternalServerErrorException(error.message);
return new InternalServerErrorException(error.message || message);
};
1 change: 1 addition & 0 deletions redisinsight/api/src/constants/error-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default {
CONSUMER_GROUP_NOT_FOUND: 'Consumer Group with such name was not found.',
PLUGIN_STATE_NOT_FOUND: 'Plugin state was not found.',
CUSTOM_TUTORIAL_NOT_FOUND: 'Custom Tutorial was not found.',
CUSTOM_TUTORIAL_UNABLE_TO_FETCH_FROM_EXTERNAL: 'Unable fetch zip file from external source.',
UNDEFINED_INSTANCE_ID: 'Undefined redis database instance id.',
NO_CONNECTION_TO_REDIS_DB: 'No connection to the Redis Database.',
WRONG_DATABASE_TYPE: 'Wrong database type.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
mockCustomTutorialManifestManifest, mockCustomTutorialManifestManifest2,
mockCustomTutorialManifestProvider,
mockCustomTutorialRepository,
MockType, mockUploadCustomTutorialDto,
MockType, mockUploadCustomTutorialDto, mockUploadCustomTutorialExternalLinkDto
} from 'src/__mocks__';
import * as fs from 'fs-extra';
import { CustomTutorialFsProvider } from 'src/modules/custom-tutorial/providers/custom-tutorial.fs.provider';
import { InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { BadRequestException, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { CustomTutorialService } from 'src/modules/custom-tutorial/custom-tutorial.service';
import { CustomTutorialRepository } from 'src/modules/custom-tutorial/repositories/custom-tutorial.repository';
import {
Expand Down Expand Up @@ -63,12 +63,27 @@ describe('CustomTutorialService', () => {
});

describe('create', () => {
it('Should create custom tutorial', async () => {
it('Should create custom tutorial from file', async () => {
const result = await service.create(mockUploadCustomTutorialDto);

expect(result).toEqual(mockCustomTutorialManifestManifest);
});

it('Should create custom tutorial from external url', async () => {
const result = await service.create(mockUploadCustomTutorialExternalLinkDto);

expect(result).toEqual(mockCustomTutorialManifestManifest);
});

it('Should throw BadRequestException in case when either link or file was not provided', async () => {
try {
await service.create({} as any);
} catch (e) {
expect(e).toBeInstanceOf(BadRequestException);
expect(e.message).toEqual('File or external link should be provided');
}
});

it('Should throw InternalServerError in case of any non-HttpException error', async () => {
customTutorialRepository.create.mockRejectedValueOnce(new Error('Unable to create'));

Expand Down Expand Up @@ -98,27 +113,27 @@ describe('CustomTutorialService', () => {

const result = await service.getGlobalManifest();

expect(result).toEqual({
'custom-tutorials': {
...globalCustomTutorialManifest['custom-tutorials'],
children: {
[mockCustomTutorialManifestManifest.id]: mockCustomTutorialManifestManifest,
},
expect(result).toEqual([
{
...globalCustomTutorialManifest[0],
children: [
mockCustomTutorialManifestManifest,
],
},
});
]);
});

it('Should return global manifest without children in case of any error', async () => {
customTutorialRepository.list.mockRejectedValueOnce(new Error('Unable to get list of tutorials'));

const result = await service.getGlobalManifest();

expect(result).toEqual({
'custom-tutorials': {
...globalCustomTutorialManifest['custom-tutorials'],
children: {},
expect(result).toEqual([
{
...globalCustomTutorialManifest[0],
children: [],
},
});
]);
});
});

Expand Down
Loading