-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathuserProfileController.deleteUserProfile.spec.js
More file actions
100 lines (91 loc) · 3.41 KB
/
userProfileController.deleteUserProfile.spec.js
File metadata and controls
100 lines (91 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
jest.mock('../helpers/userHelper', () => () => ({}));
jest.mock('../models/timeentry', () => ({}));
jest.mock('../models/team', () => ({
updateMany: jest.fn(),
}));
jest.mock('../models/badge', () => ({}));
jest.mock('../utilities/nodeCache', () =>
jest.fn(() => ({
removeCache: jest.fn(),
getCache: jest.fn(() => null),
setCache: jest.fn(),
})),
);
jest.mock('../models/followUp', () => ({
findOneAndDelete: jest.fn(),
}));
jest.mock('../models/task', () => ({
updateMany: jest.fn(),
}));
jest.mock('../models/hgnFormResponse', () => ({}));
jest.mock('../services/userService', () => ({}));
jest.mock('../utilities/permissions', () => ({
hasPermission: jest.fn(),
canRequestorUpdateUser: jest.fn(),
}));
jest.mock('../utilities/emailSender', () => jest.fn());
jest.mock('../utilities/objectUtils', () => ({
deepCopyMongooseObjectWithLodash: jest.fn((value) => value),
}));
jest.mock('../startup/logger', () => ({
logInfo: jest.fn(),
logException: jest.fn(),
}));
jest.mock('./reportsController', () => () => ({}));
const mongoose = require('mongoose');
const Team = require('../models/team');
const Task = require('../models/task');
const followUp = require('../models/followUp');
const { hasPermission, canRequestorUpdateUser } = require('../utilities/permissions');
const { mockReq, mockRes } = require('../test');
const userProfileController = require('./userProfileController');
describe('userProfileController deleteUserProfile', () => {
const userId = '65cf6c3706d8ac105827bb2e';
const mockUserProfileModel = {
findById: jest.fn(),
deleteOne: jest.fn(),
};
const makeSut = () => userProfileController(mockUserProfileModel, {});
beforeEach(() => {
jest.clearAllMocks();
hasPermission.mockResolvedValue(true);
canRequestorUpdateUser.mockResolvedValue(true);
mockUserProfileModel.findById.mockResolvedValue({
_id: userId,
email: 'volunteer@example.org',
});
mockUserProfileModel.deleteOne.mockResolvedValue({ deletedCount: 1 });
followUp.findOneAndDelete.mockResolvedValue({});
Task.updateMany.mockResolvedValue({ modifiedCount: 1 });
Team.updateMany.mockResolvedValue({ modifiedCount: 1 });
mockRes.status.mockReturnThis();
mockReq.body = {
...mockReq.body,
userId,
option: 'delete',
role: 'Volunteer',
requestor: {
...mockReq.body.requestor,
requestorId: '507f1f77bcf86cd799439011',
},
};
});
test('removes deleted users from tasks and teams using both string and ObjectId matches', async () => {
const { deleteUserProfile } = makeSut();
await deleteUserProfile(mockReq, mockRes);
const expectedObjectId = new mongoose.Types.ObjectId(userId);
const expectedMatchedUserIds = [userId, expectedObjectId];
expect(mockUserProfileModel.deleteOne).toHaveBeenCalledWith({ _id: userId });
expect(followUp.findOneAndDelete).toHaveBeenCalledWith({ userId });
expect(Task.updateMany).toHaveBeenCalledWith(
{ 'resources.userID': { $in: expectedMatchedUserIds } },
{ $pull: { resources: { userID: { $in: expectedMatchedUserIds } } } },
);
expect(Team.updateMany).toHaveBeenCalledWith(
{ 'members.userId': { $in: expectedMatchedUserIds } },
{ $pull: { members: { userId: { $in: expectedMatchedUserIds } } } },
);
expect(mockRes.status).toHaveBeenCalledWith(200);
expect(mockRes.send).toHaveBeenCalledWith({ message: 'Executed Successfully' });
});
});