CartoDB-SQL-API/test/unit/pubsub-metrics-test.js
Esther Lozano 419adea234
Add pubsub metrics (#642)
* Create middleware and service for pubsub metrics

* Use pubsub middleware for all request

* Replace isEnabled with isDisabled to avoid negation

* Use new headers names

* Add acceptance and unit tests

* Remove commented log calls

* Remove only filters in tests

Remove typo

* Refactor service to ease integration test

* Fix interaction with query controller

* Use middleware at api-router and test all controllers

* Rename user middleware function

* Use sinon latest version

* Create middleware and service for pubsub metrics

* Use pubsub middleware for all request

* Replace isEnabled with isDisabled to avoid negation

* Use new headers names

* Add acceptance and unit tests

* Remove commented log calls

* Remove only filters in tests

Remove typo

* Refactor service to ease integration test

* Fix interaction with query controller

* Use middleware at api-router and test all controllers

* Rename user middleware function

* Use sinon latest version

* Fix tests

* Fix typos

* Checks if pubsub config exists to enable the service

* Fix typo

* Normalize headers values for pubsub

* Trim fields when normalizing

* Trim fields when normalizing
2020-02-26 17:19:06 +01:00

43 lines
1.2 KiB
JavaScript

'use strict';
require('../helper');
const sinon = require('sinon');
const assert = require('../support/assert');
const PubSubMetricsService = require('../../lib/services/pubsub-metrics');
const fakeTopic = {
name: 'test-topic',
publish: sinon.stub().returns(Promise.resolve())
};
const fakePubSub = {
topic: () => fakeTopic
};
const eventAttributes = {
event_source: 'test',
user_id: '123',
event_group_id: '1',
response_code: '200',
source_domain: 'vizzuality.cartodb.com',
event_time: new Date().toISOString(),
event_version: '1'
};
describe('pubsub metrics service', function () {
it('should not send event if not enabled', function () {
const pubSubMetricsService = new PubSubMetricsService(fakePubSub, false);
pubSubMetricsService.sendEvent('test-event', eventAttributes);
assert(fakeTopic.publish.notCalled);
});
it('should send event if enabled', function () {
const pubSubMetricsService = new PubSubMetricsService(fakePubSub, true);
pubSubMetricsService.sendEvent('test-event', eventAttributes);
assert(fakeTopic.publish.calledOnceWith(Buffer.from('test-event'), eventAttributes));
});
});