probot/test/private-key.test.ts

160 lines
4.5 KiB
TypeScript
Raw Normal View History

import fs from 'fs'
const readFileSync = fs.readFileSync
const existsSync = fs.existsSync
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-----'
2017-08-23 11:06:31 +08:00
keyfilePath = '/some/path'
fs.readFileSync = jest.fn().mockReturnValue(privateKey)
2017-08-23 11:06:31 +08:00
})
afterEach(() => {
fs.readFileSync = readFileSync
2017-08-23 11:06:31 +08:00
})
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-27 03:22:11 +08:00
describe('findPrivateKey(undefined)', () => {
describe('when a filepath is provided', () => {
it('should read the file at given filepath', () => {
2017-08-23 11:06:31 +08:00
findPrivateKey(keyfilePath)
2019-02-21 00:58:10 +08:00
expect(fs.readFileSync).toHaveBeenCalledWith(keyfilePath, 'utf8')
2017-08-23 11:06:31 +08:00
})
it('should return the key', () => {
expect(findPrivateKey(keyfilePath)).toEqual(privateKey)
2017-08-23 11:06:31 +08:00
})
})
describe('when a PRIVATE_KEY env var is provided', () => {
beforeEach(() => {
2017-08-23 11:06:31 +08:00
process.env.PRIVATE_KEY = privateKey
})
afterEach(() => {
2017-08-23 11:06:31 +08:00
delete process.env.PRIVATE_KEY
})
it('should return the key', () => {
process.env.PRIVATE_KEY = privateKey
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-27 03:22:11 +08:00
expect(findPrivateKey(undefined)).toEqual(privateKey)
2017-08-23 11:06:31 +08:00
})
})
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-----'
2017-08-23 11:06:31 +08:00
})
afterEach(() => {
2017-08-23 11:06:31 +08:00
delete process.env.PRIVATE_KEY
})
it('should return the key', () => {
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-27 03:22:11 +08:00
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', () => {
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-27 03:22:11 +08:00
expect(findPrivateKey(undefined)).toEqual(privateKey)
2017-08-23 11:06:31 +08:00
})
})
describe('when a PRIVATE_KEY_PATH env var is provided', () => {
beforeEach(() => {
2017-08-23 11:06:31 +08:00
process.env.PRIVATE_KEY_PATH = keyfilePath
fs.existsSync = jest.fn().mockReturnValue(true)
2017-08-23 11:06:31 +08:00
})
afterEach(() => {
2017-08-23 11:06:31 +08:00
delete process.env.PRIVATE_KEY_PATH
fs.existsSync = existsSync
2017-08-23 11:06:31 +08:00
})
it('should read the file at given filepath', () => {
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-27 03:22:11 +08:00
findPrivateKey(undefined)
expect(fs.readFileSync).toHaveBeenCalledWith(keyfilePath, 'utf8')
2017-08-23 11:06:31 +08:00
})
it('should return the key', () => {
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-27 03:22:11 +08:00
expect(findPrivateKey(undefined)).toEqual(privateKey)
2017-08-23 11:06:31 +08:00
})
})
describe('when a PRIVATE_KEY_PATH env var is provided but file is not present at that path', () => {
beforeEach(() => {
process.env.PRIVATE_KEY_PATH = keyfilePath
})
afterEach(() => {
delete process.env.PRIVATE_KEY_PATH
})
it('should throw an error', () => {
expect(findPrivateKey).toThrow(`Private key does not exists at path: ${keyfilePath}. Please check to ensure that the PRIVATE_KEY_PATH is correct.`)
})
})
describe('when no private key is provided', () => {
beforeEach(() => {
fs.readdirSync = jest.fn().mockReturnValue([
'foo.txt',
'foo.pem'
])
2017-08-23 11:06:31 +08:00
})
it('should look for one in the current directory', () => {
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-27 03:22:11 +08:00
findPrivateKey(undefined)
expect(fs.readdirSync).toHaveBeenCalledWith(process.cwd())
2017-08-23 11:06:31 +08:00
})
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', () => {
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-27 03:22:11 +08:00
findPrivateKey(undefined)
2019-02-21 00:58:10 +08:00
expect(fs.readFileSync).toHaveBeenCalledWith('foo.pem', 'utf8')
2017-08-23 11:06:31 +08:00
})
})
describe('and a key file is not present', () => {
beforeEach(() => {
fs.readdirSync = readdirSync
2017-08-23 11:06:31 +08:00
})
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-27 03:22:11 +08:00
it('should return null', () => {
expect(findPrivateKey()).toBe(null)
2017-08-23 11:06:31 +08:00
})
})
})
})
})
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-27 03:22:11 +08:00
// https://stackoverflow.com/questions/30734509/how-to-pass-optional-parameters-in-typescript-while-omitting-some-other-optional wtf