Skip to content

Commit 0ebd5b0

Browse files
committed
update tests
1 parent cc358b8 commit 0ebd5b0

File tree

2 files changed

+69
-83
lines changed

2 files changed

+69
-83
lines changed

server/controllers/project.controller/__test__/deleteProject.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Request, Response } from 'jest-express';
55

66
import Project from '../../../models/project';
77
import User from '../../../models/user';
8-
import deleteProject from '../../project.controller/deleteProject';
8+
import deleteProject from '../deleteProject';
99
import { deleteObjectsFromS3 } from '../../aws.controller';
1010

1111
jest.mock('../../../models/project');
@@ -21,11 +21,13 @@ describe('project.controller', () => {
2121
request = new Request();
2222
response = new Response();
2323
Project.findById = jest.fn();
24+
jest.spyOn(console, 'error').mockImplementation(() => {});
2425
});
2526

2627
afterEach(() => {
2728
request.resetMocked();
2829
response.resetMocked();
30+
console.error.mockRestore();
2931
});
3032

3133
it('returns 403 if project is not owned by authenticated user', async () => {
@@ -64,18 +66,22 @@ describe('project.controller', () => {
6466
const user = new User();
6567
const project = new Project();
6668
project.user = user;
67-
project.remove = jest.fn().mockResolvedValue();
69+
project.files = [];
70+
project.deleteOne = jest.fn().mockResolvedValue();
6871

6972
request.setParams({ project_id: project._id });
7073
request.user = { _id: user._id };
7174

7275
Project.findById.mockResolvedValue(project);
7376
deleteObjectsFromS3.mockResolvedValue();
7477

78+
response.end = jest.fn();
79+
7580
await deleteProject(request, response);
7681

7782
expect(deleteObjectsFromS3).toHaveBeenCalled();
78-
expect(project.remove).toHaveBeenCalled();
83+
expect(project.deleteOne).toHaveBeenCalled();
7984
expect(response.status).toHaveBeenCalledWith(200);
85+
expect(response.end).toHaveBeenCalled();
8086
});
8187
});

server/controllers/user.controller/__tests__/apiKey.test.js

Lines changed: 60 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,156 +3,136 @@
33
import { last } from 'lodash';
44
import { Request, Response } from 'jest-express';
55

6-
import User, { createMock, createInstanceMock } from '../../../models/user';
7-
import { createApiKey, removeApiKey } from '../../user.controller/apiKey';
6+
import User from '../../../models/user';
7+
import { createApiKey, removeApiKey } from '../apiKey';
88

99
jest.mock('../../../models/user');
1010

1111
describe('user.controller', () => {
12-
let UserMock;
13-
let UserInstanceMock;
12+
let request;
13+
let response;
1414

1515
beforeEach(() => {
16-
UserMock = createMock();
17-
UserInstanceMock = createInstanceMock();
16+
request = new Request();
17+
response = new Response();
1818
});
1919

2020
afterEach(() => {
21-
UserMock.restore();
22-
UserInstanceMock.restore();
21+
request.resetMocked();
22+
response.resetMocked();
23+
jest.clearAllMocks();
2324
});
2425

2526
describe('createApiKey', () => {
26-
it("returns an error if user doesn't exist", () => {
27-
const request = { user: { id: '1234' } };
28-
const response = new Response();
27+
it("returns an error if user doesn't exist", async () => {
28+
request.user = { id: '1234' };
29+
response = new Response();
2930

30-
UserMock.expects('findById').withArgs('1234').yields(null, null);
31+
User.findById = jest.fn().mockResolvedValue(null);
3132

32-
createApiKey(request, response);
33+
await createApiKey(request, response);
3334

3435
expect(response.status).toHaveBeenCalledWith(404);
3536
expect(response.json).toHaveBeenCalledWith({
3637
error: 'User not found'
3738
});
3839
});
3940

40-
it('returns an error if label not provided', () => {
41-
const request = { user: { id: '1234' }, body: {} };
42-
const response = new Response();
41+
it('returns an error if label not provided', async () => {
42+
request.user = { id: '1234' };
43+
request.body = {};
4344

44-
UserMock.expects('findById').withArgs('1234').yields(null, new User());
45+
const user = new User();
46+
User.findById = jest.fn().mockResolvedValue(user);
4547

46-
createApiKey(request, response);
48+
await createApiKey(request, response);
4749

4850
expect(response.status).toHaveBeenCalledWith(400);
4951
expect(response.json).toHaveBeenCalledWith({
5052
error: "Expected field 'label' was not present in request body"
5153
});
5254
});
5355

54-
it('returns generated API key to the user', (done) => {
55-
const request = new Request();
56+
it('returns generated API key to the user', async () => {
5657
request.setBody({ label: 'my key' });
5758
request.user = { id: '1234' };
5859

59-
const response = new Response();
60-
6160
const user = new User();
61+
user.apiKeys = [];
6262

63-
UserMock.expects('findById').withArgs('1234').yields(null, user);
64-
65-
UserInstanceMock.expects('save').yields();
66-
67-
function expectations() {
68-
const lastKey = last(user.apiKeys);
63+
User.findById = jest.fn().mockResolvedValue(user);
64+
user.save = jest.fn().mockResolvedValue();
6965

70-
expect(lastKey.label).toBe('my key');
71-
expect(typeof lastKey.hashedKey).toBe('string');
66+
await createApiKey(request, response);
7267

73-
const responseData = response.json.mock.calls[0][0];
68+
const lastKey = last(user.apiKeys);
7469

75-
expect(responseData.apiKeys.length).toBe(1);
76-
expect(responseData.apiKeys[0]).toMatchObject({
77-
label: 'my key',
78-
token: lastKey.hashedKey,
79-
lastUsedAt: undefined,
80-
createdAt: undefined
81-
});
70+
expect(lastKey.label).toBe('my key');
71+
expect(typeof lastKey.hashedKey).toBe('string');
8272

83-
done();
84-
}
85-
86-
const promise = createApiKey(request, response);
87-
88-
promise.then(expectations, expectations).catch(expectations);
73+
const responseData = response.json.mock.calls[0][0];
74+
expect(responseData.apiKeys.length).toBe(1);
75+
expect(responseData.apiKeys[0]).toMatchObject({
76+
label: 'my key',
77+
token: lastKey.hashedKey
78+
});
8979
});
9080
});
9181

9282
describe('removeApiKey', () => {
93-
it("returns an error if user doesn't exist", () => {
94-
const request = { user: { id: '1234' } };
95-
const response = new Response();
83+
it("returns an error if user doesn't exist", async () => {
84+
request.user = { id: '1234' };
85+
response = new Response();
9686

97-
UserMock.expects('findById').withArgs('1234').yields(null, null);
87+
User.findById = jest.fn().mockResolvedValue(null);
9888

99-
removeApiKey(request, response);
89+
await removeApiKey(request, response);
10090

10191
expect(response.status).toHaveBeenCalledWith(404);
10292
expect(response.json).toHaveBeenCalledWith({
10393
error: 'User not found'
10494
});
10595
});
10696

107-
it("returns an error if specified key doesn't exist", () => {
108-
const request = {
109-
user: { id: '1234' },
110-
params: { keyId: 'not-a-real-key' }
111-
};
112-
const response = new Response();
97+
it("returns an error if specified key doesn't exist", async () => {
98+
request.user = { id: '1234' };
99+
request.params = { keyId: 'not-a-real-key' };
113100
const user = new User();
101+
user.apiKeys = [];
114102

115-
UserMock.expects('findById').withArgs('1234').yields(null, user);
103+
User.findById = jest.fn().mockResolvedValue(user);
116104

117-
removeApiKey(request, response);
105+
await removeApiKey(request, response);
118106

119107
expect(response.status).toHaveBeenCalledWith(404);
120108
expect(response.json).toHaveBeenCalledWith({
121109
error: 'Key does not exist for user'
122110
});
123111
});
124112

125-
it('removes key if it exists', () => {
126-
const user = new User();
127-
user.apiKeys.push({ label: 'first key' }); // id 0
128-
user.apiKeys.push({ label: 'second key' }); // id 1
113+
it('removes key if it exists', async () => {
114+
const mockKey1 = { _id: 'id1', id: 'id1', label: 'first key' };
115+
const mockKey2 = { _id: 'id2', id: 'id2', label: 'second key' };
129116

130-
const firstKeyId = user.apiKeys[0]._id.toString();
131-
const secondKeyId = user.apiKeys[1]._id.toString();
117+
const apiKeys = [mockKey1, mockKey2];
118+
apiKeys.find = Array.prototype.find;
119+
apiKeys.pull = jest.fn();
132120

133-
const request = {
134-
user: { id: '1234' },
135-
params: { keyId: firstKeyId }
121+
const user = {
122+
apiKeys,
123+
save: jest.fn().mockResolvedValue()
136124
};
137-
const response = new Response();
138125

139-
UserMock.expects('findById').withArgs('1234').yields(null, user);
126+
request.user = { id: '1234' };
127+
request.params = { keyId: 'id1' };
140128

141-
UserInstanceMock.expects('save').yields();
129+
User.findById = jest.fn().mockResolvedValue(user);
142130

143-
removeApiKey(request, response);
131+
await removeApiKey(request, response);
144132

133+
expect(user.apiKeys.pull).toHaveBeenCalledWith({ _id: 'id1' });
134+
expect(user.save).toHaveBeenCalled();
145135
expect(response.status).toHaveBeenCalledWith(200);
146-
147-
const responseData = response.json.mock.calls[0][0];
148-
149-
expect(responseData.apiKeys.length).toBe(1);
150-
expect(responseData.apiKeys[0]).toMatchObject({
151-
id: secondKeyId,
152-
label: 'second key',
153-
lastUsedAt: undefined,
154-
createdAt: undefined
155-
});
156136
});
157137
});
158138
});

0 commit comments

Comments
 (0)