Skip to content

Commit 96cab2d

Browse files
committed
test: coverage for checkUserPushPermission
1 parent a7fc824 commit 96cab2d

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

src/proxy/processors/push-action/checkUserPushPermission.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ const exec = async (req: any, action: Action): Promise<Action> => {
1111
// n.b. action.user will be set to whatever the user had set in their user.name config in their git client.
1212
// it is not necessarily the GitHub username. GitHub looks users by email address as should we
1313
const userEmail = action.userEmail;
14-
let username = "unknown";
14+
let username = 'unknown';
1515

1616
// Find the user associated with this email address
1717
const list = await getUsers({ email: action.userEmail });
1818

1919
if (list.length > 1) {
2020
console.warn(`Multiple users found with email address ${userEmail}, using the first only`);
21-
} else if (list.length == 0){
21+
} else if (list.length == 0) {
2222
console.error(`No user with email address ${userEmail} found`);
2323
} else {
24-
username = list[0].username
24+
username = list[0].username;
2525
isUserAllowed = await isUserPushAllowed(repoName, username);
2626
}
2727

@@ -30,14 +30,16 @@ const exec = async (req: any, action: Action): Promise<Action> => {
3030
if (!isUserAllowed) {
3131
console.log('User not allowed to Push');
3232
step.error = true;
33-
step.log(`User ${username} <${userEmail}> is not allowed to push on repo ${action.repo}, ending`);
33+
step.log(
34+
`User ${username} <${userEmail}> is not allowed to push on repo ${action.repo}, ending`,
35+
);
3436

3537
console.log('setting error');
3638

3739
step.setError(
38-
`Rejecting push as user ${action.user} ` +
39-
`is not allowed to push on repo ` +
40-
`${action.repo}`,
40+
`Your push has been blocked (${action.userEmail} ` +
41+
`is not allowed to push on repo ` +
42+
`${action.repo})`,
4143
);
4244
action.addStep(step);
4345
return action;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const chai = require('chai');
2+
const processor = require('../src/proxy/processors/push-action/checkUserPushPermission');
3+
const { Action } = require('../src/proxy/actions/Action');
4+
const { expect } = chai;
5+
const db = require('../src/db');
6+
chai.should();
7+
8+
const TEST_ORG = 'finos';
9+
const TEST_REPO = 'test';
10+
const TEST_URL = 'https://github.com/finos/user-push-perms-test.git';
11+
const TEST_USERNAME_1 = 'push-perms-test';
12+
const TEST_EMAIL_1 = '[email protected]';
13+
const TEST_USERNAME_2 = 'push-perms-test-2';
14+
const TEST_EMAIL_2 = '[email protected]';
15+
const TEST_EMAIL_3 = '[email protected]';
16+
17+
describe('CheckUserPushPermissions...', async () => {
18+
before(async function () {
19+
await db.deleteRepo(TEST_REPO);
20+
await db.deleteUser(TEST_USERNAME_1);
21+
22+
await db.createRepo({
23+
project: TEST_ORG,
24+
name: TEST_REPO,
25+
url: TEST_URL,
26+
});
27+
await db.createUser(TEST_USERNAME_1, 'abc', TEST_EMAIL_1, TEST_USERNAME_1, false);
28+
await db.addUserCanPush(TEST_REPO, TEST_USERNAME_1);
29+
30+
await db.createUser(TEST_USERNAME_2, 'abc', TEST_EMAIL_2, TEST_USERNAME_2, false);
31+
});
32+
33+
after(async function () {
34+
await db.deleteRepo(TEST_REPO);
35+
await db.deleteUser(TEST_USERNAME_1);
36+
await db.deleteUser(TEST_USERNAME_2);
37+
});
38+
39+
it('A committer that is approved should be allowed to push...', async () => {
40+
const action = new Action('1', 'type', 'method', 1, TEST_ORG + '/' + TEST_REPO);
41+
action.userEmail = TEST_EMAIL_1;
42+
const { error } = await processor.exec(null, action);
43+
expect(error).to.be.false;
44+
});
45+
46+
it('A committer that is NOT approved should NOT be allowed to push...', async () => {
47+
const action = new Action('1', 'type', 'method', 1, TEST_ORG + '/' + TEST_REPO);
48+
action.userEmail = TEST_EMAIL_2;
49+
const { error, errorMessage } = await processor.exec(null, action);
50+
expect(error).to.be.true;
51+
expect(errorMessage).to.contains('Your push has been blocked');
52+
});
53+
54+
it('An unknown committer should NOT be allowed to push...', async () => {
55+
const action = new Action('1', 'type', 'method', 1, TEST_ORG + '/' + TEST_REPO);
56+
action.userEmail = TEST_EMAIL_3;
57+
const { error, errorMessage } = await processor.exec(null, action);
58+
expect(error).to.be.true;
59+
expect(errorMessage).to.contains('Your push has been blocked');
60+
});
61+
});

0 commit comments

Comments
 (0)