-
Notifications
You must be signed in to change notification settings - Fork 100
Add dotfiles to user directory #213
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| const fs = require('fs'); | ||
| const os = require('os'); | ||
| const path = require('path'); | ||
|
|
||
| const normalizeFilePath = filePath => { | ||
| if (typeof filePath !== 'string') { | ||
| throw new TypeError(`Expected a string, got ${typeof filePath}`); | ||
| } | ||
|
|
||
| const homeDirectory = os.homedir(); | ||
|
|
||
| if (homeDirectory) { | ||
| return filePath.replace(/^~(?=$|\/|\\)/, homeDirectory); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a path helper that sanitize strings instead of a regex? I always prefer platform agnostic helper/libs for those sort of things |
||
| } | ||
|
|
||
| return filePath; | ||
| }; | ||
|
|
||
| const getDataDirectory = () => { | ||
| const environmentDataDirectory = process.env.FB_MESSENGER_DATA_DIR; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allows the user to specify a custom location for their dotfiles. |
||
| const homeDirectory = os.homedir(); | ||
| const thisDirectory = path.resolve(__dirname, '../'); | ||
|
|
||
| if (environmentDataDirectory) { | ||
| return normalizeFilePath(environmentDataDirectory); | ||
| } | ||
|
|
||
| if (homeDirectory) { | ||
| return homeDirectory; | ||
| } | ||
|
|
||
| return thisDirectory; | ||
| }; | ||
|
|
||
| module.exports = { | ||
| getDataDirectory, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,37 +6,39 @@ const Crypt = require('../crypt.js'); | |
| const Messenger = require('../messenger.js'); | ||
| const Settings = require('../settings.js'); | ||
| const path = require('path'); | ||
| const oldCryptFilename = Crypt.filename; | ||
| const oldCryptFilepath = Crypt.filepath; | ||
|
|
||
| describe('Crypt', () => { | ||
| it('getInstance() should create a new singleton object', () => { | ||
| const crypt = Crypt.getInstance(); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had an issue using It creates a |
||
| expect(crypt).to.not.equal(undefined); | ||
| before(() => { | ||
| const filename = 'test.fbmessenger.enc'; | ||
| Crypt.filename = filename; | ||
| Crypt.filepath = path.resolve('.', filename); | ||
| }); | ||
|
|
||
| it('getInstance() always returns the same instance', () => { | ||
| let crypt = Crypt.getInstance(); | ||
| crypt.password = 'test123_%'; | ||
| crypt = Crypt.getInstance(); | ||
| expect(crypt.password).to.equal('test123_%'); | ||
| afterEach(() => { | ||
| Crypt.data = undefined; | ||
| }); | ||
|
|
||
| after(() => { | ||
| Crypt.filename = oldCryptFilename; | ||
| Crypt.filepath = oldCryptFilepath; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We restore |
||
| }); | ||
|
|
||
| it('encrypt() and decrypt() should return original data', () => { | ||
| const data = "{some: 'things', are: 'not equal'}"; | ||
| const crypt = Crypt.getInstance(); | ||
| const encrypted = crypt.encrypt(data); | ||
| const decrypted = crypt.decrypt(encrypted); | ||
| expect(decrypted).to.equal(data); | ||
| const encrypted = Crypt.encrypt(data); | ||
| Crypt.decrypt(encrypted, (_err, decrypted) => { | ||
| expect(decrypted).to.equal(data); | ||
| }); | ||
| }); | ||
|
|
||
| it('save() and load()', (done) => { | ||
| const crypt = Crypt.getInstance(); | ||
| const file = '.test_kryptonite'; | ||
| const data = JSON.stringify({theTest: 'data', is: 5012344}); | ||
|
|
||
| crypt.filename = file; | ||
| crypt.save(data); | ||
| Crypt.save(data); | ||
|
|
||
| crypt.load((err, result) => { | ||
| Crypt.load((err, result) => { | ||
| expect(result).to.equal(data); | ||
| done(); | ||
| }); | ||
|
|
@@ -48,15 +50,14 @@ describe('Messenger', () => { | |
| let json; | ||
| let messenger; | ||
|
|
||
| it('Load cookie', (done) => { | ||
| // Reset crypt | ||
| const crypt = new Crypt(); | ||
| crypt.filename = '.kryptonite'; | ||
|
|
||
| crypt.load((err, result) => { | ||
| afterEach(() => { | ||
| Crypt.data = undefined; | ||
| }); | ||
|
|
||
| it('Load cookie', (done) => { | ||
| // Reset crypt | ||
| Crypt.load((err, result) => { | ||
| expect(() => {JSON.parse(result);}).not.throw(Error); | ||
|
|
||
| json = JSON.parse(result); | ||
| expect(json.cookie).to.not.equal.undefined; | ||
| expect(json.fb_dtsg).to.not.equal.undefined; | ||
|
|
@@ -65,61 +66,81 @@ describe('Messenger', () => { | |
| }); | ||
| }); | ||
|
|
||
| it('Create Messenger', () => { | ||
| messenger = new Messenger(json.cookie, json.c_user, json.fb_dtsg); | ||
| }); | ||
| describe('when data is loaded from the cookie', () => { | ||
| beforeEach(() => { | ||
| Crypt.load((err, data) => { | ||
| json = JSON.parse(data); | ||
| }); | ||
| }); | ||
|
|
||
| it('Send message', function(done) { | ||
| // Allow more time for network calls | ||
| this.timeout(5000); | ||
| it('Create Messenger', () => { | ||
| messenger = new Messenger(json.cookie, json.c_user, json.fb_dtsg); | ||
| }); | ||
|
|
||
| messenger.sendMessage('ar.alexandre.rose', '731419306', 'Running tests - Send message', done); | ||
| }); | ||
| it('getFriends', function(done) { | ||
| // Set a higher timeout for network calls | ||
| this.timeout(5000); | ||
| messenger.getFriends((_err, friends) => { | ||
| const friendIds = Object.keys(friends); | ||
| expect(friendIds.length).to.be.above(1); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('GetLastMessage', function(done) { | ||
| // Allow more time for network calls | ||
| this.timeout(5000); | ||
| it('Get threads', function (done) { | ||
| // Allow more time for network calls | ||
| this.timeout(5000); | ||
|
|
||
| messenger.getMessages('ar.alexandre.rose', '731419306', 10, (err, messages) => { | ||
| expect(messages.length).is.equal(10); | ||
| done(); | ||
| messenger.getThreads((_err, messages) => { | ||
| expect(messages.length).to.be.above(1); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('Get threads', function(done) { | ||
| // Allow more time for network calls | ||
| this.timeout(5000); | ||
|
|
||
| messenger.getThreads(true, done); | ||
| describe('interacting with friends', () => { | ||
| it('Send message', function (done) { | ||
| // Allow more time for network calls | ||
| this.timeout(5000); | ||
|
|
||
| messenger.getFriends((_, friends) => { | ||
| const myself = friends[json.c_user]; | ||
| messenger.sendMessage(myself.vanity, myself.id, 'Running tests - Send message', done); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }); | ||
| }); | ||
|
|
||
| it('GetLastMessage', function (done) { | ||
| // Set a higher timeout for network calls | ||
| this.timeout(5000); | ||
| messenger.getFriends((_, friends) => { | ||
| const myself = friends[json.c_user]; | ||
|
|
||
| messenger.getMessagesGraphQl(myself.vanity, myself.id, 10, (_err, messages) => { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed this test to use |
||
| expect(messages.length).to.be.equal(10); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Settings', () => { | ||
| it('getInstance() should create a new singleton object', () => { | ||
| const settings = Settings.getInstance(); | ||
| expect(settings).to.not.equal(undefined); | ||
| }); | ||
|
|
||
| it('getInstance() always returns the same instance', () => { | ||
| let settings = Settings.getInstance(); | ||
| settings.filename = 'test123_%'; | ||
| settings = Settings.getInstance(); | ||
| expect(settings.filename).to.equal('test123_%'); | ||
| beforeEach(() => { | ||
| const filename = 'test.fbmessengerrc'; | ||
| Settings.filename = filename; | ||
| Settings.filepath = path.resolve('.', filename); | ||
| }); | ||
|
|
||
| it('save() and load()', (done) => { | ||
| const options = {'lights': 'on', 'engine': 'on', 'fuel_pump': 'on', 'running': true}; | ||
| const settings = Settings.getInstance(); | ||
| const file = '.test_settings'; | ||
| const settings = Settings; | ||
|
|
||
| settings.properties = options; | ||
| settings.filename = file; | ||
| settings.save(); | ||
|
|
||
| settings.load((err, result) => { | ||
| settings.read((err, result) => { | ||
| expect(result).to.deep.equal(options); | ||
| done(); | ||
| }); | ||
|
|
||
| }); | ||
| }); | ||

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.
This expands the home directory if it contains a tilde.
This is copied from the
untildifyjavascript library.