|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +import winston from 'winston'; |
| 3 | + |
| 4 | +import { validateRepo, getSchema, localGitMiddleware } from '.'; |
| 5 | + |
| 6 | +import type Joi from 'joi'; |
| 7 | +import type express from 'express'; |
| 8 | + |
| 9 | +jest.mock('../utils/APIUtils', () => jest.fn()); |
| 10 | +jest.mock('simple-git'); |
| 11 | + |
| 12 | +function assetFailure(result: Joi.ValidationResult, expectedMessage: string) { |
| 13 | + const { error } = result; |
| 14 | + expect(error).not.toBeNull(); |
| 15 | + expect(error!.details).toHaveLength(1); |
| 16 | + const message = error!.details.map(({ message }) => message)[0]; |
| 17 | + expect(message).toBe(expectedMessage); |
| 18 | +} |
| 19 | + |
| 20 | +const defaultParams = { |
| 21 | + branch: 'master', |
| 22 | +}; |
| 23 | + |
| 24 | +describe('localGitMiddleware', () => { |
| 25 | + const simpleGit = require('simple-git'); |
| 26 | + |
| 27 | + const git = { |
| 28 | + checkIsRepo: jest.fn(), |
| 29 | + silent: jest.fn(), |
| 30 | + branchLocal: jest.fn(), |
| 31 | + checkout: jest.fn(), |
| 32 | + }; |
| 33 | + git.silent.mockReturnValue(git); |
| 34 | + |
| 35 | + simpleGit.mockReturnValue(git); |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('validateRepo', () => { |
| 42 | + it('should throw on non valid git repo', async () => { |
| 43 | + git.checkIsRepo.mockResolvedValue(false); |
| 44 | + await expect(validateRepo({ repoPath: '/Users/user/code/repo' })).rejects.toEqual( |
| 45 | + new Error('/Users/user/code/repo is not a valid git repository'), |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should not throw on valid git repo', async () => { |
| 50 | + git.checkIsRepo.mockResolvedValue(true); |
| 51 | + await expect(validateRepo({ repoPath: '/Users/user/code/repo' })).resolves.toBeUndefined(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('getSchema', () => { |
| 56 | + it('should throw on path traversal', () => { |
| 57 | + const schema = getSchema({ repoPath: '/Users/user/documents/code/repo' }); |
| 58 | + |
| 59 | + assetFailure( |
| 60 | + schema.validate({ |
| 61 | + action: 'getEntry', |
| 62 | + params: { ...defaultParams, path: '../' }, |
| 63 | + }), |
| 64 | + '"params.path" must resolve to a path under the configured repository', |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not throw on valid path', () => { |
| 69 | + const schema = getSchema({ repoPath: '/Users/user/documents/code/repo' }); |
| 70 | + |
| 71 | + const { error } = schema.validate({ |
| 72 | + action: 'getEntry', |
| 73 | + params: { ...defaultParams, path: 'src/content/posts/title.md' }, |
| 74 | + }); |
| 75 | + |
| 76 | + expect(error).toBeUndefined(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should throw on folder traversal', () => { |
| 80 | + const schema = getSchema({ repoPath: '/Users/user/documents/code/repo' }); |
| 81 | + |
| 82 | + assetFailure( |
| 83 | + schema.validate({ |
| 84 | + action: 'entriesByFolder', |
| 85 | + params: { ...defaultParams, folder: '../', extension: 'md', depth: 1 }, |
| 86 | + }), |
| 87 | + '"params.folder" must resolve to a path under the configured repository', |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should not throw on valid folder', () => { |
| 92 | + const schema = getSchema({ repoPath: '/Users/user/documents/code/repo' }); |
| 93 | + |
| 94 | + const { error } = schema.validate({ |
| 95 | + action: 'entriesByFolder', |
| 96 | + params: { ...defaultParams, folder: 'src/posts', extension: 'md', depth: 1 }, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(error).toBeUndefined(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should throw on media folder traversal', () => { |
| 103 | + const schema = getSchema({ repoPath: '/Users/user/documents/code/repo' }); |
| 104 | + |
| 105 | + assetFailure( |
| 106 | + schema.validate({ |
| 107 | + action: 'getMedia', |
| 108 | + params: { ...defaultParams, mediaFolder: '../' }, |
| 109 | + }), |
| 110 | + '"params.mediaFolder" must resolve to a path under the configured repository', |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should not throw on valid folder', () => { |
| 115 | + const schema = getSchema({ repoPath: '/Users/user/documents/code/repo' }); |
| 116 | + const { error } = schema.validate({ |
| 117 | + action: 'getMedia', |
| 118 | + params: { ...defaultParams, mediaFolder: 'static/images' }, |
| 119 | + }); |
| 120 | + |
| 121 | + expect(error).toBeUndefined(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('localGitMiddleware', () => { |
| 126 | + const json = jest.fn(); |
| 127 | + const status = jest.fn(() => ({ json })); |
| 128 | + const res: express.Response = { status } as unknown as express.Response; |
| 129 | + |
| 130 | + const repoPath = '.'; |
| 131 | + |
| 132 | + it("should return error when default branch doesn't exist", async () => { |
| 133 | + git.branchLocal.mockResolvedValue({ all: ['master'] }); |
| 134 | + |
| 135 | + const req = { |
| 136 | + body: { |
| 137 | + action: 'getMedia', |
| 138 | + params: { |
| 139 | + mediaFolder: 'mediaFolder', |
| 140 | + branch: 'develop', |
| 141 | + }, |
| 142 | + }, |
| 143 | + } as express.Request; |
| 144 | + |
| 145 | + await localGitMiddleware({ repoPath, logger: winston.createLogger() })(req, res); |
| 146 | + |
| 147 | + expect(status).toHaveBeenCalledTimes(1); |
| 148 | + expect(status).toHaveBeenCalledWith(422); |
| 149 | + |
| 150 | + expect(json).toHaveBeenCalledTimes(1); |
| 151 | + expect(json).toHaveBeenCalledWith({ error: "Default branch 'develop' doesn't exist" }); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments