|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | + |
| 5 | +const sinon = require('sinon'); |
| 6 | +const FormData = require('form-data'); |
| 7 | + |
| 8 | +const { |
| 9 | + RunPRJob, |
| 10 | + CI_CRUMB_URL, |
| 11 | + CI_PR_URL |
| 12 | +} = require('../../lib/ci/run_ci'); |
| 13 | +const TestCLI = require('../fixtures/test_cli'); |
| 14 | + |
| 15 | +describe('Jenkins', () => { |
| 16 | + it('should fail if starting node-pull-request fails', async() => { |
| 17 | + const cli = new TestCLI(); |
| 18 | + const crumb = 'asdf1234'; |
| 19 | + const request = { |
| 20 | + text: sinon.stub().throws(), |
| 21 | + json: sinon.stub().withArgs(CI_CRUMB_URL) |
| 22 | + .returns(Promise.resolve({ crumb })) |
| 23 | + }; |
| 24 | + const owner = 'nodejs'; |
| 25 | + const repo = 'node-auto-test'; |
| 26 | + const prid = 123456; |
| 27 | + |
| 28 | + const jobRunner = new RunPRJob(cli, request, owner, repo, prid); |
| 29 | + assert.strictEqual(await jobRunner.start(), false); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return false if crumb fails', async() => { |
| 33 | + const cli = new TestCLI(); |
| 34 | + const request = { |
| 35 | + json: sinon.stub().throws() |
| 36 | + }; |
| 37 | + const owner = 'nodejs'; |
| 38 | + const repo = 'node-auto-test'; |
| 39 | + const prid = 123456; |
| 40 | + |
| 41 | + const jobRunner = new RunPRJob(cli, request, owner, repo, prid); |
| 42 | + assert.strictEqual(await jobRunner.start(), false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should start node-pull-request', async() => { |
| 46 | + const cli = new TestCLI(); |
| 47 | + const crumb = 'asdf1234'; |
| 48 | + const owner = 'nodejs'; |
| 49 | + const repo = 'node-auto-test'; |
| 50 | + const prid = 123456; |
| 51 | + |
| 52 | + sinon.stub(FormData.prototype, 'append').callsFake(function(key, value) { |
| 53 | + assert.strictEqual(key, 'json'); |
| 54 | + const { parameter } = JSON.parse(value); |
| 55 | + const expectedParameters = { |
| 56 | + CERTIFY_SAFE: 'on', |
| 57 | + TARGET_GITHUB_ORG: owner, |
| 58 | + TARGET_REPO_NAME: repo, |
| 59 | + PR_ID: prid, |
| 60 | + REBASE_ONTO: '<pr base branch>', |
| 61 | + DESCRIPTION_SETTER_DESCRIPTION: '' |
| 62 | + }; |
| 63 | + for (const { name, value } of parameter) { |
| 64 | + assert.strictEqual(value, expectedParameters[name]); |
| 65 | + delete expectedParameters[name]; |
| 66 | + } |
| 67 | + assert.strictEqual(Object.keys(expectedParameters).length, 0); |
| 68 | + |
| 69 | + this._validated = true; |
| 70 | + |
| 71 | + return FormData.prototype.append.wrappedMethod.bind(this)(key, value); |
| 72 | + }); |
| 73 | + |
| 74 | + const request = { |
| 75 | + text: sinon.stub() |
| 76 | + .callsFake((url, { method, headers, body }) => { |
| 77 | + assert.strictEqual(url, CI_PR_URL); |
| 78 | + assert.strictEqual(method, 'POST'); |
| 79 | + assert.deepStrictEqual(headers, { 'Jenkins-Crumb': crumb }); |
| 80 | + assert.ok(body._validated); |
| 81 | + }), |
| 82 | + json: sinon.stub().withArgs(CI_CRUMB_URL) |
| 83 | + .returns(Promise.resolve({ crumb })) |
| 84 | + }; |
| 85 | + const jobRunner = new RunPRJob(cli, request, owner, repo, prid); |
| 86 | + assert.ok(await jobRunner.start()); |
| 87 | + }); |
| 88 | +}); |
0 commit comments