Files
probot/test/private-key.test.ts
Brandon Keepers f6946a0607 Probot Support for GitHub App Manifests (#650)
* Add button to setup GitHub app

* Add app.yml

* Add some comments to app.yml

* Associate events with permissions

* Allow configuring app with manifest

* Hacky version of callback URL for configuring app

* Remove manifest stuff for now

* Return nil if private key is not found

* Move setup stuff to a separate plugin

* Revert changes to default plugin

* Remove FIXME

* Revert changes to default template

* Use separate template for setup

* Fix lint warnings

* Convert test helper to typescript

* Initial test for setup app

* Require tests files to match `.test.(js|ts)`

* Account for multiple protocols in x-forwarded-proto

* Wrap pem in quotes

* Collapse class into request method for now

* run `refresh` after updating .env on Glitch

* Extract update-dotenv to a node module

* Create a smee url if one does not exist

* Hacky version of serving up the manifest

* WIP Manifest with code

* add comments for plan + figure out port

* using user-agent header for review lab

* add success view to redirect to after installation

* api is actually on github

* start making post

* fix quoting issue on POST

* everything is off review lab and on dotcom

* working version of app manifest flow

* Start trying to write tests against setup

* more refactor into thingerator; basic tests; write plans for other tests

* merge better..

* ok atom conflict handling broke

* hack the tests back together

* pass the tests 👊🏼

* moar test

* make it open in a new tab for Wil 💖

* make boolean work

* clean up logic, move messgae, just return html url not response

* clean up tests 👷🏾‍♀️

* rename Brandon's thingerator to manifest-creation
2018-09-26 15:22:11 -04:00

143 lines
3.9 KiB
TypeScript

import fs from 'fs'
const readFileSync = fs.readFileSync
const readdirSync = fs.readdirSync
import { findPrivateKey } from '../src/private-key'
describe('private-key', () => {
let privateKey: string
let keyfilePath: string
beforeEach(() => {
privateKey = '-----BEGIN RSA PRIVATE KEY-----\nTHIs+is+A+Fak3+K3y\n-----END RSA PRIVATE KEY-----'
keyfilePath = '/some/path'
fs.readFileSync = jest.fn().mockReturnValue(privateKey)
})
afterEach(() => {
fs.readFileSync = readFileSync
})
describe('findPrivateKey(undefined)', () => {
describe('when a filepath is provided', () => {
it('should read the file at given filepath', () => {
findPrivateKey(keyfilePath)
expect(fs.readFileSync).toHaveBeenCalledWith(keyfilePath)
})
it('should return the key', () => {
expect(findPrivateKey(keyfilePath)).toEqual(privateKey)
})
})
describe('when a PRIVATE_KEY env var is provided', () => {
beforeEach(() => {
process.env.PRIVATE_KEY = privateKey
})
afterEach(() => {
delete process.env.PRIVATE_KEY
})
it('should return the key', () => {
process.env.PRIVATE_KEY = privateKey
expect(findPrivateKey(undefined)).toEqual(privateKey)
})
})
describe('when a PRIVATE_KEY has line breaks', () => {
beforeEach(() => {
process.env.PRIVATE_KEY = '-----BEGIN RSA PRIVATE KEY-----\\nTHIs+is+A+Fak3+K3y\\n-----END RSA PRIVATE KEY-----'
})
afterEach(() => {
delete process.env.PRIVATE_KEY
})
it('should return the key', () => {
expect(findPrivateKey(undefined)).toEqual(privateKey)
})
})
describe('when a PRIVATE_KEY is base64 encoded', () => {
beforeEach(() => {
process.env.PRIVATE_KEY = Buffer.from(privateKey).toString('base64')
})
afterEach(() => {
delete process.env.PRIVATE_KEY
})
it('should decode and return the key', () => {
expect(findPrivateKey(undefined)).toEqual(privateKey)
})
})
describe('when a PRIVATE_KEY_PATH env var is provided', () => {
beforeEach(() => {
process.env.PRIVATE_KEY_PATH = keyfilePath
})
afterEach(() => {
delete process.env.PRIVATE_KEY_PATH
})
it('should read the file at given filepath', () => {
findPrivateKey(undefined)
expect(fs.readFileSync).toHaveBeenCalledWith(keyfilePath)
})
it('should return the key', () => {
expect(findPrivateKey(undefined)).toEqual(privateKey)
})
})
describe('when no private key is provided', () => {
beforeEach(() => {
fs.readdirSync = jest.fn().mockReturnValue([
'foo.txt',
'foo.pem'
])
})
it('should look for one in the current directory', () => {
findPrivateKey(undefined)
expect(fs.readdirSync).toHaveBeenCalledWith(process.cwd())
})
describe('and several key files are present', () => {
beforeEach(() => {
fs.readdirSync = jest.fn().mockReturnValue([
'foo.txt',
'foo.pem',
'bar.pem'
])
})
it('should throw an error', () => {
expect(findPrivateKey).toThrow(/Found several private keys: foo.pem, bar.pem/i)
})
})
describe('and a key file is present', () => {
it('should load the key file', () => {
findPrivateKey(undefined)
expect(fs.readFileSync).toHaveBeenCalledWith('foo.pem')
})
})
describe('and a key file is not present', () => {
beforeEach(() => {
fs.readdirSync = readdirSync
})
it('should return null', () => {
expect(findPrivateKey()).toBe(null)
})
})
})
})
})
// https://stackoverflow.com/questions/30734509/how-to-pass-optional-parameters-in-typescript-while-omitting-some-other-optional wtf