mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-consumer.git
synced 2026-09-21 16:52:03 -07:00
Merge pull request #178 from Permissionless-Software-Foundation/dh-test-coverage
feat(tests): Increased unit test coverage
This commit is contained in:
@@ -128,6 +128,7 @@ class UsageUseCases {
|
|||||||
// Delete this code after debugging
|
// Delete this code after debugging
|
||||||
const usage = await this.UsageModel.find({})
|
const usage = await this.UsageModel.find({})
|
||||||
console.log('clearUsage() usage: ', usage)
|
console.log('clearUsage() usage: ', usage)
|
||||||
|
return true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error in usage-use-cases.js/clearUsage()')
|
console.error('Error in usage-use-cases.js/clearUsage()')
|
||||||
throw err
|
throw err
|
||||||
@@ -141,9 +142,9 @@ class UsageUseCases {
|
|||||||
const thisRestCall = restCalls[i]
|
const thisRestCall = restCalls[i]
|
||||||
|
|
||||||
// Debugging: delete this code after debugging
|
// Debugging: delete this code after debugging
|
||||||
if (i === 5) {
|
// if (i === 5) {
|
||||||
console.log('saveUsage() thisRestCall: ', thisRestCall)
|
// console.log('saveUsage() thisRestCall: ', thisRestCall)
|
||||||
}
|
// }
|
||||||
|
|
||||||
const usageData = {
|
const usageData = {
|
||||||
ip: thisRestCall.ip,
|
ip: thisRestCall.ip,
|
||||||
@@ -155,6 +156,7 @@ class UsageUseCases {
|
|||||||
const usage = new this.UsageModel(usageData)
|
const usage = new this.UsageModel(usageData)
|
||||||
await usage.save()
|
await usage.save()
|
||||||
}
|
}
|
||||||
|
return true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error in usage-use-cases.js/saveUsage()')
|
console.error('Error in usage-use-cases.js/saveUsage()')
|
||||||
throw err
|
throw err
|
||||||
@@ -167,13 +169,16 @@ class UsageUseCases {
|
|||||||
const usage = await this.UsageModel.find({})
|
const usage = await this.UsageModel.find({})
|
||||||
// console.log('usage: ', usage)
|
// console.log('usage: ', usage)
|
||||||
|
|
||||||
|
// Debugging: delete this code after debugging
|
||||||
if (usage[5]) {
|
if (usage[5]) {
|
||||||
console.log('loadUsage() usage[5]: ', usage[5])
|
console.log('loadUsage() usage[5]: ', usage[5])
|
||||||
}
|
}
|
||||||
|
|
||||||
restCalls = usage
|
restCalls = usage
|
||||||
|
return usage
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error in usage-use-cases.js/loadUsage(): ', err)
|
console.error('Error in usage-use-cases.js/loadUsage(): ', err)
|
||||||
|
return false
|
||||||
// throw err
|
// throw err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,21 @@ describe('#adapters', () => {
|
|||||||
|
|
||||||
assert.equal(result, true)
|
assert.equal(result, true)
|
||||||
})
|
})
|
||||||
|
it('should not start ipfs on test enviroment', async () => {
|
||||||
|
// Mock dependencies
|
||||||
|
uut.config.getJwtAtStartup = true
|
||||||
|
uut.config.useIpfs = true
|
||||||
|
uut.config.env = 'test'
|
||||||
|
|
||||||
|
sandbox.stub(uut.fullStackJwt, 'getJWT').resolves()
|
||||||
|
sandbox.stub(uut.fullStackJwt, 'instanceBchjs').resolves()
|
||||||
|
const ipfsSpy = sandbox.stub(uut.ipfs, 'start').resolves(null)
|
||||||
|
|
||||||
|
const result = await uut.start()
|
||||||
|
|
||||||
|
assert.isTrue(ipfsSpy.notCalled)
|
||||||
|
assert.equal(result, true)
|
||||||
|
})
|
||||||
|
|
||||||
it('should catch and throw an error', async () => {
|
it('should catch and throw an error', async () => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -19,16 +19,17 @@ describe('Admin', () => {
|
|||||||
|
|
||||||
if (!config.noMongo) {
|
if (!config.noMongo) {
|
||||||
describe('loginAdmin()', () => {
|
describe('loginAdmin()', () => {
|
||||||
// it('should login admin', async () => {
|
it('should login admin', async () => {
|
||||||
// try {
|
try {
|
||||||
// sandbox.stub(uut.axios, 'request').resolves(true)
|
sandbox.stub(uut.jsonFiles, 'readJSON').resolves({ password: 'pass' })
|
||||||
|
sandbox.stub(uut.axios, 'request').resolves(true)
|
||||||
|
|
||||||
// const result = await uut.loginAdmin()
|
const result = await uut.loginAdmin()
|
||||||
// assert.isTrue(result)
|
assert.isTrue(result)
|
||||||
// } catch (err) {
|
} catch (err) {
|
||||||
// assert(false, 'Unexpected result')
|
assert(false, 'Unexpected result')
|
||||||
// }
|
}
|
||||||
// })
|
})
|
||||||
|
|
||||||
it('should handle axios error', async () => {
|
it('should handle axios error', async () => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -41,5 +41,17 @@ describe('#passport.js', () => {
|
|||||||
assert.include(err.message, 'cant auth user')
|
assert.include(err.message, 'cant auth user')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
it('should authenticate user', async () => {
|
||||||
|
const ctx = {}
|
||||||
|
const errMock = null
|
||||||
|
const userMock = {
|
||||||
|
_id: '123',
|
||||||
|
email: 'test@test.com'
|
||||||
|
}
|
||||||
|
sandbox.stub(uut.passport, 'authenticate').yields(errMock, userMock)
|
||||||
|
const user = await uut.authUser(ctx)
|
||||||
|
assert.equal(user._id, userMock._id)
|
||||||
|
assert.equal(user.email, userMock.email)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -54,6 +54,13 @@ describe('#User-Adapter', () => {
|
|||||||
|
|
||||||
assert.notEqual(testuser.password, 'password')
|
assert.notEqual(testuser.password, 'password')
|
||||||
})
|
})
|
||||||
|
it('should ignore password encrption if password property is not provided', async () => {
|
||||||
|
const lastPassword = testuser.password
|
||||||
|
await testuser.save()
|
||||||
|
// console.log('testuser: ', testuser)
|
||||||
|
|
||||||
|
assert.equal(testuser.password, lastPassword)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('#validatePassword', () => {
|
describe('#validatePassword', () => {
|
||||||
|
|||||||
@@ -38,4 +38,25 @@ describe('#Controllers', () => {
|
|||||||
await uut.attachControllers(app)
|
await uut.attachControllers(app)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('#attachRESTControllers', () => {
|
||||||
|
it('should attach the controllers', async () => {
|
||||||
|
const app = {
|
||||||
|
use: () => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
await uut.attachRESTControllers(app)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('#initAdapters', () => {
|
||||||
|
it('should attach the controllers', async () => {
|
||||||
|
sandbox.stub(uut.adapters, 'start').resolves({})
|
||||||
|
await uut.initAdapters()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('#initUseCases', () => {
|
||||||
|
it('should attach the controllers', async () => {
|
||||||
|
sandbox.stub(uut.useCases, 'start').resolves({})
|
||||||
|
await uut.initUseCases()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -79,11 +79,22 @@ describe('#Auth-REST-Router', () => {
|
|||||||
await uut.authUser(ctx)
|
await uut.authUser(ctx)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should catch and throw an error', async () => {
|
it('should catch and throw passport error', async () => {
|
||||||
try {
|
try {
|
||||||
// Force an error
|
// Force an error
|
||||||
sandbox.stub(uut.passport, 'authUser').rejects('test error')
|
sandbox.stub(uut.passport, 'authUser').rejects('test error')
|
||||||
|
|
||||||
|
await uut.authUser(ctx)
|
||||||
|
} catch (err) {
|
||||||
|
// console.log('err: ', err)
|
||||||
|
assert.include(err.message, 'Unauthorized')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
it('should handle error if user is not found!', async () => {
|
||||||
|
try {
|
||||||
|
// Force an error
|
||||||
|
sandbox.stub(uut.passport, 'authUser').resolves(null)
|
||||||
|
|
||||||
await uut.authUser(ctx)
|
await uut.authUser(ctx)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// console.log('err: ', err)
|
// console.log('err: ', err)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ describe('#RESTControllers', () => {
|
|||||||
let sandbox
|
let sandbox
|
||||||
// let ctx
|
// let ctx
|
||||||
|
|
||||||
before(async () => {})
|
before(async () => { })
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const useCases = new UseCasesMock()
|
const useCases = new UseCasesMock()
|
||||||
@@ -64,4 +64,19 @@ describe('#RESTControllers', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('#attachRESTControllers', () => {
|
||||||
|
it('should attach controllers without mongo service', () => {
|
||||||
|
uut.config.noMongo = true
|
||||||
|
|
||||||
|
const app = { use: () => {} }
|
||||||
|
uut.attachRESTControllers(app)
|
||||||
|
})
|
||||||
|
it('should attach controllers with mongo service', () => {
|
||||||
|
uut.config.noMongo = false
|
||||||
|
|
||||||
|
const app = { use: () => {} }
|
||||||
|
uut.attachRESTControllers(app)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -153,6 +153,20 @@ describe('#Users-REST-Controller', () => {
|
|||||||
// Assert that expected properties exist in the returned data.
|
// Assert that expected properties exist in the returned data.
|
||||||
assert.property(ctx.response.body, 'user')
|
assert.property(ctx.response.body, 'user')
|
||||||
})
|
})
|
||||||
|
it('should run next function if it exists', async () => {
|
||||||
|
// Mock dependencies
|
||||||
|
const nextSpy = sandbox.spy()
|
||||||
|
sandbox.stub(uut.useCases.user, 'getUser').resolves({ _id: '123' })
|
||||||
|
|
||||||
|
await uut.getUser(ctx, nextSpy)
|
||||||
|
|
||||||
|
// Assert the expected HTTP response
|
||||||
|
assert.equal(ctx.status, 200)
|
||||||
|
|
||||||
|
// Assert that expected properties exist in the returned data.
|
||||||
|
assert.property(ctx.response.body, 'user')
|
||||||
|
assert.isTrue(nextSpy.calledOnce)
|
||||||
|
})
|
||||||
|
|
||||||
it('should return other error status passed by biz logic', async () => {
|
it('should return other error status passed by biz logic', async () => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -107,4 +107,42 @@ describe('#Users-REST-Router', () => {
|
|||||||
assert.isTrue(validationSpy.calledOnce, 'Admin validator should be called')
|
assert.isTrue(validationSpy.calledOnce, 'Admin validator should be called')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('#getAll', () => {
|
||||||
|
it('should route to controller', async () => {
|
||||||
|
sandbox.stub(uut.validators, 'ensureUser').resolves(true)
|
||||||
|
const spy = sandbox.stub(uut.userRESTController, 'getUsers').resolves(true)
|
||||||
|
|
||||||
|
await uut.getAll()
|
||||||
|
assert.isTrue(spy.calledOnce)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('#getById', () => {
|
||||||
|
it('should route to controller', async () => {
|
||||||
|
sandbox.stub(uut.validators, 'ensureUser').resolves(true)
|
||||||
|
const spy = sandbox.stub(uut.userRESTController, 'getUser').resolves(true)
|
||||||
|
|
||||||
|
await uut.getById()
|
||||||
|
assert.isTrue(spy.calledOnce)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('#updateUser', () => {
|
||||||
|
it('should route to controller', async () => {
|
||||||
|
sandbox.stub(uut.validators, 'ensureTargetUserOrAdmin').resolves(true)
|
||||||
|
sandbox.stub(uut.userRESTController, 'getUser').resolves(true)
|
||||||
|
const spy = sandbox.stub(uut.userRESTController, 'updateUser').resolves(true)
|
||||||
|
|
||||||
|
await uut.updateUser()
|
||||||
|
assert.isTrue(spy.calledOnce)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
describe('#deleteUser', () => {
|
||||||
|
it('should route to controller', async () => {
|
||||||
|
sandbox.stub(uut.validators, 'ensureTargetUserOrAdmin').resolves(true)
|
||||||
|
sandbox.stub(uut.userRESTController, 'getUser').resolves(true)
|
||||||
|
const spy = sandbox.stub(uut.userRESTController, 'deleteUser').resolves(true)
|
||||||
|
|
||||||
|
await uut.deleteUser()
|
||||||
|
assert.isTrue(spy.calledOnce)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -34,6 +34,12 @@ describe('#passport', () => {
|
|||||||
|
|
||||||
passportCallback(id, 'password', done)
|
passportCallback(id, 'password', done)
|
||||||
})
|
})
|
||||||
|
it('should handle not found user', () => {
|
||||||
|
// Mock Users model.
|
||||||
|
sandbox.stub(User, 'findOne').resolves(null)
|
||||||
|
|
||||||
|
passportCallback(id, 'password', done)
|
||||||
|
})
|
||||||
|
|
||||||
it('should return if password is validated', () => {
|
it('should return if password is validated', () => {
|
||||||
// Mock Users model.
|
// Mock Users model.
|
||||||
@@ -41,6 +47,15 @@ describe('#passport', () => {
|
|||||||
|
|
||||||
passportCallback(id, 'password', done)
|
passportCallback(id, 'password', done)
|
||||||
})
|
})
|
||||||
|
it('should handle error on password validation', () => {
|
||||||
|
// Mock Users model.
|
||||||
|
const userMock = {
|
||||||
|
validatePassword: () => false
|
||||||
|
}
|
||||||
|
sandbox.stub(User, 'findOne').resolves(userMock)
|
||||||
|
|
||||||
|
passportCallback(id, 'password', done)
|
||||||
|
})
|
||||||
|
|
||||||
it('should catch a high-level error', () => {
|
it('should catch a high-level error', () => {
|
||||||
// Force an error
|
// Force an error
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ describe('#server', () => {
|
|||||||
sandbox.stub(uut.adminLib, 'createSystemUser').resolves(true)
|
sandbox.stub(uut.adminLib, 'createSystemUser').resolves(true)
|
||||||
sandbox.stub(uut.controllers, 'attachControllers').resolves()
|
sandbox.stub(uut.controllers, 'attachControllers').resolves()
|
||||||
uut.config.env = 'dev'
|
uut.config.env = 'dev'
|
||||||
|
uut.config.port = 5040
|
||||||
const result = await uut.startServer()
|
const result = await uut.startServer()
|
||||||
// console.log('result: ', result)
|
// console.log('result: ', result)
|
||||||
|
|
||||||
|
|||||||
@@ -70,6 +70,39 @@ const localdb = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Usage: class Usage {
|
||||||
|
static findById () {}
|
||||||
|
static find () {}
|
||||||
|
static findOne () {
|
||||||
|
return {
|
||||||
|
validatePassword: localdb.validatePassword
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async save () {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
generateToken () {
|
||||||
|
return '123'
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON () {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
async remove () {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
async validatePassword () {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
static async deleteMany(){
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
validatePassword: () => {
|
validatePassword: () => {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -255,4 +255,67 @@ describe('#usage-use-case', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('#clearUsage', () => {
|
||||||
|
it('should clear the usage database data', async () => {
|
||||||
|
const res = await uut.clearUsage()
|
||||||
|
assert.isTrue(res)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle error', async () => {
|
||||||
|
try {
|
||||||
|
sandbox.stub(uut.UsageModel, 'deleteMany').throws(new Error('uut error'))
|
||||||
|
await uut.clearUsage()
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (error) {
|
||||||
|
assert.equal(error.message, 'uut error')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#saveUsage', () => {
|
||||||
|
it('should save usage', async () => {
|
||||||
|
// Set mock data
|
||||||
|
restCalls.push({
|
||||||
|
timestamp: new Date().getTime(),
|
||||||
|
ip: 'localhost',
|
||||||
|
url: 'fakeUrl',
|
||||||
|
method: 'unit test'
|
||||||
|
})
|
||||||
|
const res = await uut.saveUsage()
|
||||||
|
assert.isTrue(res)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should handle error', async () => {
|
||||||
|
try {
|
||||||
|
restCalls.push(null)
|
||||||
|
await uut.saveUsage()
|
||||||
|
assert.fail('Unexpected code path')
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
assert.include(error.message, 'Cannot read properties')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('#loadUsage', () => {
|
||||||
|
it('should load usage', async () => {
|
||||||
|
// Set mock data
|
||||||
|
const mockObj = {
|
||||||
|
timestamp: new Date().getTime(),
|
||||||
|
ip: 'localhost',
|
||||||
|
url: 'fakeUrl',
|
||||||
|
method: 'unit test'
|
||||||
|
}
|
||||||
|
sandbox.stub(uut.UsageModel, 'find').returns(new Array(10).fill(null).map((_, i) => (mockObj)))
|
||||||
|
const res = await uut.loadUsage()
|
||||||
|
assert.equal(restCalls.length, 10)
|
||||||
|
assert.equal(res.length, 10)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should skip error', async () => {
|
||||||
|
sandbox.stub(uut.UsageModel, 'find').throws(new Error('uut error'))
|
||||||
|
const res = await uut.loadUsage()
|
||||||
|
assert.isFalse(res)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user