Skip to content

Commit b52ca15

Browse files
committed
test: checkUserPushPermission
1 parent 70857f5 commit b52ca15

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const chai = require('chai');
2+
const sinon = require('sinon');
3+
const proxyquire = require('proxyquire');
4+
const { Action, Step } = require('../../src/proxy/actions');
5+
6+
chai.should();
7+
const expect = chai.expect;
8+
9+
describe('checkUserPushPermission', () => {
10+
let exec;
11+
let getUsersStub;
12+
let isUserPushAllowedStub;
13+
let logStub;
14+
15+
beforeEach(() => {
16+
logStub = sinon.stub(console, 'log');
17+
18+
getUsersStub = sinon.stub();
19+
isUserPushAllowedStub = sinon.stub();
20+
21+
const checkUserPushPermission = proxyquire('../../src/proxy/processors/push-action/checkUserPushPermission', {
22+
'../../../db': {
23+
getUsers: getUsersStub,
24+
isUserPushAllowed: isUserPushAllowedStub
25+
}
26+
});
27+
28+
exec = checkUserPushPermission.exec;
29+
});
30+
31+
afterEach(() => {
32+
sinon.restore();
33+
});
34+
35+
describe('exec', () => {
36+
let action;
37+
let req;
38+
let stepSpy;
39+
40+
beforeEach(() => {
41+
req = {};
42+
action = new Action(
43+
'1234567890',
44+
'push',
45+
'POST',
46+
1234567890,
47+
'test/repo.git'
48+
);
49+
action.user = 'git-user';
50+
stepSpy = sinon.spy(Step.prototype, 'log');
51+
});
52+
53+
it('should allow push when user has permission', async () => {
54+
getUsersStub.resolves([{ username: 'db-user', gitAccount: 'git-user' }]);
55+
isUserPushAllowedStub.resolves(true);
56+
57+
const result = await exec(req, action);
58+
59+
expect(result.steps).to.have.lengthOf(1);
60+
expect(result.steps[0].error).to.be.false;
61+
expect(stepSpy.calledWith('User db-user is allowed to push on repo test/repo.git')).to.be.true;
62+
expect(logStub.calledWith('User db-user permission on Repo repo : true')).to.be.true;
63+
});
64+
65+
it('should reject push when user has no permission', async () => {
66+
getUsersStub.resolves([{ username: 'db-user', gitAccount: 'git-user' }]);
67+
isUserPushAllowedStub.resolves(false);
68+
69+
const result = await exec(req, action);
70+
71+
expect(result.steps).to.have.lengthOf(1);
72+
expect(result.steps[0].error).to.be.true;
73+
expect(stepSpy.calledWith('User db-user is not allowed to push on repo test/repo.git, ending')).to.be.true;
74+
expect(result.steps[0].errorMessage).to.include('Rejecting push as user git-user');
75+
expect(logStub.calledWith('User not allowed to Push')).to.be.true;
76+
});
77+
78+
it('should reject push when no user found for git account', async () => {
79+
getUsersStub.resolves([]);
80+
81+
const result = await exec(req, action);
82+
83+
expect(result.steps).to.have.lengthOf(1);
84+
expect(result.steps[0].error).to.be.true;
85+
expect(stepSpy.calledWith('User git-user is not allowed to push on repo test/repo.git, ending')).to.be.true;
86+
expect(result.steps[0].errorMessage).to.include('Rejecting push as user git-user');
87+
});
88+
89+
it('should handle multiple users for git account by rejecting push', async () => {
90+
getUsersStub.resolves([
91+
{ username: 'user1', gitAccount: 'git-user' },
92+
{ username: 'user2', gitAccount: 'git-user' }
93+
]);
94+
95+
const result = await exec(req, action);
96+
97+
expect(result.steps).to.have.lengthOf(1);
98+
expect(result.steps[0].error).to.be.true;
99+
expect(logStub.calledWith('Users for this git account: [{"username":"user1","gitAccount":"git-user"},{"username":"user2","gitAccount":"git-user"}]')).to.be.true;
100+
});
101+
});
102+
});

0 commit comments

Comments
 (0)