|
| 1 | +const chai = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const proxyquire = require('proxyquire'); |
| 4 | +const { Action, Step } = require('../../src/proxy/actions'); |
| 5 | + |
| 6 | +chai.should(); |
| 7 | +const expect = chai.expect; |
| 8 | + |
| 9 | +describe('gitleaks', () => { |
| 10 | + describe('exec', () => { |
| 11 | + let exec; |
| 12 | + let stubs; |
| 13 | + let action; |
| 14 | + let req; |
| 15 | + let stepSpy; |
| 16 | + let logStub; |
| 17 | + let errorStub; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + stubs = { |
| 21 | + getAPIs: sinon.stub(), |
| 22 | + fs: { |
| 23 | + stat: sinon.stub(), |
| 24 | + access: sinon.stub(), |
| 25 | + constants: { R_OK: 0 } |
| 26 | + }, |
| 27 | + spawn: sinon.stub() |
| 28 | + }; |
| 29 | + |
| 30 | + logStub = sinon.stub(console, 'log'); |
| 31 | + errorStub = sinon.stub(console, 'error'); |
| 32 | + |
| 33 | + const gitleaksModule = proxyquire('../../src/proxy/processors/push-action/gitleaks', { |
| 34 | + '../../../config': { getAPIs: stubs.getAPIs }, |
| 35 | + 'node:fs/promises': stubs.fs, |
| 36 | + 'node:child_process': { spawn: stubs.spawn } |
| 37 | + }); |
| 38 | + |
| 39 | + exec = gitleaksModule.exec; |
| 40 | + |
| 41 | + req = {}; |
| 42 | + action = new Action( |
| 43 | + '1234567890', |
| 44 | + 'push', |
| 45 | + 'POST', |
| 46 | + 1234567890, |
| 47 | + 'test/repo' |
| 48 | + ); |
| 49 | + action.proxyGitPath = '/tmp'; |
| 50 | + action.repoName = 'test-repo'; |
| 51 | + action.commitFrom = 'abc123'; |
| 52 | + action.commitTo = 'def456'; |
| 53 | + |
| 54 | + stepSpy = sinon.spy(Step.prototype, 'setError'); |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(() => { |
| 58 | + sinon.restore(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle config loading failure', async () => { |
| 62 | + stubs.getAPIs.throws(new Error('Config error')); |
| 63 | + |
| 64 | + const result = await exec(req, action); |
| 65 | + |
| 66 | + expect(result.error).to.be.true; |
| 67 | + expect(result.steps).to.have.lengthOf(1); |
| 68 | + expect(result.steps[0].error).to.be.true; |
| 69 | + expect(stepSpy.calledWith('failed setup gitleaks, please contact an administrator\n')).to.be.true; |
| 70 | + expect(errorStub.calledWith('failed to get gitleaks config, please fix the error:')).to.be.true; |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments