X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Fsrc%2Fdb%2Fsqlite.js;h=46163b88e9510b53f409be139cbb2b540f9a2c1a;hb=f0bf29c75b0fd405ff92fa76f058e61162b87e43;hp=f6935943eb1f088174185c15aac0ea5f9e373441;hpb=b0103b0d496262c438b40bc20304081dbfe41e73;p=squeep-indie-auther diff --git a/test/src/db/sqlite.js b/test/src/db/sqlite.js index f693594..46163b8 100644 --- a/test/src/db/sqlite.js +++ b/test/src/db/sqlite.js @@ -1,12 +1,10 @@ -/* 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'); -const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require +const sinon = require('sinon'); const StubDatabase = require('../../stub-db'); const StubLogger = require('../../stub-logger'); const DB = require('../../../src/db/sqlite'); @@ -112,7 +110,7 @@ describe('DatabaseSQLite', function () { db._optimize(); assert(db.db.pragma.called); assert(db.statement._optimize.all.called); - assert.strictEqual(db.changesSinceLastOptimize, 0n) + assert.strictEqual(db.changesSinceLastOptimize, 0n); }); }); // _optimize @@ -216,6 +214,32 @@ describe('DatabaseSQLite', function () { }); }); // almanacGetAll + describe('almanacUpsert', function () { + let event, date, dbResult; + beforeEach(function () { + event = 'test_event'; + date = new Date('Fri Dec 22 03:27 UTC 2023'); + sinon.stub(db.statement.almanacUpsert, 'run'); + dbResult = { + changes: 1, + lastInsertRowid: undefined, + }; + }); + it('success', function () { + db.statement.almanacUpsert.run.returns(dbResult); + db.almanacUpsert(dbCtx, event, date); + }); + it('success with default date', function () { + db.statement.almanacUpsert.run.returns(dbResult); + db.almanacUpsert(dbCtx, event); + }); + it('failure', function () { + dbResult.changes = 0; + db.statement.almanacUpsert.run.returns(dbResult); + assert.throws(() => db.almanacUpsert(dbCtx, { event, date }), DBErrors.UnexpectedResult); + }); + }); // almanacUpsert + describe('authenticationGet', function () { let identifier, credential; beforeEach(function () { @@ -283,6 +307,54 @@ describe('DatabaseSQLite', function () { }); }); // authenticationUpsert + describe('authenticationUpdateCredential', function () { + let identifier, credential; + beforeEach(function () { + identifier = 'username'; + credential = '$z$foo'; + }); + it('success', function() { + const dbResult = { + changes: 1, + lastInsertRowid: undefined, + }; + sinon.stub(db.statement.authenticationUpdateCredential, 'run').returns(dbResult); + db.authenticationUpdateCredential(dbCtx, identifier, credential); + }); + it('failure', function () { + const dbResult = { + changes: 0, + lastInsertRowid: undefined, + }; + sinon.stub(db.statement.authenticationUpdateCredential, 'run').returns(dbResult); + assert.throws(() => db.authenticationUpdateCredential(dbCtx, identifier, credential), DBErrors.UnexpectedResult); + }); + }); // authenticationUpdateCredential + + describe('authenticationUpdateOTPKey', function () { + let identifier, otpKey; + beforeEach(function () { + identifier = 'username'; + otpKey = '1234567890123456789012'; + }); + it('success', function() { + const dbResult = { + changes: 1, + lastInsertRowid: undefined, + }; + sinon.stub(db.statement.authenticationUpdateOtpKey, 'run').returns(dbResult); + db.authenticationUpdateOTPKey(dbCtx, identifier, otpKey); + }); + it('failure', function () { + const dbResult = { + changes: 0, + lastInsertRowid: undefined, + }; + sinon.stub(db.statement.authenticationUpdateOtpKey, 'run').returns(dbResult); + assert.throws(() => db.authenticationUpdateOTPKey(dbCtx, identifier, otpKey), DBErrors.UnexpectedResult); + }); + }); // authenticationUpdateOTPKey + describe('profileIdentifierInsert', function () { let profile, identifier; beforeEach(function () { @@ -500,7 +572,7 @@ describe('DatabaseSQLite', function () { expires: new Date(refreshResponse.expires * 1000), refreshExpires: new Date(refreshResponse.refreshExpires * 1000), scopes: ['blah'], - } + }; const response = db.refreshCode(dbCtx, codeId, refreshed, removeScopes); assert.deepStrictEqual(response, expectedResponse); }); @@ -510,7 +582,7 @@ describe('DatabaseSQLite', function () { const expectedResponse = { expires: new Date(refreshResponse.expires * 1000), refreshExpires: new Date(refreshResponse.refreshExpires * 1000), - } + }; removeScopes = []; const response = db.refreshCode(dbCtx, codeId, refreshed, removeScopes); assert.deepStrictEqual(response, expectedResponse); @@ -522,7 +594,7 @@ describe('DatabaseSQLite', function () { expires: new Date(refreshResponse.expires * 1000), refreshExpires: new Date(refreshResponse.refreshExpires * 1000), scopes: [], - } + }; const response = db.refreshCode(dbCtx, codeId, refreshed, removeScopes); assert.deepStrictEqual(response, expectedResponse); }); @@ -649,7 +721,7 @@ describe('DatabaseSQLite', function () { sinon.stub(db.statement.scopeInUse, 'get'); dbGetResult = { inUse: false, - } + }; sinon.stub(db.statement.scopeDelete, 'run'); dbRunResult = { changes: 1, @@ -824,7 +896,7 @@ describe('DatabaseSQLite', function () { let dbResult, codeId; beforeEach(function () { codeId = '2f226616-3e79-11ec-ad0f-0025905f714a'; - sinon.stub(db.statement.tokenRevokeByCodeId, 'run') + sinon.stub(db.statement.tokenRevokeByCodeId, 'run'); dbResult = { changes: 1, lastInsertRowid: undefined, @@ -915,4 +987,120 @@ describe('DatabaseSQLite', function () { }); }); // tokensGetByIdentifier + describe('ticketRedeemed', function () { + let redeemedData, dbResult; + beforeEach(function () { + redeemedData = { + resource: 'https://resource.example.com/', + subject: 'https://subject.example.com/', + iss: 'https://idp.example.com/', + ticket: 'xxxTICKETxxx', + token: 'xxxTOKENxxx', + }; + sinon.stub(db.statement.ticketRedeemed, 'run'); + dbResult = { + changes: 1, + lastInsertRowid: undefined, + }; + }); + it('success', function () { + db.statement.ticketRedeemed.run.returns(dbResult); + db.ticketRedeemed(dbCtx, redeemedData); + }); + it('failure', function () { + dbResult.changes = 0; + db.statement.ticketRedeemed.run.returns(dbResult); + assert.throws(() => db.ticketRedeemed(dbCtx, redeemedData), DBErrors.UnexpectedResult); + }); + }); // ticketRedeemed + + describe('ticketTokenPublished', function () { + let redeemedData, dbResult; + beforeEach(function () { + redeemedData = { + resource: 'https://resource.example.com/', + subject: 'https://subject.example.com/', + iss: 'https://idp.example.com/', + ticket: 'xxxTICKETxxx', + token: 'xxxTOKENxxx', + }; + sinon.stub(db.statement.ticketTokenPublished, 'run'); + sinon.stub(db.statement.almanacUpsert, 'run'); + dbResult = { + changes: 1, + lastInsertRowid: undefined, + }; + }); + it('success', function () { + db.statement.ticketTokenPublished.run.returns(dbResult); + db.statement.almanacUpsert.run.returns(dbResult); + db.ticketTokenPublished(dbCtx, redeemedData); + assert(db.statement.ticketTokenPublished.run.called); + assert(db.statement.almanacUpsert.run.called); + }); + it('failure', function () { + dbResult.changes = 0; + db.statement.ticketTokenPublished.run.returns(dbResult); + assert.throws(() => db.ticketTokenPublished(dbCtx, redeemedData), DBErrors.UnexpectedResult); + }); + it('failure of almanac', function () { + const dbResultAlmanac = { + ...dbResult, + changes: 0, + }; + db.statement.ticketTokenPublished.run.returns(dbResult); + db.statement.almanacUpsert.run.returns(dbResultAlmanac); + assert.throws(() => db.ticketTokenPublished(dbCtx, redeemedData), DBErrors.UnexpectedResult); + }); + }); // ticketTokenPublished + + describe('ticketTokenGetUnpublished', function () { + beforeEach(function () { + sinon.stub(db.statement.ticketTokenGetUnpublished, 'all'); + }); + it('success', function () { + db.statement.ticketTokenGetUnpublished.all.returns([]); + const result = db.ticketTokenGetUnpublished(); + assert.deepStrictEqual(result, []); + }); + it('failure', function () { + db.statement.ticketTokenGetUnpublished.all.throws(expectedException); + assert.throws(() => db.ticketTokenGetUnpublished(), expectedException); + }); + }); // ticketTokenGetUnpublished + + describe('_redeemedTicketToNative', function () { + let redeemedData; + beforeEach(function () { + redeemedData = { + resource: 'https://resource.example.com/', + subject: 'https://subject.example.com/', + iss: 'https://idp.example.com/', + ticket: 'xxxTICKETxxx', + token: 'xxxTOKENxxx', + created: 1701970607n, + published: 1701970670n, + }; + }); + it('covers', function () { + const expected = { + ...redeemedData, + created: new Date('2023-12-07T17:36:47.000Z'), + published: new Date('2023-12-07T17:37:50.000Z'), + }; + const result = DB._redeemedTicketToNative(redeemedData); + assert.deepStrictEqual(result, expected); + }); + it('covers no published', function () { + redeemedData.published = null; + const expected = { + ...redeemedData, + created: new Date('2023-12-07T17:36:47.000Z'), + published: null, + }; + const result = DB._redeemedTicketToNative(redeemedData); + assert.deepStrictEqual(result, expected); + }); + }); // _redeemedTicketToNative + }); // DatabaseSQLite