forked from ucfopen/Obojobo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocks.test.js
More file actions
228 lines (202 loc) · 7.71 KB
/
locks.test.js
File metadata and controls
228 lines (202 loc) · 7.71 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
jest.mock('../../../server/models/edit_lock', () => ({
fetchByDraftId: jest.fn(),
create: jest.fn(),
deleteExpiredLocks: jest.fn(),
deleteByDraftIdAndUser: jest.fn()
}))
jest.mock('obojobo-repository/server/models/draft_permissions')
jest.unmock('express') // needed to use supertest
const mockCurrentUser = { id: 'mock-current-user-id' }
const mockValidatorThatPasses = jest.fn().mockImplementation((req, res, next) => {
req.currentUser = mockCurrentUser
next()
})
jest.mock('../../../server/express_validators', () => ({
checkValidationRules: mockValidatorThatPasses,
requireDraftId: mockValidatorThatPasses,
requireContentId: mockValidatorThatPasses,
requireCanViewEditor: mockValidatorThatPasses
}))
describe('Route api/locks', () => {
let request
let bodyParser
let EditLock
let app
let DraftPermissions
beforeEach(() => {
jest.clearAllMocks()
// jest.resetModules()
global.oboJestMockConfig()
DraftPermissions = require('obojobo-repository/server/models/draft_permissions')
request = require('supertest')
const express = require('express')
bodyParser = require('body-parser')
app = express()
EditLock = require('../../../server/models/edit_lock')
app.use(bodyParser.json())
app.use('', oboRequire('server/express_response_decorator'))
app.use('/api/locks', oboRequire('server/routes/api/locks')) // mounting under api so response_decorator assumes json content type
})
test('get lock calls expected validators', () => {
expect.hasAssertions()
EditLock.fetchByDraftId.mockReturnValueOnce('mockLockReturn')
expect(mockValidatorThatPasses).toHaveBeenCalledTimes(0)
return request(app)
.get('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(200)
expect(mockValidatorThatPasses).toHaveBeenCalledTimes(3)
})
})
test('get lock calls EditLock.fetchByDraftId', () => {
expect.hasAssertions()
EditLock.fetchByDraftId.mockReturnValueOnce('mockLockReturn')
return request(app)
.get('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.header['content-type']).toContain('application/json')
expect(response.body).toHaveProperty('status', 'ok')
expect(response.body).toHaveProperty('value', 'mockLockReturn')
expect(EditLock.fetchByDraftId).toHaveBeenCalledTimes(1)
expect(EditLock.fetchByDraftId).toHaveBeenCalledWith('mock-draft-id')
})
})
test('post lock calls expected Validators', () => {
expect.hasAssertions()
DraftPermissions.userHasPermissionToDraft.mockResolvedValueOnce(true)
EditLock.fetchByDraftId.mockReturnValueOnce({ userId: mockCurrentUser.id })
EditLock.create.mockReturnValueOnce({ userId: mockCurrentUser.id })
expect(mockValidatorThatPasses).toHaveBeenCalledTimes(0)
return request(app)
.post('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(200)
expect(mockValidatorThatPasses).toHaveBeenCalledTimes(4)
})
})
test('post lock returns expected results', () => {
expect.hasAssertions()
DraftPermissions.userHasPermissionToDraft.mockResolvedValueOnce(true)
EditLock.fetchByDraftId.mockReturnValueOnce({ userId: mockCurrentUser.id })
EditLock.create.mockReturnValueOnce({ userId: mockCurrentUser.id })
expect(mockValidatorThatPasses).toHaveBeenCalledTimes(0)
return request(app)
.post('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(200)
expect(response.header['content-type']).toContain('application/json')
expect(response.body).toHaveProperty('status', 'ok')
})
})
test('post lock returns not authorized when user doesnt have permission to draft', () => {
expect.hasAssertions()
DraftPermissions.userHasPermissionToDraft.mockResolvedValueOnce(false)
return request(app)
.post('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(401)
expect(response.header['content-type']).toContain('application/json')
expect(response.body).toHaveProperty('status', 'error')
expect(response.body).toHaveProperty('value')
expect(response.body.value).toHaveProperty(
'message',
'You do not have the required access to edit this module.'
)
})
})
test('post lock returns not authorized when a different user has a lock', () => {
expect.hasAssertions()
DraftPermissions.userHasPermissionToDraft.mockResolvedValueOnce(true)
EditLock.fetchByDraftId.mockReturnValueOnce({ userId: 'someone-else' })
return request(app)
.post('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(401)
expect(response.header['content-type']).toContain('application/json')
expect(response.body).toHaveProperty('status', 'error')
expect(response.body).toHaveProperty(
'value.message',
'Someone else is currently editing this module.'
)
})
})
test('post lock returns not authorized when creating a lock doesnt work', () => {
expect.hasAssertions()
DraftPermissions.userHasPermissionToDraft.mockResolvedValueOnce(true)
EditLock.fetchByDraftId.mockReturnValueOnce({ userId: mockCurrentUser.id })
EditLock.create.mockReturnValueOnce({ userId: 'some-other-user' })
return request(app)
.post('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(401)
expect(response.header['content-type']).toContain('application/json')
expect(response.body).toHaveProperty('status', 'error')
expect(response.body).toHaveProperty(
'value.message',
'Someone else is currently editing this module.'
)
})
})
test('post lock returns a 403 when the contentId does not match', () => {
expect.hasAssertions()
DraftPermissions.userHasPermissionToDraft.mockImplementationOnce(() => {
throw new Error('Current version of draft does not match requested lock.')
})
return request(app)
.post('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(403)
expect(response.header['content-type']).toContain('application/json')
expect(response.body).toHaveProperty('status', 'error')
expect(response.body).toHaveProperty('value')
expect(response.body.value).toHaveProperty(
'message',
'Draft has been updated by someone else.'
)
})
})
test('post lock returns a 500 when an unexpected error occurs', () => {
expect.hasAssertions()
DraftPermissions.userHasPermissionToDraft.mockImplementationOnce(() => {
throw new Error('mock-error')
})
return request(app)
.post('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(500)
expect(response.header['content-type']).toContain('application/json')
expect(response.body).toHaveProperty('status', 'error')
expect(response.body).toHaveProperty('value')
expect(response.body.value).toHaveProperty(
'message',
'Unexpected error while creating edit lock.'
)
})
})
test('post lock calls EditLock.deleteExpiredLocks', () => {
expect.hasAssertions()
DraftPermissions.userHasPermissionToDraft.mockResolvedValueOnce(true)
EditLock.fetchByDraftId.mockReturnValueOnce({ userId: mockCurrentUser.id })
EditLock.create.mockReturnValueOnce({ userId: mockCurrentUser.id })
return request(app)
.post('/api/locks/mock-draft-id')
.then(response => {
expect(response.statusCode).toBe(200)
expect(EditLock.deleteExpiredLocks).toHaveBeenCalledTimes(1)
})
})
test('post delete calls deleteByDraftIdAndUser', () => {
expect.hasAssertions()
return request(app)
.post('/api/locks/mock-draft-id/delete')
.then(response => {
expect(response.statusCode).toBe(200)
expect(EditLock.deleteByDraftIdAndUser).toHaveBeenCalledTimes(1)
expect(EditLock.deleteByDraftIdAndUser).toHaveBeenCalledWith(
'mock-current-user-id',
'mock-draft-id'
)
})
})
})