tap.test('listFiles fail', async (t) => { const scope = nock('https://api.github.com') .filteringPath(ignoreQueryParams) .get(`/repos/${options.owner}/${options.repo}/pulls/${options.prId}/files`) .reply(500) await t.rejects(listFiles(options)) scope.done() t.end() })
tap.test('pingOwners success', async (t) => { const fixture = readFixture('pull-request-create-comment.json') const owners = [ '@owner1', '@owner2' ] const body = JSON.stringify({ body: getCommentForOwners(owners) }) const scope = nock('https://api.github.com') .filteringPath(ignoreQueryParams) .post(`/repos/${options.owner}/${options.repo}/issues/${options.prId}/comments`, body) .reply(201, fixture) await pingOwners(options, owners) scope.done() t.end() })
nocks.forEach(function(n) { n.filteringPath(path => filteringPath(n, path)); n.filteringRequestBody((b, rb) => filteringRequestBody(b, rb)); });
it('Should return content for http using OAuth', done => { const api = nock('http://foo.com') .filteringPath(/\?.*/g, '?xxx') .post('/orders?xxx', {}) .reply(200, '{ "success": true }'); rOAuth.complete('post', '/orders', {}, (err, data) => { should.not.exist(err); data.should.be.an.instanceof(Object) .and.have.property('success', true); api.done(); done(); }); });
tap.test('getDefaultBranch success', async (t) => { const fixture = readFixture('get-repository.json') const scope = nock('https://api.github.com') .filteringPath(ignoreQueryParams) .get(`/repos/${options.owner}/${options.repo}`) .reply(200, fixture) const defaultBranch = await getDefaultBranch(options) t.strictDeepEqual(defaultBranch, fixture.default_branch) scope.done() t.end() })
tap.test('fetchExistingLabels(): caches existing repository labels', async (t) => { const owner = 'nodejs' const repo = 'node1' // Test passes if nock is only called once, no other checks to run const scope = nock('https://api.github.com') .filteringPath(ignoreQueryParams) .get(`/repos/${owner}/${repo}/labels`) .once() // should only be called once .reply(200, []) await nodeRepo._fetchExistingLabels({ owner, repo, logger }) await nodeRepo._fetchExistingLabels({ owner, repo, logger }) scope.done() })
function setupListCommitsMock (repoName) { const commitsResponse = readFixture('pr-commits.json') return nock('https://api.github.com') .filteringPath(ignoreQueryParams) .get(`/repos/nodejs/${repoName}/pulls/12345/commits`) .reply(200, commitsResponse) }
it('Should return an error on bad request', done => { const api = nock('http://foo.com') .filteringPath(/\?.*/g, '?xxx') .post('/orders?xxx', {}) .reply(400, { success: true }); rOAuth.complete('post', '/orders', {}, err => { err.should.be.an.instanceof(Error); api.done(); done(); }); });
tap.test('fetchExistingLabels(): yields an array of existing label names', async (t) => { const labelsFixture = readFixture('repo-labels.json') const owner = 'nodejs' const repo = 'node3' const scope = nock('https://api.github.com') .filteringPath(ignoreQueryParams) .get(`/repos/${owner}/${repo}/labels`) .reply(200, labelsFixture.data) t.plan(1) const existingLabels = await nodeRepo._fetchExistingLabels({ owner, repo, logger }) t.ok(existingLabels.includes('cluster')) scope.done() })
tap.test('listFiles success', async (t) => { const fixture = readFixture('pull-request-files.json') const scope = nock('https://api.github.com') .filteringPath(ignoreQueryParams) .get(`/repos/${options.owner}/${options.repo}/pulls/${options.prId}/files`) .reply(200, fixture) const files = await listFiles(options) t.strictDeepEqual(files, fixture.map(({ filename }) => filename)) scope.done() t.end() })
tap.test('getCodeOwnersFile fail', async (t) => { const base = 'https://localhost' const filePath = '/CODEOWNERS' const url = `${base}${filePath}` const scope = nock(base) .filteringPath(ignoreQueryParams) .get(filePath) .reply(500) await t.rejects(getCodeOwnersFile(url, options)) scope.done() t.end() })
tap.test('getDefaultBranch fail', async (t) => { const scope = nock('https://api.github.com') .filteringPath(ignoreQueryParams) .get(`/repos/${options.owner}/${options.repo}`) .reply(500) await t.rejects(getDefaultBranch(options)) scope.done() t.end() })
tap.test('pingOwners fail', async (t) => { const scope = nock('https://api.github.com') .filteringPath(ignoreQueryParams) .post(`/repos/${options.owner}/${options.repo}/issues/${options.prId}/comments`) .reply(500) await t.rejects(pingOwners(options, [])) scope.done() t.end() })
tap.test('getDefaultBranch empty response', async (t) => { const scope = nock('https://api.github.com') .filteringPath(ignoreQueryParams) .get(`/repos/${options.owner}/${options.repo}`) .reply(200) await t.rejects(getDefaultBranch(options)) scope.done() t.end() })
it('Should return an error on internal server error', done => { const api = nock('http://foo.com') .filteringPath(/\?.*/g, '?xxx') .post('/orders?xxx', {}) .reply(500, { success: true }); rOAuth.complete('post', '/orders', {}, err => { err.should.be.an.instanceof(Error); api.done(); done(); }); });