diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/helpers.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/helpers.test.ts index 2740d16efb..3c4a677d4d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/helpers.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/helpers.test.ts @@ -51,158 +51,165 @@ describe('fetchContent helper', () => { outputPath: os.tmpdir(), }; - it('fetch contents should reject absolute file locations', async () => { - await expect( - fetchContents({ - ...options, - baseUrl: 'file:///some/path', - fetchUrl: '/etc/passwd', - }), - ).rejects.toThrow( - 'Relative path is not allowed to refer to a directory outside its parent', - ); - }); - - it('fetch contents should reject relative file locations that exit the baseUrl', async () => { - await expect( - fetchContents({ - ...options, - baseUrl: 'file:///some/path', - fetchUrl: '../test', - }), - ).rejects.toThrow( - 'Relative path is not allowed to refer to a directory outside its parent', - ); - }); - - it('fetch contents should copy file to outputpath', async () => { - await fetchContents({ - ...options, - baseUrl: 'file:///some/path', - fetchUrl: 'foo', - outputPath: 'somepath', + describe('fetch contents', () => { + it('should reject absolute file locations', async () => { + await expect( + fetchContents({ + ...options, + baseUrl: 'file:///some/path', + fetchUrl: '/etc/passwd', + }), + ).rejects.toThrow( + 'Relative path is not allowed to refer to a directory outside its parent', + ); }); - expect(fs.copy).toHaveBeenCalledWith(resolvePath('/some/foo'), 'somepath'); - }); - it('fetch contents should reject if no integration matches location', async () => { - await expect( - fetchContents({ - ...options, - baseUrl: 'http://example.com/some/folder', - }), - ).rejects.toThrow( - 'No integration found for location http://example.com/some/folder', - ); - }); + it('should reject relative file locations that exit the baseUrl', async () => { + await expect( + fetchContents({ + ...options, + baseUrl: 'file:///some/path', + fetchUrl: '../test', + }), + ).rejects.toThrow( + 'Relative path is not allowed to refer to a directory outside its parent', + ); + }); - it('fetch contents should reject if fetch url is relative and no base url is specified', async () => { - await expect( - fetchContents({ + it('should copy file to outputpath', async () => { + await fetchContents({ ...options, + baseUrl: 'file:///some/path', fetchUrl: 'foo', - }), - ).rejects.toThrow( - 'Failed to fetch, template location could not be determined and the fetch URL is relative, foo', - ); + outputPath: 'somepath', + }); + expect(fs.copy).toHaveBeenCalledWith( + resolvePath('/some/foo'), + 'somepath', + ); + }); + + it('should reject if no integration matches location', async () => { + await expect( + fetchContents({ + ...options, + baseUrl: 'http://example.com/some/folder', + }), + ).rejects.toThrow( + 'No integration found for location http://example.com/some/folder', + ); + }); + + it('should reject if fetch url is relative and no base url is specified', async () => { + await expect( + fetchContents({ + ...options, + fetchUrl: 'foo', + }), + ).rejects.toThrow( + 'Failed to fetch, template location could not be determined and the fetch URL is relative, foo', + ); + }); + + it('should fetch url contents', async () => { + const dirFunction = jest.fn(); + readTree.mockResolvedValue({ + dir: dirFunction, + }); + await fetchContents({ + ...options, + outputPath: 'foo', + fetchUrl: 'https://github.com/backstage/foo', + }); + expect(fs.ensureDir).toHaveBeenCalledWith('foo'); + expect(dirFunction).toHaveBeenCalledWith({ targetDir: 'foo' }); + }); }); - it('fetch contents should fetch url contents', async () => { - const dirFunction = jest.fn(); - readTree.mockResolvedValue({ - dir: dirFunction, + describe('fetch file', () => { + it('should reject absolute file locations', async () => { + await expect( + fetchFile({ + ...options, + baseUrl: 'file:///some/path', + fetchUrl: '/etc/passwd', + }), + ).rejects.toThrow( + 'Relative path is not allowed to refer to a directory outside its parent', + ); }); - await fetchContents({ - ...options, - outputPath: 'foo', - fetchUrl: 'https://github.com/backstage/foo', - }); - expect(fs.ensureDir).toHaveBeenCalledWith('foo'); - expect(dirFunction).toHaveBeenCalledWith({ targetDir: 'foo' }); - }); - it('fetch file should reject absolute file locations', async () => { - await expect( - fetchFile({ + it('should reject relative file locations that exit the baseUrl', async () => { + await expect( + fetchFile({ + ...options, + baseUrl: 'file:///some/path', + fetchUrl: '../test', + }), + ).rejects.toThrow( + 'Relative path is not allowed to refer to a directory outside its parent', + ); + }); + + it('should copy file to outputpath', async () => { + await fetchFile({ ...options, baseUrl: 'file:///some/path', - fetchUrl: '/etc/passwd', - }), - ).rejects.toThrow( - 'Relative path is not allowed to refer to a directory outside its parent', - ); - }); - - it('fetch file should reject relative file locations that exit the baseUrl', async () => { - await expect( - fetchFile({ - ...options, - baseUrl: 'file:///some/path', - fetchUrl: '../test', - }), - ).rejects.toThrow( - 'Relative path is not allowed to refer to a directory outside its parent', - ); - }); - - it('fetch file should copy file to outputpath', async () => { - await fetchFile({ - ...options, - baseUrl: 'file:///some/path', - fetchUrl: 'foo', - outputPath: 'somepath', - }); - expect(fs.copyFile).toHaveBeenCalledWith( - resolvePath('/some/foo'), - 'somepath', - ); - }); - - it('fetch file should reject if no integration matches location', async () => { - await expect( - fetchFile({ - ...options, - baseUrl: 'http://example.com/some/folder', - }), - ).rejects.toThrow( - 'No integration found for location http://example.com/some/folder', - ); - }); - - it('fetch file should reject if fetch url is relative and no base url is specified', async () => { - await expect( - fetchFile({ - ...options, fetchUrl: 'foo', - }), - ).rejects.toThrow( - 'Failed to fetch, template location could not be determined and the fetch URL is relative, foo', - ); - }); + outputPath: 'somepath', + }); + expect(fs.copyFile).toHaveBeenCalledWith( + resolvePath('/some/foo'), + 'somepath', + ); + }); - it('fetch file should fetch content from url', async () => { - readUrl.mockResolvedValue({ - buffer: () => Buffer.from('test', 'utf8'), + it('should reject if no integration matches location', async () => { + await expect( + fetchFile({ + ...options, + baseUrl: 'http://example.com/some/folder', + }), + ).rejects.toThrow( + 'No integration found for location http://example.com/some/folder', + ); }); - await fetchFile({ - ...options, - outputPath: 'foo', - fetchUrl: 'https://github.com/backstage/foo', - }); - expect(fs.ensureDir).toHaveBeenCalledWith('.'); - expect(fs.outputFile).toHaveBeenCalledWith('foo', 'test'); - }); - it('fetch file should fetch content from url into directory', async () => { - readUrl.mockResolvedValue({ - buffer: () => Buffer.from('test', 'utf8'), + it('should reject if fetch url is relative and no base url is specified', async () => { + await expect( + fetchFile({ + ...options, + fetchUrl: 'foo', + }), + ).rejects.toThrow( + 'Failed to fetch, template location could not be determined and the fetch URL is relative, foo', + ); }); - await fetchFile({ - ...options, - outputPath: 'mydir/foo', - fetchUrl: 'https://github.com/backstage/foo', + + it('should fetch content from url', async () => { + readUrl.mockResolvedValue({ + buffer: () => Buffer.from('test', 'utf8'), + }); + await fetchFile({ + ...options, + outputPath: 'foo', + fetchUrl: 'https://github.com/backstage/foo', + }); + expect(fs.ensureDir).toHaveBeenCalledWith('.'); + expect(fs.outputFile).toHaveBeenCalledWith('foo', 'test'); + }); + + it('should fetch content from url into directory', async () => { + readUrl.mockResolvedValue({ + buffer: () => Buffer.from('test', 'utf8'), + }); + await fetchFile({ + ...options, + outputPath: 'mydir/foo', + fetchUrl: 'https://github.com/backstage/foo', + }); + expect(fs.ensureDir).toHaveBeenCalledWith('mydir'); + expect(fs.outputFile).toHaveBeenCalledWith('mydir/foo', 'test'); }); - expect(fs.ensureDir).toHaveBeenCalledWith('mydir'); - expect(fs.outputFile).toHaveBeenCalledWith('mydir/foo', 'test'); }); });