Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/git-proxy-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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 },
},
Expand Down
65 changes: 37 additions & 28 deletions packages/git-proxy-cli/test/testCli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ***
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand All @@ -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',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 28 additions & 1 deletion packages/git-proxy-cli/test/testCliUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down Expand Up @@ -72,6 +72,10 @@ async function runCli(
expect(error.stderr).to.include(expectedErrorMessage);
});
}
} finally {
if (debug) {
console.log(`cli: '${cli}': done`);
}
}
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -210,6 +236,7 @@ module.exports = {
runCli: runCli,
startServer: startServer,
closeServer: closeServer,
addRepoToDb: addRepoToDb,
addGitPushToDb: addGitPushToDb,
addUserToDb: addUserToDb,
createCookiesFileWithExpiredCookie: createCookiesFileWithExpiredCookie,
Expand Down
4 changes: 2 additions & 2 deletions src/service/routes/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down