mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-service.git
synced 2026-09-21 16:52:03 -07:00
fix(users): Achieved 100% test coverage of users controller
This commit is contained in:
@@ -170,12 +170,18 @@ class UserController {
|
||||
user
|
||||
}
|
||||
} catch (err) {
|
||||
if (err === 404 || err.name === 'CastError') {
|
||||
// Handle different error types.
|
||||
if (
|
||||
err === 404 ||
|
||||
err.name === 'CastError' ||
|
||||
err.message.toString().includes('Not Found')
|
||||
) {
|
||||
ctx.throw(404)
|
||||
}
|
||||
|
||||
ctx.throw(500)
|
||||
}
|
||||
|
||||
if (next) {
|
||||
return next()
|
||||
}
|
||||
|
||||
@@ -374,6 +374,40 @@ describe('Users', () => {
|
||||
'Password property should not be returned'
|
||||
)
|
||||
})
|
||||
|
||||
it('should catch and handle errors', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
sandbox.stub(uut.User, 'findById').rejects(new Error('test error'))
|
||||
|
||||
// Mock the context object.
|
||||
const ctx = mockContext()
|
||||
|
||||
await uut.getUser(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
assert.include(err.message, 'Internal Server Error')
|
||||
}
|
||||
})
|
||||
|
||||
it('should handle user not found', async () => {
|
||||
try {
|
||||
// Force an error
|
||||
sandbox.stub(uut.User, 'findById').resolves(false)
|
||||
|
||||
// Mock the context object.
|
||||
const ctx = mockContext()
|
||||
ctx.params = { id: 1 }
|
||||
|
||||
await uut.getUser(ctx)
|
||||
|
||||
assert.fail('Unexpected result')
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
assert.include(err.message, 'Not Found')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('PUT /users/:id', () => {
|
||||
|
||||
Reference in New Issue
Block a user