-/* eslint-disable sonarjs/no-identical-functions */
-/* eslint-env mocha */
-/* eslint-disable sonarjs/no-duplicate-string */
'use strict';
/* This provides implementation coverage, stubbing pg-promise. */
-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');
httpRemoteAddr = '127.0.0.1';
httpFrom = 'user@example.com';
wanted = 5;
-});
+ });
afterEach(function () {
sinon.restore();
});
columnTwo: 4,
},
];
- db.pgpInitOptions.receive(data, result, event)
+ db.pgpInitOptions.receive({ data, result, ctx: event });
assert(db.logger.debug.called);
assert.deepStrictEqual(data, expectedData);
});
columnTwo: 4,
},
];
- db.pgpInitOptions.receive(data, result, event)
+ db.pgpInitOptions.receive({ data, result, ctx: event });
assert(!db.logger.debug.called);
assert.deepStrictEqual(data, expectedData);
});
});
it('covers migration', async function() {
sinon.stub(db.db, 'oneOrNone').resolves({});
- sinon.stub(db.db, 'multiResult');
- sinon.stub(db, '_currentSchema').resolves(db.schemaVersionsSupported.max);
+ sinon.stub(db.db, 'multiResult').resolves({});
+ sinon.stub(db, '_currentSchema').resolves(db.schemaVersionsSupported.min);
sinon.stub(db.db, 'one').resolves(db.schemaVersionsSupported.max);
await db.initialize();
});
+ it('covers migration failure', async function() {
+ const expected = new Error('oh no');
+ sinon.stub(db.db, 'oneOrNone').resolves({});
+ sinon.stub(db.db, 'multiResult').rejects(expected);
+ sinon.stub(db, '_currentSchema').resolves(db.schemaVersionsSupported.min);
+ sinon.stub(db.db, 'one').resolves(db.schemaVersionsSupported.max);
+ try {
+ await db.initialize();
+ assert.fail(noExpectedException);
+ } catch (e) {
+ assert.deepStrictEqual(e, expected);
+ }
+ });
it('covers listener', async function() {
db.listener = {
start: sinon.stub(),
}); // 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 = {
duration: 22,
};
sinon.stub(db.db, 'result').resolves(dbResult);
- await db.authenticationUpsert(dbCtx, identifier, credential);
+ await db.authenticationUpsert(dbCtx, identifier, credential, otpKey);
});
it('failure', async function() {
credential = undefined;
};
sinon.stub(db.db, 'result').resolves(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';
+ });
+ it('success', async function () {
+ const dbResult = {
+ rowCount: 1,
+ rows: undefined,
+ duration: 22,
+ };
+ sinon.stub(db.db, 'result').resolves(dbResult);
+ await db.authenticationUpdateCredential(dbCtx, identifier, credential);
+ });
+ it('failure', async function() {
+ credential = undefined;
+ const dbResult = {
+ rowCount: 0,
+ rows: undefined,
+ duration: 22,
+ };
+ sinon.stub(db.db, 'result').resolves(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 = {
+ rowCount: 1,
+ rows: undefined,
+ duration: 22,
+ };
+ sinon.stub(db.db, 'result').resolves(dbResult);
+ await db.authenticationUpdateOTPKey(dbCtx, identifier, otpKey);
+ });
+ it('failure', async function() {
+ const dbResult = {
+ rowCount: 0,
+ rows: undefined,
+ duration: 22,
+ };
+ sinon.stub(db.db, 'result').resolves(dbResult);
+ try {
+ await db.authenticationUpdateOTPKey(dbCtx, identifier, otpKey);
+ assert.fail(noExpectedException);
+ } catch (e) {
+ assert(e instanceof DBErrors.UnexpectedResult);
+ }
+ });
+ }); // authenticationUpdateOTPKey
+
describe('subscriptionsByTopicId', function () {
it('success', async function () {
const expected = [];
const expected = new Error();
sinon.stub(db.db, 'manyOrNone').throws(expected);
try {
- await db.subscriptionDeliveryClaim(dbCtx, wanted, claimTimeoutSeconds, claimant );
+ await db.subscriptionDeliveryClaim(dbCtx, wanted, claimTimeoutSeconds, claimant);
assert.fail(noExpectedException);
} catch (e) {
assert.deepStrictEqual(e, expected);
changes: 1,
lastInsertRowid: 'c2e254c5-aa6e-4a8f-b1a1-e474b07392bb',
duration: 11,
- }
+ };
sinon.stub(db.db, 'result').resolves(dbResult);
const result = await db.subscriptionDeliveryClaimById(dbCtx, subscriptionId, claimTimeoutSeconds, claimant);
assert.deepStrictEqual(result, expected);
}); // subscriptionDeliveryClaimById
describe('subscriptionDeliveryComplete', function () {
+ let topicContentUpdated;
+ before(function () {
+ topicContentUpdated = new Date();
+ });
it('success', async function() {
const dbResult = {
rowCount: 1,
};
sinon.stub(db.db, 'result').resolves(dbResult);
- await db.subscriptionDeliveryComplete(dbCtx, callback, topicId);
+ await db.subscriptionDeliveryComplete(dbCtx, callback, topicId, topicContentUpdated);
});
it('failure', async function () {
const dbResult = {
};
sinon.stub(db.db, 'result').onCall(0).resolves(dbResult);
try {
- await db.subscriptionDeliveryComplete(dbCtx, callback, topicId);
+ await db.subscriptionDeliveryComplete(dbCtx, callback, topicId, topicContentUpdated);
assert.fail(noExpectedException);
} catch (e) {
assert(e instanceof DBErrors.UnexpectedResult);
};
sinon.stub(db.db, 'result').onCall(0).resolves(dbResult0).onCall(1).resolves(dbResult1);
try {
- await db.subscriptionDeliveryComplete(dbCtx, callback, topicId);
+ await db.subscriptionDeliveryComplete(dbCtx, callback, topicId, topicContentUpdated);
assert.fail(noExpectedException);
} catch (e) {
assert(e instanceof DBErrors.UnexpectedResult);
rowCount: 1,
rows: [],
duration: 10,
- }
+ };
const expected = {
changes: 1,
lastInsertRowid: undefined,
rowCount: 1,
rows: [],
duration: 10,
- }
+ };
const expected = {
changes: 1,
lastInsertRowid: undefined,
rowCount: 0,
rows: [],
duration: 10,
- }
+ };
sinon.stub(db.db, 'one').resolves(dbOne);
sinon.stub(db.db, 'result').onCall(0).resolves(dbResult0).onCall(1).resolves(dbResult1);
try {
rowCount: 0,
rows: [],
duration: 10,
- }
+ };
sinon.stub(db.db, 'one').resolves(dbOne);
sinon.stub(db.db, 'result').onCall(0).resolves(dbResult0).onCall(1).resolves(dbResult1);
try {
describe('topicGetByUrl', function () {
it('success', async function() {
- const expected = [];
+ const expected = { id: topicId };
sinon.stub(db.db, 'oneOrNone').resolves(expected);
const result = await db.topicGetByUrl(dbCtx, topicUrl);
assert.deepStrictEqual(result, expected);
});
+ it('success, no default', async function() {
+ const expected = { id: topicId };
+ sinon.stub(db.db, 'oneOrNone').resolves(expected);
+ const result = await db.topicGetByUrl(dbCtx, topicUrl, false);
+ assert.deepStrictEqual(result, expected);
+ });
it('failure', async function () {
const expected = new Error();
sinon.stub(db.db, 'oneOrNone').throws(expected);
});
});
+ describe('topicPublishHistory', function () {
+ beforeEach(function () {
+ sinon.stub(db.db, 'manyOrNone');
+ });
+ it('success', async function () {
+ db.db.manyOrNone.returns([
+ { daysAgo: 1, contentUpdates: 1 },
+ { daysAgo: 3, contentUpdates: 2 },
+ ]);
+ const result = await db.topicPublishHistory(dbCtx, topicId, 7);
+ const expected = [0, 1, 0, 2, 0, 0, 0];
+ assert.deepStrictEqual(result, expected);
+ });
+ }); // topicPublishHistory
+
describe('topicSet', function () {
let data;
beforeEach(function () {
contentType: 'text/plain',
contentHash: 'abc123',
};
+ sinon.stub(db.db, 'result');
});
it('success', async function() {
const dbResult = {
lastInsertRowid: undefined,
duration: 10,
};
- sinon.stub(db.db, 'result').resolves(dbResult);
+ db.db.result.resolves(dbResult);
const result = await db.topicSetContent(dbCtx, data);
assert.deepStrictEqual(result, expected);
});
rows: [],
duration: 10,
};
- sinon.stub(db.db, 'result').resolves(dbResult);
+ db.db.result.resolves(dbResult);
+ try {
+ await db.topicSetContent(dbCtx, data);
+ assert.fail(noExpectedException);
+ } catch (e) {
+ assert(e instanceof DBErrors.UnexpectedResult);
+ }
+ });
+ it('failure 2', async function () {
+ const dbResultSuccess = {
+ rowCount: 1,
+ rows: [],
+ duration: 10,
+ };
+ const dbResultFail = {
+ rowCount: 0,
+ rows: [],
+ duration: 10,
+ };
+ db.db.result
+ .onCall(0).resolves(dbResultSuccess)
+ .onCall(1).resolves(dbResultFail);
try {
await db.topicSetContent(dbCtx, data);
assert.fail(noExpectedException);
rowCount: 0,
rows: [],
duration: 10,
- }
+ };
sinon.stub(db.db, 'result').resolves(dbResult);
try {
await db.verificationUpdate(dbCtx, verificationId, data);
rowCount: 1,
rows: [],
duration: 10,
- }
+ };
sinon.stub(db.db, 'result').resolves(dbResult);
await db.verificationValidated(dbCtx, verificationId);
});
rowCount: 0,
rows: [],
duration: 10,
- }
+ };
sinon.stub(db.db, 'result').resolves(dbResult);
try {
await db.verificationValidated(dbCtx, verificationId);