|
| 1 | +const chai = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const db = require('../../src/db'); |
| 4 | + |
| 5 | +const { expect } = chai; |
| 6 | + |
| 7 | +describe('db', () => { |
| 8 | + afterEach(() => { |
| 9 | + sinon.restore(); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('isUserPushAllowed', () => { |
| 13 | + it('returns true if user is in canPush', async () => { |
| 14 | + sinon.stub(db, 'getRepoByUrl').resolves({ |
| 15 | + users: { |
| 16 | + canPush: ['alice'], |
| 17 | + canAuthorise: [], |
| 18 | + }, |
| 19 | + }); |
| 20 | + const result = await db.isUserPushAllowed('myrepo', 'alice'); |
| 21 | + expect(result).to.be.true; |
| 22 | + }); |
| 23 | + |
| 24 | + it('returns true if user is in canAuthorise', async () => { |
| 25 | + sinon.stub(db, 'getRepoByUrl').resolves({ |
| 26 | + users: { |
| 27 | + canPush: [], |
| 28 | + canAuthorise: ['bob'], |
| 29 | + }, |
| 30 | + }); |
| 31 | + const result = await db.isUserPushAllowed('myrepo', 'bob'); |
| 32 | + expect(result).to.be.true; |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns false if user is in neither', async () => { |
| 36 | + sinon.stub(db, 'getRepoByUrl').resolves({ |
| 37 | + users: { |
| 38 | + canPush: [], |
| 39 | + canAuthorise: [], |
| 40 | + }, |
| 41 | + }); |
| 42 | + const result = await db.isUserPushAllowed('myrepo', 'charlie'); |
| 43 | + expect(result).to.be.false; |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns false if repo is not registered', async () => { |
| 47 | + sinon.stub(db, 'getRepoByUrl').resolves(null); |
| 48 | + const result = await db.isUserPushAllowed('myrepo', 'charlie'); |
| 49 | + expect(result).to.be.false; |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
0 commit comments