-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(1415): Add tests for other events and fix queries
- Loading branch information
Showing
2 changed files
with
153 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -399,6 +399,7 @@ describe('webhooks plugin test', () => { | |
const checkoutUrl = '[email protected]:baxterthehacker/public-repo.git'; | ||
const fullCheckoutUrl = '[email protected]:baxterthehacker/public-repo.git#master'; | ||
const scmUri = 'github.com:123456:master'; | ||
const scmRepoId = `github.com:123456`; | ||
const pipelineId = 'pipelineHash'; | ||
const jobId = 2; | ||
const buildId = 'buildHash'; | ||
|
@@ -588,6 +589,9 @@ describe('webhooks plugin test', () => { | |
pipelineMock.workflowGraph = workflowGraph; | ||
pipelineMock.jobs = Promise.resolve([mainJobMock, jobMock]); | ||
pipelineFactoryMock.scm.parseHook.withArgs(reqHeaders, payload).resolves(parsed); | ||
pipelineFactoryMock.list | ||
.withArgs({ search: { field: 'subscribedScmUrls', keyword: `%${scmRepoId}:%` } }) | ||
.resolves([pipelineMock]); | ||
pipelineFactoryMock.list.resolves([pipelineMock]); | ||
}); | ||
|
||
|
@@ -1148,6 +1152,9 @@ describe('webhooks plugin test', () => { | |
}); | ||
|
||
pipelineFactoryMock.list.resolves([pipelineMock, pMock1, pMock2, pMock3]); | ||
pipelineFactoryMock.list | ||
.withArgs({ search: { field: 'subscribedScmUrls', keyword: `%${scmRepoId}:%` } }) | ||
.resolves([]); | ||
|
||
return server.inject(options).then(reply => { | ||
assert.equal(reply.statusCode, 201); | ||
|
@@ -1245,6 +1252,9 @@ describe('webhooks plugin test', () => { | |
|
||
pipelineFactoryMock.scm.getChangedFiles.resolves(['lib/test.js']); | ||
pipelineFactoryMock.list.resolves([pipelineMock, pMock1, pMock2]); | ||
pipelineFactoryMock.list | ||
.withArgs({ search: { field: 'subscribedScmUrls', keyword: `%${scmRepoId}:%` } }) | ||
.resolves([]); | ||
|
||
return server.inject(options).then(reply => { | ||
assert.equal(reply.statusCode, 201); | ||
|
@@ -1337,6 +1347,43 @@ describe('webhooks plugin test', () => { | |
}); | ||
}); | ||
|
||
it('returns 201 when the hook source triggers subscribed event', () => { | ||
pipelineFactoryMock.scm.parseUrl | ||
.withArgs({ checkoutUrl: fullCheckoutUrl, token, scmContext }) | ||
.resolves('github.com:789123:master'); | ||
pipelineFactoryMock.list.resolves([]); | ||
pipelineFactoryMock.list | ||
.withArgs({ search: { field: 'subscribedScmUrls', keyword: '%github.com:789123:%' } }) | ||
.resolves([pipelineMock]); | ||
const scmConfigSubscribe = { | ||
scmUri: 'github.com:789123:master', | ||
token, | ||
scmContext | ||
}; | ||
|
||
return server.inject(options).then(reply => { | ||
assert.equal(reply.statusCode, 201); | ||
assert.calledWith(eventFactoryMock.create, { | ||
pipelineId, | ||
type: 'pipeline', | ||
webhooks: true, | ||
username, | ||
scmContext, | ||
sha, | ||
configPipelineSha: latestSha, | ||
startFrom: '~commit', | ||
baseBranch: 'master', | ||
causeMessage: `Merged by ${username}`, | ||
changedFiles, | ||
releaseName: undefined, | ||
ref: undefined, | ||
meta: {}, | ||
subscribedEvent: true, | ||
subscribedScmConfig: scmConfigSubscribe | ||
}); | ||
}); | ||
}); | ||
|
||
it('returns 204 when no pipeline', () => { | ||
pipelineFactoryMock.get.resolves(null); | ||
pipelineFactoryMock.list.resolves([]); | ||
|
@@ -1821,6 +1868,47 @@ describe('webhooks plugin test', () => { | |
}); | ||
}); | ||
|
||
it('returns 201 when the hook source triggers subscribed event', () => { | ||
pipelineFactoryMock.scm.parseUrl | ||
.withArgs({ checkoutUrl: fullCheckoutUrl, token, scmContext }) | ||
.resolves('github.com:789123:master'); | ||
pipelineFactoryMock.list.resolves([]); | ||
pipelineFactoryMock.list | ||
.withArgs({ search: { field: 'subscribedScmUrls', keyword: '%github.com:789123:%' } }) | ||
.resolves([pipelineMock]); | ||
const scmConfigSubscribe = { | ||
scmUri: 'github.com:789123:master', | ||
token, | ||
scmContext, | ||
prNum: 2 | ||
}; | ||
|
||
return server.inject(options).then(reply => { | ||
assert.equal(reply.statusCode, 201); | ||
assert.calledWith(eventFactoryMock.create, { | ||
prInfo, | ||
pipelineId, | ||
type: 'pr', | ||
webhooks: true, | ||
username, | ||
scmContext, | ||
sha, | ||
configPipelineSha: latestSha, | ||
startFrom: '~pr', | ||
prNum: 2, | ||
prTitle: 'Update the README with new information', | ||
prRef, | ||
prSource: 'branch', | ||
changedFiles, | ||
causeMessage: `Opened by ${scmDisplayName}:${username}`, | ||
chainPR: false, | ||
baseBranch: 'master', | ||
subscribedEvent: true, | ||
subscribedScmConfig: scmConfigSubscribe | ||
}); | ||
}); | ||
}); | ||
|
||
it('returns 201 when getCommitSha() is rejected', () => { | ||
pipelineFactoryMock.scm.getCommitSha.rejects(new Error('some error')); | ||
|
||
|
@@ -2227,6 +2315,10 @@ describe('webhooks plugin test', () => { | |
it('has the workflow for stopping builds before starting a new one', () => { | ||
const abortMsg = 'Aborted because new commit was pushed to PR#1'; | ||
|
||
pipelineFactoryMock.list | ||
.withArgs({ search: { field: 'subscribedScmUrls', keyword: `%${scmRepoId}:%` } }) | ||
.resolves([]); | ||
|
||
return server.inject(options).then(reply => { | ||
assert.calledOnce(model1.update); | ||
assert.calledOnce(model2.update); | ||
|
@@ -2459,13 +2551,18 @@ describe('webhooks plugin test', () => { | |
pipelineFactoryMock.scm.parseHook.withArgs(reqHeaders, options.payload).resolves(parsed); | ||
}); | ||
|
||
it('returns 200 on success', () => | ||
server.inject(options).then(reply => { | ||
it('returns 200 on success', () => { | ||
pipelineFactoryMock.list | ||
.withArgs({ search: { field: 'subscribedScmUrls', keyword: `%${scmRepoId}:%` } }) | ||
.resolves([]); | ||
|
||
return server.inject(options).then(reply => { | ||
assert.equal(reply.statusCode, 200); | ||
assert.calledOnce(jobMock.update); | ||
assert.strictEqual(jobMock.state, 'ENABLED'); | ||
assert.isTrue(jobMock.archived); | ||
})); | ||
}); | ||
}); | ||
|
||
it('returns 204 when pipeline to be closed does not exist', () => { | ||
pipelineFactoryMock.list.resolves([]); | ||
|
@@ -2475,18 +2572,26 @@ describe('webhooks plugin test', () => { | |
}); | ||
}); | ||
|
||
it('stops running builds', () => | ||
server.inject(options).then(() => { | ||
it('stops running builds', () => { | ||
pipelineFactoryMock.list | ||
.withArgs({ search: { field: 'subscribedScmUrls', keyword: `%${scmRepoId}:%` } }) | ||
.resolves([]); | ||
|
||
return server.inject(options).then(() => { | ||
assert.calledOnce(model1.update); | ||
assert.calledOnce(model2.update); | ||
assert.strictEqual(model1.status, 'ABORTED'); | ||
assert.strictEqual(model1.statusMessage, 'Aborted because PR#1 was closed'); | ||
assert.strictEqual(model2.status, 'ABORTED'); | ||
assert.strictEqual(model2.statusMessage, 'Aborted because PR#1 was closed'); | ||
})); | ||
}); | ||
}); | ||
|
||
it('returns 500 when failed', () => { | ||
jobMock.update.rejects(new Error('Failed to update')); | ||
pipelineFactoryMock.list | ||
.withArgs({ search: { field: 'subscribedScmUrls', keyword: `%${scmRepoId}:%` } }) | ||
.resolves([]); | ||
|
||
return server.inject(options).then(reply => { | ||
assert.equal(reply.statusCode, 500); | ||
|