UNPKG

particle-cli

Version:

Simple Node commandline application for working with your Particle devices and using the Particle Cloud

96 lines (75 loc) 2.25 kB
'use strict'; const fs = require('fs'); const proxyquire = require('proxyquire'); const { expect, sinon } = require('../../test/setup'); let api; function ApiClient() { return api; } const settings = { username: 'test' }; const utilities = () => {}; const KeysCommand = proxyquire('./keys', { '../../settings': settings, '../lib/utilities': utilities, '../lib/api-client': ApiClient }); describe('Key Command', () => { let key; let filename; function setupCommand(options = {}) { utilities.deferredChildProcess = sinon.stub().returns(Promise.resolve()); options = Object.assign({ params: {} }, options); key = new KeysCommand(options); key.madeSSL = false; key.platform = 'photon'; api = {}; api.ensureToken = sinon.stub(); api.sendPublicKey = sinon.stub(); api.ready = sinon.stub().returns(true); } // TODO: fill these in it.skip('Can create device key', () => { }); it.skip('Can load device key', () => { }); it.skip('Can save device key', () => { }); it.skip('Can send device key', () => { }); it.skip('Can switch server key', () => { }); it.skip('Can read server address from key', () => { }); describe('send key to server', () => { // This test fails because of mock-fs used in another part of the tests // Just skip it for now it.skip('lowercases the device ID and removes the file argument', () => { const deviceID = 'deadBEEF'; setupCommand(); filename = key.serverKeyFilename({ alg: 'rsa' }); let tempfile; utilities.deferredChildProcess = sinon.spy((cmd) => { const args = cmd.split(' '); tempfile = args[args.length - 1]; fs.writeFileSync(tempfile, ''); return Promise.resolve(); }); return Promise.resolve(key.sendPublicKeyToServer(deviceID, filename, {})) .then(() => { expect(api.sendPublicKey).has.been.calledWith(deviceID.toLowerCase(), new Buffer([]), 'rsa'); }) .finally(() => { if (tempfile) { // the file should be removed expect(fs.existsSync(tempfile)).to.be.eql(false); } }); }); }); describe('serverKeyFilename', () => { before(setupCommand); it('returns name with algorithm', () => { expect(key.serverKeyFilename({ alg: 'ec' })).to.match(/ec.pub.der$/); }); }); });