1f2d719067a73f8f5e67914c16acc38ae7ca4984
3 const assert
= require('node:assert');
4 const sinon
= require('sinon');
6 const stubDB
= require('../../stub-db');
7 const stubLogger
= require('../../stub-logger');
8 const DB
= require('../../../src/db/base');
9 const DBErrors
= require('../../../src/db/errors');
11 describe('DatabaseBase', function () {
13 beforeEach(function () {
14 db
= new DB(stubLogger
);
16 afterEach(function () {
20 it('covers no options', function () {
24 describe('Interface', function () {
25 it('covers abstract methods', async
function () {
26 await Promise
.all(stubDB
._implementation
.map(async (m
) => {
28 // eslint-disable-next-line security/detect-object-injection
30 assert
.fail(`${m}: did not catch NotImplemented exception`);
32 assert(e
instanceof DBErrors
.NotImplemented
, `${m}: unexpected exception ${e.name}`);
35 }); // covers abstract methods
36 it('covers private abstract methods', async
function () {
41 // eslint-disable-next-line security/detect-object-injection
44 assert(e
instanceof DBErrors
.NotImplemented
, `${m}: unexpected exception ${e.name}`);
50 describe('_camelfy', function () {
51 it('empty arg', function () {
52 const result
= DB
._camelfy();
53 assert
.strictEqual(result
, undefined);
55 it('no change', function () {
56 const str
= 'camelCase';
57 const result
= DB
._camelfy(str
);
58 assert
.strictEqual(result
, str
);
60 it('does expected', function () {
61 const str
= 'snake_case_thing';
62 const result
= DB
._camelfy(str
);
63 assert
.strictEqual(result
, 'snakeCaseThing');
67 describe('_ensureTypes', function () {
69 beforeEach(function () {
76 buf: Buffer
.from('foop'),
79 it('succeeds', function () {
80 db
._ensureTypes(object
, ['num', 'bignum'], ['number']);
81 db
._ensureTypes(object
, ['str', 'veryNull'], ['string', 'null']);
82 db
._ensureTypes(object
, ['buf'], ['buffer']);
84 it('data failure', function () {
86 db
._ensureTypes(object
, ['missingField'], ['string', 'null']);
87 assert
.fail('validation should have failed');
89 assert(e
instanceof DBErrors
.DataValidation
);
92 it('failure covers singular', function () {
94 db
._ensureTypes(object
, ['missingField'], ['string']);
95 assert
.fail('validation should have failed');
97 assert(e
instanceof DBErrors
.DataValidation
);
100 it('parameter failure', function () {
102 db
._ensureTypes(object
, ['missingField'], undefined);
103 assert
.fail('validation should have failed');
105 assert(e
instanceof DBErrors
.DataValidation
);
110 describe('initialize', function () {
112 beforeEach(function () {
118 db
.schemaVersionsSupported
= {
119 min: { ...currentSchema
},
120 max: { ...currentSchema
},
122 sinon
.stub(db
, '_currentSchema').resolves(currentSchema
);
124 it('covers success', async
function () {
125 await db
.initialize();
127 it('covers failure', async
function() {
128 db
.schemaVersionsSupported
= {
141 await db
.initialize();
142 assert
.fail('did not get expected exception');
144 assert(e
instanceof DBErrors
.MigrationNeeded
);
149 describe('_topicDefaults', function () {
151 beforeEach(function () {
154 it('covers', function () {
155 db
._topicDefaults(topic
);
156 assert
.strictEqual(topic
.leaseSecondsPreferred
, db
.topicLeaseDefaults
.leaseSecondsPreferred
);
158 it('covers empty', function () {
161 }); // _topicDefaults
163 describe('_topicSetDataValidate', function () {
165 beforeEach(function () {
167 url: 'https://example.com/',
171 it('covers success', function () {
172 db
._topicSetDataValidate(data
);
174 it('covers invalid value', function () {
175 data
.leaseSecondsPreferred
= -100;
177 db
._topicSetDataValidate(data
);
178 assert
.fail('did not get expected exception');
180 assert(e
instanceof DBErrors
.DataValidation
);
183 it('covers invalid range', function () {
184 data
.leaseSecondsPreferred
= 10000;
185 data
.leaseSecondsMax
= 1000;
187 db
._topicSetDataValidate(data
);
188 assert
.fail('did not get expected exception');
190 assert(e
instanceof DBErrors
.DataValidation
);
193 }); // _topicSetDataValidation
195 describe('_topicSetContentDataValidate', function () {
196 it('covers', function () {
197 db
._topicSetContentDataValidate({
198 content: Buffer
.from('foo'),
202 }); // _topicSetContentDataValidate
204 describe('_topicUpdateDataValidate', function () {
205 it('succeeds', function () {
206 db
._topicUpdateDataValidate({
207 leaseSecondsPreferred: 123,
208 leaseSecondsMin: 100,
209 leaseSecondsMax: 1000,
210 publisherValidationUrl: 'https://example.com/pub/',
211 contentHashAlgorithm: 'sha256',
214 it('covers no url', function () {
215 db
._topicUpdateDataValidate({
216 leaseSecondsPreferred: 123,
217 leaseSecondsMin: 100,
218 leaseSecondsMax: 1000,
219 contentHashAlgorithm: 'sha256',
222 it('rejects invalid url', function () {
224 db
._topicUpdateDataValidate({
225 leaseSecondsPreferred: 123,
226 leaseSecondsMin: 100,
227 leaseSecondsMax: 1000,
228 publisherValidationUrl: 'flarbl',
229 contentHashAlgorithm: 'sha256',
231 assert
.fail('did not get expected exception');
233 assert(e
instanceof DBErrors
.DataValidation
);
236 it('rejects invalid algorithm', function () {
238 db
._topicUpdateDataValidate({
239 leaseSecondsPreferred: 123,
240 leaseSecondsMin: 100,
241 leaseSecondsMax: 1000,
242 publisherValidationUrl: 'https://example.com/pub/',
243 contentHashAlgorithm: 'md6',
245 assert
.fail('did not get expected exception');
247 assert(e
instanceof DBErrors
.DataValidation
);
250 }); // _topicUpdateDataValidate
252 describe('_verificationDataValidate', function () {
253 it('covers', function () {
254 db
._verificationDataValidate({
255 topicId: 'b9ede5aa-e595-11eb-b30f-0025905f714a',
256 callback: 'https://example.com/cb',
259 isPublisherValidated: true,
262 }); // _verificationDataValidate
264 describe('_subscriptionUpsertDataValidate', function () {
265 it('covers', function () {
266 db
._subscriptionUpsertDataValidate({
267 topicId: 'b9ede5aa-e595-11eb-b30f-0025905f714a',
268 callback: 'https://example.com/cb',
272 }); // _subscriptionUpsertDataValidate
274 describe('_subscriptionUpdateDataValidate', function () {
275 it('succeeds', function () {
276 db
._subscriptionUpdateDataValidate({
277 signatureAlgorithm: 'sha256',
280 it('rejects invalid', function () {
282 db
._subscriptionUpdateDataValidate({
283 signatureAlgorithm: 'md5',
285 assert
.fail('did not get expected exception');
287 assert(e
instanceof DBErrors
.DataValidation
);
290 }); // _subscriptionUpdateDataValidate
292 describe('_verificationUpdateDataValidate', function () {
293 it('covers', function () {
294 db
._verificationUpdateDataValidate({
295 verificationId: 'b9ede5aa-e595-11eb-b30f-0025905f714a',
297 isPublisherValidated: true,
300 }); // _verificationUpdateDataValidate