-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: show more meaningful error message for failed auth
- Loading branch information
1 parent
3585f53
commit 79e5e47
Showing
2 changed files
with
108 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as _ from 'lodash'; | ||
import { test } from 'tap'; | ||
import * as sinon from 'sinon'; | ||
import * as isAuthed from '../../src/cli/commands/auth/is-authed'; | ||
import stripAnsi from 'strip-ansi'; | ||
const port = process.env.PORT || process.env.SNYK_PORT || '12345'; | ||
|
||
const apiKey = '123456789'; | ||
const notAuthorizedApiKey = 'notAuthorized'; | ||
process.env.SNYK_API = 'http://localhost:' + port + '/api/v1'; | ||
process.env.SNYK_HOST = 'http://localhost:' + port; | ||
process.env.LOG_LEVEL = '0'; | ||
|
||
// tslint:disable-next-line:no-var-requires | ||
const server = require('../cli-server')( | ||
process.env.SNYK_API, | ||
apiKey, | ||
notAuthorizedApiKey, | ||
); | ||
|
||
// ensure this is required *after* the demo server, since this will | ||
// configure our fake configuration too | ||
import * as cli from '../../src/cli/commands'; | ||
|
||
test('setup', (t) => { | ||
t.plan(1); | ||
|
||
server.listen(port, () => { | ||
t.pass('started demo server'); | ||
}); | ||
}); | ||
|
||
test('auth shows an appropriate error message when a request times out', async (t) => { | ||
const errors = require('../../src/lib/errors/legacy-errors'); | ||
const failedReq = new Promise((resolve) => { | ||
return resolve({ res: { statusCode: 502 } }); | ||
}); | ||
const verifyStub = sinon.stub(isAuthed, 'verifyAPI').returns(failedReq); | ||
|
||
t.teardown(() => { | ||
verifyStub.restore(); | ||
}); | ||
|
||
try { | ||
await cli.auth(apiKey); | ||
t.fail('Authentication should have failed'); | ||
} catch (e) { | ||
const message = stripAnsi(errors.message(e)); | ||
t.ok(message.includes('request has timed out'), 'correct error message'); | ||
} | ||
}); | ||
|
||
test('auth shows an appropriate error message when a request fails with a user message', async (t) => { | ||
const errors = require('../../src/lib/errors/legacy-errors'); | ||
const userMessage = 'Oh no! The request failed'; | ||
const failedReq = new Promise((resolve) => { | ||
return resolve({ res: { statusCode: 502, body: { userMessage } } }); | ||
}); | ||
const verifyStub = sinon.stub(isAuthed, 'verifyAPI').returns(failedReq); | ||
|
||
t.teardown(() => { | ||
verifyStub.restore(); | ||
}); | ||
|
||
try { | ||
await cli.auth(apiKey); | ||
t.fail('Authentication should have failed'); | ||
} catch (e) { | ||
const message = stripAnsi(errors.message(e)); | ||
t.equal(message, userMessage, 'userMessage shown on auth failure'); | ||
} | ||
}); | ||
|
||
test('teardown', (t) => { | ||
t.plan(2); | ||
|
||
delete process.env.SNYK_API; | ||
delete process.env.SNYK_HOST; | ||
delete process.env.SNYK_PORT; | ||
t.notOk(process.env.SNYK_PORT, 'fake env values cleared'); | ||
|
||
server.close(() => { | ||
t.pass('server shutdown'); | ||
}); | ||
}); |