UNPKG

datafire

Version:

[![Travis][travis-image]][travis-link] [![Downloads][downloads-image]][npm-link] [![NPM version][npm-image]][npm-link] [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://www.npmjs.com/package/datafire) <!--[![Dependency status][deps-i

143 lines (136 loc) 3.44 kB
"use strict"; var expect = require('chai').expect; var datafire = require('../entry'); var requestCB = require('request'); var request = function request(opts) { return new Promise(function (resolve, reject) { requestCB(opts, function (err, resp, body) { if (err) return reject(err); resolve({ response: resp, body: body }); }); }); }; var integrationOpenAPI = { schemes: ['http'], host: 'localhost:3333', info: { version: '1' }, securityDefinitions: { oauth: { type: 'oauth2', authorizationUrl: 'http://localhost:3333/authorize', tokenUrl: 'http://localhost:3333/token', flow: 'implicit' } }, paths: { '/401': { get: { operationId: 'maybe401', responses: { '200': { description: 'OK' } } } } } }; var integration = datafire.Integration.fromOpenAPI(integrationOpenAPI, 'test_integration'); var latestError = null; var latestRefresh = null; var VALID_ACCESS_TOKEN = 'abcde'; var project = new datafire.Project({ accounts: { fluffy: { access_token: 'invalid', refresh_token: 'refresh', integration: 'test_integration' } }, paths: { '/error': { get: { action: { handler: function handler(input) { throw new Error('testing'); } } } }, '/oauth_refresh': { get: { action: { handler: function handler(input, context) { context.accounts.test_integration = context.accounts.fluffy; return integration.actions.maybe401(input, context); } } } }, '/401': { get: { action: { handler: function handler(input, context) { if (context.request.headers['authorization'] === 'Bearer ' + VALID_ACCESS_TOKEN) { return "OK"; } else { return new datafire.Response({ statusCode: 401 }); } } } } }, '/token': { post: { action: { handler: function handler(input) { return { access_token: VALID_ACCESS_TOKEN }; } } } } }, events: { error: { action: { handler: function handler(input) { return latestError = input; } } }, oauth_refresh: { action: { handler: function handler(input) { return latestRefresh = input; } } } } }); describe("Event Handlers", function () { before(function () { return project.startServer(3333); }); after(function () { return project.server.close(); }); it('should respect error handler', function () { return request({ method: 'get', url: 'http://localhost:3333/error' }).then(function (result) { expect(result.response.statusCode).to.equal(500); expect(latestError).to.not.equal(null); expect(latestError.error.message).to.equal('testing'); }); }); it('should respect oauth_refresh handler', function () { return request({ method: 'get', url: 'http://localhost:3333/oauth_refresh' }).then(function (result) { expect(result.response.statusCode).to.equal(200); expect(latestRefresh).to.not.equal(null); expect(latestRefresh.alias).to.equal('fluffy'); expect(latestRefresh.account.access_token).to.equal(VALID_ACCESS_TOKEN); }); }); });