-/* eslint-disable sonarjs/no-identical-functions */
-/* eslint-env mocha */
-/* eslint-disable sonarjs/no-duplicate-string */
'use strict';
/* This provides implementation coverage, stubbing parts of better-sqlite3. */
-const assert = require('assert');
-// eslint-disable-next-line node/no-unpublished-require
+const assert = require('node:assert');
const sinon = require('sinon');
const DBStub = require('../../stub-db');
const stubLogger = require('../../stub-logger');
sinon.restore();
});
+ it('covers options', function () {
+ const xoptions = new Config('test');
+ delete xoptions.db.connectionString;
+ db = new DB(stubLogger, xoptions);
+ });
+
// Ensure all interface methods are implemented
describe('Implementation', function () {
it('implements interface', async function () {
contentFetchNextAttempt: now,
contentUpdated: now,
url: topic.url,
- }
+ };
const result = DB._topicDataToNative(topic);
assert.deepStrictEqual(result, expected);
});
}); // authenticationGet
describe('authenticationUpsert', function () {
- let identifier, credential;
+ let identifier, credential, otpKey;
beforeEach(function () {
identifier = 'username';
credential = '$z$foo';
+ otpKey = '12345678901234567890123456789012';
});
it('success', async function() {
const dbResult = {
lastInsertRowid: undefined,
};
sinon.stub(db.statement.authenticationUpsert, 'run').returns(dbResult);
- await db.authenticationUpsert(dbCtx, identifier, credential);
+ await db.authenticationUpsert(dbCtx, identifier, credential, otpKey);
});
it('failure', async function () {
const dbResult = {
};
sinon.stub(db.statement.authenticationUpsert, 'run').returns(dbResult);
try {
- await db.authenticationUpsert(dbCtx, identifier, credential);
+ await db.authenticationUpsert(dbCtx, identifier, credential, otpKey);
+ assert.fail(noExpectedException);
+ } catch (e) {
+ assert(e instanceof DBErrors.UnexpectedResult);
+ }
+ });
+ }); // authenticationUpsert
+
+ describe('authenticationUpdateCredential', function () {
+ let identifier, credential;
+ beforeEach(function () {
+ identifier = 'username';
+ credential = '$z$foo';
+ });
+ it('success', async function() {
+ const dbResult = {
+ changes: 1,
+ lastInsertRowid: undefined,
+ };
+ sinon.stub(db.statement.authenticationUpdateCredential, 'run').returns(dbResult);
+ await db.authenticationUpdateCredential(dbCtx, identifier, credential);
+ });
+ it('failure', async function () {
+ const dbResult = {
+ changes: 0,
+ lastInsertRowid: undefined,
+ };
+ sinon.stub(db.statement.authenticationUpdateCredential, 'run').returns(dbResult);
+ try {
+ await db.authenticationUpdateCredential(dbCtx, identifier, credential);
+ assert.fail(noExpectedException);
+ } catch (e) {
+ assert(e instanceof DBErrors.UnexpectedResult);
+ }
+ });
+ }); // authenticationUpdateCredential
+
+ describe('authenticationUpdateOTPKey', function () {
+ let identifier, otpKey;
+ beforeEach(function () {
+ identifier = 'username';
+ otpKey = '12345678901234567890123456789012';
+ });
+ it('success', async function() {
+ const dbResult = {
+ changes: 1,
+ lastInsertRowid: undefined,
+ };
+ sinon.stub(db.statement.authenticationUpdateOtpKey, 'run').returns(dbResult);
+ await db.authenticationUpdateOTPKey(dbCtx, identifier, otpKey);
+ });
+ it('failure', async function () {
+ const dbResult = {
+ changes: 0,
+ lastInsertRowid: undefined,
+ };
+ sinon.stub(db.statement.authenticationUpdateOtpKey, 'run').returns(dbResult);
+ try {
+ await db.authenticationUpdateOTPKey(dbCtx, identifier, otpKey);
assert.fail(noExpectedException);
} catch (e) {
assert(e instanceof DBErrors.UnexpectedResult);
assert.deepStrictEqual(e, expected);
}
});
- });
+ }); // subscriptionDeleteExpired
describe('subscriptionDeliveryClaim', function () {
it('success', async function () {
const dbResult1 = {
changes: 1,
lastInsertRowid: undefined,
- }
+ };
const expected = {
changes: 1,
lastInsertRowid: undefined,
const dbResult1 = {
changes: 1,
lastInsertRowid: undefined,
- }
+ };
const expected = {
changes: 1,
lastInsertRowid: undefined,
const dbResult1 = {
changes: 0,
lastInsertRowid: undefined,
- }
+ };
sinon.stub(db.statement.topicAttempts, 'get').returns(dbGet);
sinon.stub(db.statement.topicAttemptsIncrement, 'run').returns(dbResult0);
sinon.stub(db.statement.topicContentFetchDone, 'run').returns(dbResult1);
const dbResult1 = {
changes: 0,
lastInsertRowid: undefined,
- }
+ };
sinon.stub(db.statement.topicAttempts, 'get').returns(dbGet);
sinon.stub(db.statement.topicAttemptsIncrement, 'run').returns(dbResult0);
sinon.stub(db.statement.topicContentFetchDone, 'run').returns(dbResult1);
const result = await db.topicGetByUrl(dbCtx, topicUrl);
assert.deepStrictEqual(result, expected);
});
+ it('success, no defaults', async function() {
+ const expected = [];
+ sinon.stub(db.statement.topicGetByUrl, 'get').returns(expected);
+ const result = await db.topicGetByUrl(dbCtx, topicUrl, false);
+ assert.deepStrictEqual(result, expected);
+ });
it('failure', async function () {
const expected = new Error();
sinon.stub(db.statement.topicGetByUrl, 'get').throws(expected);
}
assert(db.statement.topicDeleteById.run.called);
});
- });
+ }); // topicPendingDelete
+
+ describe('topicPublishHistory', function () {
+ beforeEach(function () {
+ sinon.stub(db.statement.topicPublishHistory, 'all');
+ });
+ it('success', function () {
+ db.statement.topicPublishHistory.all.returns([
+ { daysAgo: 1, contentUpdates: 1 },
+ { daysAgo: 3, contentUpdates: 2 },
+ ]);
+ const result = db.topicPublishHistory(dbCtx, topicId, 7);
+ const expected = [0, 1, 0, 2, 0, 0, 0];
+ assert.deepStrictEqual(result, expected);
+ });
+ }); // topicPublishHistory
describe('topicSet', function () {
let data;
DB._verificationDataToEngine(data);
assert.strictEqual(data.isPublisherValidated, 0);
});
- }) // _verificationDataToEngine
+ }); // _verificationDataToEngine
describe('verificationInsert', function () {
let verification;
const dbResult = {
changes: 0,
lastInsertRowid: undefined,
- }
+ };
sinon.stub(db.statement.verificationUpdate, 'run').returns(dbResult);
try {
await db.verificationUpdate(dbCtx, verificationId, data);
const dbResult = {
changes: 1,
lastInsertRowid: undefined,
- }
+ };
sinon.stub(db.statement.verificationValidate, 'run').returns(dbResult);
await db.verificationValidated(dbCtx, verificationId);
});
const dbResult = {
changes: 0,
lastInsertRowid: undefined,
- }
+ };
sinon.stub(db.statement.verificationValidate, 'run').returns(dbResult);
try {
await db.verificationValidated(dbCtx, verificationId);