-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(init): fixes an issue where the init command exited with bad cred…
- Loading branch information
1 parent
7058451
commit 5d07d8a
Showing
4 changed files
with
134 additions
and
19 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
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,80 @@ | ||
const octokit = require('@octokit/rest') | ||
const test = require('ava') | ||
const sinon = require('sinon') | ||
|
||
const githubAuth = require('../gh-auth') | ||
|
||
let getAuthenticatedResponse | ||
|
||
const octokitStub = sinon.stub(octokit, 'Octokit').callsFake(() => ({ | ||
rest: { | ||
users: { | ||
getAuthenticated: () => { | ||
if (getAuthenticatedResponse instanceof Error) { | ||
throw getAuthenticatedResponse | ||
} | ||
return Promise.resolve(getAuthenticatedResponse) | ||
}, | ||
}, | ||
}, | ||
})) | ||
|
||
// stub the await ghauth() call for a new token | ||
sinon.stub(githubAuth, 'getGitHubToken').callsFake(() => | ||
Promise.resolve({ | ||
provider: 'github', | ||
token: 'new_token', | ||
user: 'spongebob', | ||
}), | ||
) | ||
|
||
const { getGitHubToken } = require('./config-github') | ||
|
||
// mocked configstore | ||
let globalConfig | ||
|
||
test.beforeEach(() => { | ||
const values = new Map() | ||
globalConfig = { | ||
values: new Map(), | ||
get: (key) => values.get(key), | ||
set: (key, value) => { | ||
values.set(key, value) | ||
}, | ||
} | ||
globalConfig.set('userId', 'spongebob') | ||
globalConfig.set(`users.spongebob.auth.github`, { | ||
provider: 'github', | ||
token: 'old_token', | ||
user: 'spongebob', | ||
}) | ||
}) | ||
|
||
test.serial('should create a octokit client with the provided token if the token is valid', async (t) => { | ||
getAuthenticatedResponse = { status: 200 } | ||
const token = await getGitHubToken({ globalConfig }) | ||
t.is(octokitStub.callCount, 1) | ||
t.deepEqual(octokitStub.getCall(0).args[0], { auth: 'token old_token' }) | ||
t.is(token, 'old_token') | ||
t.deepEqual(globalConfig.get(`users.spongebob.auth.github`), { | ||
provider: 'github', | ||
token: 'old_token', | ||
user: 'spongebob', | ||
}) | ||
octokitStub.resetHistory() | ||
}) | ||
|
||
test.serial('should renew the github token when the provided token is not valid', async (t) => { | ||
getAuthenticatedResponse = new Error('Bad Credentials') | ||
getAuthenticatedResponse.status = 401 | ||
const token = await getGitHubToken({ globalConfig }) | ||
t.is(octokitStub.callCount, 1) | ||
t.is(token, 'new_token') | ||
t.deepEqual(octokitStub.getCall(0).args[0], { auth: 'token old_token' }) | ||
t.deepEqual(globalConfig.get(`users.spongebob.auth.github`), { | ||
provider: 'github', | ||
token: 'new_token', | ||
user: 'spongebob', | ||
}) | ||
octokitStub.resetHistory() | ||
}) |
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
5d07d8a
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.
📊 Benchmark results
Package size: 365 MB