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
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
process.env.SUPPRESS_NO_CONFIG_WARNING = true
const httpMocks = require('node-mocks-http')
const random = require('lodash/random')
const fixtures = require('./../fixtures/fixtures')
const Chance = require('chance')
const Model = require('./../helpers/Model')
const config = require('config')
const configRoles = config.get('roles')
const models = Model.build()
jest.mock('pubsweet-component-mail-service', () => ({
setupInviteEmail: jest.fn(),
setupAssignEmail: jest.fn(),
setupDeclineEmail: jest.fn(),
}))
const chance = new Chance()
const roles = configRoles.collection
const body = {
email: chance.email(),
role: roles[random(0, roles.length - 1)],
firstName: chance.first(),
lastName: chance.last(),
title: 'Mr',
affiliation: chance.company(),
}
body.admin = body.role === 'admin'
const notFoundError = new Error()
notFoundError.name = 'NotFoundError'
notFoundError.status = 404
const { admin, editorInChief, author, invitedHandlingEditor } = fixtures.users
const { standardCollection } = fixtures.collections
const { heTeam } = fixtures.teams
const postPath = '../../routes/collectionsInvitations/post'
describe('Post collections invitations route handler', () => {
it('should return an error params are missing', async () => {
delete body.email
const req = httpMocks.createRequest({
body,
})
req.user = admin.id
const res = httpMocks.createResponse()
await require(postPath)(models)(req, res)
expect(res.statusCode).toBe(400)
const data = JSON.parse(res._getData())
expect(data.error).toEqual('Email and role are required')
body.email = chance.email()
})
it('should return success when the editor in chief invites a handlingEditor with a collection', async () => {
const body = {
email: author.email,
role: 'handlingEditor',
}
const req = httpMocks.createRequest({
body,
})
req.user = editorInChief.id
const initialSize = standardCollection.invitations.length
req.params.collectionId = standardCollection.id
const res = httpMocks.createResponse()
await require(postPath)(models)(req, res)
expect(res.statusCode).toBe(200)
const data = JSON.parse(res._getData())
expect(data.email).toEqual(body.email)
expect(standardCollection.invitations.length).toBeGreaterThan(initialSize)
expect(heTeam.members).toContain(author.id)
expect(author.teams).toContain(heTeam.id)
const matchingInvitation = standardCollection.invitations.find(
inv => inv.userId === author.id && inv.role === body.role,
)
expect(matchingInvitation).toBeDefined()
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
})
it('should return an error when inviting his self', async () => {
body.role = roles[random(0, roles.length - 1)]
body.email = editorInChief.email
const req = httpMocks.createRequest({
body,
})
req.user = editorInChief.id
const res = httpMocks.createResponse()
await require(postPath)(models)(req, res)
expect(res.statusCode).toBe(400)
const data = JSON.parse(res._getData())
expect(data.error).toEqual('Cannot invite yourself')
})
it('should return an error when the role is invalid', async () => {
body.role = 'someRandomRole'
const req = httpMocks.createRequest({
body,
})
req.user = editorInChief.id
const res = httpMocks.createResponse()
await require(postPath)(models)(req, res)
const data = JSON.parse(res._getData())
expect(data.error).toEqual(`Role ${body.role} is invalid`)
})
it('should return success when the EiC resends an invitation to a handlingEditor with a collection', async () => {
const body = {
email: invitedHandlingEditor.email,
role: 'handlingEditor',
}
const req = httpMocks.createRequest({
body,
})
req.user = editorInChief.id
req.params.collectionId = standardCollection.id
const res = httpMocks.createResponse()
await require(postPath)(models)(req, res)
expect(res.statusCode).toBe(200)
const data = JSON.parse(res._getData())
expect(data.email).toEqual(body.email)
})
})