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/common/decorators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './client-metadata';
export * from './object-as-map.decorator';
export * from './is-multi-number.decorator';
export * from './is-bigger-than.decorator';
export * from './is-github-link.decorator';
18 changes: 18 additions & 0 deletions redisinsight/api/src/common/decorators/is-github-link.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
registerDecorator,
ValidationOptions,
} from 'class-validator';
import { GitHubLink } from 'src/common/validators';

export function IsGitHubLink(validationOptions?: ValidationOptions) {
return (object: any, propertyName: string) => {
registerDecorator({
name: 'IsGitHubLink',
target: object.constructor,
propertyName,
constraints: [],
options: validationOptions,
validator: GitHubLink,
});
};
}
17 changes: 17 additions & 0 deletions redisinsight/api/src/common/validators/github-link.validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';

@ValidatorConstraint({ name: 'GitHubLink', async: false })
export class GitHubLink implements ValidatorConstraintInterface {
validate(value: any) {
// Regular expression to match any GitHub URL
const githubUrlRegex = /^https:\/\/github\.com(?:\/[^\s/]+(?:\/[^\s/]+)*)?\/?$/;
return typeof value === 'string' && githubUrlRegex.test(value);
}

defaultMessage() {
return 'Enter a full GitHub link';
}
}
1 change: 1 addition & 0 deletions redisinsight/api/src/common/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './redis-string.validator';
export * from './zset-score.validator';
export * from './multi-number.validator';
export * from './bigger-than.validator';
export * from './github-link.validator';
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
import {
HasMimeType, IsFile, MaxFileSize, MemoryStoredFile,
} from 'nestjs-form-data';
import { IsGitHubLink } from 'src/common/decorators';

export class UploadCustomTutorialDto {
@ApiPropertyOptional({
Expand All @@ -23,5 +24,6 @@ export class UploadCustomTutorialDto {
@IsOptional()
@IsString()
@IsNotEmpty()
@IsGitHubLink()
link?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
path,
serverConfig, requirements,
before,
nock,
_,
} from '../deps';
import { getBaseURL } from '../../helpers/server';
const { server, request, localDb } = deps;

// create endpoint
Expand Down Expand Up @@ -107,6 +107,8 @@ const globalManifest = {
children: [],
};

const nockScope = nock('https://github.com/somerepo');

describe('POST /custom-tutorials', () => {
requirements('rte.serverType=local');

Expand Down Expand Up @@ -197,7 +199,9 @@ describe('POST /custom-tutorials', () => {

it('should import tutorial from the external link with manifest', async () => {
const zip = new AdmZip(path.join(staticsFolder, 'test.zip'));
const link = `${getBaseURL()}/static/test.zip`;
const file = await fsExtra.readFile(path.join(staticsFolder, 'test.zip'))
nockScope.get('/test.zip').reply(200, file);
const link = `https://github.com/somerepo/test.zip`;

await validateApiCall({
endpoint: creatEndpoint,
Expand Down