113 lines
3.2 KiB
JavaScript
113 lines
3.2 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* Integration tests – user controller (controllers/user.js)
|
||
*/
|
||
|
||
const { connectDB, disconnectDB, clearCollection } = require('./jest.setup');
|
||
const { mockApplicator, mockReq, mockRes, newId } = require('./mock_data');
|
||
|
||
let User, Customer;
|
||
|
||
beforeAll(async () => {
|
||
await connectDB();
|
||
User = require('../../model/user');
|
||
Customer = require('../../model/customer');
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await disconnectDB();
|
||
});
|
||
|
||
const userCtl = require('../../controllers/user');
|
||
|
||
describe('user controller – data methods', () => {
|
||
let applicator;
|
||
|
||
beforeAll(async () => {
|
||
applicator = await Customer.create(mockApplicator());
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await Customer.deleteMany({ _id: applicator._id });
|
||
});
|
||
|
||
const makeReq = (extra = {}) =>
|
||
mockReq({ uid: applicator._id, puid: applicator._id, ut: '1', ...extra });
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('getUser_get', () => {
|
||
it('returns a user by id', async () => {
|
||
const req = makeReq({ params: { id: String(applicator._id) }, query: {} });
|
||
const res = mockRes();
|
||
|
||
await userCtl.getUser_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(String(res._data._id)).toBe(String(applicator._id));
|
||
});
|
||
|
||
it('returns null when user not found', async () => {
|
||
const req = makeReq({ params: { id: String(newId()) }, query: {} });
|
||
const res = mockRes();
|
||
|
||
await userCtl.getUser_get(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data).toBeNull();
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('updateUser_put', () => {
|
||
it('updates a user field', async () => {
|
||
const req = makeReq({
|
||
params: { id: String(applicator._id) },
|
||
body: { name: 'Updated Applicator Name', kind: '1' },
|
||
});
|
||
const res = mockRes();
|
||
|
||
await userCtl.updateUser_put(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data.name).toBe('Updated Applicator Name');
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('usernameExists_post', () => {
|
||
it('returns true when username exists', async () => {
|
||
const req = makeReq({ body: { username: applicator.username } });
|
||
const res = mockRes();
|
||
|
||
await userCtl.isUserNameExists_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data).toBe(1);
|
||
});
|
||
|
||
it('returns false when username does not exist', async () => {
|
||
const req = makeReq({ body: { username: `nonexistent_${Date.now()}@test.com` } });
|
||
const res = mockRes();
|
||
|
||
await userCtl.isUserNameExists_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(res._data).toBe(0);
|
||
});
|
||
});
|
||
|
||
// -------------------------------------------------------------------------
|
||
describe('searchUsers_post', () => {
|
||
it('returns matching users', async () => {
|
||
const req = makeReq({ body: { byPuid: applicator._id } });
|
||
const res = mockRes();
|
||
|
||
await userCtl.search_post(req, res);
|
||
|
||
expect(res.json).toHaveBeenCalled();
|
||
expect(Array.isArray(res._data)).toBe(true);
|
||
});
|
||
});
|
||
});
|