Skip to content

Commit 5380e2f

Browse files
committed
test: tests for CLI set better user data and clean up afterwards
1 parent 16def58 commit 5380e2f

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

packages/git-proxy-cli/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const util = require('util');
77

88
const GIT_PROXY_COOKIE_FILE = 'git-proxy-cookie';
99
// GitProxy UI HOST and PORT (configurable via environment variable)
10-
const { GIT_PROXY_UI_HOST: uiHost = 'http://localhost', GIT_PROXY_UI_PORT: uiPort = 8080 } = process.env;
10+
const { GIT_PROXY_UI_HOST: uiHost = 'http://localhost', GIT_PROXY_UI_PORT: uiPort = 8080 } =
11+
process.env;
1112

1213
const baseUrl = `${uiHost}:${uiPort}`;
1314

@@ -175,7 +176,8 @@ async function authoriseGitPush(id) {
175176
if (error.response) {
176177
switch (error.response.status) {
177178
case 401:
178-
errorMessage = 'Error: Authorise: Authentication required';
179+
errorMessage =
180+
'Error: Authorise: Authentication required (401): ' + error?.response?.data?.message;
179181
process.exitCode = 3;
180182
break;
181183
case 404:
@@ -222,7 +224,8 @@ async function rejectGitPush(id) {
222224
if (error.response) {
223225
switch (error.response.status) {
224226
case 401:
225-
errorMessage = 'Error: Reject: Authentication required';
227+
errorMessage =
228+
'Error: Reject: Authentication required (401): ' + error?.response?.data?.message;
226229
process.exitCode = 3;
227230
break;
228231
case 404:
@@ -269,7 +272,8 @@ async function cancelGitPush(id) {
269272
if (error.response) {
270273
switch (error.response.status) {
271274
case 401:
272-
errorMessage = 'Error: Cancel: Authentication required';
275+
errorMessage =
276+
'Error: Cancel: Authentication required (401): ' + error?.response?.data?.message;
273277
process.exitCode = 3;
274278
break;
275279
case 404:

packages/git-proxy-cli/test/testCli.test.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const TEST_REPO_CONFIG = {
2222
url: 'https://github.com/finos/git-proxy-test.git',
2323
};
2424
const TEST_REPO = 'finos/git-proxy-test.git';
25+
//user for test cases
26+
const TEST_USER = 'testuser';
27+
const TEST_PASSWORD = 'testpassword';
28+
const TEST_EMAIL = '[email protected]';
29+
const TEST_GIT_ACCOUNT = 'testGitAccount';
2530

2631
describe('test git-proxy-cli', function () {
2732
// *** help ***
@@ -87,16 +92,12 @@ describe('test git-proxy-cli', function () {
8792
// *** login ***
8893

8994
describe('test git-proxy-cli :: login', function () {
90-
const testUser = 'testuser';
91-
const testPassword = 'testpassword';
92-
const testEmail = '[email protected]';
93-
9495
before(async function () {
95-
await helper.addUserToDb(testUser, testPassword, testEmail, 'testGitAccount');
96+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
9697
});
9798

9899
after(async function () {
99-
await helper.removeUserFromDb(testUser);
100+
await helper.removeUserFromDb(TEST_USER);
100101
});
101102

102103
it('login shoud fail when server is down', async function () {
@@ -140,9 +141,9 @@ describe('test git-proxy-cli', function () {
140141
});
141142

142143
it('login shoud be successful with valid credentials (non-admin)', async function () {
143-
const cli = `npx -- @finos/git-proxy-cli login --username ${testUser} --password ${testPassword}`;
144+
const cli = `npx -- @finos/git-proxy-cli login --username ${TEST_USER} --password ${TEST_PASSWORD}`;
144145
const expectedExitCode = 0;
145-
const expectedMessages = [`Login "${testUser}" <${testEmail}>: OK`];
146+
const expectedMessages = [`Login "${TEST_USER}" <${TEST_EMAIL}>: OK`];
146147
const expectedErrorMessages = null;
147148
try {
148149
await helper.startServer(service);
@@ -219,11 +220,13 @@ describe('test git-proxy-cli', function () {
219220

220221
before(async function () {
221222
await helper.addRepoToDb(TEST_REPO_CONFIG);
222-
await helper.addGitPushToDb(pushId, TEST_REPO);
223+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
224+
await helper.addGitPushToDb(pushId, TEST_USER, TEST_EMAIL, TEST_REPO);
223225
});
224226

225227
after(async function () {
226228
await helper.removeGitPushFromDb(pushId);
229+
await helper.removeUserFromDb(TEST_USER);
227230
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
228231
});
229232

@@ -294,11 +297,13 @@ describe('test git-proxy-cli', function () {
294297

295298
before(async function () {
296299
await helper.addRepoToDb(TEST_REPO_CONFIG);
297-
await helper.addGitPushToDb(pushId, TEST_REPO);
300+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
301+
await helper.addGitPushToDb(pushId, TEST_USER, TEST_EMAIL, TEST_REPO);
298302
});
299303

300304
after(async function () {
301305
await helper.removeGitPushFromDb(pushId);
306+
await helper.removeUserFromDb(TEST_USER);
302307
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
303308
});
304309

@@ -415,11 +420,13 @@ describe('test git-proxy-cli', function () {
415420

416421
before(async function () {
417422
await helper.addRepoToDb(TEST_REPO_CONFIG);
418-
await helper.addGitPushToDb(pushId, TEST_REPO);
423+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
424+
await helper.addGitPushToDb(pushId, TEST_USER, TEST_EMAIL, TEST_REPO);
419425
});
420426

421427
after(async function () {
422428
await helper.removeGitPushFromDb(pushId);
429+
await helper.removeUserFromDb(TEST_USER);
423430
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
424431
});
425432

@@ -487,17 +494,16 @@ describe('test git-proxy-cli', function () {
487494

488495
describe('test git-proxy-cli :: git push administration', function () {
489496
const pushId = `0000000000000000000000000000000000000000__${Date.now()}`;
490-
const gitAccount = 'testGitAccount1';
491497

492498
before(async function () {
493499
await helper.addRepoToDb(TEST_REPO_CONFIG);
494-
await helper.addUserToDb('testuser1', 'testpassword', '[email protected]', gitAccount);
495-
await helper.addGitPushToDb(pushId, TEST_REPO, gitAccount);
500+
await helper.addUserToDb(TEST_USER, TEST_PASSWORD, TEST_EMAIL, TEST_GIT_ACCOUNT);
501+
await helper.addGitPushToDb(pushId, TEST_REPO, TEST_USER, TEST_EMAIL);
496502
});
497503

498504
after(async function () {
499-
await helper.removeUserFromDb('testuser1');
500505
await helper.removeGitPushFromDb(pushId);
506+
await helper.removeUserFromDb(TEST_USER);
501507
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
502508
});
503509

packages/git-proxy-cli/test/testCliUtils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,10 @@ async function removeRepoFromDb(repoName) {
177177
* @param {string} id The ID of the git push.
178178
* @param {string} repo The repository of the git push.
179179
* @param {string} user The user who pushed the git push.
180+
* @param {string} userEmail The email of the user who pushed the git push.
180181
* @param {boolean} debug Flag to enable logging for debugging.
181182
*/
182-
async function addGitPushToDb(id, repo, user = null, debug = false) {
183+
async function addGitPushToDb(id, repo, user = null, userEmail = null, debug = false) {
183184
const action = new actions.Action(
184185
id,
185186
'push', // type
@@ -188,6 +189,7 @@ async function addGitPushToDb(id, repo, user = null, debug = false) {
188189
repo,
189190
);
190191
action.user = user;
192+
action.userEmail = userEmail;
191193
const step = new steps.Step(
192194
'authBlock', // stepName
193195
false, // error

src/service/routes/push.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ router.post('/:id/reject', async (req, res) => {
4949

5050
if (list.length === 0) {
5151
res.status(401).send({
52-
message: `The user with email ${committerEmail} could not be found`,
52+
message: `There was no registered user with the committer's email address: ${committerEmail}`,
5353
});
5454
return;
5555
}
@@ -104,7 +104,7 @@ router.post('/:id/authorise', async (req, res) => {
104104

105105
if (list.length === 0) {
106106
res.status(401).send({
107-
message: `The email address ${committerEmail} could not be found`,
107+
message: `There was no registered user with the committer's email address: ${committerEmail}`,
108108
});
109109
return;
110110
}

0 commit comments

Comments
 (0)