X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Fsrc%2Fdb%2Fpostgres.js;h=825a295fc93a2e7bdd515755b2ca6348f9b0ba1f;hb=8cd88ab4087a7fab2ccd6e231c64d7f0f1299f26;hp=c7621354de979ce9a78039813cfb20bf78b76570;hpb=71587de3ea9839d14d9f7bffa6c1db19e52dd9b5;p=websub-hub diff --git a/test/src/db/postgres.js b/test/src/db/postgres.js index c762135..825a295 100644 --- a/test/src/db/postgres.js +++ b/test/src/db/postgres.js @@ -1,12 +1,8 @@ -/* 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'); @@ -490,10 +486,11 @@ describe('DatabasePostgres', function () { }); // 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 = { @@ -502,7 +499,7 @@ describe('DatabasePostgres', function () { 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; @@ -513,7 +510,7 @@ describe('DatabasePostgres', function () { }; 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); @@ -521,6 +518,68 @@ describe('DatabasePostgres', function () { }); }); // 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 = []; @@ -1193,11 +1252,17 @@ describe('DatabasePostgres', function () { 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);