|
3 | 3 | */
|
4 | 4 | import { Request, Response } from 'jest-express';
|
5 | 5 |
|
6 |
| -import Project, { |
7 |
| - createMock, |
8 |
| - createInstanceMock |
9 |
| -} from '../../../models/project'; |
| 6 | +import Project from '../../../models/project'; |
10 | 7 | import User from '../../../models/user';
|
11 | 8 | import deleteProject from '../../project.controller/deleteProject';
|
12 | 9 | import { deleteObjectsFromS3 } from '../../aws.controller';
|
13 | 10 |
|
14 | 11 | jest.mock('../../../models/project');
|
15 | 12 | jest.mock('../../aws.controller');
|
16 | 13 |
|
17 |
| -describe('project.controller', () => { |
18 |
| - describe('deleteProject()', () => { |
19 |
| - let ProjectMock; |
20 |
| - let ProjectInstanceMock; |
21 |
| - |
22 |
| - beforeEach(() => { |
23 |
| - ProjectMock = createMock(); |
24 |
| - ProjectInstanceMock = createInstanceMock(); |
25 |
| - }); |
26 |
| - |
27 |
| - afterEach(() => { |
28 |
| - ProjectMock.restore(); |
29 |
| - ProjectInstanceMock.restore(); |
30 |
| - }); |
| 14 | +// TODO: incomplete test, 500 response status needs to be added |
31 | 15 |
|
32 |
| - it('returns 403 if project is not owned by authenticated user', (done) => { |
33 |
| - const user = new User(); |
34 |
| - const project = new Project(); |
35 |
| - project.user = user; |
| 16 | +describe('project.controller', () => { |
| 17 | + let request; |
| 18 | + let response; |
36 | 19 |
|
37 |
| - const request = new Request(); |
38 |
| - request.setParams({ project_id: project._id }); |
39 |
| - request.user = { _id: 'abc123' }; |
| 20 | + beforeEach(() => { |
| 21 | + request = new Request(); |
| 22 | + response = new Response(); |
| 23 | + Project.findById = jest.fn(); |
| 24 | + }); |
40 | 25 |
|
41 |
| - const response = new Response(); |
| 26 | + afterEach(() => { |
| 27 | + request.resetMocked(); |
| 28 | + response.resetMocked(); |
| 29 | + }); |
42 | 30 |
|
43 |
| - ProjectMock.expects('findById').resolves(project); |
| 31 | + it('returns 403 if project is not owned by authenticated user', async () => { |
| 32 | + const user = new User(); |
| 33 | + const project = new Project(); |
| 34 | + project.user = user; |
44 | 35 |
|
45 |
| - const promise = deleteProject(request, response); |
| 36 | + request.setParams({ project_id: project._id }); |
| 37 | + request.user = { _id: 'abc123' }; |
46 | 38 |
|
47 |
| - function expectations() { |
48 |
| - expect(response.status).toHaveBeenCalledWith(403); |
49 |
| - expect(response.json).toHaveBeenCalledWith({ |
50 |
| - message: 'Authenticated user does not match owner of project' |
51 |
| - }); |
| 39 | + Project.findById.mockResolvedValue(project); |
52 | 40 |
|
53 |
| - done(); |
54 |
| - } |
| 41 | + await deleteProject(request, response); |
55 | 42 |
|
56 |
| - promise.then(expectations, expectations).catch(expectations); |
| 43 | + expect(response.status).toHaveBeenCalledWith(403); |
| 44 | + expect(response.json).toHaveBeenCalledWith({ |
| 45 | + message: 'Authenticated user does not match owner of project' |
57 | 46 | });
|
| 47 | + }); |
58 | 48 |
|
59 |
| - it('returns 404 if project does not exist', (done) => { |
60 |
| - const user = new User(); |
61 |
| - const project = new Project(); |
62 |
| - project.user = user; |
63 |
| - |
64 |
| - const request = new Request(); |
65 |
| - request.setParams({ project_id: project._id }); |
66 |
| - request.user = { _id: 'abc123' }; |
67 |
| - |
68 |
| - const response = new Response(); |
69 |
| - |
70 |
| - ProjectMock.expects('findById').resolves(null); |
71 |
| - |
72 |
| - const promise = deleteProject(request, response); |
| 49 | + it('returns 404 if project does not exist', async () => { |
| 50 | + request.setParams({ project_id: 'random_id' }); |
| 51 | + request.user = { _id: 'abc123' }; |
73 | 52 |
|
74 |
| - function expectations() { |
75 |
| - expect(response.status).toHaveBeenCalledWith(404); |
76 |
| - expect(response.json).toHaveBeenCalledWith({ |
77 |
| - message: 'Project with that id does not exist' |
78 |
| - }); |
| 53 | + Project.findById.mockResolvedValue(null); |
79 | 54 |
|
80 |
| - done(); |
81 |
| - } |
| 55 | + await deleteProject(request, response); |
82 | 56 |
|
83 |
| - promise.then(expectations, expectations).catch(expectations); |
| 57 | + expect(response.status).toHaveBeenCalledWith(404); |
| 58 | + expect(response.json).toHaveBeenCalledWith({ |
| 59 | + message: 'Project with that id does not exist' |
84 | 60 | });
|
| 61 | + }); |
85 | 62 |
|
86 |
| - it('deletes project and dependent files from S3 ', (done) => { |
87 |
| - const user = new User(); |
88 |
| - const project = new Project(); |
89 |
| - project.user = user; |
90 |
| - |
91 |
| - const request = new Request(); |
92 |
| - request.setParams({ project_id: project._id }); |
93 |
| - request.user = { _id: user._id }; |
94 |
| - |
95 |
| - const response = new Response(); |
96 |
| - |
97 |
| - ProjectMock.expects('findById').resolves(project); |
98 |
| - |
99 |
| - ProjectInstanceMock.expects('remove').yields(); |
| 63 | + it('delete project and dependent files from S3', async () => { |
| 64 | + const user = new User(); |
| 65 | + const project = new Project(); |
| 66 | + project.user = user; |
| 67 | + project.remove = jest.fn().mockResolvedValue(); |
100 | 68 |
|
101 |
| - const promise = deleteProject(request, response); |
| 69 | + request.setParams({ project_id: project._id }); |
| 70 | + request.user = { _id: user._id }; |
102 | 71 |
|
103 |
| - function expectations() { |
104 |
| - expect(response.status).toHaveBeenCalledWith(200); |
105 |
| - expect(response.json).not.toHaveBeenCalled(); |
106 |
| - expect(deleteObjectsFromS3).toHaveBeenCalled(); |
| 72 | + Project.findById.mockResolvedValue(project); |
| 73 | + deleteObjectsFromS3.mockResolvedValue(); |
107 | 74 |
|
108 |
| - done(); |
109 |
| - } |
| 75 | + await deleteProject(request, response); |
110 | 76 |
|
111 |
| - promise.then(expectations, expectations).catch(expectations); |
112 |
| - }); |
| 77 | + expect(deleteObjectsFromS3).toHaveBeenCalled(); |
| 78 | + expect(project.remove).toHaveBeenCalled(); |
| 79 | + expect(response.status).toHaveBeenCalledWith(200); |
113 | 80 | });
|
114 | 81 | });
|
0 commit comments