|
3 | 3 | import { last } from 'lodash'; |
4 | 4 | import { Request, Response } from 'jest-express'; |
5 | 5 |
|
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'; |
8 | 8 |
|
9 | 9 | jest.mock('../../../models/user'); |
10 | 10 |
|
11 | 11 | describe('user.controller', () => { |
12 | | - let UserMock; |
13 | | - let UserInstanceMock; |
| 12 | + let request; |
| 13 | + let response; |
14 | 14 |
|
15 | 15 | beforeEach(() => { |
16 | | - UserMock = createMock(); |
17 | | - UserInstanceMock = createInstanceMock(); |
| 16 | + request = new Request(); |
| 17 | + response = new Response(); |
18 | 18 | }); |
19 | 19 |
|
20 | 20 | afterEach(() => { |
21 | | - UserMock.restore(); |
22 | | - UserInstanceMock.restore(); |
| 21 | + request.resetMocked(); |
| 22 | + response.resetMocked(); |
| 23 | + jest.clearAllMocks(); |
23 | 24 | }); |
24 | 25 |
|
25 | 26 | 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(); |
29 | 30 |
|
30 | | - UserMock.expects('findById').withArgs('1234').yields(null, null); |
| 31 | + User.findById = jest.fn().mockResolvedValue(null); |
31 | 32 |
|
32 | | - createApiKey(request, response); |
| 33 | + await createApiKey(request, response); |
33 | 34 |
|
34 | 35 | expect(response.status).toHaveBeenCalledWith(404); |
35 | 36 | expect(response.json).toHaveBeenCalledWith({ |
36 | 37 | error: 'User not found' |
37 | 38 | }); |
38 | 39 | }); |
39 | 40 |
|
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 = {}; |
43 | 44 |
|
44 | | - UserMock.expects('findById').withArgs('1234').yields(null, new User()); |
| 45 | + const user = new User(); |
| 46 | + User.findById = jest.fn().mockResolvedValue(user); |
45 | 47 |
|
46 | | - createApiKey(request, response); |
| 48 | + await createApiKey(request, response); |
47 | 49 |
|
48 | 50 | expect(response.status).toHaveBeenCalledWith(400); |
49 | 51 | expect(response.json).toHaveBeenCalledWith({ |
50 | 52 | error: "Expected field 'label' was not present in request body" |
51 | 53 | }); |
52 | 54 | }); |
53 | 55 |
|
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 () => { |
56 | 57 | request.setBody({ label: 'my key' }); |
57 | 58 | request.user = { id: '1234' }; |
58 | 59 |
|
59 | | - const response = new Response(); |
60 | | - |
61 | 60 | const user = new User(); |
| 61 | + user.apiKeys = []; |
62 | 62 |
|
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(); |
69 | 65 |
|
70 | | - expect(lastKey.label).toBe('my key'); |
71 | | - expect(typeof lastKey.hashedKey).toBe('string'); |
| 66 | + await createApiKey(request, response); |
72 | 67 |
|
73 | | - const responseData = response.json.mock.calls[0][0]; |
| 68 | + const lastKey = last(user.apiKeys); |
74 | 69 |
|
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'); |
82 | 72 |
|
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 | + }); |
89 | 79 | }); |
90 | 80 | }); |
91 | 81 |
|
92 | 82 | 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(); |
96 | 86 |
|
97 | | - UserMock.expects('findById').withArgs('1234').yields(null, null); |
| 87 | + User.findById = jest.fn().mockResolvedValue(null); |
98 | 88 |
|
99 | | - removeApiKey(request, response); |
| 89 | + await removeApiKey(request, response); |
100 | 90 |
|
101 | 91 | expect(response.status).toHaveBeenCalledWith(404); |
102 | 92 | expect(response.json).toHaveBeenCalledWith({ |
103 | 93 | error: 'User not found' |
104 | 94 | }); |
105 | 95 | }); |
106 | 96 |
|
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' }; |
113 | 100 | const user = new User(); |
| 101 | + user.apiKeys = []; |
114 | 102 |
|
115 | | - UserMock.expects('findById').withArgs('1234').yields(null, user); |
| 103 | + User.findById = jest.fn().mockResolvedValue(user); |
116 | 104 |
|
117 | | - removeApiKey(request, response); |
| 105 | + await removeApiKey(request, response); |
118 | 106 |
|
119 | 107 | expect(response.status).toHaveBeenCalledWith(404); |
120 | 108 | expect(response.json).toHaveBeenCalledWith({ |
121 | 109 | error: 'Key does not exist for user' |
122 | 110 | }); |
123 | 111 | }); |
124 | 112 |
|
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' }; |
129 | 116 |
|
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(); |
132 | 120 |
|
133 | | - const request = { |
134 | | - user: { id: '1234' }, |
135 | | - params: { keyId: firstKeyId } |
| 121 | + const user = { |
| 122 | + apiKeys, |
| 123 | + save: jest.fn().mockResolvedValue() |
136 | 124 | }; |
137 | | - const response = new Response(); |
138 | 125 |
|
139 | | - UserMock.expects('findById').withArgs('1234').yields(null, user); |
| 126 | + request.user = { id: '1234' }; |
| 127 | + request.params = { keyId: 'id1' }; |
140 | 128 |
|
141 | | - UserInstanceMock.expects('save').yields(); |
| 129 | + User.findById = jest.fn().mockResolvedValue(user); |
142 | 130 |
|
143 | | - removeApiKey(request, response); |
| 131 | + await removeApiKey(request, response); |
144 | 132 |
|
| 133 | + expect(user.apiKeys.pull).toHaveBeenCalledWith({ _id: 'id1' }); |
| 134 | + expect(user.save).toHaveBeenCalled(); |
145 | 135 | 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 | | - }); |
156 | 136 | }); |
157 | 137 | }); |
158 | 138 | }); |
0 commit comments