Skip to content

Commit

Permalink
Add nodesync export and sync integration tests (#884)
Browse files Browse the repository at this point in the history
* Creatornode /export integration test working

* Polish

* Sync test working

* Lint fixes

* Reorganized hooks and test structure

* Add nock dependency

* PR revisions
  • Loading branch information
piazzatron committed Oct 5, 2020
1 parent bd8d544 commit 09ce76b
Show file tree
Hide file tree
Showing 11 changed files with 2,407 additions and 43 deletions.
88 changes: 69 additions & 19 deletions creator-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions creator-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
},
"devDependencies": {
"mocha": "^5.2.0",
"nock": "^13.0.4",
"nodemon": "^1.18.6",
"nyc": "^15.0.0",
"sequelize-cli": "^5.3.0",
Expand Down
3 changes: 2 additions & 1 deletion creator-node/src/routes/nodeSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ async function _nodesync (req, walletPublicKeys, creatorNodeEndpoint, dbOnlySync
walletPublicKey: fetchedWalletPublicKey,
latestBlockNumber: fetchedLatestBlockNumber,
lastLogin: fetchedCNodeUser.lastLogin,
clock: fetchedCNodeUser.clock
clock: fetchedCNodeUser.clock,
createdAt: fetchedCNodeUser.createdAt
}, { transaction })
req.logger.info(redisKey, `Inserted nodeUser for cnodeUserUUID ${fetchedCnodeUserUUID}`)

Expand Down
14 changes: 2 additions & 12 deletions creator-node/test/dbManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const models = require('../src/models')
const DBManager = require('../src/dbManager')
const blacklistManager = require('../src/blacklistManager')
const utils = require('../src/utils')
const { createStarterCNodeUser } = require('./lib/dataSeeds')
const { createStarterCNodeUser, getCNodeUser, destroyUsers } = require('./lib/dataSeeds')
const { getApp } = require('./lib/app')
const { getIPFSMock } = require('./lib/ipfsMock')
const { getLibsMock } = require('./lib/libsMock')
Expand All @@ -18,11 +18,6 @@ describe('Test createNewDataRecord()', () => {
}
}

const getCNodeUser = async (cnodeUserUUID) => {
const cnodeUser = await models.CNodeUser.findOne({ where: { cnodeUserUUID } })
return cnodeUser.dataValues
}

const initialClockVal = 0
const timeoutMs = 1000

Expand All @@ -37,12 +32,7 @@ describe('Test createNewDataRecord()', () => {
/** Reset DB state + Create cnodeUser + confirm initial clock state + define global vars */
beforeEach(async function () {
// Wipe all CNodeUsers + dependent data
await models.CNodeUser.destroy({
where: {},
truncate: true,
cascade: true // cascades delete to all rows with foreign key on cnodeUser
})

await destroyUsers()
const resp = await createStarterCNodeUser()
cnodeUserUUID = resp.cnodeUserUUID
req.session = { cnodeUserUUID }
Expand Down
1 change: 1 addition & 0 deletions creator-node/test/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async function getApp (ipfsMock, libsMock, blacklistManager) {

const mockServiceRegistry = {
ipfs: ipfsMock,
ipfsLatest: ipfsMock,
libs: libsMock,
blacklistManager: blacklistManager,
redis: redisClient
Expand Down
15 changes: 14 additions & 1 deletion creator-node/test/lib/dataSeeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ const testEthereumConstants = {
privKeyHex: 'acd6db99f7354043bf7a14a4fbb81b348e028717933eda978afd97b3e80cf1da'
}

const getCNodeUser = async (cnodeUserUUID) => {
const { dataValues } = await CNodeUser.findOne({ where: { cnodeUserUUID } })
return dataValues
}

const destroyUsers = async () => (
CNodeUser.destroy({
where: {},
truncate: true,
cascade: true // cascades delete to all rows with foreign key on cnodeUser
})
)

async function createStarterCNodeUser () {
return createStarterCNodeUserWithKey(testEthereumConstants.pubKey.toLowerCase())
}
Expand All @@ -19,4 +32,4 @@ async function createStarterCNodeUserWithKey (walletPublicKey) {
}
}

module.exports = { createStarterCNodeUser, createStarterCNodeUserWithKey, testEthereumConstants }
module.exports = { createStarterCNodeUser, createStarterCNodeUserWithKey, testEthereumConstants, getCNodeUser, destroyUsers }
18 changes: 17 additions & 1 deletion creator-node/test/lib/ipfsMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ function getIPFSMock () {
pin: {
add: sinon.mock(),
rm: sinon.mock()
}
},
id: sinon.stub(),
swarm: {
connect: sinon.stub()
},
bootstrap: {
add: sinon.stub()
},
cat: sinon.stub(),
get: sinon.stub()
}
ipfsMock.add.returns([{ hash: 'testCIDLink' }])
ipfsMock.addFromFs.returns([{ hash: 'testCIDLink' }])
ipfsMock.id.returns({
addresses: ['/ip4/127.0.0.1/tcp/4001/ipfs/QmPjSNZPCTmQsKAUM7QDjAzymcbxaVVbRcV5pZvBRMZmca']
})
ipfsMock.swarm.connect.returns({ Strings: ['test-res'] })
ipfsMock.bootstrap.add.returns('')
ipfsMock.cat.throws()
ipfsMock.get.throws()

return ipfsMock
}
Expand Down
1 change: 1 addition & 0 deletions creator-node/test/lib/libsMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function getLibsMock () {
libsMock.ethContracts.ServiceProviderFactoryClient.getServiceProviderIdFromAddress.returns('1')
libsMock.ethContracts.ServiceProviderFactoryClient.getServiceProviderInfo.returns({ 'endpoint': 'http://localhost:5000' })
libsMock.User.getUsers.returns([{ 'creator_node_endpoint': 'http://localhost:5000', 'blocknumber': 10, 'track_blocknumber': 10 }])
libsMock.User.getUsers.atMost(10)

return libsMock
}
Expand Down
8 changes: 8 additions & 0 deletions creator-node/test/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const stringifiedDateFields = (obj) => {
const newObj = { ...obj }
if (newObj.createdAt) newObj.createdAt = newObj.createdAt.toISOString()
if (newObj.updatedAt) newObj.updatedAt = newObj.updatedAt.toISOString()
return newObj
}

module.exports = { stringifiedDateFields }

0 comments on commit 09ce76b

Please sign in to comment.