-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix and test the github admin route #1886
Merged
paulmelnikow
merged 5 commits into
badges:master
from
paulmelnikow:fix-github-admin-route
Aug 11, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
694dadf
Fix and test the github admin route
paulmelnikow f4808e3
Make this work when no shieldsSecret is defined
paulmelnikow a2056d0
Fix a second production crash from #1813
paulmelnikow df413c6
Fix undef
paulmelnikow 865f4d3
Fix require
paulmelnikow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const Camp = require('camp') | ||
const fetch = require('node-fetch') | ||
const config = require('../../../lib/test-config') | ||
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() | ||
}) | ||
|
||
const baseUrl = `http://127.0.0.1:${config.port}` | ||
|
||
let camp | ||
before(function(done) { | ||
camp = Camp.start({ port: config.port, hostname: '::' }) | ||
camp.on('listening', () => done()) | ||
}) | ||
after(function(done) { | ||
if (camp) { | ||
camp.close(() => done()) | ||
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."') | ||
// }) | ||
// }) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit annoying this must be done manually. We may want to consider a different request library like got or axios.