Changeset View
Changeset View
Standalone View
Standalone View
apps/alias-server/test/dbTests.js
// Copyright (c) 2023 The Bitcoin developers | // Copyright (c) 2023 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
'use strict'; | 'use strict'; | ||||
const assert = require('assert'); | const assert = require('assert'); | ||||
const { | const { | ||||
initializeDb, | initializeDb, | ||||
getServerState, | getServerState, | ||||
updateServerState, | updateServerState, | ||||
addOneAliasToDb, | addOneAliasToDb, | ||||
addAliasesToDb, | addAliasesToDb, | ||||
getAliasesFromDb, | getAliasesFromDb, | ||||
} = require('../src/db'); | } = require('../src/db'); | ||||
// Mock mongodb | // Mock mongodb | ||||
const { MongoClient } = require('mongodb'); | const { MongoClient } = require('mongodb'); | ||||
const { MongoMemoryServer } = require('mongodb-memory-server'); | const { MongoMemoryServer } = require('mongodb-memory-server'); | ||||
const { testAddressAliases } = require('./mocks/aliasMocks'); | const { generated } = require('./mocks/aliasMocks'); | ||||
describe('alias-server db.js', async function () { | describe('alias-server db.js', async function () { | ||||
let mongoServer, testMongoClient; | let mongoServer, testMongoClient; | ||||
before(async () => { | before(async () => { | ||||
// Start mongo memory server before running this suite of unit tests | // Start mongo memory server before running this suite of unit tests | ||||
mongoServer = await MongoMemoryServer.create(); | mongoServer = await MongoMemoryServer.create(); | ||||
const mongoUri = mongoServer.getUri(); | const mongoUri = mongoServer.getUri(); | ||||
testMongoClient = new MongoClient(mongoUri); | testMongoClient = new MongoClient(mongoUri); | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | it('If serverState exists on startup, initializeDb does not overwrite it', async function () { | ||||
); | ); | ||||
// Verify serverState has not reverted to initial value | // Verify serverState has not reverted to initial value | ||||
assert.deepEqual(fetchedServerStateOnRestart, newServerState); | assert.deepEqual(fetchedServerStateOnRestart, newServerState); | ||||
}); | }); | ||||
it('addAliasesToDb successfully adds new valid aliases to an empty collection', async function () { | it('addAliasesToDb successfully adds new valid aliases to an empty collection', async function () { | ||||
// newValidAliases needs to be a clone of the mock because | // newValidAliases needs to be a clone of the mock because | ||||
// each object gets an _id field when added to the database | // each object gets an _id field when added to the database | ||||
const newValidAliases = JSON.parse( | const newValidAliases = JSON.parse( | ||||
JSON.stringify(testAddressAliases.validAliasTxs), | JSON.stringify(generated.validAliasRegistrations), | ||||
); | ); | ||||
await addAliasesToDb(testDb, newValidAliases); | await addAliasesToDb(testDb, newValidAliases); | ||||
// Get the newly added valid aliases | // Get the newly added valid aliases | ||||
// Note we return valid aliases without the database _id field | // Note we return valid aliases without the database _id field | ||||
const addedValidAliases = await getAliasesFromDb(testDb); | const addedValidAliases = await getAliasesFromDb(testDb); | ||||
// Verify addedValidAliases match the added mock | // Verify addedValidAliases match the added mock | ||||
assert.deepEqual(addedValidAliases, testAddressAliases.validAliasTxs); | assert.deepEqual(addedValidAliases, generated.validAliasRegistrations); | ||||
}); | }); | ||||
it('addOneAliasToDb successfully adds a new valid alias to an empty collection', async function () { | it('addOneAliasToDb successfully adds a new valid alias to an empty collection', async function () { | ||||
// newValidAliases needs to be a clone of the mock because | // newValidAliases needs to be a clone of the mock because | ||||
// each object gets an _id field when added to the database | // each object gets an _id field when added to the database | ||||
const newValidAliases = JSON.parse( | const newValidAliases = JSON.parse( | ||||
JSON.stringify(testAddressAliases.validAliasTxs), | JSON.stringify(generated.validAliasRegistrations), | ||||
); | ); | ||||
const aliasAddedSuccess = await addOneAliasToDb( | const aliasAddedSuccess = await addOneAliasToDb( | ||||
testDb, | testDb, | ||||
newValidAliases[0], | newValidAliases[0], | ||||
); | ); | ||||
// Get the newly added valid aliases | // Get the newly added valid aliases | ||||
// Note we return valid aliases without the database _id field | // Note we return valid aliases without the database _id field | ||||
const addedValidAliases = await getAliasesFromDb(testDb); | const addedValidAliases = await getAliasesFromDb(testDb); | ||||
// Verify the function returns true on alias add success | // Verify the function returns true on alias add success | ||||
assert.strictEqual(aliasAddedSuccess, true); | assert.strictEqual(aliasAddedSuccess, true); | ||||
// Verify the database has the expected alias | // Verify the database has the expected alias | ||||
assert.deepEqual(addedValidAliases, [ | assert.deepEqual(addedValidAliases, [ | ||||
testAddressAliases.validAliasTxs[0], | generated.validAliasRegistrations[0], | ||||
]); | ]); | ||||
}); | }); | ||||
it('addOneAliasToDb successfully adds a new valid alias to an existing collection', async function () { | it('addOneAliasToDb successfully adds a new valid alias to an existing collection', async function () { | ||||
// newValidAliases needs to be a clone of the mock because | // newValidAliases needs to be a clone of the mock because | ||||
// each object gets an _id field when added to the database | // each object gets an _id field when added to the database | ||||
const newValidAliases = JSON.parse( | const newValidAliases = JSON.parse( | ||||
JSON.stringify(testAddressAliases.validAliasTxs), | JSON.stringify(generated.validAliasRegistrations), | ||||
); | ); | ||||
// Pre-populate the aliases collection | // Pre-populate the aliases collection | ||||
await addAliasesToDb(testDb, newValidAliases); | await addAliasesToDb(testDb, newValidAliases); | ||||
const newMockAlias = { | const newMockAlias = { | ||||
address: 'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48', | address: 'ecash:qrmz0egsqxj35x5jmzf8szrszdeu72fx0uxgwk3r48', | ||||
alias: 'rico', | alias: 'rico', | ||||
txid: '3ff9c28fa07cb88c87000ef0f5ee61953d874ffade154cd3f88fd60b88ea2879', | txid: '3ff9c28fa07cb88c87000ef0f5ee61953d874ffade154cd3f88fd60b88ea2879', | ||||
blockheight: 787674, | blockheight: 1787674, // Note, blockheight is purposefully set to be higher than mocks | ||||
}; | }; | ||||
// clone to check unit test result as _id will be added to newMockAlias | // clone to check unit test result as _id will be added to newMockAlias | ||||
const newMockAliasClone = JSON.parse(JSON.stringify(newMockAlias)); | const newMockAliasClone = JSON.parse(JSON.stringify(newMockAlias)); | ||||
// Add an alias tx that does not exist | // Add an alias tx that does not exist | ||||
const aliasAddedSuccess = await addOneAliasToDb(testDb, newMockAlias); | const aliasAddedSuccess = await addOneAliasToDb(testDb, newMockAlias); | ||||
// Get the newly added valid aliases | // Get the newly added valid aliases | ||||
// Note we return valid aliases without the database _id field | // Note we return valid aliases without the database _id field | ||||
const addedValidAliases = await getAliasesFromDb(testDb); | const addedValidAliases = await getAliasesFromDb(testDb); | ||||
// Verify the function returns true on alias add success | // Verify the function returns true on alias add success | ||||
assert.strictEqual(aliasAddedSuccess, true); | assert.strictEqual(aliasAddedSuccess, true); | ||||
// Verify the database has the expected alias | // Verify the database has the expected alias | ||||
assert.deepEqual( | assert.deepEqual( | ||||
addedValidAliases, | addedValidAliases, | ||||
testAddressAliases.validAliasTxs.concat(newMockAliasClone), | generated.validAliasRegistrations.concat(newMockAliasClone), | ||||
); | ); | ||||
}); | }); | ||||
it('addOneAliasToDb returns false and fails to add an alias if it is already in the database', async function () { | it('addOneAliasToDb returns false and fails to add an alias if it is already in the database', async function () { | ||||
// newValidAliases needs to be a clone of the mock because | // newValidAliases needs to be a clone of the mock because | ||||
// each object gets an _id field when added to the database | // each object gets an _id field when added to the database | ||||
const newValidAliases = JSON.parse( | const newValidAliases = JSON.parse( | ||||
JSON.stringify(testAddressAliases.validAliasTxs), | JSON.stringify(generated.validAliasRegistrations), | ||||
); | ); | ||||
// Pre-populate the aliases collection | // Pre-populate the aliases collection | ||||
await addAliasesToDb(testDb, newValidAliases); | await addAliasesToDb(testDb, newValidAliases); | ||||
const newMockAliasTxRegisteringExistingAlias = { | const newMockAliasTxRegisteringExistingAlias = newValidAliases[0]; | ||||
address: 'ecash:qzvydd4n3lm3xv62cx078nu9rg0e3srmqq0knykfed', | |||||
alias: 'foo', | |||||
txid: 'f41ccfbd88d228bbb695b771dd0c266b0351eda9a35aeb8c5e3cb7670e7e17cc', | |||||
blockheight: 776576, | |||||
}; | |||||
// Add an alias tx that does not exist | // Add an alias tx that does not exist | ||||
const aliasAddedSuccess = await addOneAliasToDb( | const aliasAddedSuccess = await addOneAliasToDb( | ||||
testDb, | testDb, | ||||
newMockAliasTxRegisteringExistingAlias, | newMockAliasTxRegisteringExistingAlias, | ||||
); | ); | ||||
// Get the newly added valid aliases | // Get the newly added valid aliases | ||||
// Note we return valid aliases without the database _id field | // Note we return valid aliases without the database _id field | ||||
const addedValidAliases = await getAliasesFromDb(testDb); | const addedValidAliases = await getAliasesFromDb(testDb); | ||||
// Verify the function returns true on alias add success | // Verify the function returns true on alias add success | ||||
assert.strictEqual(aliasAddedSuccess, false); | assert.strictEqual(aliasAddedSuccess, false); | ||||
// Verify the database has the expected aliases (without the failed add) | // Verify the database has the expected aliases (without the failed add) | ||||
assert.deepEqual(addedValidAliases, testAddressAliases.validAliasTxs); | assert.deepEqual(addedValidAliases, generated.validAliasRegistrations); | ||||
}); | }); | ||||
it('getAliasesFromDb returns an empty array if no aliases have been added to the collection', async function () { | it('getAliasesFromDb returns an empty array if no aliases have been added to the collection', async function () { | ||||
const validAliases = await getAliasesFromDb(testDb); | const validAliases = await getAliasesFromDb(testDb); | ||||
assert.deepEqual(validAliases, []); | assert.deepEqual(validAliases, []); | ||||
}); | }); | ||||
it('addAliasesToDb returns false if you attempt to add aliases whose alias already exists in the database', async function () { | it('addAliasesToDb returns false if you attempt to add aliases whose alias already exists in the database', async function () { | ||||
// Startup the app and initialize serverState | // Startup the app and initialize serverState | ||||
const testDb = await initializeDb(testMongoClient); | const testDb = await initializeDb(testMongoClient); | ||||
// newValidAliases needs to be a clone of the mock because | // newValidAliases needs to be a clone of the mock because | ||||
// each object gets an _id field when added to the database | // each object gets an _id field when added to the database | ||||
const newValidAliases = JSON.parse( | const newValidAliases = JSON.parse( | ||||
JSON.stringify(testAddressAliases.validAliasTxs), | JSON.stringify(generated.validAliasRegistrations), | ||||
); | ); | ||||
await addAliasesToDb(testDb, newValidAliases); | await addAliasesToDb(testDb, newValidAliases); | ||||
// Try to add three aliases that already exists in the database | // Try to add three aliases that already exists in the database | ||||
const newValidAliasAlreadyInDb = JSON.parse( | const newValidAliasAlreadyInDb = JSON.parse( | ||||
JSON.stringify(testAddressAliases.validAliasTxs.slice(0, 3)), | JSON.stringify(generated.validAliasRegistrations.slice(0, 3)), | ||||
); | ); | ||||
const failedResult = await addAliasesToDb( | const failedResult = await addAliasesToDb( | ||||
testDb, | testDb, | ||||
newValidAliasAlreadyInDb, | newValidAliasAlreadyInDb, | ||||
); | ); | ||||
// Verify addAliasesToDb returned false on attempt to add duplicate aliases to the db | // Verify addAliasesToDb returned false on attempt to add duplicate aliases to the db | ||||
assert.deepEqual(failedResult, false); | assert.deepEqual(failedResult, false); | ||||
}); | }); | ||||
}); | }); |