mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-service.git
synced 2026-09-21 16:52:03 -07:00
Fixed merge conflicts
This commit is contained in:
@@ -3,21 +3,25 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
import { assert } from 'chai'
|
||||
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local support libraries
|
||||
const adapters = require('../../../mocks/adapters')
|
||||
const UseCasesMock = require('../../../mocks/use-cases')
|
||||
import adapters from '../../../mocks/adapters/index.js'
|
||||
|
||||
import UseCasesMock from '../../../mocks/use-cases/index.js'
|
||||
|
||||
// const app = require('../../../mocks/app-mock')
|
||||
|
||||
const AuthRESTController = require('../../../../../src/controllers/rest-api/auth/controller')
|
||||
import AuthRESTController from '../../../../../src/controllers/rest-api/auth/controller.js'
|
||||
|
||||
import { context as mockContext } from '../../../../unit/mocks/ctx-mock.js'
|
||||
|
||||
let uut
|
||||
let sandbox
|
||||
let ctx
|
||||
|
||||
const mockContext = require('../../../../unit/mocks/ctx-mock').context
|
||||
|
||||
describe('#Auth-REST-Router', () => {
|
||||
// const testUser = {}
|
||||
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
import { assert } from 'chai'
|
||||
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local support libraries
|
||||
const adapters = require('../../../mocks/adapters')
|
||||
const UseCasesMock = require('../../../mocks/use-cases')
|
||||
import adapters from '../../../mocks/adapters/index.js'
|
||||
|
||||
import UseCasesMock from '../../../mocks/use-cases/index.js'
|
||||
|
||||
// const app = require('../../../mocks/app-mock')
|
||||
|
||||
const AuthRouter = require('../../../../../src/controllers/rest-api/auth')
|
||||
import AuthRouter from '../../../../../src/controllers/rest-api/auth/index.js'
|
||||
|
||||
let uut
|
||||
let sandbox
|
||||
// let ctx
|
||||
|
||||
@@ -3,16 +3,16 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
import { assert } from 'chai'
|
||||
|
||||
const ContactController = require('../../../../../src/controllers/rest-api/contact/controller')
|
||||
import sinon from 'sinon'
|
||||
import ContactController from '../../../../../src/controllers/rest-api/contact/controller.js'
|
||||
|
||||
import { context as mockContext } from '../../../../unit/mocks/ctx-mock.js'
|
||||
let uut
|
||||
let sandbox
|
||||
let ctx
|
||||
|
||||
const mockContext = require('../../../../unit/mocks/ctx-mock').context
|
||||
|
||||
describe('Contact', () => {
|
||||
before(async () => {
|
||||
})
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
import { assert } from 'chai'
|
||||
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local support libraries
|
||||
const adapters = require('../../../mocks/adapters')
|
||||
const UseCasesMock = require('../../../mocks/use-cases')
|
||||
import adapters from '../../../mocks/adapters/index.js'
|
||||
|
||||
import UseCasesMock from '../../../mocks/use-cases/index.js'
|
||||
|
||||
// const app = require('../../../mocks/app-mock')
|
||||
|
||||
const ContactRouter = require('../../../../../src/controllers/rest-api/contact')
|
||||
import ContactRouter from '../../../../../src/controllers/rest-api/contact/index.js'
|
||||
|
||||
let uut
|
||||
let sandbox
|
||||
// let ctx
|
||||
|
||||
@@ -0,0 +1,251 @@
|
||||
/*
|
||||
Unit tests for the REST API handler for the /ipfs endpoints.
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
import { assert } from 'chai'
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local libraries
|
||||
import IpfsApiController from '../../../../../src/controllers/rest-api/ipfs/controller.js'
|
||||
import adapters from '../../../mocks/adapters/index.js'
|
||||
import UseCasesMock from '../../../mocks/use-cases/index.js'
|
||||
|
||||
import { context as mockContext } from '../../../mocks/ctx-mock.js'
|
||||
let uut
|
||||
let sandbox
|
||||
let ctx
|
||||
|
||||
describe('#IPFS REST API', () => {
|
||||
before(async () => {
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
const useCases = new UseCasesMock()
|
||||
|
||||
uut = new IpfsApiController({ adapters, useCases })
|
||||
|
||||
sandbox = sinon.createSandbox()
|
||||
|
||||
// Mock the context object.
|
||||
ctx = mockContext()
|
||||
})
|
||||
|
||||
afterEach(() => sandbox.restore())
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error if adapters are not passed in', () => {
|
||||
try {
|
||||
uut = new IpfsApiController()
|
||||
|
||||
assert.fail('Unexpected code path')
|
||||
} catch (err) {
|
||||
assert.include(
|
||||
err.message,
|
||||
'Instance of Adapters library required when instantiating /ipfs REST Controller.'
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw an error if useCases are not passed in', () => {
|
||||
try {
|
||||
uut = new IpfsApiController({ adapters })
|
||||
|
||||
assert.fail('Unexpected code path')
|
||||
} catch (err) {
|
||||
assert.include(
|
||||
err.message,
|
||||
'Instance of Use Cases library required when instantiating /ipfs REST Controller.'
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('#GET /status', () => {
|
||||
it('should return 422 status on biz logic error', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
sandbox.stub(uut.adapters.ipfs, 'getStatus').rejects(new Error('test error'))
|
||||
|
||||
await uut.getStatus(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(err.status, 422)
|
||||
assert.include(err.message, 'test error')
|
||||
}
|
||||
})
|
||||
|
||||
it('should return 200 status on success', async () => {
|
||||
// Mock dependencies
|
||||
sandbox.stub(uut.adapters.ipfs, 'getStatus').resolves({ a: 'b' })
|
||||
|
||||
await uut.getStatus(ctx)
|
||||
// console.log('ctx.body: ', ctx.body)
|
||||
|
||||
assert.property(ctx.body, 'status')
|
||||
assert.equal(ctx.body.status.a, 'b')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#POST /peers', () => {
|
||||
it('should return 422 status on biz logic error', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
sandbox.stub(uut.adapters.ipfs, 'getPeers').rejects(new Error('test error'))
|
||||
|
||||
ctx.request.body = {
|
||||
showAll: true
|
||||
}
|
||||
|
||||
await uut.getPeers(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(err.status, 422)
|
||||
assert.include(err.message, 'test error')
|
||||
}
|
||||
})
|
||||
|
||||
it('should return 200 status on success', async () => {
|
||||
// Mock dependencies
|
||||
sandbox.stub(uut.adapters.ipfs, 'getPeers').resolves({ a: 'b' })
|
||||
|
||||
ctx.request.body = {
|
||||
showAll: true
|
||||
}
|
||||
|
||||
await uut.getPeers(ctx)
|
||||
// console.log('ctx.body: ', ctx.body)
|
||||
|
||||
assert.property(ctx.body, 'peers')
|
||||
assert.equal(ctx.body.peers.a, 'b')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#POST /relays', () => {
|
||||
it('should return 422 status on biz logic error', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
sandbox.stub(uut.adapters.ipfs, 'getRelays').rejects(new Error('test error'))
|
||||
|
||||
await uut.getRelays(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(err.status, 422)
|
||||
assert.include(err.message, 'test error')
|
||||
}
|
||||
})
|
||||
|
||||
it('should return 200 status on success', async () => {
|
||||
// Mock dependencies
|
||||
sandbox.stub(uut.adapters.ipfs, 'getRelays').resolves({ a: 'b' })
|
||||
|
||||
await uut.getRelays(ctx)
|
||||
// console.log('ctx.body: ', ctx.body)
|
||||
|
||||
assert.property(ctx.body, 'relays')
|
||||
assert.equal(ctx.body.relays.a, 'b')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#POST /connect', () => {
|
||||
it('should return 422 status on biz logic error', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
sandbox.stub(uut.adapters.ipfs.ipfsCoordAdapter.ipfsCoord.adapters.ipfs, 'connectToPeer').rejects(new Error('test error'))
|
||||
|
||||
ctx.request.body = {
|
||||
multiaddr: '/ip4/161.35.99.207/tcp/4001/p2p/12D3KooWDtj9cfj1SKuLbDNKvKRKSsGN8qivq9M8CYpLPDpcD5pu'
|
||||
}
|
||||
|
||||
await uut.connect(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.equal(err.status, 422)
|
||||
assert.include(err.message, 'test error')
|
||||
}
|
||||
})
|
||||
|
||||
it('should return 200 status on success', async () => {
|
||||
// Mock dependencies
|
||||
sandbox.stub(uut.adapters.ipfs.ipfsCoordAdapter.ipfsCoord.adapters.ipfs, 'connectToPeer').resolves({ success: true })
|
||||
|
||||
ctx.request.body = {
|
||||
multiaddr: '/ip4/161.35.99.207/tcp/4001/p2p/12D3KooWDtj9cfj1SKuLbDNKvKRKSsGN8qivq9M8CYpLPDpcD5pu'
|
||||
}
|
||||
|
||||
await uut.connect(ctx)
|
||||
// console.log('ctx.body: ', ctx.body)
|
||||
|
||||
assert.property(ctx.body, 'success')
|
||||
assert.equal(ctx.body.success, true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#handleError', () => {
|
||||
it('should still throw error if there is no message', () => {
|
||||
try {
|
||||
const err = {
|
||||
status: 404
|
||||
}
|
||||
|
||||
uut.handleError(ctx, err)
|
||||
} catch (err) {
|
||||
assert.include(err.message, 'Not Found')
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw error with message', () => {
|
||||
try {
|
||||
const err = {
|
||||
status: 422,
|
||||
message: 'test error'
|
||||
}
|
||||
|
||||
uut.handleError(ctx, err)
|
||||
} catch (err) {
|
||||
assert.include(err.message, 'test error')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getThisNode', () => {
|
||||
it('should return 422 status on biz logic error', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
// sandbox.stub(uut.adapters.ipfs.ipfsCoordAdapter.ipfsCoord, 'thisNode').rejects(new Error('test error'))
|
||||
uut.adapters.ipfs.ipfsCoordAdapter = {}
|
||||
|
||||
ctx.request.body = {}
|
||||
|
||||
await uut.getThisNode(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
// console.log('err: ', err)
|
||||
assert.equal(err.status, 422)
|
||||
assert.include(err.message, 'Cannot read')
|
||||
}
|
||||
})
|
||||
|
||||
it('should return 200 status on success', async () => {
|
||||
// Mock dependencies
|
||||
// sandbox.stub(uut.adapters.ipfs.ipfsCoordAdapter.ipfsCoord.adapters.ipfs, 'connectToPeer').resolves({ success: true })
|
||||
|
||||
uut.adapters.ipfs.ipfsCoordAdapter = {
|
||||
ipfsCoord: {
|
||||
thisNode: {}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.request.body = {}
|
||||
|
||||
await uut.getThisNode(ctx)
|
||||
|
||||
assert.property(ctx.body, 'thisNode')
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
Unit tests for the REST API handler for the /ipfs endpoints.
|
||||
*/
|
||||
|
||||
// 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 IpfsRouter from '../../../../../src/controllers/rest-api/ipfs/index.js'
|
||||
// const app = require('../../../mocks/app-mock')
|
||||
|
||||
let uut
|
||||
let sandbox
|
||||
// let ctx
|
||||
|
||||
// const mockContext = require('../../../../unit/mocks/ctx-mock').context
|
||||
|
||||
describe('#IPFS-REST-Router', () => {
|
||||
// const testUser = {}
|
||||
|
||||
beforeEach(() => {
|
||||
const useCases = new UseCasesMock()
|
||||
uut = new IpfsRouter({ adapters, useCases })
|
||||
|
||||
sandbox = sinon.createSandbox()
|
||||
|
||||
// Mock the context object.
|
||||
// ctx = mockContext()
|
||||
})
|
||||
|
||||
afterEach(() => sandbox.restore())
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error if adapters are not passed in', () => {
|
||||
try {
|
||||
uut = new IpfsRouter()
|
||||
|
||||
assert.fail('Unexpected code path')
|
||||
} catch (err) {
|
||||
assert.include(
|
||||
err.message,
|
||||
'Instance of Adapters library required when instantiating IPFS REST Controller.'
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw an error if useCases are not passed in', () => {
|
||||
try {
|
||||
uut = new IpfsRouter({ adapters })
|
||||
|
||||
assert.fail('Unexpected code path')
|
||||
} catch (err) {
|
||||
assert.include(
|
||||
err.message,
|
||||
'Instance of Use Cases library required when instantiating IPFS REST Controller.'
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('#attach', () => {
|
||||
it('should throw an error if app is not passed in.', () => {
|
||||
try {
|
||||
uut.attach()
|
||||
|
||||
assert.fail('Unexpected code path')
|
||||
} catch (err) {
|
||||
assert.include(
|
||||
err.message,
|
||||
'Must pass app object when attaching REST API controllers.'
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -3,16 +3,16 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
import { assert } from 'chai'
|
||||
|
||||
const LogsApiController = require('../../../../../src/controllers/rest-api/logs/controller')
|
||||
import sinon from 'sinon'
|
||||
import LogsApiController from '../../../../../src/controllers/rest-api/logs/controller.js'
|
||||
|
||||
import { context as mockContext } from '../../../../unit/mocks/ctx-mock.js'
|
||||
let uut
|
||||
let sandbox
|
||||
let ctx
|
||||
|
||||
const mockContext = require('../../../../unit/mocks/ctx-mock').context
|
||||
|
||||
describe('Logapi', () => {
|
||||
before(async () => {
|
||||
})
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
import { assert } from 'chai'
|
||||
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local support libraries
|
||||
const adapters = require('../../../mocks/adapters')
|
||||
const UseCasesMock = require('../../../mocks/use-cases')
|
||||
import adapters from '../../../mocks/adapters/index.js'
|
||||
|
||||
import UseCasesMock from '../../../mocks/use-cases/index.js'
|
||||
|
||||
// const app = require('../../../mocks/app-mock')
|
||||
|
||||
const LogsRouter = require('../../../../../src/controllers/rest-api/logs')
|
||||
import LogsRouter from '../../../../../src/controllers/rest-api/logs/index.js'
|
||||
|
||||
let uut
|
||||
let sandbox
|
||||
// let ctx
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -3,14 +3,17 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
import { assert } from 'chai'
|
||||
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local libraries
|
||||
const RESTControllers = require('../../../../src/controllers/rest-api/')
|
||||
import RESTControllers from '../../../../src/controllers/rest-api/index.js'
|
||||
|
||||
// const mockContext = require('../../../unit/mocks/ctx-mock').context
|
||||
const adapters = require('../../mocks/adapters')
|
||||
const UseCasesMock = require('../../mocks/use-cases')
|
||||
import adapters from '../../mocks/adapters/index.js'
|
||||
|
||||
import UseCasesMock from '../../mocks/use-cases/index.js'
|
||||
|
||||
describe('#RESTControllers', () => {
|
||||
let uut
|
||||
|
||||
@@ -3,20 +3,19 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
import { assert } from 'chai'
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local support libraries
|
||||
const adapters = require('../../../mocks/adapters')
|
||||
const UseCasesMock = require('../../../mocks/use-cases')
|
||||
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'
|
||||
|
||||
const UserController = require('../../../../../src/controllers/rest-api/users/controller')
|
||||
import { context as mockContext } from '../../../../unit/mocks/ctx-mock.js'
|
||||
let uut
|
||||
let sandbox
|
||||
let ctx
|
||||
|
||||
const mockContext = require('../../../../unit/mocks/ctx-mock').context
|
||||
|
||||
describe('#Users-REST-Controller', () => {
|
||||
// const testUser = {}
|
||||
|
||||
|
||||
@@ -3,15 +3,19 @@
|
||||
*/
|
||||
|
||||
// Public npm libraries
|
||||
const assert = require('chai').assert
|
||||
const sinon = require('sinon')
|
||||
import { assert } from 'chai'
|
||||
|
||||
import sinon from 'sinon'
|
||||
|
||||
// Local support libraries
|
||||
const adapters = require('../../../mocks/adapters')
|
||||
const UseCasesMock = require('../../../mocks/use-cases')
|
||||
import adapters from '../../../mocks/adapters/index.js'
|
||||
|
||||
import UseCasesMock from '../../../mocks/use-cases/index.js'
|
||||
|
||||
// const app = require('../../../mocks/app-mock')
|
||||
|
||||
const UserRouter = require('../../../../../src/controllers/rest-api/users')
|
||||
import UserRouter from '../../../../../src/controllers/rest-api/users/index.js'
|
||||
|
||||
let uut
|
||||
let sandbox
|
||||
// let ctx
|
||||
|
||||
Reference in New Issue
Block a user