describe('functions_bearer_token', () => { it('should run', async () => { const {sample, mocks} = getSample(); await sample.callingFunction(null, mocks.res); assert(mocks.axios.get.calledTwice); assert.deepEqual(mocks.axios.get.firstCall.args[1], { headers: {'Metadata-Flavor': 'Google'}, }); assert(mocks.res.send.calledWith('function-response')); }); });
it('handles strings', function () { const ui = new UI(); const log = sinon.stub(ui, 'log'); const formatDebug = sinon.stub(ui, '_formatDebug'); ui.error('That\'s a known issue'); expect(formatDebug.calledOnce).to.be.true; expect(log.calledTwice).to.be.true; expect(log.args[0][0]).to.match(/error occured/); expect(stripAnsi(log.args[1][0])).to.match(/Try running ghost doctor to check your system for known issues./); expect(log.args[0][2]).to.be.true; });
it('isSetup accessor works', function () { const hasInstance = sinon.stub(); hasInstance.onFirstCall().returns(true); hasInstance.onSecondCall().returns(false); const instance = new Instance({}, {hasInstance}, '/dir/a'); expect(instance.isSetup).to.be.true; expect(hasInstance.calledOnce).to.be.true; expect(hasInstance.calledWithExactly(instance)).to.be.true; expect(instance.isSetup).to.be.false; expect(hasInstance.calledTwice).to.be.true; });
it('returns false if the current user is ghost', function () { const platformStub = sinon.stub(os, 'platform').returns('linux'); const execaStub = sinon.stub(execa, 'shellSync').returns({stdout: '50'}); const fsStub = sinon.stub(fs, 'lstatSync').returns({uid: 50, gid: 50}); const originalGetuid = process.getuid; process.getuid = sinon.stub().returns(50); const result = ghostUser.shouldUseGhostUser('/some-dir/content'); expect(result).to.be.false; expect(platformStub.calledTwice).to.be.true; expect(execaStub.calledTwice).to.be.true; expect(fsStub.calledOnce).to.be.true; expect(process.getuid.calledOnce).to.be.true; process.getuid = originalGetuid; });
it('returns false if running property not set in config & neither config exists', async function () { const hasStub = sinon.stub().withArgs('running').returns(false); const setEnvironmentStub = sinon.stub(); const testInstance = new Instance({}, {development: true, setEnvironment: setEnvironmentStub}, ''); testInstance._cliConfig = {has: hasStub}; const existsStub = sinon.stub(Config, 'exists').returns(false); const running = await testInstance.isRunning(); expect(running).to.be.false; expect(hasStub.calledOnce).to.be.true; expect(existsStub.calledTwice).to.be.true; expect(setEnvironmentStub.calledOnce).to.be.true; expect(setEnvironmentStub.calledWithExactly(true)).to.be.true; });
it('Removes https config', function () { const sudoExp = new RegExp(/(available|enabled)\/ghost\.dev-ssl\.conf/); const {exists, inst, restartNginx, ui} = stub(); exists.callsFake(testEs); return inst.uninstall(instance).then(() => { expect(ui.sudo.calledTwice).to.be.true; expect(ui.sudo.args[0][0]).to.match(sudoExp); expect(ui.sudo.args[1][0]).to.match(sudoExp); expect(restartNginx.calledOnce).to.be.true; }); });
it('Leaves nginx alone when no config file exists', function () { const {exists, inst, restartNginx} = stub(); exists.returns(false); return inst.uninstall(instance).then(() => { expect(exists.calledTwice).to.be.true; expect(restartNginx.called).to.be.false; }); });
it('Removes http config', function () { const sudoExp = new RegExp(/(available|enabled)\/ghost\.dev\.conf/); const {exists, inst, restartNginx, ui} = stub(); exists.callsFake(val => !testEs(val)); return inst.uninstall(instance).then(() => { expect(ui.sudo.calledTwice).to.be.true; expect(ui.sudo.args[0][0]).to.match(sudoExp); expect(ui.sudo.args[1][0]).to.match(sudoExp); expect(restartNginx.calledOnce).to.be.true; }); });
it('skips if cert has not been generated using the old method', function () { const skip = sinon.stub(); const existsSync = sinon.stub(); existsSync.withArgs('/var/www/ghost/system/files/ghost.org-ssl.conf').returns(true); existsSync.withArgs('/home/ghost/.acme.sh/ghost.org').returns(false); const migrate = proxyquire(modulePath, { 'fs-extra': {existsSync: existsSync}, os: {homedir: () => '/home/ghost'} }); migrate.migrateSSL(context, {skip: skip}); expect(existsSync.calledTwice).to.be.true; expect(existsSync.calledWithExactly('/var/www/ghost/system/files/ghost.org-ssl.conf')).to.be.true; expect(existsSync.calledWithExactly('/home/ghost/.acme.sh/ghost.org')).to.be.true; expect(skip.calledOnce).to.be.true; });
it('opens editor and generates template with contents if choice is edit (with --verbose)', function () { const promptStub = sinon.stub(); promptStub.onCall(0).resolves({choice: 'edit'}); promptStub.onCall(1).resolves({contents: 'some edited contents'}); const testExtension = new Extension({prompt: promptStub, allowPrompt: true, verbose: true}, {}, {}, ''); const generateStub = sinon.stub(testExtension, '_generateTemplate').resolves(true); return testExtension.template({}, 'some contents', 'a file', 'file.txt', '/some/dir').then((result) => { expect(result).to.be.true; expect(promptStub.calledTwice).to.be.true; expect(generateStub.calledOnce).to.be.true; expect(generateStub.args[0][1]).to.equal('some edited contents'); }); });
it('returns false if the ghost owner/group is not the owner of the content folder', function () { const platformStub = sinon.stub(os, 'platform').returns('linux'); const execaStub = sinon.stub(execa, 'shellSync').returns({stdout: '50'}); const fsStub = sinon.stub(fs, 'lstatSync').returns({uid: 30, gid: 30}); const result = ghostUser.shouldUseGhostUser('/some-dir/content'); expect(result).to.be.false; expect(platformStub.calledTwice).to.be.true; expect(execaStub.calledTwice).to.be.true; expect(fsStub.calledOnce).to.be.true; expect(fsStub.args[0][0]).to.equal('/some-dir/content'); });
it('calls reset-failed if service is failed and returns false', function () { const sudo = sinon.stub(); sudo.onFirstCall().rejects(Object.assign(new Error(), {stdout: 'failed'})); sudo.onSecondCall().resolves(); const ext = makeSystemd(null, {sudo}); const expectedCmd = 'systemctl is-active ghost_ghost_org'; const resetCmd = 'systemctl reset-failed ghost_ghost_org'; ext.isRunning().then((result) => { expect(result).to.be.false; expect(sudo.calledTwice).to.be.true; expect(sudo.firstCall.calledWithExactly(expectedCmd)); expect(sudo.secondCall.calledWithExactly(resetCmd)); }); });
it('outputs key if key defined and value is not', async function () { const ConfigCommand = fake(); const log = sinon.stub(); const checkEnvironment = sinon.stub(); const config = new Config('config.json'); const getInstance = sinon.stub().returns({checkEnvironment, config}); config.set('url', 'http://localhost:2368'); const cmd = new ConfigCommand({log}, {getInstance}); await cmd.run({key: 'url'}); expect(checkEnvironment.calledOnce).to.be.true; expect(log.calledOnce).to.be.true; expect(log.args[0][0]).to.equal('http://localhost:2368'); await cmd.run({key: 'nope'}); expect(checkEnvironment.calledTwice).to.be.true; expect(log.calledOnce).to.be.true; });
it('returns false if no ghost user found', function () { const platformStub = sinon.stub(os, 'platform').returns('linux'); const execaStub = sinon.stub(execa, 'shellSync').throws(new Error('no such user')); const result = ghostUser.shouldUseGhostUser(); expect(platformStub.calledTwice).to.be.true; expect(execaStub.calledOnce).to.be.true; expect(result).to.be.false; });
it('returns true if user is not ghost', function () { const platformStub = sinon.stub(os, 'platform').returns('linux'); const execaStub = sinon.stub(execa, 'shellSync').returns({stdout: '50'}); const fsStub = sinon.stub(fs, 'lstatSync').returns({uid: 50, gid: 50}); const originalGetuid = process.getuid; process.getuid = sinon.stub().returns(0); const result = ghostUser.shouldUseGhostUser('/some-dir/content'); expect(result).to.be.true; expect(platformStub.calledTwice).to.be.true; expect(execaStub.calledTwice).to.be.true; expect(fsStub.calledOnce).to.be.true; expect(process.getuid.calledOnce).to.be.true; process.getuid = originalGetuid; });