Files
shields/services/github/auth/admin.spec.js
Paul Melnikow 5c665a70da Overhaul initialization pattern for server + server tests (#2519)
Because `server.js` was long a monolith, there are a bunch of shims in place to facilitate unit testing. A few of the test suites share port 1111 which means if one of them fails to set up, the port won't be freed and other unrelated tests will fail. Some of the tests which trigger server setup include timeouts which were added to give setup code time to run. In one the test suites, we actually modify `process.argv`, which seems completely gross.

This implements a few changes which improve this:

1. Separate the server from the server startup script, splitting out `lib/server.js`.
2. Inject config into the server and validate the config schema.
3. Inject config into the service test runner.
4. Use `portfinder`, a popular utility for grabbing open ports during testing.
5. Switch more of the setup code from callbacks to async-await.

Overall it leaves everything acting more reliably and looking rather cleaner, if in a few places more verbose.

It also fixes the root cause of #1455, a `setTimeout` in `rate-limit`. Off and on during development of this changeset, Mocha would decide not to exit, and that turned out to be the culprit.

Fix #1455
2018-12-23 11:24:22 -05:00

81 lines
2.3 KiB
JavaScript

'use strict'
const { expect } = require('chai')
const sinon = require('sinon')
const Camp = require('camp')
const fetch = require('node-fetch')
const portfinder = require('portfinder')
const serverSecrets = require('../../../lib/server-secrets')
const { setRoutes } = require('./admin')
function createAuthHeader({ username, password }) {
const headers = new fetch.Headers()
const encoded = Buffer.from(`${username}:${password}`).toString('base64')
headers.append('authorization', `Basic ${encoded}`)
return headers
}
describe('GitHub admin route', function() {
const validCredentials = {
username: '',
password: '7'.repeat(40),
}
let sandbox
beforeEach(function() {
sandbox = sinon.createSandbox()
// Make this work when there is no `shieldsSecret` defined.
serverSecrets.shieldsSecret = undefined
sandbox
.stub(serverSecrets, 'shieldsSecret')
.value(validCredentials.password)
})
afterEach(function() {
sandbox.restore()
})
let port, baseUrl
before(async function() {
port = await portfinder.getPortPromise()
baseUrl = `http://127.0.0.1:${port}`
})
let camp
before(async function() {
camp = Camp.start({ port, hostname: '::' })
await new Promise(resolve => camp.on('listening', () => resolve()))
})
after(async function() {
if (camp) {
await new Promise(resolve => camp.close(resolve))
camp = undefined
}
})
before(function() {
setRoutes(camp)
})
context('the password is correct', function() {
it('returns a valid JSON response', async function() {
const res = await fetch(`${baseUrl}/$github-auth/tokens`, {
headers: createAuthHeader(validCredentials),
})
expect(res.ok).to.be.true
expect(await res.json()).to.be.ok
})
})
// Disabled because this code isn't modified often and the test is very
// slow. I wasn't able to make this work with fake timers:
// https://github.com/sinonjs/sinon/issues/1739
// context('the password is missing', function() {
// it('returns the expected message', async function() {
// this.timeout(11000)
// const res = await fetch(`${baseUrl}/$github-auth/tokens`)
// expect(res.ok).to.be.true
// expect(await res.text()).to.equal('"Invalid secret."')
// })
// })
})