|
| 1 | +const chai = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const fc = require('fast-check'); |
| 4 | +const actions = require('../../src/proxy/actions/Action'); |
| 5 | +const processor = require('../../src/proxy/processors/push-action/checkRepoInAuthorisedList'); |
| 6 | +const expect = chai.expect; |
| 7 | +const db = require('../../src/db'); |
| 8 | + |
| 9 | +describe('Check a Repo is in the authorised list', async () => { |
| 10 | + afterEach(() => { |
| 11 | + sinon.restore(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('accepts the action if the repository is whitelisted in the db', async () => { |
| 15 | + sinon.stub(db, 'getRepoByUrl').resolves({ |
| 16 | + name: 'repo-is-ok', |
| 17 | + project: 'thisproject', |
| 18 | + url: 'https://github.com/thisproject/repo-is-ok', |
| 19 | + }); |
| 20 | + |
| 21 | + const action = new actions.Action('123', 'type', 'get', 1234, 'thisproject/repo-is-ok'); |
| 22 | + const result = await processor.exec(null, action); |
| 23 | + expect(result.error).to.be.false; |
| 24 | + expect(result.steps[0].logs[0]).to.eq( |
| 25 | + 'checkRepoInAuthorisedList - repo thisproject/repo-is-ok is in the authorisedList', |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + it('rejects the action if repository not in the db', async () => { |
| 30 | + sinon.stub(db, 'getRepoByUrl').resolves(null); |
| 31 | + |
| 32 | + const action = new actions.Action('123', 'type', 'get', 1234, 'thisproject/repo-is-not-ok'); |
| 33 | + const result = await processor.exec(null, action); |
| 34 | + expect(result.error).to.be.true; |
| 35 | + expect(result.steps[0].logs[0]).to.eq( |
| 36 | + 'checkRepoInAuthorisedList - repo thisproject/repo-is-not-ok is not in the authorised whitelist, ending', |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('fuzzing', () => { |
| 41 | + it('should not crash on random repo names', async () => { |
| 42 | + await fc.assert( |
| 43 | + fc.asyncProperty(fc.string(), async (repoName) => { |
| 44 | + const action = new actions.Action('123', 'type', 'get', 1234, repoName); |
| 45 | + const result = await processor.exec(null, action); |
| 46 | + expect(result.error).to.be.true; |
| 47 | + }), |
| 48 | + { numRuns: 1000 }, |
| 49 | + ); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
0 commit comments