-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and test [github] token persistence (#1863)
- Loading branch information
1 parent
39d3930
commit 9007658
Showing
10 changed files
with
213 additions
and
60 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
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,45 @@ | ||
'use strict' | ||
|
||
const autosave = require('json-autosave') | ||
const githubAuth = require('./github-auth') | ||
|
||
// This is currently bound to the legacy github auth code. That will be | ||
// replaced with a dependency-injected token provider. | ||
class TokenPersistence { | ||
constructor({ path }) { | ||
this.path = path | ||
this.save = undefined | ||
} | ||
|
||
async initialize() { | ||
// This code is a bit difficult to understand, in part because it's | ||
// working against the interface of `json-autosave` which wants to own the | ||
// data structure. | ||
const save = await autosave(this.path, { data: [] }) | ||
this.save = save | ||
|
||
save.data.forEach(tokenString => { | ||
githubAuth.addGithubToken(tokenString) | ||
}) | ||
|
||
// Override the autosave handler to refresh the token data before | ||
// saving. | ||
save.autosave = () => { | ||
save.data = githubAuth.getAllTokenIds() | ||
return save.save() | ||
} | ||
// Put the change in autosave handler into effect. | ||
save.stop() | ||
save.start() | ||
} | ||
|
||
async stop() { | ||
if (this.save) { | ||
this.save.stop() | ||
await this.save.autosave() | ||
this.save = undefined | ||
} | ||
} | ||
} | ||
|
||
module.exports = TokenPersistence |
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,93 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const tmp = require('tmp') | ||
const readFile = require('fs-readfile-promise') | ||
const sinon = require('sinon') | ||
const { sleep } = require('wait-promise') | ||
const { expect } = require('chai') | ||
const TokenPersistence = require('./token-persistence') | ||
const githubAuth = require('./github-auth') | ||
|
||
describe('Token persistence', function() { | ||
// Fake timers must be set up before any timers are scheduled. | ||
let clock | ||
beforeEach(function() { | ||
clock = sinon.useFakeTimers() | ||
}) | ||
afterEach(function() { | ||
clock.restore() | ||
}) | ||
|
||
beforeEach(githubAuth.removeAllTokens) | ||
afterEach(githubAuth.removeAllTokens) | ||
|
||
let path, persistence | ||
beforeEach(function() { | ||
path = tmp.tmpNameSync() | ||
persistence = new TokenPersistence({ path }) | ||
}) | ||
afterEach(function() { | ||
if (persistence) { | ||
persistence.stop() | ||
persistence = null | ||
} | ||
}) | ||
|
||
context('when the file does not exist', function() { | ||
it('creates it with an empty array', async function() { | ||
await persistence.initialize() | ||
const json = JSON.parse(await readFile(path)) | ||
|
||
expect(json).to.deep.equal([]) | ||
}) | ||
}) | ||
|
||
context('when the file exists', function() { | ||
it('loads the contents', async function() { | ||
const initialTokens = ['a', 'b', 'c'].map(char => char.repeat(40)) | ||
fs.writeFileSync(path, JSON.stringify(initialTokens)) | ||
|
||
await persistence.initialize() | ||
|
||
expect(githubAuth.getAllTokenIds()).to.deep.equal(initialTokens) | ||
}) | ||
}) | ||
|
||
context('when shutting down', function() { | ||
it('writes added tokens to the file', async function() { | ||
const initialTokens = ['a', 'b', 'c'].map(char => char.repeat(40)) | ||
fs.writeFileSync(path, JSON.stringify(initialTokens)) | ||
|
||
const newToken = 'e'.repeat(40) | ||
const expected = initialTokens.slice() | ||
expected.push(newToken) | ||
|
||
await persistence.initialize() | ||
githubAuth.addGithubToken(newToken) | ||
await persistence.stop() | ||
|
||
const json = JSON.parse(await readFile(path)) | ||
expect(json).to.deep.equal(expected) | ||
}) | ||
}) | ||
|
||
context('time has elapsed', function() { | ||
it('writes added tokens to the file', async function() { | ||
const addedTokens = ['d', 'e'].map(char => char.repeat(40)) | ||
|
||
await persistence.initialize() | ||
|
||
addedTokens.forEach(githubAuth.addGithubToken) | ||
|
||
// Fake time passing to trigger autosaving, then give the save a brief | ||
// moment of real time to complete before reading. | ||
clock.tick(5000) | ||
clock.restore() | ||
await sleep(200) | ||
|
||
const json = JSON.parse(await readFile(path)) | ||
expect(json).to.deep.equal(addedTokens) | ||
}) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.