Files
probot/test/github/graphql.test.js
Morgan Delagrange 73ac539f45 fix: Fix path to GraphQL API when using GitHub Enterprise (#580)
The API path should be <ghe_host>/api/graphql, but
it is defaulting to <ghe_host>/api/v3/graphql.
2018-06-27 19:11:34 -05:00

104 lines
2.8 KiB
JavaScript

const { GitHubAPI } = require('../../src/github')
const nock = require('nock')
const Bottleneck = require('bottleneck')
describe('github/graphql', () => {
let github
// Expect there are no more pending nock requests
beforeEach(async () => nock.cleanAll())
afterEach(() => expect(nock.pendingMocks()).toEqual([]))
beforeEach(() => {
const logger = {
debug: jest.fn(),
trace: jest.fn()
}
// Set a shorter limiter, otherwise tests are _slow_
const limiter = new Bottleneck()
github = new GitHubAPI({ logger, limiter })
})
describe('query', () => {
const query = 'query { viewer { login } }'
let data
test('makes a graphql query', async () => {
data = { viewer: { login: 'bkeepers' } }
nock('https://api.github.com', {
reqheaders: { 'content-type': 'application/json' }
}).post('/graphql', {query})
.reply(200, { data })
expect(await github.query(query)).toEqual(data)
})
test('makes a graphql query with variables', async () => {
const variables = {owner: 'probot', repo: 'test'}
nock('https://api.github.com', {
reqheaders: { 'content-type': 'application/json' }
}).post('/graphql', {query, variables})
.reply(200, { data })
expect(await github.query(query, variables)).toEqual(data)
})
test('uses authentication', async () => {
github.authenticate({type: 'token', token: 'testing'})
nock('https://api.github.com', {
reqheaders: { authorization: 'token testing' }
}).post('/graphql', {query})
.reply(200, { data })
await github.query(query)
})
test('allows custom headers', async () => {
nock('https://api.github.com', {
reqheaders: { 'foo': 'bar' }
}).post('/graphql', {query})
.reply(200, { data })
await github.query(query, undefined, {foo: 'bar'})
})
test('raises errors', async () => {
const response = {'data': null, 'errors': [{'message': 'Unexpected end of document'}]}
nock('https://api.github.com').post('/graphql', {query})
.reply(200, response)
await expect(github.query(query)).rejects.toThrow('Unexpected end of document')
})
})
describe('ghe support', () => {
const query = 'query { viewer { login } }'
let data
beforeEach(() => {
process.env.GHE_HOST = 'notreallygithub.com'
})
afterEach(() => {
delete process.env.GHE_HOST
})
test('makes a graphql query', async () => {
data = { viewer: { login: 'bkeepers' } }
nock('https://notreallygithub.com', {
reqheaders: { 'content-type': 'application/json' }
}).post('/api/graphql', {query})
.reply(200, { data })
expect(await github.query(query)).toEqual(data)
})
})
})