4 const assert
= require('assert');
5 const sinon
= require('sinon'); // eslint-disable-line node/no-unpublished-require
7 const stubDB
= require('../../stub-db');
8 const stubLogger
= require('../../stub-logger');
9 const DB
= require('../../../src/db/base');
10 const DBErrors
= require('../../../src/db/errors');
12 describe('DatabaseBase', function () {
14 beforeEach(function () {
15 db
= new DB(stubLogger
);
17 afterEach(function () {
21 it('covers no options', function () {
25 describe('Interface', function () {
26 it('covers abstract methods', async
function () {
27 await Promise
.all(stubDB
._implementation
.map(async (m
) => {
29 // eslint-disable-next-line security/detect-object-injection
31 assert
.fail(`${m}: did not catch NotImplemented exception`);
33 assert(e
instanceof DBErrors
.NotImplemented
, `${m}: unexpected exception ${e.name}`);
36 }); // covers abstract methods
37 it('covers private abstract methods', async
function () {
42 // eslint-disable-next-line security/detect-object-injection
45 assert(e
instanceof DBErrors
.NotImplemented
, `${m}: unexpected exception ${e.name}`);
51 describe('_camelfy', function () {
52 it('empty arg', function () {
53 const result
= DB
._camelfy();
54 assert
.strictEqual(result
, undefined);
56 it('no change', function () {
57 const str
= 'camelCase';
58 const result
= DB
._camelfy(str
);
59 assert
.strictEqual(result
, str
);
61 it('does expected', function () {
62 const str
= 'snake_case_thing';
63 const result
= DB
._camelfy(str
);
64 assert
.strictEqual(result
, 'snakeCaseThing');
68 describe('_ensureTypes', function () {
70 beforeEach(function () {
77 buf: Buffer
.from('foop'),
80 it('succeeds', function () {
81 db
._ensureTypes(object
, ['num', 'bignum'], ['number']);
82 db
._ensureTypes(object
, ['str', 'veryNull'], ['string', 'null']);
83 db
._ensureTypes(object
, ['buf'], ['buffer']);
85 it('data failure', function () {
87 db
._ensureTypes(object
, ['missingField'], ['string', 'null']);
88 assert
.fail('validation should have failed');
90 assert(e
instanceof DBErrors
.DataValidation
);
93 it('failure covers singular', function () {
95 db
._ensureTypes(object
, ['missingField'], ['string']);
96 assert
.fail('validation should have failed');
98 assert(e
instanceof DBErrors
.DataValidation
);
101 it('parameter failure', function () {
103 db
._ensureTypes(object
, ['missingField'], undefined);
104 assert
.fail('validation should have failed');
106 assert(e
instanceof DBErrors
.DataValidation
);
111 describe('initialize', function () {
113 beforeEach(function () {
119 db
.schemaVersionsSupported
= {
120 min: { ...currentSchema
},
121 max: { ...currentSchema
},
123 sinon
.stub(db
, '_currentSchema').resolves(currentSchema
);
125 it('covers success', async
function () {
126 await db
.initialize();
128 it('covers failure', async
function() {
129 db
.schemaVersionsSupported
= {
142 await db
.initialize();
143 assert
.fail('did not get expected exception');
145 assert(e
instanceof DBErrors
.MigrationNeeded
);
150 describe('_topicDefaults', function () {
152 beforeEach(function () {
155 it('covers', function () {
156 db
._topicDefaults(topic
);
157 assert
.strictEqual(topic
.leaseSecondsPreferred
, db
.topicLeaseDefaults
.leaseSecondsPreferred
);
159 it('covers empty', function () {
162 }); // _topicDefaults
164 describe('_topicSetDataValidate', function () {
166 beforeEach(function () {
168 url: 'https://example.com/',
172 it('covers success', function () {
173 db
._topicSetDataValidate(data
);
175 it('covers invalid value', function () {
176 data
.leaseSecondsPreferred
= -100;
178 db
._topicSetDataValidate(data
);
179 assert
.fail('did not get expected exception');
181 assert(e
instanceof DBErrors
.DataValidation
);
184 it('covers invalid range', function () {
185 data
.leaseSecondsPreferred
= 10000;
186 data
.leaseSecondsMax
= 1000;
188 db
._topicSetDataValidate(data
);
189 assert
.fail('did not get expected exception');
191 assert(e
instanceof DBErrors
.DataValidation
);
194 }); // _topicSetDataValidation
196 describe('_topicSetContentDataValidate', function () {
197 it('covers', function () {
198 db
._topicSetContentDataValidate({
199 content: Buffer
.from('foo'),
203 }); // _topicSetContentDataValidate
205 describe('_topicUpdateDataValidate', function () {
206 it('succeeds', function () {
207 db
._topicUpdateDataValidate({
208 leaseSecondsPreferred: 123,
209 leaseSecondsMin: 100,
210 leaseSecondsMax: 1000,
211 publisherValidationUrl: 'https://example.com/pub/',
212 contentHashAlgorithm: 'sha256',
215 it('covers no url', function () {
216 db
._topicUpdateDataValidate({
217 leaseSecondsPreferred: 123,
218 leaseSecondsMin: 100,
219 leaseSecondsMax: 1000,
220 contentHashAlgorithm: 'sha256',
223 it('rejects invalid url', function () {
225 db
._topicUpdateDataValidate({
226 leaseSecondsPreferred: 123,
227 leaseSecondsMin: 100,
228 leaseSecondsMax: 1000,
229 publisherValidationUrl: 'flarbl',
230 contentHashAlgorithm: 'sha256',
232 assert
.fail('did not get expected exception');
234 assert(e
instanceof DBErrors
.DataValidation
);
237 it('rejects invalid algorithm', function () {
239 db
._topicUpdateDataValidate({
240 leaseSecondsPreferred: 123,
241 leaseSecondsMin: 100,
242 leaseSecondsMax: 1000,
243 publisherValidationUrl: 'https://example.com/pub/',
244 contentHashAlgorithm: 'md6',
246 assert
.fail('did not get expected exception');
248 assert(e
instanceof DBErrors
.DataValidation
);
251 }); // _topicUpdateDataValidate
253 describe('_verificationDataValidate', function () {
254 it('covers', function () {
255 db
._verificationDataValidate({
256 topicId: 'b9ede5aa-e595-11eb-b30f-0025905f714a',
257 callback: 'https://example.com/cb',
260 isPublisherValidated: true,
263 }); // _verificationDataValidate
265 describe('_subscriptionUpsertDataValidate', function () {
266 it('covers', function () {
267 db
._subscriptionUpsertDataValidate({
268 topicId: 'b9ede5aa-e595-11eb-b30f-0025905f714a',
269 callback: 'https://example.com/cb',
273 }); // _subscriptionUpsertDataValidate
275 describe('_subscriptionUpdateDataValidate', function () {
276 it('succeeds', function () {
277 db
._subscriptionUpdateDataValidate({
278 signatureAlgorithm: 'sha256',
281 it('rejects invalid', function () {
283 db
._subscriptionUpdateDataValidate({
284 signatureAlgorithm: 'md5',
286 assert
.fail('did not get expected exception');
288 assert(e
instanceof DBErrors
.DataValidation
);
291 }); // _subscriptionUpdateDataValidate
293 describe('_verificationUpdateDataValidate', function () {
294 it('covers', function () {
295 db
._verificationUpdateDataValidate({
296 verificationId: 'b9ede5aa-e595-11eb-b30f-0025905f714a',
298 isPublisherValidated: true,
301 }); // _verificationUpdateDataValidate