mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-service.git
synced 2026-09-21 16:52:03 -07:00
fix(tests): Increased coverage of REST API validators
This commit is contained in:
@@ -0,0 +1,306 @@
|
||||
/*
|
||||
Unit tests for the REST API middleware that validates users.
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
import { assert } from 'chai'
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local libraries
|
||||
import Validators from '../../../../../src/controllers/rest-api/middleware/validators.js'
|
||||
import { context as mockContext } from '../../../../unit/mocks/ctx-mock.js'
|
||||
|
||||
describe('#Validators', () => {
|
||||
let uut
|
||||
let ctx
|
||||
let sandbox
|
||||
|
||||
beforeEach(() => {
|
||||
uut = new Validators()
|
||||
|
||||
// Mock the context object.
|
||||
ctx = mockContext()
|
||||
|
||||
sandbox = sinon.createSandbox()
|
||||
})
|
||||
|
||||
afterEach(() => sandbox.restore())
|
||||
|
||||
describe('#getToken', () => {
|
||||
it('should return null if no header is provided', () => {
|
||||
const result = uut.getToken(ctx)
|
||||
|
||||
assert.equal(result, null)
|
||||
})
|
||||
|
||||
it('should return null if header is not in two parts', () => {
|
||||
ctx.request.header.authorization = 'Bearer'
|
||||
|
||||
const result = uut.getToken(ctx)
|
||||
|
||||
assert.equal(result, null)
|
||||
})
|
||||
|
||||
it('should return null if first part of header does not container the word bearer', () => {
|
||||
ctx.request.header.authorization = 'some thing'
|
||||
|
||||
const result = uut.getToken(ctx)
|
||||
|
||||
assert.equal(result, null)
|
||||
})
|
||||
|
||||
it('should return the JWT token from the header', () => {
|
||||
const jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYzMjNiNTUwNzgxYWYzNTc4YzI0ZmU5YiIsImlhdCI6MTY2MzQ0NDczNCwiZXhwIjoxNjYzNTMxMTM0fQ.BY5sOfXc4z5axS98CdTfyqnO9y2wijOlwnv52rcvxHA'
|
||||
ctx.request.header.authorization = `Bearer ${jwt}`
|
||||
|
||||
const result = uut.getToken(ctx)
|
||||
|
||||
assert.equal(result, jwt)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#ensureUser', () => {
|
||||
it('should throw error if token is not provided', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns()
|
||||
|
||||
await uut.ensureUser(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error if token can not be verified', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').throws(new Error('test error'))
|
||||
|
||||
await uut.ensureUser(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error if user can not be found in database', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves(false)
|
||||
|
||||
await uut.ensureUser(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should return true if the user is verified', async () => {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves({ user: 'alice' })
|
||||
|
||||
const result = await uut.ensureUser(ctx)
|
||||
|
||||
assert.equal(result, true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#ensureUser', () => {
|
||||
it('should throw error if token is not provided', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns()
|
||||
|
||||
await uut.ensureAdmin(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error if token can not be verified', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').throws(new Error('test error'))
|
||||
|
||||
await uut.ensureAdmin(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error if user can not be found in database', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves(false)
|
||||
|
||||
await uut.ensureAdmin(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error if user is not an admin', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves({ type: 'user' })
|
||||
|
||||
await uut.ensureAdmin(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should return true if the user is an admin', async () => {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves({ type: 'admin' })
|
||||
|
||||
const result = await uut.ensureAdmin(ctx)
|
||||
|
||||
assert.equal(result, true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#ensureTargetUserOrAdmin', () => {
|
||||
it('should throw error if token is not provided', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns()
|
||||
|
||||
await uut.ensureTargetUserOrAdmin(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error if token can not be verified', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').throws(new Error('test error'))
|
||||
|
||||
ctx.params = {
|
||||
id: '456'
|
||||
}
|
||||
|
||||
await uut.ensureTargetUserOrAdmin(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error if user can not be found in database', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves(false)
|
||||
|
||||
ctx.params = {
|
||||
id: '456'
|
||||
}
|
||||
|
||||
await uut.ensureTargetUserOrAdmin(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error if user is not an admin', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves({ type: 'user' })
|
||||
|
||||
ctx.params = {
|
||||
id: '456'
|
||||
}
|
||||
|
||||
await uut.ensureTargetUserOrAdmin(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error is user is not admin or target user', async () => {
|
||||
try {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves({ type: 'user', _id: '123' })
|
||||
|
||||
ctx.params = {
|
||||
id: '456'
|
||||
}
|
||||
|
||||
await uut.ensureTargetUserOrAdmin(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(ctx.status, 401)
|
||||
}
|
||||
})
|
||||
|
||||
it('should return true if the user is an admin', async () => {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves({ type: 'admin', _id: '123' })
|
||||
|
||||
ctx.params = {
|
||||
id: '456'
|
||||
}
|
||||
|
||||
const result = await uut.ensureTargetUserOrAdmin(ctx)
|
||||
|
||||
assert.equal(result, true)
|
||||
})
|
||||
|
||||
it('should return true if the user is the target user', async () => {
|
||||
// Mock dependencies and force desired code path
|
||||
sandbox.stub(uut, 'getToken').returns('fake-jwt')
|
||||
sandbox.stub(uut.jwt, 'verify').returns({})
|
||||
sandbox.stub(uut.User, 'findById').resolves({ type: 'user', _id: '123' })
|
||||
|
||||
ctx.params = {
|
||||
id: '123'
|
||||
}
|
||||
|
||||
const result = await uut.ensureTargetUserOrAdmin(ctx)
|
||||
|
||||
assert.equal(result, true)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -4,12 +4,10 @@
|
||||
|
||||
// Public npm libraries
|
||||
import { assert } from 'chai'
|
||||
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local support libraries
|
||||
import adapters from '../../../mocks/adapters/index.js'
|
||||
|
||||
import UseCasesMock from '../../../mocks/use-cases/index.js'
|
||||
import UserController from '../../../../../src/controllers/rest-api/users/controller.js'
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
Unit tests for the config directory
|
||||
*/
|
||||
|
||||
import { assert } from 'chai'
|
||||
|
||||
let currentEnv
|
||||
|
||||
describe('#config', () => {
|
||||
@@ -15,8 +17,34 @@ describe('#config', () => {
|
||||
process.env.SVC_ENV = currentEnv
|
||||
})
|
||||
|
||||
it('Should return development environment config', () => {
|
||||
// import config from '../../../config/index.js'
|
||||
it('Should return development environment config by default', async () => {
|
||||
const importedConfig = await import('../../../config/index.js')
|
||||
const config = importedConfig.default
|
||||
// console.log('config: ', config)
|
||||
|
||||
assert.equal(config.env, 'dev')
|
||||
})
|
||||
|
||||
it('Should return test environment config', async () => {
|
||||
// Hack to dynamically import a library multiple times:
|
||||
// https://github.com/denoland/deno/issues/6946
|
||||
|
||||
process.env.SVC_ENV = 'test'
|
||||
|
||||
const importedConfig2 = await import('../../../config/index.js?foo=bar1')
|
||||
const config = importedConfig2.default
|
||||
// console.log('config: ', config)
|
||||
|
||||
assert.equal(config.env, 'test')
|
||||
})
|
||||
|
||||
it('Should return test environment config', async () => {
|
||||
process.env.SVC_ENV = 'prod'
|
||||
|
||||
const importedConfig3 = await import('../../../config/index.js?foo=bar2')
|
||||
const config = importedConfig3.default
|
||||
// console.log('config: ', config)
|
||||
|
||||
assert.equal(config.env, 'prod')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,78 +1,93 @@
|
||||
// /*
|
||||
// Unit tests for the passport library.
|
||||
// */
|
||||
//
|
||||
// // Public npm libraries
|
||||
// // const assert = require('chai').assert
|
||||
// import sinon from 'sinon';
|
||||
//
|
||||
// // Local libraries
|
||||
// import User from '../../../src/adapters/localdb/models/users.js';
|
||||
//
|
||||
// import { passport, passportCallback } from '../../../config/passport.js';
|
||||
// import adaptersMock from '../mocks/adapters/index.js';
|
||||
//
|
||||
// describe('#passport', () => {
|
||||
// let sandbox
|
||||
// let id
|
||||
// let done
|
||||
//
|
||||
// beforeEach(() => {
|
||||
// sandbox = sinon.createSandbox()
|
||||
//
|
||||
// id = 'abc123'
|
||||
// done = () => {}
|
||||
// })
|
||||
//
|
||||
// afterEach(() => sandbox.restore())
|
||||
//
|
||||
// describe('#serializeUser', () => {
|
||||
// it('should serialize a user', () => {
|
||||
// const user = {
|
||||
// id: 'abc123'
|
||||
// }
|
||||
// const done = () => {}
|
||||
//
|
||||
// passport.serializeUser(user, done)
|
||||
// })
|
||||
// })
|
||||
//
|
||||
// describe('#deserializeUser', () => {
|
||||
// it('should deserialize a user', () => {
|
||||
// // Mock Users model.
|
||||
// sandbox.stub(User, 'findById').resolves({ id })
|
||||
//
|
||||
// passport.deserializeUser(id, done)
|
||||
// })
|
||||
//
|
||||
// it('should catch and handle errors', () => {
|
||||
// // Force an error
|
||||
// sandbox.stub(User, 'findById').rejects(new Error('test error'))
|
||||
//
|
||||
// passport.deserializeUser(id, done)
|
||||
// })
|
||||
// })
|
||||
//
|
||||
// describe('#passportCallback', () => {
|
||||
// it('should return if user is found', () => {
|
||||
// // Mock Users model.
|
||||
// sandbox.stub(User, 'findOne').resolves({ id })
|
||||
//
|
||||
// passportCallback(id, 'password', done)
|
||||
// })
|
||||
//
|
||||
// it('should return if password is validated', () => {
|
||||
// // Mock Users model.
|
||||
// sandbox.stub(User, 'findOne').resolves(new adaptersMock.localdb.Users())
|
||||
//
|
||||
// passportCallback(id, 'password', done)
|
||||
// })
|
||||
//
|
||||
// it('should catch a high-level error', () => {
|
||||
// // Force an error
|
||||
// sandbox.stub(User, 'findOne').rejects(new Error('test error'))
|
||||
//
|
||||
// passportCallback(id, 'password', done)
|
||||
// })
|
||||
// })
|
||||
// })
|
||||
/*
|
||||
Unit tests for the passport library.
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
import { assert } from 'chai'
|
||||
import sinon from 'sinon'
|
||||
import passport from 'koa-passport'
|
||||
|
||||
// Local libraries
|
||||
import User from '../../../src/adapters/localdb/models/users.js'
|
||||
|
||||
import { applyPassportMods, passportCallback } from '../../../config/passport.js'
|
||||
import adaptersMock from '../mocks/adapters/index.js'
|
||||
|
||||
describe('#passport', () => {
|
||||
let sandbox
|
||||
let id
|
||||
let done
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = sinon.createSandbox()
|
||||
|
||||
id = 'abc123'
|
||||
done = () => {}
|
||||
})
|
||||
|
||||
afterEach(() => sandbox.restore())
|
||||
|
||||
describe('#passportCallback', () => {
|
||||
it('should return if user is found', () => {
|
||||
// Mock Users model.
|
||||
sandbox.stub(User, 'findOne').resolves({ id })
|
||||
|
||||
passportCallback(id, 'password', done)
|
||||
})
|
||||
|
||||
it('should return if password is validated', () => {
|
||||
// Mock Users model.
|
||||
sandbox.stub(User, 'findOne').resolves(new adaptersMock.localdb.Users())
|
||||
|
||||
passportCallback(id, 'password', done)
|
||||
})
|
||||
|
||||
it('should catch a high-level error', () => {
|
||||
// Force an error
|
||||
sandbox.stub(User, 'findOne').rejects(new Error('test error'))
|
||||
|
||||
passportCallback(id, 'password', done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#applyPassportMods', () => {
|
||||
it('should apply modifications to default passport behavior', () => {
|
||||
const result = applyPassportMods(passport)
|
||||
|
||||
assert.equal(result, true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#serializeUser', () => {
|
||||
it('should serialize a user', () => {
|
||||
const user = {
|
||||
id: 'abc123'
|
||||
}
|
||||
const done = () => {}
|
||||
|
||||
applyPassportMods(passport)
|
||||
|
||||
passport.serializeUser(user, done)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#deserializeUser', () => {
|
||||
it('should deserialize a user', () => {
|
||||
// Mock Users model.
|
||||
sandbox.stub(User, 'findById').resolves({ id })
|
||||
|
||||
applyPassportMods(passport)
|
||||
|
||||
passport.deserializeUser(id, done)
|
||||
})
|
||||
|
||||
it('should catch and handle errors', () => {
|
||||
// Force an error
|
||||
sandbox.stub(User, 'findById').rejects(new Error('test error'))
|
||||
|
||||
applyPassportMods(passport)
|
||||
|
||||
passport.deserializeUser(id, done)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
// Public npm libraries
|
||||
import { assert } from 'chai'
|
||||
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local libraries
|
||||
|
||||
Reference in New Issue
Block a user