-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathexpress_current_user.test.js
More file actions
299 lines (251 loc) · 9.2 KB
/
Copy pathexpress_current_user.test.js
File metadata and controls
299 lines (251 loc) · 9.2 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
let mockArgs // array of mocked express middleware request arguments
const userFunctions = ['setCurrentUser', 'getCurrentUser', 'requireCurrentUser']
jest.mock('test_node')
jest.mock('../server/models/user')
jest.mock('../server/viewer/viewer_notification_state')
const viewerNotificationState = require('../server/viewer/viewer_notification_state')
jest.mock('../server/viewer/viewer_state', () => ({
get: jest.fn()
}))
const viewerState = require('../server/viewer/viewer_state')
describe('current user middleware', () => {
beforeAll(() => {})
afterAll(() => {})
beforeEach(() => {
jest.clearAllMocks()
mockArgs = (() => {
const res = {
cookie: jest.fn()
}
const req = {
session: {}
}
const mockJson = jest.fn().mockImplementation(() => {
return true
})
const mockStatus = jest.fn().mockImplementation(() => {
return { json: mockJson }
})
const mockNext = jest.fn()
res.status = mockStatus
const currentUserMiddleware = oboRequire('server/express_current_user')
currentUserMiddleware(req, res, mockNext)
return { res, req, mockJson, mockStatus, mockNext }
})()
})
afterEach(() => {
const { mockJson, mockStatus, mockNext } = mockArgs
mockNext.mockClear()
mockStatus.mockClear()
mockJson.mockClear()
})
test('calls next', () => {
const { mockNext } = mockArgs
expect(mockNext).toBeCalledWith()
})
test('sets the expected properties on req', () => {
expect.assertions(userFunctions.length * 2)
const { req } = mockArgs
userFunctions.forEach(func => {
expect(req).toHaveProperty(func)
expect(req[func]).toBeInstanceOf(Function)
})
})
test('sets the current user on req.session', () => {
expect.assertions(1)
const { req } = mockArgs
const User = oboRequire('server/models/user')
const mockUser = new User({ id: 999 })
req.setCurrentUser(mockUser)
expect(req.session.currentUserId).toBe(999)
})
test('unsets the curent user', () => {
expect.hasAssertions()
const { req } = mockArgs
const User = oboRequire('server/models/user')
const mockUser = new User({ id: 8 })
const GuestUser = oboRequire('server/models/guest_user')
User.fetchById = jest.fn().mockResolvedValue(mockUser)
req.setCurrentUser(mockUser)
return req
.getCurrentUser()
.then(user => {
expect(user).toBeInstanceOf(User)
req.resetCurrentUser()
return req.getCurrentUser()
})
.then(user => {
expect(user.id).toBe(0)
expect(user).toBeInstanceOf(GuestUser)
expect(user.isGuest()).toBe(true)
})
})
test('setCurrentUser throws when not using a User', () => {
expect.assertions(1)
const { req } = mockArgs
const mockUser = {}
expect(() => {
req.setCurrentUser(mockUser)
}).toThrow()
})
test('getCurrentUser gets the current user', () => {
expect.hasAssertions()
const { req } = mockArgs
const User = oboRequire('server/models/user')
const mockUser = new User({ id: 8 })
User.fetchById = jest.fn().mockResolvedValue(mockUser)
req.setCurrentUser(mockUser)
return req.getCurrentUser().then(user => {
expect(user).toBe(mockUser)
expect(user.id).toBe(8)
expect(user).toBeInstanceOf(User)
expect(req.currentUser).toBe(mockUser)
expect(req.currentUser).toBe(user)
})
})
test('getCurrentUser loads the user if not already loaded', () => {
expect.hasAssertions()
const { req } = mockArgs
const User = oboRequire('server/models/user')
const mockUser = new User({ id: 8 })
User.fetchById = jest.fn().mockResolvedValue(mockUser)
req.session.currentUserId = mockUser.id // only set the id, don't set req.currentUser
return req.getCurrentUser().then(user => {
expect(User.fetchById).toBeCalledWith(8)
expect(user.id).toBe(8)
expect(user).toBeInstanceOf(User)
expect(req.currentUser).toBe(mockUser)
expect(req.currentUser).toBe(user)
})
})
test('getCurrentUser gets a guest user', () => {
expect.hasAssertions()
const { req } = mockArgs
const GuestUser = oboRequire('server/models/guest_user')
return req.getCurrentUser().then(user => {
expect(user.id).toBe(0)
expect(user).toBeInstanceOf(GuestUser)
expect(user.isGuest()).toBe(true)
expect(req.currentUser).toBe(user)
})
})
test('getCurrentUser returns a guest user when fetchById fails', () => {
expect.assertions(2)
const { req } = mockArgs
const User = oboRequire('server/models/user')
const GuestUser = oboRequire('server/models/guest_user')
User.fetchById = jest.fn().mockRejectedValueOnce('mock-fetch-id-error')
req.session.currentUserId = 9
return req.getCurrentUser().then(user => {
expect(User.fetchById).toBeCalledWith(9)
expect(user).toBeInstanceOf(GuestUser)
})
})
test('requireCurrentUser rejects when fetchById fails and login is required', () => {
const { req } = mockArgs
const User = oboRequire('server/models/user')
User.fetchById = jest.fn().mockRejectedValueOnce('mock-fetch-id-error')
req.session.currentUserId = 9
return expect(req.requireCurrentUser()).rejects.toThrow('Login Required')
})
test('requireCurrentUser rejects with no current user', () => {
expect.assertions(1)
const { req } = mockArgs
return expect(req.requireCurrentUser()).rejects.toThrow('Login Required')
})
test('requireCurrentUser rejects current user as guest', () => {
expect.assertions(1)
const GuestUser = oboRequire('server/models/guest_user')
const mockUser = new GuestUser({ id: 999 })
const { req } = mockArgs
req.setCurrentUser(mockUser)
return expect(req.requireCurrentUser()).rejects.toThrow('Login Required')
})
test('requireCurrentUser returns current user when logged in', () => {
expect.assertions(1)
const User = oboRequire('server/models/user')
const mockUser = new User({ id: 999 })
const { req } = mockArgs
req.setCurrentUser(mockUser)
return expect(req.requireCurrentUser()).resolves.toBe(mockUser)
})
test('saveSessionPromise resolves when session saves', () => {
expect.assertions(1)
const { req } = mockArgs
req.session.save = jest.fn().mockImplementation(cb => {
cb()
})
return expect(req.saveSessionPromise()).resolves.toBeUndefined()
})
test('saveSessionPromise rejects when session save fails', () => {
expect.assertions(1)
const { req } = mockArgs
req.session.save = jest.fn().mockImplementation(cb => {
cb('mock-error')
})
return expect(req.saveSessionPromise()).rejects.toEqual('mock-error')
})
test('getNotifications sets notifications in cookies when notifications are available', async () => {
expect.assertions(6)
const { req, res } = mockArgs
const User = oboRequire('server/models/user')
const mockUser = new User({ id: 8, lastLogin: '2019-01-01' })
User.fetchById = jest.fn().mockResolvedValue(mockUser)
req.currentUserId = mockUser.id
req.currentUser = mockUser
req.currentUser.lastLogin = mockUser.lastLogin
jest.useFakeTimers('modern')
jest.setSystemTime(new Date(2024, 3, 1)) //mock the date so that runtime does not affect the date/time
const mockNotifications = [
{ id: 1, title: 'Notification 1', text: 'Message 1' },
{ id: 2, title: 'Notification 2', text: 'Message 2' }
]
//simulate what would be added to the cookie
const mockNotificationsToCookie = [
{ title: 'Notification 1', text: 'Message 1' },
{ title: 'Notification 2', text: 'Message 2' }
]
viewerState.get.mockResolvedValueOnce(req.currentUserId)
viewerNotificationState.getRecentNotifications.mockResolvedValueOnce(
mockNotifications.map(n => ({ id: n.id }))
)
viewerNotificationState.getNotifications.mockResolvedValueOnce(mockNotifications)
return req.getNotifications(req, res).then(() => {
const today = new Date()
expect(viewerState.get).toHaveBeenCalledWith(8)
expect(viewerNotificationState.getRecentNotifications).toHaveBeenCalled()
expect(viewerNotificationState.getNotifications).toHaveBeenCalledWith([1, 2])
expect(res.cookie).toHaveBeenCalledWith(
'notifications',
JSON.stringify(mockNotificationsToCookie)
)
expect(req.currentUser.lastLogin).toStrictEqual(today)
expect(viewerNotificationState.setLastLogin).toHaveBeenCalledWith(8, today)
jest.useRealTimers()
})
})
test('getNotifications returns empty when there are no notifications', async () => {
expect.assertions(6)
const { req, res } = mockArgs
const User = oboRequire('server/models/user')
const mockUser = new User({ id: 8, lastLogin: '2019-01-01' })
User.fetchById = jest.fn().mockResolvedValue(mockUser)
req.currentUserId = mockUser.id
req.currentUser = mockUser
req.currentUser.lastLogin = mockUser.lastLogin
jest.useFakeTimers('modern')
jest.setSystemTime(new Date(2024, 3, 1)) //mock the date so that runtime does not affect the date/time
viewerState.get.mockResolvedValueOnce(req.currentUserId)
viewerNotificationState.getRecentNotifications.mockResolvedValueOnce(null)
return req.getNotifications(req, res).then(() => {
const today = new Date()
expect(viewerState.get).toHaveBeenCalledWith(8)
expect(viewerNotificationState.getRecentNotifications).toHaveBeenCalled() //With(req.currentUser.lastLogin)
expect(viewerNotificationState.getNotifications).not.toHaveBeenCalled()
expect(res.cookie).not.toHaveBeenCalled()
expect(req.currentUser.lastLogin).toStrictEqual(today)
expect(viewerNotificationState.setLastLogin).toHaveBeenCalledWith(8, today)
jest.useRealTimers()
})
})
})