Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
12 changes: 6 additions & 6 deletions src/db/mongo/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ export const deleteRepo = async (name: string) => {
export const isUserPushAllowed = async (name: string, user: string) => {
name = name.toLowerCase();
user = user.toLowerCase();
console.log(`checking if user ${user} can push to ${name}`);
return new Promise(async (resolve) => {
const repo = await exports.getRepo(name);
console.log(repo.users.canPush);
console.log(repo.users.canAuthorise);

if (repo.users.canPush.includes(user) || repo.users.canAuthorise.includes(user)) {
resolve(true);
} else {
if( !repo ) {
console.log(`repo ${name} not found`);
resolve(false);
return;
}
resolve(repo.users.canPush.includes(user) || repo.users.canAuthorise.includes(user));
});
};

// not used in the codebase, but kept for compatibility
export const canUserApproveRejectPushRepo = async (name: string, user: string) => {
name = name.toLowerCase();
user = user.toLowerCase();
Expand Down
52 changes: 52 additions & 0 deletions test/db/mongo/repo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const chai = require('chai');
const sinon = require('sinon');
const repoModule = require('../../../src/db/mongo/repo');

const { expect } = chai;

describe('mongo repo', () => {
afterEach(() => {
sinon.restore();
});

describe('isUserPushAllowed', () => {
it('returns true if user is in canPush', async () => {
sinon.stub(repoModule, 'getRepo').resolves({
users: {
canPush: ['alice'],
canAuthorise: [],
},
});
const result = await repoModule.isUserPushAllowed('myrepo', 'alice');
expect(result).to.be.true;
});

it('returns true if user is in canAuthorise', async () => {
sinon.stub(repoModule, 'getRepo').resolves({
users: {
canPush: [],
canAuthorise: ['bob'],
},
});
const result = await repoModule.isUserPushAllowed('myrepo', 'bob');
expect(result).to.be.true;
});

it('returns false if user is in neither', async () => {
sinon.stub(repoModule, 'getRepo').resolves({
users: {
canPush: [],
canAuthorise: [],
},
});
const result = await repoModule.isUserPushAllowed('myrepo', 'charlie');
expect(result).to.be.false;
});

it('returns false if repo is not registered', async () => {
sinon.stub(repoModule, 'getRepo').resolves(null);
const result = await repoModule.isUserPushAllowed('myrepo', 'charlie');
expect(result).to.be.false;
});
});
});
Loading