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')); }); });
test('#initConnection :: add event hooks', t => { const connection = { on: stub(), write: stub() } const err = { on: stub() } p2p.initConnection(connection) t.deepEqual(p2p.peers, [connection]) t.is(connection.on.callCount, 2) t.is(connection.on.firstCall.args[0], 'data') t.is(connection.on.secondCall.args[0], 'error') })
test('#initConnection :: write event', t => { const connection = { on: stub(), write: stub() } const payload = JSON.stringify(messages.getQueryChainLengthMsg()) p2p.initConnection(connection) t.is(connection.write.callCount, 1) t.is(connection.write.firstCall.args[0], payload) })
it('passes an empty payload to the handler if none sent at dispatch', function(done) { const handler = sinon.spy(); const burns = require('../../src/burns'); burns.registerEvents({ eventZ: handler }); burns.dispatch('eventZ'); setTimeout(() => { expect(handler.calledOnce).to.equal(true); expect(handler.getCall(0).args[0]).to.deep.equal({}); done(); }, 0); });
it('defaults HTTP server to port 80 if neither `http` or `https`', async () => { gasketAPI.config = {}; await start(); const createServerOpts = createServersModule.lastCall.args[0]; assume(createServerOpts).property('http', 80); assume(createServerOpts).not.property('https'); });
it('http:helloHttp: should handle GET', () => { const mocks = getMocks(); const httpSample = getSample(); mocks.req.method = 'GET'; httpSample.sample.helloHttp(mocks.req, mocks.res); assert.strictEqual(mocks.res.status.calledOnce, true); assert.strictEqual(mocks.res.status.firstCall.args[0], 200); assert.strictEqual(mocks.res.send.calledOnce, true); assert.strictEqual(mocks.res.send.firstCall.args[0], 'Hello World!'); });
it('outputs message to proper stream', function () { const stdout = {write: sinon.stub()}; const stderr = {write: sinon.stub()}; const ui = new UI({stdout: stdout, stderr: stderr}); ui.log('Error', null, true); ui.log('Good', null, false); expect(stdout.write.calledOnce).to.be.true; expect(stderr.write.calledOnce).to.be.true; expect(stderr.write.firstCall.args[0]).to.equal('Error\n'); expect(stdout.write.firstCall.args[0]).to.equal('Good\n'); });
it('should write to log', () => { const expectedMsg = 'I am a log entry!'; const res = {end: sinon.stub()}; getSample().program.helloWorld({}, res); assert.strictEqual(console.log.callCount, 1); assert.deepStrictEqual(console.log.firstCall.args, [expectedMsg]); assert.strictEqual(res.end.callCount, 1); assert.deepStrictEqual(res.end.firstCall.args, []); });
it('should handle get error', () => { const error = new Error('error'); const sample = getSample(); const callback = sinon.stub(); sample.mocks.storagetransfer.transferJobs.get.yields(error); sample.program.getTransferJob(jobName, callback); assert.strictEqual(callback.calledOnce, true); assert.deepStrictEqual(callback.firstCall.args, [error]); });
describe('.log', function () { it('proxies to winston.log with predefined level', async function () { const stub = sinon.stub(log.winston, 'log'); assume(log.log).to.be.a('function'); log.log('Testing'); assume(stub.getCall(0).args).to.deep.equal(['debug', 'Testing']); }); });
test('user resolver returns a user by its unique identifier', t => { const exec = sinon.stub(); const id = 'id-123'; const user = {_id: 'id-123'}; const findUserById = UserModel.findById = sinon.stub().returns({exec}); Query.user(null, {id}).then(result => { t.is(findUserById.lastCall.args[0], 'id-123'); t.is(result, user); t.end(); }); exec.callArgWith(0, null, user); });
test('author resolver fetches the user', t => { const exec = sinon.stub(); const user = {id: 1, name: 'Foo'}; const findUser = UserModel.findById = sinon.stub().returns({exec}); const p = Post.author({authorId: 1}).then(result => { t.is(result, user); t.is(findUser.lastCall.args[0], 1); }); exec.callArgWith(0, null, user); return p; });
it('should handle list error', () => { const error = new Error('error'); const sample = getSample(); const callback = sinon.stub(); sample.mocks.storagetransfer.transferJobs.list.yields(error); sample.program.listTransferJobs(callback); assert.strictEqual(callback.calledOnce, true); assert.deepStrictEqual(callback.firstCall.args, [error]); });
it('defaults HTTP server to port 8080 if env is local', async () => { gasketAPI.config = { env: 'local' }; await start(); const createServerOpts = createServersModule.lastCall.args[0]; assume(createServerOpts).property('http', 8080); assume(createServerOpts).not.property('https'); });
it('http:helloHttp: should handle PUT', () => { const mocks = getMocks(); const httpSample = getSample(); mocks.req.method = 'PUT'; httpSample.sample.helloHttp(mocks.req, mocks.res); assert.strictEqual(mocks.res.status.calledOnce, true); assert.strictEqual(mocks.res.status.firstCall.args[0], 403); assert.strictEqual(mocks.res.send.calledOnce, true); assert.strictEqual(mocks.res.send.firstCall.args[0], 'Forbidden!'); });