2021-04-09 08:39:53 -07:00
|
|
|
/*
|
|
|
|
|
Unit tests for the REST API handler for the /users endpoints.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// Public npm libraries
|
2022-09-08 08:58:39 -07:00
|
|
|
import { assert } from 'chai'
|
2021-04-09 08:39:53 -07:00
|
|
|
|
2022-09-08 08:58:39 -07:00
|
|
|
import sinon from 'sinon'
|
|
|
|
|
import ContactController from '../../../../../src/controllers/rest-api/contact/controller.js'
|
|
|
|
|
|
|
|
|
|
import { context as mockContext } from '../../../../unit/mocks/ctx-mock.js'
|
2021-04-09 08:39:53 -07:00
|
|
|
let uut
|
|
|
|
|
let sandbox
|
|
|
|
|
let ctx
|
|
|
|
|
|
|
|
|
|
describe('Contact', () => {
|
2022-03-08 09:39:54 -08:00
|
|
|
before(async () => {
|
|
|
|
|
})
|
2021-04-09 08:39:53 -07:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
uut = new ContactController()
|
|
|
|
|
|
|
|
|
|
sandbox = sinon.createSandbox()
|
|
|
|
|
|
|
|
|
|
// Mock the context object.
|
|
|
|
|
ctx = mockContext()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => sandbox.restore())
|
|
|
|
|
|
|
|
|
|
describe('#POST /contact', () => {
|
|
|
|
|
it('should return 422 status on biz logic error', async () => {
|
|
|
|
|
try {
|
|
|
|
|
await uut.email(ctx)
|
|
|
|
|
|
|
|
|
|
assert.fail('Unexpected result')
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// console.log(err)
|
|
|
|
|
assert.equal(err.status, 422)
|
2022-03-08 09:39:54 -08:00
|
|
|
assert.include(err.message, 'Cannot read')
|
2021-04-09 08:39:53 -07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should return 200 status on success', async () => {
|
|
|
|
|
sandbox.stub(uut.contactLib, 'sendEmail').resolves(true)
|
|
|
|
|
|
|
|
|
|
ctx.request.body = {
|
|
|
|
|
email: 'test02@test.com',
|
|
|
|
|
formMessage: 'test'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await uut.email(ctx)
|
|
|
|
|
|
|
|
|
|
// Assert the expected HTTP response
|
|
|
|
|
assert.equal(ctx.status, 200)
|
|
|
|
|
|
|
|
|
|
// Assert that expected properties exist in the returned data.
|
|
|
|
|
assert.property(ctx.response.body, 'success')
|
|
|
|
|
assert.isTrue(ctx.response.body.success)
|
|
|
|
|
})
|
|
|
|
|
})
|
2021-07-13 07:41:35 -07:00
|
|
|
|
|
|
|
|
describe('#handleError', () => {
|
|
|
|
|
it('should pass an error message', () => {
|
|
|
|
|
try {
|
|
|
|
|
const err = {
|
|
|
|
|
status: 422,
|
|
|
|
|
message: 'Unprocessable Entity'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uut.handleError(ctx, err)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
assert.include(err.message, 'Unprocessable Entity')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
2021-04-09 08:39:53 -07:00
|
|
|
})
|