Newer
Older
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
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
process.env.SUPPRESS_NO_CONFIG_WARNING = true
const httpMocks = require('node-mocks-http')
const fixtures = require('./../fixtures/fixtures')
const Model = require('./../helpers/Model')
const cloneDeep = require('lodash/cloneDeep')
jest.mock('pubsweet-component-mail-service', () => ({
setupReviewerDeclinedEmail: jest.fn(),
}))
const reqBody = {
invitationToken: 'inv-token-123',
}
const patchPath = '../../routes/collectionsInvitations/decline'
describe('Patch collections invitations route handler', () => {
let testFixtures = {}
let body = {}
let models
beforeEach(() => {
testFixtures = cloneDeep(fixtures)
body = cloneDeep(reqBody)
models = Model.build(testFixtures)
})
it('should return success when the reviewer declines work on a collection', async () => {
const { reviewer } = testFixtures.users
const { collection } = testFixtures.collections
body.isAccepted = false
const req = httpMocks.createRequest({
body,
})
req.user = reviewer.id
req.params.collectionId = collection.id
const inv = collection.invitations.find(
inv => inv.role === 'reviewer' && inv.hasAnswer === false,
)
req.params.invitationId = inv.id
const res = httpMocks.createResponse()
await require(patchPath)(models)(req, res)
expect(res.statusCode).toBe(200)
})
it('should return an error params are missing', async () => {
const { handlingEditor } = testFixtures.users
const { collection } = testFixtures.collections
delete body.isAccepted
const req = httpMocks.createRequest({
body,
})
req.user = handlingEditor.id
req.params.collectionId = collection.id
req.params.invitationId = collection.invitations[0].id
const res = httpMocks.createResponse()
await require(patchPath)(models)(req, res)
expect(res.statusCode).toBe(400)
const data = JSON.parse(res._getData())
expect(data.error).toEqual('Missing parameters')
})
it('should return an error if the collection does not exists', async () => {
const { handlingEditor } = testFixtures.users
const req = httpMocks.createRequest({
body,
})
req.user = handlingEditor.id
req.params.collectionId = 'invalid-id'
const res = httpMocks.createResponse()
await require(patchPath)(models)(req, res)
expect(res.statusCode).toBe(404)
const data = JSON.parse(res._getData())
expect(data.error).toEqual('collection not found')
})
it('should return an error when the invitation does not exist', async () => {
const { user } = testFixtures.users
const { collection } = testFixtures.collections
const req = httpMocks.createRequest({
body,
})
req.user = user.id
req.params.collectionId = collection.id
req.params.invitationId = 'invalid-id'
const res = httpMocks.createResponse()
await require(patchPath)(models)(req, res)
expect(res.statusCode).toBe(404)
const data = JSON.parse(res._getData())
expect(data.error).toEqual('Invitation invalid-id not found')
})
it("should return an error when a user tries to patch another user's invitation", async () => {
const { reviewer } = testFixtures.users
const { collection } = testFixtures.collections
const req = httpMocks.createRequest({
body,
})
req.user = reviewer.id
req.params.collectionId = collection.id
const inv = collection.invitations.find(
inv => inv.role === 'handlingEditor' && inv.hasAnswer === false,
)
req.params.invitationId = inv.id
const res = httpMocks.createResponse()
await require(patchPath)(models)(req, res)
expect(res.statusCode).toBe(403)
const data = JSON.parse(res._getData())
expect(data.error).toEqual(
`User ${reviewer.email} is not allowed to modify invitation ${inv.id}`,
)
})
it('should return an error when the invitation is already answered', async () => {
const { handlingEditor } = testFixtures.users
const { collection } = testFixtures.collections
const req = httpMocks.createRequest({
body,
})
req.user = handlingEditor.id
req.params.collectionId = collection.id
const inv = collection.invitations.find(inv => inv.hasAnswer)
req.params.invitationId = inv.id
const res = httpMocks.createResponse()
await require(patchPath)(models)(req, res)
expect(res.statusCode).toBe(400)
const data = JSON.parse(res._getData())
expect(data.error).toEqual(`${inv.id} has already been answered`)
})
})