Windshaft-cartodb/test/integration/pubsub-metrics-test.js
2020-02-25 14:14:44 +01:00

139 lines
3.9 KiB
JavaScript

'use strict';
const sinon = require('sinon');
const assert = require('assert');
const redis = require('redis');
const TestClient = require('../support/test-client');
const PubSubMetricsBackend = require('../../lib/backends/pubsub-metrics');
const metricsHeaders = {
'Carto-Event': 'test-event',
'Carto-Event-Source': 'test',
'Carto-Event-Group-Id': '1'
};
const mapConfig = {
version: '1.7.0',
layers: [
{
options: {
sql: 'select * FROM test_table_localhost_regular1',
cartocss: TestClient.CARTOCSS.POINTS,
cartocss_version: '2.3.0'
}
}
]
};
function buildEventAttributes (statusCode) {
return {
event_source: 'test',
user_id: '1',
event_group_id: '1',
response_code: statusCode.toString(),
source_domain: 'localhost',
event_time: new Date().toISOString(),
event_version: '1'
};
}
const fakeTopic = {
name: 'test-topic',
publish: sinon.stub().returns(Promise.resolve())
};
const fakePubSub = {
topic: () => fakeTopic
};
describe('pubsub metrics middleware', function () {
let redisClient;
let testClient;
let clock;
before(function () {
redisClient = redis.createClient(global.environment.redis.port);
clock = sinon.useFakeTimers();
sinon.stub(PubSubMetricsBackend, 'createPubSub').returns(fakePubSub);
});
after(function () {
clock.restore();
PubSubMetricsBackend.createPubSub.restore();
global.environment.pubSubMetrics.enabled = false;
});
afterEach(function (done) {
fakeTopic.publish.resetHistory();
redisClient.SELECT(0, () => {
redisClient.del('user:localhost:mapviews:global');
redisClient.SELECT(5, () => {
redisClient.del('user:localhost:mapviews:global');
done();
});
});
});
it('should not send event if not enabled', function (done) {
global.environment.pubSubMetrics.enabled = false;
testClient = new TestClient(mapConfig, 1234, metricsHeaders);
testClient.getLayergroup((err, body) => {
if (err) {
return done(err);
}
assert.strictEqual(typeof body.metadata, 'object');
assert(fakeTopic.publish.notCalled);
return done();
});
});
it('should not send event if headers not present', function (done) {
global.environment.pubSubMetrics.enabled = true;
testClient = new TestClient(mapConfig, 1234);
testClient.getLayergroup((err, body) => {
if (err) {
return done(err);
}
assert.strictEqual(typeof body.metadata, 'object');
assert(fakeTopic.publish.notCalled);
return done();
});
});
it('should send event for map requests', function (done) {
global.environment.pubSubMetrics.enabled = true;
const eventAttributes = buildEventAttributes(200);
testClient = new TestClient(mapConfig, 1234, metricsHeaders);
testClient.getLayergroup((err, body) => {
if (err) {
return done(err);
}
assert.strictEqual(typeof body.metadata, 'object');
assert(fakeTopic.publish.calledOnceWith(Buffer.from('test-event'), eventAttributes));
return done();
});
});
it('should send event when error', function (done) {
global.environment.pubSubMetrics.enabled = true;
const eventAttributes = buildEventAttributes(400);
eventAttributes.user_id = undefined;
testClient = new TestClient({}, 1234, metricsHeaders);
testClient.getLayergroup(() => {
assert(fakeTopic.publish.calledOnceWith(Buffer.from('test-event'), eventAttributes));
assert(fakeTopic.publish.calledOnce);
return done();
});
});
});