showing results for - "knex mocking"
Maya
13 Aug 2018
1import 'mocha'
2import bcrypt from 'bcrypt'
3import chai from 'chai'
4import knexMock from 'mock-knex'
5import httpMock from 'node-mocks-http'
6import db from '../../lib/db'
7import { login } from '../../controllers/auth/login'
8import { mockingData } from '../mock-data/mock.data'
9
10let req, res, tracker, hashPassword
11
12describe('[Unit Testing] - Server API V1', function () {
13	beforeEach(function () {
14		// create model mock
15		tracker = knexMock.getTracker()
16		tracker.install()
17		knexMock.mock(db)
18
19		// create express request and response mock
20		req = httpMock.createRequest()
21		res = httpMock.createResponse()
22	})
23
24	afterEach(function () {
25		// reset mock after every each test finish
26		tracker.uninstall()
27		knexMock.unmock(db)
28	})
29
30	it('[POST] - /auth/login - Should be login username/password wrong', async function () {
31		req.body.email = mockingData.login.active.email
32		req.body.password = mockingData.login.active.password
33
34		hashPassword = bcrypt.hashSync(mockingData.login.active.password, 12)
35		mockingData.login.active.password = hashPassword
36
37		tracker.on('query', (query) => {
38			chai.expect(query.method).to.equal('first')
39			query.response(undefined)
40		})
41
42		await login(req, res)
43		const data = res._getJSONData()
44		chai.expect(res._isEndCalled()).to.be.true
45		chai.expect(res._getStatusCode()).to.be.equal(403)
46		chai.expect(res._getHeaders()).to.be.contain({ 'content-type': 'application/json' })
47		chai.expect(data.message).to.be.equal('Invalid email or password.')
48	})
49
50	it('[POST] - /auth/login - Should be login username/password is not active', async function () {
51		req.body.email = mockingData.login.noActive.email
52		req.body.password = mockingData.login.noActive.password
53
54		hashPassword = bcrypt.hashSync(mockingData.login.noActive.password, 12)
55		mockingData.login.noActive.password = hashPassword
56
57		tracker.on('query', (query) => {
58			chai.expect(query.method).to.equal('first')
59			query.response(mockingData.login.noActive)
60		})
61
62		await login(req, res)
63		const data = res._getJSONData()
64
65		chai.expect(res._isEndCalled()).to.be.true
66		chai.expect(res._getStatusCode()).to.be.equal(403)
67		chai.expect(res._getHeaders()).to.be.contain({ 'content-type': 'application/json' })
68		chai.expect(data.message).to.be.equal('Account is not active')
69	})
70
71	it('[POST] - /auth/login - Should be login successfully', async function () {
72		req.body.email = mockingData.login.active.email
73		req.body.password = mockingData.login.active.password
74
75		hashPassword = bcrypt.hashSync(mockingData.login.active.password, 12)
76		mockingData.login.active.password = hashPassword
77
78		tracker.on('query', (query) => {
79			chai.expect(query.method).to.equal('first')
80			query.response(mockingData.login.active)
81		})
82
83		await login(req, res)
84		const data = res._getJSONData()
85		chai.expect(res._isEndCalled()).to.be.true
86		chai.expect(res._getStatusCode()).to.be.equal(200)
87		chai.expect(res._getHeaders()).to.be.contain({ 'content-type': 'application/json' })
88		chai.expect(data.status).to.be.equal('Ok')
89	})
90})
queries leading to this page
knex mockingknex mocking