|
| 1 | +const { expect } = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const proxyqquire = require('proxyquire'); |
| 4 | + |
| 5 | +const repoCollection = { |
| 6 | + findOne: sinon.stub(), |
| 7 | +}; |
| 8 | + |
| 9 | +const connectionStub = sinon.stub().returns(repoCollection); |
| 10 | + |
| 11 | +const { getRepo, getRepoByUrl } = proxyqquire('../../../src/db/mongo/repo', { |
| 12 | + './helper': { connect: connectionStub }, |
| 13 | +}); |
| 14 | + |
| 15 | +describe('MongoDB', () => { |
| 16 | + afterEach(function () { |
| 17 | + sinon.restore(); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('getRepo', () => { |
| 21 | + it('should get the repo using the name', async () => { |
| 22 | + const repoData = { |
| 23 | + name: 'sample', |
| 24 | + users: { canPush: [] }, |
| 25 | + url: 'http://example.com/sample-repo', |
| 26 | + }; |
| 27 | + repoCollection.findOne.resolves(repoData); |
| 28 | + |
| 29 | + const result = await getRepo('Sample'); |
| 30 | + expect(result).to.equal(repoData); |
| 31 | + expect(connectionStub.calledWith('repos')).to.be.true; |
| 32 | + expect(repoCollection.findOne.calledWith({ name: { $eq: 'sample' } })).to.be.true; |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('getRepoByUrl', () => { |
| 37 | + it('should get the repo using the url', async () => { |
| 38 | + const repoData = { |
| 39 | + name: 'sample', |
| 40 | + users: { canPush: [] }, |
| 41 | + url: 'https://github.com/finos/git-proxy', |
| 42 | + }; |
| 43 | + repoCollection.findOne.resolves(repoData); |
| 44 | + |
| 45 | + const result = await getRepoByUrl('https://github.com/finos/git-proxy'); |
| 46 | + expect(result).to.equal(repoData); |
| 47 | + expect(connectionStub.calledWith('repos')).to.be.true; |
| 48 | + expect( |
| 49 | + repoCollection.findOne.calledWith({ url: { $eq: 'https://github.com/finos/git-proxy' } }), |
| 50 | + ).to.be.true; |
| 51 | + }); |
| 52 | + |
| 53 | + it('should get the repo using the url, stripping off the .git', async () => { |
| 54 | + const repoData = { |
| 55 | + name: 'sample', |
| 56 | + users: { canPush: [] }, |
| 57 | + url: 'https://github.com/finos/git-proxy', |
| 58 | + }; |
| 59 | + repoCollection.findOne.resolves(repoData); |
| 60 | + |
| 61 | + const result = await getRepoByUrl('https://github.com/finos/git-proxy.git'); |
| 62 | + expect(result).to.equal(repoData); |
| 63 | + expect(connectionStub.calledWith('repos')).to.be.true; |
| 64 | + expect( |
| 65 | + repoCollection.findOne.calledWith({ url: { $eq: 'https://github.com/finos/git-proxy' } }), |
| 66 | + ).to.be.true; |
| 67 | + }); |
| 68 | + |
| 69 | + it('should get the repo using the url, ignoring the case', async () => { |
| 70 | + const repoData = { |
| 71 | + name: 'sample', |
| 72 | + users: { canPush: [] }, |
| 73 | + url: 'https://github.com/finos/git-proxy', |
| 74 | + }; |
| 75 | + repoCollection.findOne.resolves(repoData); |
| 76 | + |
| 77 | + const result = await getRepoByUrl('https://github.com/Finos/Git-Proxy.git'); |
| 78 | + expect(result).to.equal(repoData); |
| 79 | + expect(connectionStub.calledWith('repos')).to.be.true; |
| 80 | + expect( |
| 81 | + repoCollection.findOne.calledWith({ url: { $eq: 'https://github.com/finos/git-proxy' } }), |
| 82 | + ).to.be.true; |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments