keyserver/test/integration/mongo-test.js

100 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-05-25 14:13:49 +00:00
'use strict';
2017-08-18 10:01:34 +00:00
const log = require('winston');
const config = require('config');
const Mongo = require('../../src/dao/mongo');
2016-05-25 14:13:49 +00:00
describe('Mongo Integration Tests', function() {
this.timeout(20000);
const DB_TYPE = 'apple';
const sandbox = sinon.createSandbox();
2016-05-25 14:13:49 +00:00
let mongo;
2017-08-17 09:44:26 +00:00
before(async () => {
2017-08-17 11:16:49 +00:00
sandbox.stub(log);
2016-06-08 12:01:30 +00:00
mongo = new Mongo();
2017-08-16 04:03:32 +00:00
await mongo.init(config.mongo);
2016-05-25 14:13:49 +00:00
});
2017-08-17 09:44:26 +00:00
beforeEach(async () => {
2017-08-16 04:03:32 +00:00
await mongo.clear(DB_TYPE);
2016-05-25 14:13:49 +00:00
});
2017-08-17 09:44:26 +00:00
after(async () => {
2017-08-17 11:16:49 +00:00
sandbox.restore();
2017-08-16 04:03:32 +00:00
await mongo.clear(DB_TYPE);
await mongo.disconnect();
2016-05-25 14:13:49 +00:00
});
2019-03-06 09:21:29 +00:00
describe('create', () => {
2017-08-17 09:44:26 +00:00
it('should insert a document', async () => {
2017-08-16 04:03:32 +00:00
const r = await mongo.create({_id: '0'}, DB_TYPE);
2016-05-25 14:13:49 +00:00
expect(r.insertedCount).to.equal(1);
});
2017-08-17 09:44:26 +00:00
it('should fail if two with the same ID are inserted', async () => {
2017-08-16 04:03:32 +00:00
let r = await mongo.create({_id: '0'}, DB_TYPE);
2016-05-25 14:13:49 +00:00
expect(r.insertedCount).to.equal(1);
try {
2017-08-16 04:03:32 +00:00
r = await mongo.create({_id: '0'}, DB_TYPE);
2017-08-15 08:03:06 +00:00
} catch (e) {
expect(e.message).to.match(/duplicate/);
}
2016-05-25 14:13:49 +00:00
});
});
2016-05-25 14:13:49 +00:00
2019-03-06 09:21:29 +00:00
describe('batch', () => {
2017-08-17 09:44:26 +00:00
it('should insert a document', async () => {
2017-08-16 04:03:32 +00:00
const r = await mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE);
expect(r.insertedCount).to.equal(2);
});
2017-08-17 09:44:26 +00:00
it('should fail if docs with the same ID are inserted', async () => {
2017-08-16 04:03:32 +00:00
let r = await mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE);
expect(r.insertedCount).to.equal(2);
2016-05-25 14:13:49 +00:00
try {
2017-08-16 04:03:32 +00:00
r = await mongo.batch([{_id: '0'}, {_id: '1'}], DB_TYPE);
2017-08-15 08:03:06 +00:00
} catch (e) {
2016-05-25 14:13:49 +00:00
expect(e.message).to.match(/duplicate/);
}
});
});
2019-03-06 09:21:29 +00:00
describe('update', () => {
2017-08-17 09:44:26 +00:00
it('should update a document', async () => {
2017-08-16 04:03:32 +00:00
let r = await mongo.create({_id: '0'}, DB_TYPE);
r = await mongo.update({_id: '0'}, {foo: 'bar'}, DB_TYPE);
2016-05-25 14:13:49 +00:00
expect(r.modifiedCount).to.equal(1);
2017-08-16 04:03:32 +00:00
r = await mongo.get({_id: '0'}, DB_TYPE);
2016-05-25 14:13:49 +00:00
expect(r.foo).to.equal('bar');
});
});
2019-03-06 09:21:29 +00:00
describe('get', () => {
2017-08-17 09:44:26 +00:00
it('should get a document', async () => {
2017-08-16 04:03:32 +00:00
let r = await mongo.create({_id: '0'}, DB_TYPE);
r = await mongo.get({_id: '0'}, DB_TYPE);
2016-05-25 14:13:49 +00:00
expect(r).to.exist;
});
});
2019-03-06 09:21:29 +00:00
describe('list', () => {
2017-08-17 09:44:26 +00:00
it('should list documents', async () => {
2017-08-16 04:03:32 +00:00
let r = await mongo.batch([{_id: '0', foo: 'bar'}, {_id: '1', foo: 'bar'}], DB_TYPE);
r = await mongo.list({foo: 'bar'}, DB_TYPE);
2017-08-15 08:03:06 +00:00
expect(r).to.deep.equal([{_id: '0', foo: 'bar'}, {_id: '1', foo: 'bar'}], DB_TYPE);
2016-05-25 14:13:49 +00:00
});
});
2019-03-06 09:21:29 +00:00
describe('remove', () => {
2017-08-17 09:44:26 +00:00
it('should remove a document', async () => {
2017-08-16 04:03:32 +00:00
let r = await mongo.create({_id: '0'}, DB_TYPE);
r = await mongo.remove({_id: '0'}, DB_TYPE);
r = await mongo.get({_id: '0'}, DB_TYPE);
2016-05-25 14:13:49 +00:00
expect(r).to.not.exist;
});
});
2017-08-15 08:03:06 +00:00
});