diff --git a/packages/git-proxy-cli/index.js b/packages/git-proxy-cli/index.js index 6da44e52..270f2b45 100755 --- a/packages/git-proxy-cli/index.js +++ b/packages/git-proxy-cli/index.js @@ -125,13 +125,8 @@ async function getGitPushes(filters) { console.log(`${util.inspect(records, false, null, false)}`); } catch (error) { // default error - let errorMessage = `Error: List: '${error.message}'`; + const errorMessage = `Error: List: '${error.message}'`; process.exitCode = 2; - - if (error.response && error.response.status == 401) { - errorMessage = 'Error: List: Authentication required'; - process.exitCode = 3; - } console.error(errorMessage); } } @@ -156,7 +151,16 @@ async function authoriseGitPush(id) { await axios.post( `${baseUrl}/api/v1/push/${id}/authorise`, - {}, + { + params: { + attestation: [ + { + label: "Authorising via GitProxy CLI", + checked: true + } + ] + } + }, { headers: { Cookie: cookies }, }, diff --git a/packages/git-proxy-cli/test/testCli.test.js b/packages/git-proxy-cli/test/testCli.test.js index 23438786..0474bad8 100644 --- a/packages/git-proxy-cli/test/testCli.test.js +++ b/packages/git-proxy-cli/test/testCli.test.js @@ -15,6 +15,13 @@ const service = require('../../../src/service'); // push ID which does not exist const GHOST_PUSH_ID = '0000000000000000000000000000000000000000__79b4d8953cbc324bcc1eb53d6412ff89666c241f'; +// repo for test cases +const TEST_REPO_CONFIG = { + project: 'finos', + name: 'git-proxy-test', + url: 'https://github.com/finos/git-proxy-test.git' +} +const TEST_REPO = 'finos/git-proxy-test.git'; describe('test git-proxy-cli', function () { // *** help *** @@ -280,6 +287,13 @@ describe('test git-proxy-cli', function () { // *** authorise *** describe('test git-proxy-cli :: authorise', function () { + const pushId = `auth000000000000000000000000000000000000__${Date.now()}`; + + before(async function() { + await helper.addRepoToDb(TEST_REPO_CONFIG); + await helper.addGitPushToDb(pushId, TEST_REPO); + }) + it('attempt to authorise should fail when server is down', async function () { try { // start server -> login -> stop server @@ -326,7 +340,7 @@ describe('test git-proxy-cli', function () { try { await helper.createCookiesFileWithExpiredCookie(); await helper.startServer(service); - const id = GHOST_PUSH_ID; + const id = pushId; const cli = `npx -- @finos/git-proxy-cli authorise --id ${id}`; const expectedExitCode = 3; const expectedMessages = null; @@ -373,6 +387,13 @@ describe('test git-proxy-cli', function () { // *** cancel *** describe('test git-proxy-cli :: cancel', function () { + const pushId = `cancel0000000000000000000000000000000000__${Date.now()}`; + + before(async function() { + await helper.addRepoToDb(TEST_REPO_CONFIG); + await helper.addGitPushToDb(pushId, TEST_REPO); + }) + it('attempt to cancel should fail when server is down', async function () { try { // start server -> login -> stop server @@ -417,7 +438,7 @@ describe('test git-proxy-cli', function () { try { await helper.createCookiesFileWithExpiredCookie(); await helper.startServer(service); - const id = GHOST_PUSH_ID; + const id = pushId; const cli = `npx -- @finos/git-proxy-cli cancel --id ${id}`; const expectedExitCode = 3; const expectedMessages = null; @@ -501,25 +522,6 @@ describe('test git-proxy-cli', function () { ); }); - it('attempt to ls should fail when not authenticated (server restarted)', async function () { - try { - await helper.createCookiesFileWithExpiredCookie(); - await helper.startServer(service); - const cli = `npx -- @finos/git-proxy-cli ls`; - const expectedExitCode = 3; - const expectedMessages = null; - const expectedErrorMessages = ['Error: List: Authentication required']; - await helper.runCli( - cli, - expectedExitCode, - expectedMessages, - expectedErrorMessages, - ); - } finally { - await helper.closeServer(service.httpServer); - } - }); - it('attempt to ls should fail when invalid option given', async function () { try { await helper.startServer(service); @@ -546,6 +548,13 @@ describe('test git-proxy-cli', function () { // *** reject *** describe('test git-proxy-cli :: reject', function () { + const pushId = `reject0000000000000000000000000000000000__${Date.now()}`; + + before(async function() { + await helper.addRepoToDb(TEST_REPO_CONFIG); + await helper.addGitPushToDb(pushId, TEST_REPO); + }) + it('attempt to reject should fail when server is down', async function () { try { // start server -> login -> stop server @@ -590,7 +599,7 @@ describe('test git-proxy-cli', function () { try { await helper.createCookiesFileWithExpiredCookie(); await helper.startServer(service); - const id = GHOST_PUSH_ID; + const id = pushId; const cli = `npx -- @finos/git-proxy-cli reject --id ${id}`; const expectedExitCode = 3; const expectedMessages = null; @@ -636,10 +645,10 @@ describe('test git-proxy-cli', function () { describe('test git-proxy-cli :: git push administration', function () { const pushId = `0000000000000000000000000000000000000000__${Date.now()}`; - const repo = 'test-repo'; before(async function () { - await helper.addGitPushToDb(pushId, repo); + await helper.addRepoToDb(TEST_REPO_CONFIG); + await helper.addGitPushToDb(pushId, TEST_REPO); }); it('attempt to ls should list existing push', async function () { @@ -653,7 +662,7 @@ describe('test git-proxy-cli', function () { const expectedExitCode = 0; const expectedMessages = [ pushId, - repo, + TEST_REPO, 'authorised: false', 'blocked: true', 'canceled: false', @@ -791,7 +800,7 @@ describe('test git-proxy-cli', function () { cli = `npx -- @finos/git-proxy-cli ls --authorised true --canceled false --rejected false`; expectedExitCode = 0; - expectedMessages = [pushId, repo]; + expectedMessages = [pushId, TEST_REPO]; expectedErrorMessages = null; await helper.runCli( cli, @@ -835,7 +844,7 @@ describe('test git-proxy-cli', function () { cli = `npx -- @finos/git-proxy-cli ls --authorised false --canceled false --rejected true`; expectedExitCode = 0; - expectedMessages = [pushId, repo]; + expectedMessages = [pushId, TEST_REPO]; expectedErrorMessages = null; await helper.runCli( cli, @@ -879,7 +888,7 @@ describe('test git-proxy-cli', function () { cli = `npx -- @finos/git-proxy-cli ls --authorised false --canceled true --rejected false`; expectedExitCode = 0; - expectedMessages = [pushId, repo]; + expectedMessages = [pushId, TEST_REPO]; expectedErrorMessages = null; await helper.runCli( cli, diff --git a/packages/git-proxy-cli/test/testCliUtils.js b/packages/git-proxy-cli/test/testCliUtils.js index 376ecea4..fb022320 100644 --- a/packages/git-proxy-cli/test/testCliUtils.js +++ b/packages/git-proxy-cli/test/testCliUtils.js @@ -33,7 +33,7 @@ async function runCli( debug = false, ) { try { - console.log(`cli: ${cli}`); + console.log(`cli: '${cli}'`); const { stdout, stderr } = await execAsync(cli); if (debug) { console.log(`stdout: ${stdout}`); @@ -72,6 +72,10 @@ async function runCli( expect(error.stderr).to.include(expectedErrorMessage); }); } + } finally { + if (debug) { + console.log(`cli: '${cli}': done`); + } } } @@ -138,6 +142,28 @@ async function removeCookiesFile() { } } +/** + * Add a new repo to the database. + * @param {object} newRepo The new repo attributes. + * @param {boolean} debug Print debug messages to console if true. + */ +async function addRepoToDb(newRepo, debug = false) { + const repos = await db.getRepos(); + const found = repos.find((y) => y.project === newRepo.project && newRepo.name === y.name); + if (!found) { + await db.createRepo(newRepo); + await db.addUserCanPush(newRepo.name, 'admin'); + await db.addUserCanAuthorise(newRepo.name, 'admin'); + if (debug) { + console.log(`New repo added to database: ${newRepo}`); + } + } else { + if (debug) { + console.log(`New repo already found in database: ${newRepo}`); + } + } +} + /** * Add a new git push record to the database. * @param {string} id The ID of the git push. @@ -210,6 +236,7 @@ module.exports = { runCli: runCli, startServer: startServer, closeServer: closeServer, + addRepoToDb: addRepoToDb, addGitPushToDb: addGitPushToDb, addUserToDb: addUserToDb, createCookiesFileWithExpiredCookie: createCookiesFileWithExpiredCookie, diff --git a/src/service/routes/push.js b/src/service/routes/push.js index 510a0848..9750375c 100644 --- a/src/service/routes/push.js +++ b/src/service/routes/push.js @@ -83,10 +83,10 @@ router.post('/:id/reject', async (req, res) => { router.post('/:id/authorise', async (req, res) => { console.log({ req }); - const questions = req.body.params.attestation; + const questions = req.body.params?.attestation; console.log({ questions }); - const attestationComplete = questions.every((question) => !!question.checked); + const attestationComplete = questions?.every((question) => !!question.checked); console.log({ attestationComplete }); if (req.user && attestationComplete) {