|
| 1 | +const chai = require('chai'); |
| 2 | +const sinon = require('sinon'); |
| 3 | +const proxyquire = require('proxyquire'); |
| 4 | +const { Action } = require('../../src/proxy/actions'); |
| 5 | + |
| 6 | +chai.should(); |
| 7 | +const expect = chai.expect; |
| 8 | + |
| 9 | +describe('checkIfWaitingAuth', () => { |
| 10 | + let exec; |
| 11 | + let getPushStub; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + getPushStub = sinon.stub(); |
| 15 | + |
| 16 | + const checkIfWaitingAuth = proxyquire('../../src/proxy/processors/push-action/checkIfWaitingAuth', { |
| 17 | + '../../../db': { getPush: getPushStub } |
| 18 | + }); |
| 19 | + |
| 20 | + exec = checkIfWaitingAuth.exec; |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + sinon.restore(); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('exec', () => { |
| 28 | + let action; |
| 29 | + let req; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + req = {}; |
| 33 | + action = new Action( |
| 34 | + '1234567890', |
| 35 | + 'push', |
| 36 | + 'POST', |
| 37 | + 1234567890, |
| 38 | + 'test/repo' |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should set allowPush when action exists and is authorized', async () => { |
| 43 | + const authorizedAction = new Action( |
| 44 | + '1234567890', |
| 45 | + 'push', |
| 46 | + 'POST', |
| 47 | + 1234567890, |
| 48 | + 'test/repo' |
| 49 | + ); |
| 50 | + authorizedAction.authorised = true; |
| 51 | + getPushStub.resolves(authorizedAction); |
| 52 | + |
| 53 | + const result = await exec(req, action); |
| 54 | + |
| 55 | + expect(result.steps).to.have.lengthOf(1); |
| 56 | + expect(result.steps[0].error).to.be.false; |
| 57 | + expect(result.allowPush).to.be.true; |
| 58 | + expect(result).to.deep.equal(authorizedAction); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should not set allowPush when action exists but not authorized', async () => { |
| 62 | + const unauthorizedAction = new Action( |
| 63 | + '1234567890', |
| 64 | + 'push', |
| 65 | + 'POST', |
| 66 | + 1234567890, |
| 67 | + 'test/repo' |
| 68 | + ); |
| 69 | + unauthorizedAction.authorised = false; |
| 70 | + getPushStub.resolves(unauthorizedAction); |
| 71 | + |
| 72 | + const result = await exec(req, action); |
| 73 | + |
| 74 | + expect(result.steps).to.have.lengthOf(1); |
| 75 | + expect(result.steps[0].error).to.be.false; |
| 76 | + expect(result.allowPush).to.be.false; |
| 77 | + }); |
| 78 | + |
| 79 | + it('should not set allowPush when action does not exist', async () => { |
| 80 | + getPushStub.resolves(null); |
| 81 | + |
| 82 | + const result = await exec(req, action); |
| 83 | + |
| 84 | + expect(result.steps).to.have.lengthOf(1); |
| 85 | + expect(result.steps[0].error).to.be.false; |
| 86 | + expect(result.allowPush).to.be.false; |
| 87 | + }); |
| 88 | + |
| 89 | + it('should not modify action when it has an error', async () => { |
| 90 | + action.error = true; |
| 91 | + const authorizedAction = new Action( |
| 92 | + '1234567890', |
| 93 | + 'push', |
| 94 | + 'POST', |
| 95 | + 1234567890, |
| 96 | + 'test/repo' |
| 97 | + ); |
| 98 | + authorizedAction.authorised = true; |
| 99 | + getPushStub.resolves(authorizedAction); |
| 100 | + |
| 101 | + const result = await exec(req, action); |
| 102 | + |
| 103 | + expect(result.steps).to.have.lengthOf(1); |
| 104 | + expect(result.steps[0].error).to.be.false; |
| 105 | + expect(result.allowPush).to.be.false; |
| 106 | + expect(result.error).to.be.true; |
| 107 | + }); |
| 108 | + |
| 109 | + it('should add step with error when getPush throws', async () => { |
| 110 | + const error = new Error('DB error'); |
| 111 | + getPushStub.rejects(error); |
| 112 | + |
| 113 | + try { |
| 114 | + await exec(req, action); |
| 115 | + throw new Error('Should have thrown'); |
| 116 | + } catch (e) { |
| 117 | + expect(e).to.equal(error); |
| 118 | + expect(action.steps).to.have.lengthOf(1); |
| 119 | + expect(action.steps[0].error).to.be.true; |
| 120 | + expect(action.steps[0].errorMessage).to.contain('DB error'); |
| 121 | + } |
| 122 | + }); |
| 123 | + }); |
| 124 | +}); |
0 commit comments