kibana/x-pack/legacy/plugins/code/common/git_url_utils.test.ts
Mengwei Ding 2e0e8751d1
[Code] Check uri before executing the delete repository job (#47614) (#48302)
* [Code] Check uri before executing the delete repository job

* sanity check

* improve path check
2019-10-15 16:33:14 -07:00

60 lines
2.3 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { validateGitUrl } from './git_url_utils';
test('Git url validation', () => {
// An url ends with .git
expect(validateGitUrl('https://github.com/elastic/elasticsearch.git')).toBeTruthy();
// An url ends without .git
expect(validateGitUrl('https://github.com/elastic/elasticsearch')).toBeTruthy();
// An url with http://
expect(validateGitUrl('http://github.com/elastic/elasticsearch')).toBeTruthy();
// An url with ssh://
expect(validateGitUrl('ssh://elastic@github.com/elastic/elasticsearch.git')).toBeTruthy();
// An url with ssh:// and port
expect(validateGitUrl('ssh://elastic@github.com:9999/elastic/elasticsearch.git')).toBeTruthy();
// An url with git://
expect(validateGitUrl('git://elastic@github.com/elastic/elasticsearch.git')).toBeTruthy();
// An url with an invalid protocol
expect(() => {
validateGitUrl('file:///Users/elastic/elasticsearch', [], ['ssh', 'https', 'git']);
}).toThrow('Git url protocol is not whitelisted.');
// An url without protocol
expect(() => {
validateGitUrl('/Users/elastic/elasticsearch', [], ['ssh', 'https', 'git']);
}).toThrow('Git url protocol is not whitelisted.');
expect(() => {
validateGitUrl('github.com/elastic/elasticsearch', [], ['ssh', 'https', 'git']);
}).toThrow('Git url protocol is not whitelisted.');
// An valid git url but without whitelisted host
expect(() => {
validateGitUrl('https://github.com/elastic/elasticsearch.git', ['gitlab.com']);
}).toThrow('Git url host is not whitelisted.');
// An valid git url but without whitelisted protocol
expect(() => {
validateGitUrl('https://github.com/elastic/elasticsearch.git', [], ['ssh']);
}).toThrow('Git url protocol is not whitelisted.');
// An valid git url but contains invalid content
expect(() => {
validateGitUrl('https://github.com/elastic/../../elasticsearch.git', [], ['ssh']);
}).toThrow('Git url contains invalid content.');
// An valid git url with both whitelisted host and protocol
expect(
validateGitUrl('https://github.com/elastic/elasticsearch.git', ['github.com'], ['https'])
).toBeTruthy();
});