forked from mirrors/probot
test: convert final test to TypeScript (#850)
This commit is contained in:
parent
2501243a32
commit
d76c70ba23
13
src/index.ts
13
src/index.ts
|
@ -13,17 +13,15 @@ import { createDefaultCache } from './cache'
|
||||||
import { Context } from './context'
|
import { Context } from './context'
|
||||||
import { ProbotOctokit } from './github'
|
import { ProbotOctokit } from './github'
|
||||||
import { logger } from './logger'
|
import { logger } from './logger'
|
||||||
|
import { logRequestErrors } from './middleware/log-request-errors'
|
||||||
import { findPrivateKey } from './private-key'
|
import { findPrivateKey } from './private-key'
|
||||||
import { resolve } from './resolver'
|
import { resolve } from './resolver'
|
||||||
import { createServer } from './server'
|
import { createServer } from './server'
|
||||||
import { createWebhookProxy } from './webhook-proxy'
|
import { createWebhookProxy } from './webhook-proxy'
|
||||||
|
|
||||||
// tslint:disable:no-var-requires
|
|
||||||
// These needs types
|
|
||||||
const logRequestErrors = require('./middleware/log-request-errors')
|
|
||||||
|
|
||||||
const cache = createDefaultCache()
|
const cache = createDefaultCache()
|
||||||
|
|
||||||
|
// tslint:disable:no-var-requires
|
||||||
const defaultAppFns: ApplicationFunction[] = [
|
const defaultAppFns: ApplicationFunction[] = [
|
||||||
require('./apps/default'),
|
require('./apps/default'),
|
||||||
require('./apps/sentry'),
|
require('./apps/sentry'),
|
||||||
|
@ -89,12 +87,13 @@ export class Probot {
|
||||||
public httpServer?: Server
|
public httpServer?: Server
|
||||||
public webhook: Webhooks
|
public webhook: Webhooks
|
||||||
public logger: Logger
|
public logger: Logger
|
||||||
|
// These 3 need to be public for the tests to work.
|
||||||
|
public options: Options
|
||||||
|
public app?: OctokitApp
|
||||||
|
public throttleOptions: any
|
||||||
|
|
||||||
private options: Options
|
|
||||||
private apps: Application[]
|
private apps: Application[]
|
||||||
private app?: OctokitApp
|
|
||||||
private githubToken?: string
|
private githubToken?: string
|
||||||
private throttleOptions: any
|
|
||||||
private Octokit: Octokit.Static
|
private Octokit: Octokit.Static
|
||||||
|
|
||||||
constructor (options: Options) {
|
constructor (options: Options) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { NextFunction, Request, Response } from './logging'
|
import { ErrorRequestHandler, NextFunction } from 'express'
|
||||||
|
import { Request, Response } from './logging'
|
||||||
|
|
||||||
module.exports = (err: Error, req: Request, res: Response, next: NextFunction) => {
|
export const logRequestErrors: ErrorRequestHandler = (err: Error, req: Request, res: Response, next: NextFunction): void => {
|
||||||
if (req.log) {
|
if (req.log) {
|
||||||
req.log.error(err)
|
req.log.error(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
exports[`Probot ghe support throws if the GHE host includes a protocol 1`] = `[Error: Your \`GHE_HOST\` environment variable should not begin with https:// or http://]`;
|
exports[`Probot ghe support throws if the GHE host includes a protocol 1`] = `[Error: Your \`GHE_HOST\` environment variable should not begin with https:// or http://]`;
|
||||||
|
|
||||||
exports[`Probot ghe support throws if the GHE host includes a protocol 2`] = `[Error: Your \`GHE_HOST\` environment variable should not begin with https:// or http://]`;
|
exports[`Probot ghe support throws if the GHE host includes a protocol 2`] = `[Error: Your \`GHE_HOST\` environment variable should not begin with https:// or http://]`;
|
||||||
|
|
||||||
exports[`Probot run runs with a function as argument 1`] = `
|
exports[`Probot run runs with a function as argument 1`] = `
|
||||||
Object {
|
Object {
|
||||||
"cert": "-----BEGIN RSA PRIVATE KEY-----
|
"cert": "-----BEGIN RSA PRIVATE KEY-----
|
|
@ -1,20 +1,28 @@
|
||||||
const Bottleneck = require('bottleneck')
|
import Octokit = require('@octokit/rest')
|
||||||
const { Probot, createProbot } = require('../src')
|
import Bottleneck from 'bottleneck'
|
||||||
const nock = require('nock')
|
import { NextFunction, Request, Response } from 'express'
|
||||||
const Octokit = require('@octokit/rest')
|
import nock = require('nock')
|
||||||
const request = require('supertest')
|
import request = require('supertest')
|
||||||
|
import { Application, createProbot, Probot } from '../src'
|
||||||
|
import { GitHubAPI } from '../src/github'
|
||||||
|
|
||||||
const helper = require('./apps/helper')
|
import path = require('path')
|
||||||
const path = require('path')
|
import helper = require('./apps/helper')
|
||||||
|
|
||||||
|
// tslint:disable:no-empty
|
||||||
describe('Probot', () => {
|
describe('Probot', () => {
|
||||||
let probot
|
let probot: Probot
|
||||||
let event
|
let event: {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
payload: any
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
probot = createProbot({ githubToken: 'faketoken' })
|
probot = createProbot({ githubToken: 'faketoken' })
|
||||||
|
|
||||||
event = {
|
event = {
|
||||||
|
id: '0',
|
||||||
name: 'push',
|
name: 'push',
|
||||||
payload: require('./fixtures/webhook/push')
|
payload: require('./fixtures/webhook/push')
|
||||||
}
|
}
|
||||||
|
@ -29,7 +37,7 @@ describe('Probot', () => {
|
||||||
|
|
||||||
describe('run', () => {
|
describe('run', () => {
|
||||||
|
|
||||||
let env
|
let env: NodeJS.ProcessEnv
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
env = { ...process.env }
|
env = { ...process.env }
|
||||||
|
@ -46,29 +54,29 @@ describe('Probot', () => {
|
||||||
it('runs with a function as argument', async () => {
|
it('runs with a function as argument', async () => {
|
||||||
process.env.PORT = '3003'
|
process.env.PORT = '3003'
|
||||||
let initialized = false
|
let initialized = false
|
||||||
const probot = await Probot.run((app) => {
|
probot = await Probot.run((app) => {
|
||||||
initialized = true
|
initialized = true
|
||||||
})
|
})
|
||||||
expect(probot.options).toMatchSnapshot()
|
expect(probot.options).toMatchSnapshot()
|
||||||
expect(initialized).toBeTruthy()
|
expect(initialized).toBeTruthy()
|
||||||
probot.httpServer.close()
|
probot.httpServer!.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('runs with an array of strings', async () => {
|
it('runs with an array of strings', async () => {
|
||||||
const probot = await Probot.run(['run', 'file.js'])
|
probot = await Probot.run(['run', 'file.js'])
|
||||||
expect(probot.options).toMatchSnapshot()
|
expect(probot.options).toMatchSnapshot()
|
||||||
probot.httpServer.close()
|
probot.httpServer!.close()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('runs without config and loads the setup app', async () => {
|
it('runs without config and loads the setup app', async () => {
|
||||||
let initialized = false
|
let initialized = false
|
||||||
delete process.env.PRIVATE_KEY_PATH
|
delete process.env.PRIVATE_KEY_PATH
|
||||||
const probot = await Probot.run((app) => {
|
probot = await Probot.run((app) => {
|
||||||
initialized = true
|
initialized = true
|
||||||
})
|
})
|
||||||
expect(probot.options).toMatchSnapshot()
|
expect(probot.options).toMatchSnapshot()
|
||||||
expect(initialized).toBeFalsy()
|
expect(initialized).toBeFalsy()
|
||||||
probot.httpServer.close()
|
probot.httpServer!.close()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -87,7 +95,7 @@ describe('Probot', () => {
|
||||||
try {
|
try {
|
||||||
await probot.webhook.receive(event)
|
await probot.webhook.receive(event)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(probot.logger.error.mock.calls[0]).toMatchSnapshot()
|
expect((probot.logger.error as jest.Mock).mock.calls[0]).toMatchSnapshot()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -98,7 +106,7 @@ describe('Probot', () => {
|
||||||
try {
|
try {
|
||||||
await probot.webhook.receive(event)
|
await probot.webhook.receive(event)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(probot.logger.error.mock.calls[0]).toMatchSnapshot()
|
expect((probot.logger.error as jest.Mock).mock.calls[0]).toMatchSnapshot()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -109,7 +117,7 @@ describe('Probot', () => {
|
||||||
try {
|
try {
|
||||||
await probot.webhook.receive(event)
|
await probot.webhook.receive(event)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(probot.logger.error.mock.calls[0]).toMatchSnapshot()
|
expect((probot.logger.error as jest.Mock).mock.calls[0]).toMatchSnapshot()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -120,7 +128,7 @@ describe('Probot', () => {
|
||||||
try {
|
try {
|
||||||
await probot.webhook.receive(event)
|
await probot.webhook.receive(event)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(probot.logger.error.mock.calls[0]).toMatchSnapshot()
|
expect((probot.logger.error as jest.Mock).mock.calls[0]).toMatchSnapshot()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -131,7 +139,7 @@ describe('Probot', () => {
|
||||||
try {
|
try {
|
||||||
await probot.webhook.receive(event)
|
await probot.webhook.receive(event)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(probot.logger.error.mock.calls[0]).toMatchSnapshot()
|
expect((probot.logger.error as jest.Mock).mock.calls[0]).toMatchSnapshot()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -169,7 +177,7 @@ describe('Probot', () => {
|
||||||
probot.load(app => {
|
probot.load(app => {
|
||||||
const route = app.route('/' + name)
|
const route = app.route('/' + name)
|
||||||
|
|
||||||
route.use(function (req, res, next) {
|
route.use((req, res, next) => {
|
||||||
res.append('X-Test', name)
|
res.append('X-Test', name)
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
@ -190,8 +198,8 @@ describe('Probot', () => {
|
||||||
it('allows users to configure webhook paths', async () => {
|
it('allows users to configure webhook paths', async () => {
|
||||||
probot = createProbot({ webhookPath: '/webhook', githubToken: 'faketoken' })
|
probot = createProbot({ webhookPath: '/webhook', githubToken: 'faketoken' })
|
||||||
// Error handler to avoid printing logs
|
// Error handler to avoid printing logs
|
||||||
// eslint-disable-next-line handle-callback-err
|
// tslint:disable-next-line handle-callback-err
|
||||||
probot.server.use((err, req, res, next) => { })
|
probot.server.use((err: any, req: Request, res: Response, next: NextFunction) => { })
|
||||||
|
|
||||||
probot.load(app => {
|
probot.load(app => {
|
||||||
const route = app.route()
|
const route = app.route()
|
||||||
|
@ -210,8 +218,8 @@ describe('Probot', () => {
|
||||||
|
|
||||||
it('defaults webhook path to `/`', async () => {
|
it('defaults webhook path to `/`', async () => {
|
||||||
// Error handler to avoid printing logs
|
// Error handler to avoid printing logs
|
||||||
// eslint-disable-next-line handle-callback-err
|
// tslint:disable-next-line handle-callback-err
|
||||||
probot.server.use((err, req, res, next) => { })
|
probot.server.use((err: any, req: Request, res: Response, next: NextFunction) => { })
|
||||||
|
|
||||||
// POST requests to `/` should 400 b/c webhook signature will fail
|
// POST requests to `/` should 400 b/c webhook signature will fail
|
||||||
await request(probot.server).post('/')
|
await request(probot.server).post('/')
|
||||||
|
@ -238,7 +246,7 @@ describe('Probot', () => {
|
||||||
describe('receive', () => {
|
describe('receive', () => {
|
||||||
it('forwards events to each app', async () => {
|
it('forwards events to each app', async () => {
|
||||||
const spy = jest.fn()
|
const spy = jest.fn()
|
||||||
const app = probot.load(app => app.on('push', spy))
|
const app = probot.load(appl => appl.on('push', spy))
|
||||||
app.auth = jest.fn().mockReturnValue(Promise.resolve({}))
|
app.auth = jest.fn().mockReturnValue(Promise.resolve({}))
|
||||||
|
|
||||||
await probot.receive(event)
|
await probot.receive(event)
|
||||||
|
@ -247,8 +255,8 @@ describe('Probot', () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('ghe support', function () {
|
describe('ghe support', () => {
|
||||||
let app
|
let app: Application
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
process.env.GHE_HOST = 'notreallygithub.com'
|
process.env.GHE_HOST = 'notreallygithub.com'
|
||||||
|
@ -268,8 +276,8 @@ describe('Probot', () => {
|
||||||
it('requests from the correct API URL', async () => {
|
it('requests from the correct API URL', async () => {
|
||||||
const spy = jest.fn()
|
const spy = jest.fn()
|
||||||
|
|
||||||
const appFn = async app => {
|
const appFn = async (appl: Application) => {
|
||||||
const github = await app.auth()
|
const github = await appl.auth()
|
||||||
const res = await github.apps.listInstallations({})
|
const res = await github.apps.listInstallations({})
|
||||||
return spy(res)
|
return spy(res)
|
||||||
}
|
}
|
||||||
|
@ -283,6 +291,7 @@ describe('Probot', () => {
|
||||||
probot = createProbot({
|
probot = createProbot({
|
||||||
id: 1234,
|
id: 1234,
|
||||||
// Some valid RSA key to be able to sign the initial token
|
// Some valid RSA key to be able to sign the initial token
|
||||||
|
// tslint:disable-next-line:object-literal-sort-keys
|
||||||
cert: '-----BEGIN RSA PRIVATE KEY-----\n' +
|
cert: '-----BEGIN RSA PRIVATE KEY-----\n' +
|
||||||
'MIIBOQIBAAJBAIILhiN9IFpaE0pUXsesuuoaj6eeDiAqCiE49WB1tMB8ZMhC37kY\n' +
|
'MIIBOQIBAAJBAIILhiN9IFpaE0pUXsesuuoaj6eeDiAqCiE49WB1tMB8ZMhC37kY\n' +
|
||||||
'Fl52NUYbUxb7JEf6pH5H9vqw1Wp69u78XeUCAwEAAQJAb88urnaXiXdmnIK71tuo\n' +
|
'Fl52NUYbUxb7JEf6pH5H9vqw1Wp69u78XeUCAwEAAQJAb88urnaXiXdmnIK71tuo\n' +
|
||||||
|
@ -293,7 +302,7 @@ describe('Probot', () => {
|
||||||
'r1UQNnUExRh7ZT0kFbMfO9jKYZVlQdCL9Dn93vo=\n' +
|
'r1UQNnUExRh7ZT0kFbMfO9jKYZVlQdCL9Dn93vo=\n' +
|
||||||
'-----END RSA PRIVATE KEY-----'
|
'-----END RSA PRIVATE KEY-----'
|
||||||
})
|
})
|
||||||
expect(await probot.app.getInstallationAccessToken({ installationId: 5 })).toBe('github_token')
|
expect(await probot.app!.getInstallationAccessToken({ installationId: 5 })).toBe('github_token')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws if the GHE host includes a protocol', async () => {
|
it('throws if the GHE host includes a protocol', async () => {
|
||||||
|
@ -323,7 +332,7 @@ describe('Probot', () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('sets throttleOptions', async () => {
|
it('sets throttleOptions', async () => {
|
||||||
const probot = createProbot({ webhookPath: '/webhook', githubToken: 'faketoken' })
|
probot = createProbot({ webhookPath: '/webhook', githubToken: 'faketoken' })
|
||||||
|
|
||||||
expect(probot.throttleOptions.Bottleneck).toBe(Bottleneck)
|
expect(probot.throttleOptions.Bottleneck).toBe(Bottleneck)
|
||||||
expect(probot.throttleOptions.connection).toBeInstanceOf(Bottleneck.IORedisConnection)
|
expect(probot.throttleOptions.connection).toBeInstanceOf(Bottleneck.IORedisConnection)
|
||||||
|
@ -335,7 +344,7 @@ describe('Probot', () => {
|
||||||
const redisConfig = {
|
const redisConfig = {
|
||||||
host: 'test'
|
host: 'test'
|
||||||
}
|
}
|
||||||
const probot = createProbot({ webhookPath: '/webhook', githubToken: 'faketoken', redisConfig })
|
probot = createProbot({ webhookPath: '/webhook', githubToken: 'faketoken', redisConfig })
|
||||||
|
|
||||||
expect(probot.throttleOptions.Bottleneck).toBe(Bottleneck)
|
expect(probot.throttleOptions.Bottleneck).toBe(Bottleneck)
|
||||||
expect(probot.throttleOptions.connection).toBeInstanceOf(Bottleneck.IORedisConnection)
|
expect(probot.throttleOptions.connection).toBeInstanceOf(Bottleneck.IORedisConnection)
|
||||||
|
@ -344,7 +353,7 @@ describe('Probot', () => {
|
||||||
|
|
||||||
describe('custom Octokit constructor', () => {
|
describe('custom Octokit constructor', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const MyOctokit = Octokit.plugin(octokit => {
|
const MyOctokit = Octokit.plugin((octokit: Octokit & { [key: string]: any}) => {
|
||||||
octokit.foo = 'bar'
|
octokit.foo = 'bar'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -356,7 +365,7 @@ describe('Probot', () => {
|
||||||
|
|
||||||
it('is propagated to GithubAPI', async () => {
|
it('is propagated to GithubAPI', async () => {
|
||||||
const app = probot.load(() => {})
|
const app = probot.load(() => {})
|
||||||
const githubApi = await app.auth()
|
const githubApi: GitHubAPI & { [key: string]: any } = await app.auth()
|
||||||
expect(githubApi.foo).toBe('bar')
|
expect(githubApi.foo).toBe('bar')
|
||||||
})
|
})
|
||||||
})
|
})
|
Loading…
Reference in New Issue